Jakub Czapiga | f9961ff | 2020-11-16 15:25:34 +0100 | [diff] [blame] | 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. */ |
| 21 | void cbmem_run_init_hooks(int is_recovery) |
| 22 | { |
| 23 | (void)is_recovery; |
| 24 | } |
| 25 | |
| 26 | int setup_cbmemc(void **state) |
| 27 | { |
| 28 | cbmemc_init(); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int teardown_cbmemc(void **state) |
| 33 | { |
| 34 | current_console->size = 0; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 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 | |
| 46 | void test_cbmemc_tx_byte(void **state) |
| 47 | { |
| 48 | int i; |
| 49 | u32 cursor; |
| 50 | const unsigned char data[] = "Random testing string\n" |
| 51 | "`1234567890-=~!@#$%^&*()_+\n" |
| 52 | "abcdefghijklmnopqrstuvwxyz\n" |
| 53 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; |
| 54 | |
| 55 | for (i = 0; i < ARRAY_SIZE(data); ++i) |
| 56 | cbmemc_tx_byte(data[i]); |
| 57 | |
| 58 | cursor = current_console->cursor & CURSOR_MASK; |
| 59 | |
| 60 | assert_int_equal(ARRAY_SIZE(data), cursor); |
| 61 | |
| 62 | /* Check if all characters were added correctly. */ |
| 63 | assert_memory_equal(data, current_console->body, ARRAY_SIZE(data)); |
| 64 | } |
| 65 | |
| 66 | void test_cbmemc_tx_byte_overflow(void **state) |
| 67 | { |
| 68 | int i; |
| 69 | u32 cursor; |
| 70 | u32 flags; |
| 71 | const uint32_t console_size = current_console->size; |
| 72 | const unsigned char data[] = "Another random string\n" |
| 73 | "abcdefghijklmnopqrstuvwxyz\n" |
| 74 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" |
| 75 | "`1234567890-=~!@#$%^&*()_+\n"; |
| 76 | const int data_size = ARRAY_SIZE(data) - 1; |
| 77 | const int data_stream_length = console_size + data_size; |
| 78 | const int overflow_bytes = data_stream_length % console_size; |
| 79 | unsigned char *check_buffer = |
| 80 | (unsigned char *)malloc(sizeof(unsigned char) * console_size); |
| 81 | |
| 82 | /* Fill console buffer */ |
| 83 | for (i = 0; i < console_size; ++i) |
| 84 | cbmemc_tx_byte(data[i % data_size]); |
| 85 | |
| 86 | /* Store buffer for checking */ |
| 87 | memcpy(check_buffer, current_console->body, console_size); |
| 88 | |
| 89 | assert_memory_equal(current_console->body, data, overflow_bytes); |
| 90 | |
| 91 | /* Add more data to console buffer to override older bytes */ |
| 92 | for (; i < data_stream_length; ++i) |
| 93 | cbmemc_tx_byte(data[i % data_size]); |
| 94 | |
| 95 | cursor = current_console->cursor & CURSOR_MASK; |
| 96 | flags = current_console->cursor & ~CURSOR_MASK; |
| 97 | |
| 98 | /* Check if there was a overflow in buffer */ |
| 99 | assert_int_equal(OVERFLOW, flags & OVERFLOW); |
| 100 | |
| 101 | /* Check if cursor got back to the beginning of a buffer */ |
| 102 | assert_int_equal(data_size, cursor); |
| 103 | |
| 104 | /* Check if overflow buffer was overwritten */ |
| 105 | assert_memory_not_equal(current_console->body, |
| 106 | data, |
| 107 | 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, |
| 114 | console_size - overflow_bytes); |
| 115 | |
| 116 | free(check_buffer); |
| 117 | } |
| 118 | |
| 119 | int main(void) |
| 120 | { |
Jakub Czapiga | f9961ff | 2020-11-16 15:25:34 +0100 | [diff] [blame] | 121 | const struct CMUnitTest tests[] = { |
| 122 | cmocka_unit_test_teardown(test_cbmemc_init, teardown_cbmemc), |
| 123 | cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte, |
| 124 | setup_cbmemc, teardown_cbmemc), |
| 125 | cmocka_unit_test_setup_teardown(test_cbmemc_tx_byte_overflow, |
| 126 | setup_cbmemc, teardown_cbmemc), |
| 127 | }; |
| 128 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 129 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | f9961ff | 2020-11-16 15:25:34 +0100 | [diff] [blame] | 130 | } |