1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include "../lib/cbmem_console.c"
4
5 #include <inttypes.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <tests/test.h>
9
10
11 #if ENV_ROMSTAGE_OR_BEFORE
12 /* Weak references have to be here, so TEST_REGION macro will work properly */
13 __weak extern u8 _preram_cbmem_console[];
14 __weak extern u8 _epreram_cbmem_console[];
15
16 #define PRERAM_CBMEM_CONSOLE_SIZE (1 * KiB)
17 TEST_REGION(preram_cbmem_console, PRERAM_CBMEM_CONSOLE_SIZE);
18 #endif
19
20 /* Disable init hooks. This test does not need them. */
cbmem_run_init_hooks(int is_recovery)21 void cbmem_run_init_hooks(int is_recovery)
22 {
23 (void)is_recovery;
24 }
25
setup_cbmemc(void ** state)26 int setup_cbmemc(void **state)
27 {
28 cbmemc_init();
29 return 0;
30 }
31
teardown_cbmemc(void ** state)32 int teardown_cbmemc(void **state)
33 {
34 current_console->size = 0;
35 return 0;
36 }
37
test_cbmemc_init(void ** state)38 void test_cbmemc_init(void **state)
39 {
40 cbmemc_init();
41
42 /* Check if console structure was created. */
43 assert_non_null(current_console);
44 }
45
test_cbmemc_tx_byte(void ** state)46 void test_cbmemc_tx_byte(void **state)
47 {
48 int i;
49 u32 cursor;
50 const unsigned char data[] =
51 "Random testing string\n"
52 "`1234567890-=~!@#$%^&*()_+\n"
53 "abcdefghijklmnopqrstuvwxyz\n"
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
55
56 for (i = 0; i < ARRAY_SIZE(data); ++i)
57 cbmemc_tx_byte(data[i]);
58
59 cursor = current_console->cursor & CURSOR_MASK;
60
61 assert_int_equal(ARRAY_SIZE(data), cursor);
62
63 /* Check if all characters were added correctly. */
64 assert_memory_equal(data, current_console->body, ARRAY_SIZE(data));
65 }
66
test_cbmemc_tx_byte_overflow(void ** state)67 void test_cbmemc_tx_byte_overflow(void **state)
68 {
69 int i;
70 u32 cursor;
71 u32 flags;
72 const uint32_t console_size = current_console->size;
73 const unsigned char data[] =
74 "Another random string\n"
75 "abcdefghijklmnopqrstuvwxyz\n"
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
77 "`1234567890-=~!@#$%^&*()_+\n";
78 const int data_size = ARRAY_SIZE(data) - 1;
79 const int data_stream_length = console_size + data_size;
80 const int overflow_bytes = data_stream_length % console_size;
81 unsigned char *check_buffer =
82 (unsigned char *)malloc(sizeof(unsigned char) * console_size);
83
84 /* Fill console buffer */
85 for (i = 0; i < console_size; ++i)
86 cbmemc_tx_byte(data[i % data_size]);
87
88 /* Store buffer for checking */
89 memcpy(check_buffer, current_console->body, console_size);
90
91 assert_memory_equal(current_console->body, data, overflow_bytes);
92
93 /* Add more data to console buffer to override older bytes */
94 for (; i < data_stream_length; ++i)
95 cbmemc_tx_byte(data[i % data_size]);
96
97 cursor = current_console->cursor & CURSOR_MASK;
98 flags = current_console->cursor & ~CURSOR_MASK;
99
100 /* Check if there was a overflow in buffer */
101 assert_int_equal(OVERFLOW, flags & OVERFLOW);
102
103 /* Check if cursor got back to the beginning of a buffer */
104 assert_int_equal(data_size, cursor);
105
106 /* Check if overflow buffer was overwritten */
107 assert_memory_not_equal(current_console->body, data, overflow_bytes);
108
109 /* Check if rest of the buffer contents, that should not be overridden,
110 * is the same.
111 */
112 assert_memory_equal(¤t_console->body[overflow_bytes],
113 check_buffer + overflow_bytes, console_size - overflow_bytes);
114
115 free(check_buffer);
116 }
117
main(void)118 int main(void)
119 {
120 const struct CMUnitTest tests[] = {
121 cmocka_unit_test_teardown(test_cbmemc_init, teardown_cbmemc),
122 cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte, setup_cbmemc,
123 teardown_cbmemc),
124 cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte_overflow, setup_cbmemc,
125 teardown_cbmemc),
126 };
127
128 return cb_run_group_tests(tests, NULL, NULL);
129 }
130