soc/amd/sabrina,vc/amd/fsp/sabrina: Add UART support for Sabrina
[coreboot.git] / tests / lib / cbmem_console-test.c
bloba5ca3b09331a311526cc433a16f775a14957a0eb
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include "../lib/cbmem_console.c"
5 #include <inttypes.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <tests/test.h>
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[];
16 #define PRERAM_CBMEM_CONSOLE_SIZE (1 * KiB)
17 TEST_REGION(preram_cbmem_console, PRERAM_CBMEM_CONSOLE_SIZE);
18 #endif
20 /* Disable init hooks. This test does not need them. */
21 void cbmem_run_init_hooks(int is_recovery)
23 (void)is_recovery;
26 int setup_cbmemc(void **state)
28 cbmemc_init();
29 return 0;
32 int teardown_cbmemc(void **state)
34 current_console->size = 0;
35 return 0;
38 void test_cbmemc_init(void **state)
40 cbmemc_init();
42 /* Check if console structure was created. */
43 assert_non_null(current_console);
46 void test_cbmemc_tx_byte(void **state)
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";
56 for (i = 0; i < ARRAY_SIZE(data); ++i)
57 cbmemc_tx_byte(data[i]);
59 cursor = current_console->cursor & CURSOR_MASK;
61 assert_int_equal(ARRAY_SIZE(data), cursor);
63 /* Check if all characters were added correctly. */
64 assert_memory_equal(data, current_console->body, ARRAY_SIZE(data));
67 void test_cbmemc_tx_byte_overflow(void **state)
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);
84 /* Fill console buffer */
85 for (i = 0; i < console_size; ++i)
86 cbmemc_tx_byte(data[i % data_size]);
88 /* Store buffer for checking */
89 memcpy(check_buffer, current_console->body, console_size);
91 assert_memory_equal(current_console->body, data, overflow_bytes);
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]);
97 cursor = current_console->cursor & CURSOR_MASK;
98 flags = current_console->cursor & ~CURSOR_MASK;
100 /* Check if there was a overflow in buffer */
101 assert_int_equal(OVERFLOW, flags & OVERFLOW);
103 /* Check if cursor got back to the beginning of a buffer */
104 assert_int_equal(data_size, cursor);
106 /* Check if overflow buffer was overwritten */
107 assert_memory_not_equal(current_console->body, data, overflow_bytes);
109 /* Check if rest of the buffer contents, that should not be overridden,
110 * is the same.
112 assert_memory_equal(&current_console->body[overflow_bytes],
113 check_buffer + overflow_bytes, console_size - overflow_bytes);
115 free(check_buffer);
118 int main(void)
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),
128 return cb_run_group_tests(tests, NULL, NULL);