Debian Codegraph @

tests/lib/ux_locales-test.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <cbfs.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <tests/test.h>
7 #include <ux_locales.h>
8 #include <vb2_api.h>
9
10 #define DATA_DEFAULT \
11 ( \
12 "\x01" /* Version. */ \
13 "memory_training_desc\x00" /* memory_training_desc, langs = [0, 2, 30]. */ \
14 "0\x00memory_training_desc_0\x00" \
15 "2\x00memory_training_desc_2\x00" \
16 "30\x00memory_training_desc_30\x00" \
17 "\x01" \
18 "low_battery_desc\x00" /* low_battery_desc, langs = [8]. */ \
19 "8\x00low_battery_desc_8\x00" \
20 "\x01" \
21 )
22 const unsigned char data_default[] = DATA_DEFAULT;
23
24 /*
25 * The data must be set in the setup function and might be used in cbfs related functions.
26 * The size of the data must be recorded because the data contains the null character \x00.
27 *
28 * Note that sizeof(DATA_DEFAULT) will count the '\0' at the end, so DATA_DEFAULT_SIZE, i.e.
29 * the real data size must be minus one.
30 */
31 #define DATA_DEFAULT_SIZE (sizeof(DATA_DEFAULT) - 1)
32
33 #define MAX_DATA_SIZE (DATA_DEFAULT_SIZE + 1)
34 struct {
35 unsigned char raw[MAX_DATA_SIZE];
36 size_t size;
37 } data;
38
39 /* Mock functions. */
40 void cbfs_unmap(void *mapping)
41 {
42 /* No-op. */
43 }
44
45 /* We can't mock cbfs_ro_map() directly as it's a static inline function. */
46 void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg,
47 size_t *size_out, bool force_ro, enum cbfs_type *type)
48 {
49 /* Mock a successful CBFS mapping. */
50 if (mock_type(bool)) {
51 *size_out = data.size;
52 return data.raw;
53 }
54 *size_out = 0;
55 return NULL;
56 }
57
58 uint32_t vb2api_get_locale_id(struct vb2_context *ctx)
59 {
60 return mock_type(uint32_t);
61 }
62
63 struct vb2_context *vboot_get_context(void)
64 {
65 static struct vb2_context vboot2_ctx;
66 return &vboot2_ctx;
67 }
68
69 /* Test states for test_ux_locales_get_text with valid CBFS data. */
70 struct ux_locales_test_state {
71 enum ux_locale_msg msg_id;
72 uint32_t lang_id;
73 const char *expect;
74 };
75
76 /* Setup and teardown functions. */
77 static int setup_default(void **state)
78 {
79 void *ret;
80 /* Setup the mocked cbfs region data. */
81 data.size = DATA_DEFAULT_SIZE;
82 ret = memset(data.raw, 0xff, MAX_DATA_SIZE);
83 if (!ret)
84 return 1;
85 ret = memcpy(data.raw, data_default, data.size);
86 if (!ret)
87 return 1;
88 return 0;
89 }
90
91 static int setup_bad_version(void **state)
92 {
93 int ret = setup_default(state);
94 if (ret)
95 return ret;
96 /* Modify the version byte. */
97 data.raw[0] = ~data.raw[0];
98 return 0;
99 }
100
101 static int teardown_unmap(void **state)
102 {
103 /* We need to reset the cached_state in src/lib/ux_locales.c. */
104 ux_locales_unmap();
105 return 0;
106 }
107
108 /* Test items. */
109 static void test_ux_locales_get_text(void **state)
110 {
111 struct ux_locales_test_state *s = *state;
112 const char *ret;
113
114 if (s->msg_id < UX_LOCALE_MSG_NUM) {
115 will_return(_cbfs_alloc, true);
116 will_return(vb2api_get_locale_id, s->lang_id);
117 }
118
119 ret = ux_locales_get_text(s->msg_id);
120 if (s->expect) {
121 assert_non_null(ret);
122 assert_string_equal(ret, s->expect);
123 } else {
124 assert_null(ret);
125 }
126 }
127
128 static void test_ux_locales_bad_cbfs(void **state)
129 {
130 will_return(_cbfs_alloc, false);
131 will_return_maybe(vb2api_get_locale_id, 0);
132 assert_string_equal(ux_locales_get_text(UX_LOCALE_MSG_MEMORY_TRAINING),
133 "Your device is finishing an update. This may take 1-2 minutes.\n"
134 "Please do not turn off your device.");
135 }
136
137 static void test_ux_locales_bad_version(void **state)
138 {
139 will_return(_cbfs_alloc, true);
140 will_return(vb2api_get_locale_id, 0);
141 assert_string_equal(ux_locales_get_text(UX_LOCALE_MSG_MEMORY_TRAINING),
142 "Your device is finishing an update. This may take 1-2 minutes.\n"
143 "Please do not turn off your device.");
144 }
145
146 static void test_ux_locales_two_calls(void **state)
147 {
148 const char *ret;
149
150 /* We do not need to ensure that we cached the cbfs region. */
151 will_return_always(_cbfs_alloc, true);
152
153 /* Call #1: read (1, 30). */
154 will_return(vb2api_get_locale_id, 30);
155 ret = ux_locales_get_text(UX_LOCALE_MSG_MEMORY_TRAINING);
156 assert_non_null(ret);
157 assert_string_equal(ret, "memory_training_desc_30");
158
159 /* Call #2: read (1, 0). */
160 will_return(vb2api_get_locale_id, 0);
161 ret = ux_locales_get_text(UX_LOCALE_MSG_MEMORY_TRAINING);
162 assert_non_null(ret);
163 assert_string_equal(ret, "memory_training_desc_0");
164 }
165
166 static void test_ux_locales_null_terminated(void **state)
167 {
168 will_return_always(_cbfs_alloc, true);
169 will_return_always(vb2api_get_locale_id, 8);
170
171 /* Verify the access to the very last text. */
172 assert_non_null(ux_locales_get_text(UX_LOCALE_MSG_LOW_BATTERY));
173
174 /* Modify the last 2 bytes from "\x00\x01" to "XX" and unmap, */
175 data.raw[data.size - 1] = 'X';
176 data.raw[data.size - 2] = 'X';
177 ux_locales_unmap();
178
179 /* The last few characters are now changed so that the data is not NULL terminated.
180 This will prevent us from accessing the last text therefore, make use of fallback
181 text message */
182 assert_string_equal(ux_locales_get_text(UX_LOCALE_MSG_LOW_BATTERY),
183 "Battery low. Shutting down.");
184 }
185
186 /*
187 * This macro helps test ux_locales_get_text with `_msg_id` and `_lang_id`.
188 * If `_expect` is NULL, then the function should not find anything.
189 * Otherwise, the function should find the corresponding expect value.
190 */
191 #define UX_LOCALES_GET_TEXT_TEST(_msg_id, _lang_id, _expect) \
192 ((struct CMUnitTest) { \
193 .name = "test_ux_locales_get_text(msg_id=" #_msg_id ", lang_id=" #_lang_id \
194 ", expect=" #_expect ")", \
195 .test_func = test_ux_locales_get_text, \
196 .setup_func = setup_default, \
197 .teardown_func = teardown_unmap, \
198 .initial_state = &(struct ux_locales_test_state) { \
199 .msg_id = _msg_id, \
200 .lang_id = _lang_id, \
201 .expect = _expect, \
202 }, \
203 })
204
205 int main(void)
206 {
207 const struct CMUnitTest tests[] = {
208 /* Get text successfully. */
209 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 0, "memory_training_desc_0"),
210 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 2, "memory_training_desc_2"),
211 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_LOW_BATTERY, 8, "low_battery_desc_8"),
212 /* Check the whole string of lang_id. */
213 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 3, "memory_training_desc_0"),
214 /* Validity check of lang_id > 100. We will fallback to 0. */
215 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 100, "memory_training_desc_0"),
216 /* Ensure we show fallback message if `msg_id >= UX_LOCALE_MSG_NUM` */
217 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_NUM, 0, "Trying to display an unknown message?"),
218 /* Do not search for locale id with unmatched name. */
219 UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_LOW_BATTERY, 4,
220 "Battery low. Shutting down."),
221 /* cbfs not found. */
222 cmocka_unit_test_setup_teardown(test_ux_locales_bad_cbfs, setup_default,
223 teardown_unmap),
224 /* Bad version. */
225 cmocka_unit_test_setup_teardown(test_ux_locales_bad_version, setup_bad_version,
226 teardown_unmap),
227 /* Read multiple times. */
228 cmocka_unit_test_setup_teardown(test_ux_locales_two_calls, setup_default,
229 teardown_unmap),
230 /* Validity check of NULL terminated. */
231 cmocka_unit_test_setup_teardown(test_ux_locales_null_terminated,
232 setup_default, teardown_unmap),
233 };
234
235 return cb_run_group_tests(tests, NULL, NULL);
236 }