summaryrefslogtreecommitdiff
path: root/tests/lib/ux_locales-test.c
blob: d5eeef707c93e4c8a6e11de2af119621dc83a272 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* SPDX-License-Identifier: GPL-2.0-only */

#include <cbfs.h>
#include <stdbool.h>
#include <string.h>
#include <tests/test.h>
#include <ux_locales.h>
#include <vb2_api.h>

#define DATA_DEFAULT                                                                           \
	(                                                                                      \
		"\x01"  /* Version. */                                                         \
		"name_1\x00"  /* name_1, langs = [0, 2, 30]. */                                \
		"0\x00translation_1_0\x00"                                                     \
		"2\x00translation_1_2\x00"                                                     \
		"30\x00translation_1_30\x00"                                                   \
		"\x01"                                                                         \
		"name_15\x00"  /* name_15, langs = [4, 25, 60]. */                             \
		"4\x00translation_15_4\x00"                                                    \
		"25\x00translation_15_25\x00"                                                  \
		"60\x00translation_15_60\x00"                                                  \
		"\x01"                                                                         \
		"name_20\x00"  /* name_20, langs = [8]. */                                     \
		"8\x00translation_20_8\x00"                                                    \
		"\x01"                                                                         \
	)
const unsigned char data_default[] = DATA_DEFAULT;

/*
 * The data must be set in the setup function and might be used in cbfs related functions.
 * The size of the data must be recorded because the data contains the null character \x00.
 *
 * Note that sizeof(DATA_DEFAULT) will count the '\0' at the end, so DATA_DEFAULT_SIZE, i.e.
 * the real data size must be minus one.
 */
#define DATA_DEFAULT_SIZE (sizeof(DATA_DEFAULT) - 1)

#define MAX_DATA_SIZE (DATA_DEFAULT_SIZE + 1)
struct {
	unsigned char raw[MAX_DATA_SIZE];
	size_t size;
} data;

/* Mock functions. */
void cbfs_unmap(void *mapping)
{
	/* No-op. */
}

/* We can't mock cbfs_ro_map() directly as it's a static inline function. */
void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
		  size_t *size_out, bool force_ro, enum cbfs_type *type)
{
	/* Mock a successful CBFS mapping. */
	if (mock_type(bool)) {
		*size_out = data.size;
		return data.raw;
	}
	*size_out = 0;
	return NULL;
}

uint32_t vb2api_get_locale_id(struct vb2_context *ctx)
{
	return mock_type(uint32_t);
}

struct vb2_context *vboot_get_context(void)
{
	static struct vb2_context vboot2_ctx;
	return &vboot2_ctx;
}

/* Test states for test_ux_locales_get_text with valid CBFS data. */
struct ux_locales_test_state {
	const char *name;
	uint32_t lang_id;
	const char *expect;
};

/* Setup and teardown functions. */
static int setup_default(void **state)
{
	void *ret;
	/* Setup the mocked cbfs region data. */
	data.size = DATA_DEFAULT_SIZE;
	ret = memset(data.raw, 0xff, MAX_DATA_SIZE);
	if (!ret)
		return 1;
	ret = memcpy(data.raw, data_default, data.size);
	if (!ret)
		return 1;
	return 0;
}

static int setup_bad_version(void **state)
{
	int ret = setup_default(state);
	if (ret)
		return ret;
	/* Modify the version byte. */
	data.raw[0] = ~data.raw[0];
	return 0;
}

static int teardown_unmap(void **state)
{
	/* We need to reset the cached_state in src/lib/ux_locales.c. */
	ux_locales_unmap();
	return 0;
}

/* Test items. */
static void test_ux_locales_get_text(void **state)
{
	struct ux_locales_test_state *s = *state;
	const char *ret;

	will_return(_cbfs_alloc, true);
	will_return(vb2api_get_locale_id, s->lang_id);
	ret = ux_locales_get_text(s->name);
	if (s->expect) {
		assert_non_null(ret);
		assert_string_equal(ret, s->expect);
	} else {
		assert_null(ret);
	}
}

static void test_ux_locales_bad_cbfs(void **state)
{
	will_return(_cbfs_alloc, false);
	will_return_maybe(vb2api_get_locale_id, 0);
	assert_null(ux_locales_get_text("name_1"));
}

static void test_ux_locales_bad_version(void **state)
{
	will_return(_cbfs_alloc, true);
	will_return(vb2api_get_locale_id, 0);
	assert_null(ux_locales_get_text("name_1"));
}

static void test_ux_locales_two_calls(void **state)
{
	const char *ret;

	/* We do not need to ensure that we cached the cbfs region. */
	will_return_always(_cbfs_alloc, true);

	/* Call #1: read (15, 60). */
	will_return(vb2api_get_locale_id, 60);
	ret = ux_locales_get_text("name_15");
	assert_non_null(ret);
	assert_string_equal(ret, "translation_15_60");

	/* Call #2: read (1, 0). */
	will_return(vb2api_get_locale_id, 0);
	ret = ux_locales_get_text("name_1");
	assert_non_null(ret);
	assert_string_equal(ret, "translation_1_0");
}

