blob: a5ca3b09331a311526cc433a16f775a14957a0eb [file] [log] [blame]
Jakub Czapigaf9961ff2020-11-16 15:25:34 +01001/* 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)
17TEST_REGION(preram_cbmem_console, PRERAM_CBMEM_CONSOLE_SIZE);
18#endif
19
20/* Disable init hooks. This test does not need them. */
21void cbmem_run_init_hooks(int is_recovery)
22{
23 (void)is_recovery;
24}
25
26int setup_cbmemc(void **state)
27{
28 cbmemc_init();
29 return 0;
30}
31
32int teardown_cbmemc(void **state)
33{
34 current_console->size = 0;
35 return 0;
36}
37
38void 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
46void test_cbmemc_tx_byte(void **state)
47{
48 int i;
49 u32 cursor;
Jakub Czapigac08b6a72022-01-10 13:36:47 +000050 const unsigned char data[] =
51 "Random testing string\n"
52 "`1234567890-=~!@#$%^&*()_+\n"
53 "abcdefghijklmnopqrstuvwxyz\n"
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
Jakub Czapigaf9961ff2020-11-16 15:25:34 +010055
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
67void 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;
Jakub Czapigac08b6a72022-01-10 13:36:47 +000073 const unsigned char data[] =
74 "Another random string\n"
75 "abcdefghijklmnopqrstuvwxyz\n"
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
77 "`1234567890-=~!@#$%^&*()_+\n";
Jakub Czapigaf9961ff2020-11-16 15:25:34 +010078 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 =
Jakub Czapigac08b6a72022-01-10 13:36:47 +000082 (unsigned char *)malloc(sizeof(unsigned char) * console_size);
Jakub Czapigaf9961ff2020-11-16 15:25:34 +010083
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 */
Jakub Czapigac08b6a72022-01-10 13:36:47 +0000107 assert_memory_not_equal(current_console->body, data, overflow_bytes);
Jakub Czapigaf9961ff2020-11-16 15:25:34 +0100108
109 /* Check if rest of the buffer contents, that should not be overridden,
110 * is the same.
111 */
112 assert_memory_equal(&current_console->body[overflow_bytes],
Jakub Czapigac08b6a72022-01-10 13:36:47 +0000113 check_buffer + overflow_bytes, console_size - overflow_bytes);
Jakub Czapigaf9961ff2020-11-16 15:25:34 +0100114
115 free(check_buffer);
116}
117
118int main(void)
119{
Jakub Czapigaf9961ff2020-11-16 15:25:34 +0100120 const struct CMUnitTest tests[] = {
121 cmocka_unit_test_teardown(test_cbmemc_init, teardown_cbmemc),
Jakub Czapigac08b6a72022-01-10 13:36:47 +0000122 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),
Jakub Czapigaf9961ff2020-11-16 15:25:34 +0100126 };
127
Jakub Czapiga7c6081e2021-08-25 16:27:35 +0200128 return cb_run_group_tests(tests, NULL, NULL);
Jakub Czapigaf9961ff2020-11-16 15:25:34 +0100129}