summaryrefslogtreecommitdiff
path: root/tests/lib/string-test.c
blob: 17bb69ab56ecc5ea53e2c69cbd373123f2599d52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* SPDX-License-Identifier: GPL-2.0-only */

#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <tests/test.h>

/*
 * Important note: In every particular test, don't use any string-related
 * functions other than function under test. We are linking against
 * src/lib/string.c not the standard library. This is important for proper test
 * isolation.
 */

struct string_pairs_t {
	char *dst;
	char *src;
} string_pairs[] = {
	{"Hello ", "world!"},
	{"He\0llo ", "world"},
	{"", "world!"},
	{"", ""},
};

const char *strings[] = {
	"coreboot",
	"is\0very",
	"nice\n"
};

/* Used to test atol */
struct str_with_l_val_t {
	char *str;
	long value;
} str_with_l_val[] = {
	{"42", 42},
	{"four42", 0},
	{"42five5",	42},
	{"4\02", 4},
	{"+42", 42},
	{"-42", -42},
	{"\t\n\r\f\v-42", -42},
};

static void test_strdup(void **state)
{
	char str[] = "Hello coreboot\n";
	char *duplicate = strdup(str);

	/* There is a more suitable Cmocka's function 'assert_string_equal()', but it
	   is using strcmp() internally. */
	assert_int_equal(0, memcmp(str, duplicate, __builtin_strlen(str)));

	free(duplicate);
}

static void test_strconcat(void **state)
{
	int i;
	size_t str_len, str2_len, res_len;
	char *result;

	for (i = 0; i < ARRAY_SIZE(string_pairs); i++) {
		str_len = __builtin_strlen(string_pairs[i].dst);
		str2_len = __builtin_strlen(string_pairs[i].src);

		result = strconcat(string_pairs[i].dst, string_pairs[i].src);
		res_len = __builtin_strlen(result);

		assert_int_equal(res_len, str_len + str2_len);
		assert_int_equal(0, memcmp(string_pairs[i].dst, result, str_len));
		assert_int_equal(0, memcmp(string_pairs[i].src, result + str_len, str2_len));

		free(result);
	}
}

static void test_strnlen(void **state)
{
	int i, n = 5;
	size_t str_len, limited_len;

	for (i = 0; i < ARRAY_SIZE(strings); i++) {
		str_len = __builtin_strlen(strings[i]);
		limited_len = MIN(n, str_len);
		assert_int_equal(limited_len, strnlen(strings[i], n));
	}
}

static void test_strlen(void **state)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(strings); i++)
		assert_int_equal(__builtin_strlen(strings[i]), strlen(strings[i]));
}

static void test_strchr(void **state)
{
	char str[] = "Abracadabra!\n";

	assert_ptr_equal(str, strchr(str, 'A'));
	assert_ptr_equal(str + 3, strchr(str, 'a'));
	assert_ptr_equal(str + 12, strchr(str, '\n'));

	assert_null(strchr(str, 'z'));
}


static void test_strrchr(void **state)
{
	char str[] = "Abracadabra!\n";

	assert_ptr_equal(str, strrchr(str, 'A'));
	assert_ptr_equal(str + 9, strrchr(str, 'r'));
	assert_ptr_equal(str + 12, strrchr(str, '\n'));

	assert_null(strrchr(str, 'z'));
}

static void test_strncpy(void **state)
{
	int i;
	int n1 = 2, n2 = 8;
	char src[] = "Hello";
	char dst[sizeof(src) + 5];
	size_t src_len = __builtin_strlen(src);
	size_t dst_len = sizeof(dst);

	/* n1 case  */

	/* Needed for ensuring that characters behind the limit
	   are not overwritten */
	memset(dst, 'x', dst_len);

	strncpy(dst, src, n1);

	assert_int_equal(0, memcmp(dst, src, n1));

	for (i = n1; i < dst_len; i++)
		assert_true(dst[i] == 'x');

	/* n2 case: */

	memset(dst, 'x', dst_len);

	strncpy(dst, src, n2);

	assert_int_equal(0, memcmp(dst, src, src_len));

	for (i = src_len; i < n2; i++)
		assert_true(dst[i] == '\0');

	for (i = n2; i < dst_len; i++)
		assert_true(dst[i] == 'x');
}

static void test_strcpy(void **state)
{
	char src[] = "Hello coreboot\n";
	char dst[sizeof(src)];

	/* Make sure that strcpy() sets '\0' by initializing a whole
	   dst array to fixed, non-'\0' value */
	memset(dst, 'x', sizeof(dst));

	strcpy(dst, src);

	assert_int_equal(0, memcmp(dst, src, __builtin_strlen(src) + 1));
}

static void test_strcmp(void **state)
{
	char str[] = "Banana";
	char str2[] = "Banana";
	char str3[] = "Bananas";

	assert_true(strcmp(str, str3) < 0);
	assert_int_equal(0, strcmp(str, str2));
	assert_true(strcmp(str3, str2) > 0);
}

static void test_strncmp(void **state)
{
	char str[] = "Banana";
	char str2[] = "Bananas";

	size_t str2_len = __builtin_strlen(str2);

	assert_true(strncmp(str, str2, str2_len) < 0);
	assert_int_equal(0, strncmp(str, str2, str2_len - 1));
}

static void test_strspn(void **state)
{
	char str[] = "4213401234";
	char str2[] = "01234";
	char str3[] = "1234";

	assert_int_equal(5, strspn(str, str3));
	assert_int_equal(0, strspn(str2, str3));
}

static void test_strcspn(void **state)
{
	char str[] = "12340000";
	char str2[] = "00001234";
	char str3[] = "1234";

	assert_int_equal(0, strcspn(str, str3));
	assert_int_equal(4, strcspn(str2, str3));
}

/* Please bear in mind that `atol()` uses `strspn()` internally, so the result
   of `test_atol` is dependent on the result of `test_strspn`. */
static void test_atol(void **state)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(str_with_l_val); i++)
		assert_int_equal(str_with_l_val[i].value, atol(str_with_l_val[i].str));
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_strdup),
		cmocka_unit_test(test_strconcat),
		cmocka_unit_test(test_strnlen),
		cmocka_unit_test(test_strlen),
		cmocka_unit_test(test_strchr),
		cmocka_unit_test(test_strrchr),
		cmocka_unit_test(test_strncpy),
		cmocka_unit_test(test_strcpy),
		cmocka_unit_test(test_strcmp),
		cmocka_unit_test(test_strncmp),
		cmocka_unit_test(test_strspn),
		cmocka_unit_test(test_strcspn),
		cmocka_unit_test(test_atol),
	};

	return cb_run_group_tests(tests, NULL, NULL);
}