static void test_ux_locales_null_terminated(void **state)
{
	will_return_always(_cbfs_alloc, true);
	will_return_always(vb2api_get_locale_id, 8);

	/* Verify the access to the very last text. */
	assert_non_null(ux_locales_get_text("name_20"));

	/* Modify the last 2 bytes from "\x00\x01" to "XX" and unmap, */
	data.raw[data.size - 1] = 'X';
	data.raw[data.size - 2] = 'X';
	ux_locales_unmap();

	/* The last few characters are now changed so that the data is not NULL terminated.
	   This will prevent us from accessing the last text. */
	assert_null(ux_locales_get_text("name_20"));
}

/*
 * This macro helps test ux_locales_get_text with `_name` and `_lang_id`.
 * If `_expect` is NULL, then the function should not find anything.
 * Otherwise, the function should find the corresponding expect value.
 */
#define _UX_LOCALES_GET_TEXT_TEST(_test_name, _name, _lang_id, _expect)                        \
	((struct CMUnitTest) {                                                                 \
		.name = _test_name,                                                            \
		.test_func = test_ux_locales_get_text,                                         \
		.setup_func = setup_default,                                                   \
		.teardown_func = teardown_unmap,                                               \
		.initial_state = &(struct ux_locales_test_state)                               \
			{                                                                      \
				.name = _name,                                                 \
				.lang_id = _lang_id,                                           \
				.expect = _expect,                                             \
			},                                                                     \
	})

/*
 * When exporting test results to xml files, cmocka doesn't escape double quotes for test names.
 * Therefore, double quotes in CMUnitTest.name will lead to invalid xml files, causing build
 * failure (with JUNIT_OUTPUT=y). As a result, we can only use _expect for CMUnitTest.name in
 * the macro, but not #_expect.
 */
#define UX_LOCALES_GET_TEXT_FOUND_TEST(_name, _lang_id, _expect)                               \
	(_UX_LOCALES_GET_TEXT_TEST                                                             \
		(                                                                              \
			(                                                                      \
				"test_ux_locales_get_text_found(name=" _name                   \
				", lang_id=" #_lang_id ", expect=" _expect ")"                 \
			),                                                                     \
			_name, _lang_id, _expect                                               \
		))

#define UX_LOCALES_GET_TEXT_NOT_FOUND_TEST(_name, _lang_id)                                    \
	(_UX_LOCALES_GET_TEXT_TEST                                                             \
		(                                                                              \
			(                                                                      \
				"test_ux_locales_get_text_not_found(name=" _name               \
				", lang_id=" #_lang_id ")"                                     \
			),                                                                     \
			_name, _lang_id, NULL                                                  \
		))

int main(void)
{
	const struct CMUnitTest tests[] = {
		/* Get text successfully. */
		UX_LOCALES_GET_TEXT_FOUND_TEST("name_1", 0, "translation_1_0"),
		/* Get text with name and id both in the middle. */
		UX_LOCALES_GET_TEXT_FOUND_TEST("name_15", 25, "translation_15_25"),
		/* Ensure we check the whole string of 'name'.
		   ('name_2' is the prefix of 'name_20') */
		UX_LOCALES_GET_TEXT_NOT_FOUND_TEST("name_2", 3),
		/* Ensure we check the whole string of 'lang_id'.
		   (id:'2' is the prefix of id:'25' in 'name_15') */
		UX_LOCALES_GET_TEXT_NOT_FOUND_TEST("name_15", 2),
		/* Ensure we will fallback to 0. */
		UX_LOCALES_GET_TEXT_FOUND_TEST("name_1", 7, "translation_1_0"),
		/* Do not search for locale id with unmatched name. */
		UX_LOCALES_GET_TEXT_NOT_FOUND_TEST("name_15", 8),
		/* Validity check of lang_id > 100. We will fallback to 0. */
		UX_LOCALES_GET_TEXT_FOUND_TEST("name_1", 100, "translation_1_0"),
		/* cbfs not found. */
		cmocka_unit_test_setup_teardown(test_ux_locales_bad_cbfs, setup_default,
						teardown_unmap),
		/* Bad version. */
		cmocka_unit_test_setup_teardown(test_ux_locales_bad_version, setup_bad_version,
						teardown_unmap),
		/* Read multiple times. */
		cmocka_unit_test_setup_teardown(test_ux_locales_two_calls, setup_default,
						teardown_unmap),
		/* Validity check of NULL terminated. */
		cmocka_unit_test_setup_teardown(test_ux_locales_null_terminated,
						setup_default, teardown_unmap),
	};

	return cb_run_group_tests(tests, NULL, NULL);
}