| 1 |
|
| 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" \ |
| 13 |
"memory_training_desc\x00" \ |
| 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" \ |
| 19 |
"8\x00low_battery_desc_8\x00" \ |
| 20 |
"\x01" \ |
| 21 |
) |
| 22 |
const unsigned char data_default[] = DATA_DEFAULT; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 40 |
void cbfs_unmap(void *mapping) |
| 41 |
{ |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 77 |
static int setup_default(void **state) |
| 78 |
{ |
| 79 |
void *ret; |
| 80 |
|
| 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 |
|
| 97 |
data.raw[0] = ~data.raw[0]; |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int teardown_unmap(void **state) |
| 102 |
{ |
| 103 |
|
| 104 |
ux_locales_unmap(); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 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 |
|
| 151 |
will_return_always(_cbfs_alloc, true); |
| 152 |
|
| 153 |
|
| 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 |
|
| 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 |
|
| 172 |
assert_non_null(ux_locales_get_text(UX_LOCALE_MSG_LOW_BATTERY)); |
| 173 |
|
| 174 |
|
| 175 |
data.raw[data.size - 1] = 'X'; |
| 176 |
data.raw[data.size - 2] = 'X'; |
| 177 |
ux_locales_unmap(); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
assert_string_equal(ux_locales_get_text(UX_LOCALE_MSG_LOW_BATTERY), |
| 183 |
"Battery low. Shutting down."); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 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 |
|
| 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 |
|
| 213 |
UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 3, "memory_training_desc_0"), |
| 214 |
|
| 215 |
UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_MEMORY_TRAINING, 100, "memory_training_desc_0"), |
| 216 |
|
| 217 |
UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_NUM, 0, "Trying to display an unknown message?"), |
| 218 |
|
| 219 |
UX_LOCALES_GET_TEXT_TEST(UX_LOCALE_MSG_LOW_BATTERY, 4, |
| 220 |
"Battery low. Shutting down."), |
| 221 |
|
| 222 |
cmocka_unit_test_setup_teardown(test_ux_locales_bad_cbfs, setup_default, |
| 223 |
teardown_unmap), |
| 224 |
|
| 225 |
cmocka_unit_test_setup_teardown(test_ux_locales_bad_version, setup_bad_version, |
| 226 |
teardown_unmap), |
| 227 |
|
| 228 |
cmocka_unit_test_setup_teardown(test_ux_locales_two_calls, setup_default, |
| 229 |
teardown_unmap), |
| 230 |
|
| 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 |
} |