| Andrew Walbran | f3ac84d | 2019-07-26 16:10:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
| Andrew Walbran | f3ac84d | 2019-07-26 16:10:02 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/dlog.h" |
| 10 | |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 11 | #include <limits.h> |
| 12 | #include <stddef.h> |
| 13 | |
| 14 | #include "hf/ffa.h" |
| 15 | |
| Andrew Walbran | 1e7c774 | 2019-12-13 17:10:02 +0000 | [diff] [blame] | 16 | #include "test/hftest.h" |
| Andrew Walbran | f3ac84d | 2019-07-26 16:10:02 +0100 | [diff] [blame] | 17 | |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 18 | #define assert_format(expected, ...) \ |
| 19 | assert_format_impl(expected, sizeof(expected), __VA_ARGS__) |
| Andrew Walbran | f3ac84d | 2019-07-26 16:10:02 +0100 | [diff] [blame] | 20 | |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 21 | static void assert_format_impl(char* expected, size_t expected_len, |
| 22 | const char* fmt, ...) |
| 23 | { |
| 24 | va_list args; |
| 25 | size_t chars_written; |
| 26 | |
| 27 | memset_s(dlog_buffer, DLOG_BUFFER_SIZE, 0, DLOG_BUFFER_SIZE); |
| 28 | dlog_buffer_offset = 0; |
| 29 | |
| 30 | va_start(args, fmt); |
| 31 | chars_written = vdlog(fmt, args); |
| 32 | va_end(args); |
| 33 | |
| 34 | ASSERT_NE(chars_written, (size_t)-1); |
| 35 | ASSERT_EQ(chars_written, expected_len - 1); |
| 36 | |
| 37 | dlog_buffer[chars_written] = '\0'; |
| 38 | ASSERT_STRING_EQ(expected, dlog_buffer); |
| 39 | for (size_t i = expected_len - 1; i < DLOG_BUFFER_SIZE; ++i) { |
| Andrew Walbran | f3ac84d | 2019-07-26 16:10:02 +0100 | [diff] [blame] | 40 | EXPECT_EQ(dlog_buffer[i], '\0'); |
| 41 | } |
| 42 | } |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Test formatting of a format string with no format specifiers |
| 46 | */ |
| 47 | TEST(dlog, no_format_specifiers) |
| 48 | { |
| 49 | assert_format("Hello world\n", "Hello world\n"); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Test formatting of a format string with a percent format specifier (`%%`) |
| 54 | */ |
| 55 | TEST(dlog, percent_format_specifier) |
| 56 | { |
| 57 | assert_format("Hello %\n", "Hello %%\n"); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Test formatting of a format string with a char format specifier (`%c`) |
| 62 | */ |
| 63 | TEST(dlog, char_format_specifier) |
| 64 | { |
| 65 | assert_format("Hello w\n", "Hello %c\n", 'w'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Test formatting of a format string with a string format specifier (`%s`) |
| 70 | */ |
| 71 | TEST(dlog, string_format_specifier) |
| 72 | { |
| 73 | assert_format("Hello world\n", "Hello %s\n", "world"); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Test formatting of a format string with a `int` format specifier (`%d`/`%i`) |
| 78 | */ |
| 79 | TEST(dlog, int_format_specifier) |
| 80 | { |
| 81 | assert_format("Hello 0\n", "Hello %i\n", 0); |
| 82 | assert_format("Hello 0\n", "Hello %d\n", 0); |
| 83 | assert_format("Hello 1234\n", "Hello %d\n", 1234); |
| 84 | |
| 85 | assert_format("Hello 2147483647\n", "Hello %d\n", INT_MAX); |
| 86 | assert_format("Hello -2147483648\n", "Hello %d\n", INT_MIN); |
| 87 | |
| 88 | assert_format("Hello -1\n", "Hello %d\n", UINT_MAX); |
| 89 | |
| 90 | assert_format("Hello -2147483648\n", "Hello %d\n", |
| 91 | ((int64_t)INT_MAX) + 1); |
| 92 | assert_format("Hello 2147483647\n", "Hello %d\n", |
| 93 | ((int64_t)INT_MIN) - 1); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Test formatting of a format string with a `unsigned int` format specifier |
| 98 | * (`%u`) |
| 99 | */ |
| 100 | TEST(dlog, unsigned_int_format_specifier) |
| 101 | { |
| 102 | assert_format("Hello 0\n", "Hello %u\n", 0); |
| 103 | assert_format("Hello 1234567890\n", "Hello %u\n", 1234567890); |
| 104 | |
| 105 | assert_format("Hello 4294967295\n", "Hello %u\n", UINT_MAX); |
| Karl Meakin | 70894da | 2024-03-30 21:06:12 +0000 | [diff] [blame^] | 106 | assert_format("Hello 0\n", "Hello %u\n", ((uint64_t)UINT_MAX) + 1); |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 107 | |
| 108 | assert_format("Hello 2147483648\n", "Hello %u\n", INT_MIN); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Test formatting of a format string with an octal `unsigned int` format |
| 113 | * specifier (`%o`) |
| 114 | */ |
| 115 | TEST(dlog, octal_unsigned_int_format_specifier) |
| 116 | { |
| 117 | assert_format("Hello 0\n", "Hello %o\n", 0); |
| 118 | assert_format("Hello 12345670\n", "Hello %o\n", 012345670); |
| 119 | |
| 120 | assert_format("Hello 37777777777\n", "Hello %o\n", UINT_MAX); |
| Karl Meakin | 70894da | 2024-03-30 21:06:12 +0000 | [diff] [blame^] | 121 | assert_format("Hello 0\n", "Hello %o\n", ((uint64_t)UINT_MAX) + 1); |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 122 | |
| 123 | assert_format("Hello 20000000000\n", "Hello %o\n", INT_MIN); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test formatting of a format string with a hexadecimal `unsigned int` format |
| 128 | * specifier (`%x`) |
| 129 | */ |
| 130 | TEST(dlog, hexadecimal_unsigned_int_format_specifier) |
| 131 | { |
| 132 | assert_format("Hello 0\n", "Hello %x\n", 0); |
| 133 | assert_format("Hello 12345678\n", "Hello %x\n", 0x12345678); |
| 134 | assert_format("Hello 9abcdef\n", "Hello %x\n", 0x9abcdef); |
| 135 | |
| 136 | assert_format("Hello 0x0\n", "Hello %#x\n", 0); |
| 137 | assert_format("Hello 0x12345678\n", "Hello %#x\n", 0x12345678); |
| 138 | assert_format("Hello 0x9abcdef\n", "Hello %#x\n", 0x9abcdef); |
| 139 | |
| 140 | assert_format("Hello 0X0\n", "Hello %#X\n", 0); |
| 141 | assert_format("Hello 0X12345678\n", "Hello %#X\n", 0x12345678); |
| 142 | assert_format("Hello 0X9ABCDEF\n", "Hello %#X\n", 0x9abcdef); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Test formatting of a format string with a `void*` format |
| 147 | * specifier (`%p`) |
| 148 | */ |
| 149 | TEST(dlog, pointer_format_specifier) |
| 150 | { |
| Karl Meakin | e980e61 | 2024-03-28 17:46:32 +0000 | [diff] [blame] | 151 | assert_format("Hello 0x0000000000000000\n", "Hello %p\n", 0); |
| 152 | assert_format("Hello 0x123456789abcdef0\n", "Hello %p\n", |
| Karl Meakin | 93157d0 | 2024-03-26 22:57:53 +0000 | [diff] [blame] | 153 | 0x123456789abcdef0); |
| 154 | } |