Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1 | #include <endian.h> |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 2 | #include <libpldm/utils.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 3 | |
| 4 | #include <cfloat> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | /* We're exercising the implementation so disable the asserts for now */ |
| 9 | #ifndef NDEBUG |
| 10 | #define NDEBUG 1 |
| 11 | #endif |
| 12 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 13 | #include "msgbuf.h" |
| 14 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 15 | TEST(msgbuf, init_bad_minsize) |
| 16 | { |
| 17 | struct pldm_msgbuf _ctx; |
| 18 | struct pldm_msgbuf* ctx = &_ctx; |
| 19 | uint8_t buf[1] = {}; |
| 20 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 21 | EXPECT_NE(pldm_msgbuf_init_errno(ctx, sizeof(buf) + 1U, buf, sizeof(buf)), |
| 22 | 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 23 | } |
| 24 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 25 | TEST(msgbuf, init_bad_len) |
| 26 | { |
| 27 | struct pldm_msgbuf _ctx; |
| 28 | struct pldm_msgbuf* ctx = &_ctx; |
| 29 | uint8_t buf[1] = {}; |
| 30 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 31 | EXPECT_NE(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, SIZE_MAX), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | TEST(msgbuf, init_overflow) |
| 35 | { |
| 36 | struct pldm_msgbuf _ctx; |
| 37 | struct pldm_msgbuf* ctx = &_ctx; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 38 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 39 | void* buf = (void*)UINTPTR_MAX; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 40 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 41 | EXPECT_NE(pldm_msgbuf_init_errno(ctx, 0, buf, 2), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(msgbuf, init_success) |
| 45 | { |
| 46 | struct pldm_msgbuf _ctx; |
| 47 | struct pldm_msgbuf* ctx = &_ctx; |
| 48 | uint8_t buf[1] = {}; |
| 49 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 50 | EXPECT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST(msgbuf, destroy_none) |
| 54 | { |
| 55 | struct pldm_msgbuf _ctx; |
| 56 | struct pldm_msgbuf* ctx = &_ctx; |
| 57 | uint8_t buf[1] = {}; |
| 58 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 59 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 60 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST(msgbuf, destroy_exact) |
| 64 | { |
| 65 | struct pldm_msgbuf _ctx; |
| 66 | struct pldm_msgbuf* ctx = &_ctx; |
| 67 | uint8_t buf[1] = {0xa5}; |
| 68 | uint8_t val; |
| 69 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 70 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 71 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 72 | EXPECT_EQ(val, 0xa5); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 73 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(msgbuf, destroy_over) |
| 77 | { |
| 78 | struct pldm_msgbuf _ctx; |
| 79 | struct pldm_msgbuf* ctx = &_ctx; |
| 80 | uint8_t buf[1] = {0xa5}; |
| 81 | uint8_t val; |
| 82 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 83 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 84 | ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 85 | ASSERT_EQ(val, 0xa5); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 86 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0); |
| 87 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(msgbuf, destroy_under) |
| 91 | { |
| 92 | struct pldm_msgbuf _ctx; |
| 93 | struct pldm_msgbuf* ctx = &_ctx; |
| 94 | uint8_t buf[2] = {0x5a, 0xa5}; |
| 95 | uint8_t val; |
| 96 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 97 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 98 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 99 | EXPECT_EQ(val, 0x5a); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 100 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | TEST(msgbuf, extract_one_uint8) |
| 104 | { |
| 105 | struct pldm_msgbuf _ctx; |
| 106 | struct pldm_msgbuf* ctx = &_ctx; |
| 107 | uint8_t buf[1] = {0xa5}; |
| 108 | uint8_t val; |
| 109 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 110 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 111 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 112 | EXPECT_EQ(val, 0xa5); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 113 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST(msgbuf, extract_over_uint8) |
| 117 | { |
| 118 | struct pldm_msgbuf _ctx; |
| 119 | struct pldm_msgbuf* ctx = &_ctx; |
| 120 | uint8_t buf[1] = {}; |
| 121 | uint8_t val; |
| 122 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 123 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 124 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0); |
| 125 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 126 | } |
| 127 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 128 | TEST(msgbuf, extract_under_uint8) |
| 129 | { |
| 130 | struct pldm_msgbuf _ctx; |
| 131 | struct pldm_msgbuf* ctx = &_ctx; |
| 132 | |
| 133 | uint8_t buf[1] = {}; |
| 134 | uint8_t val; |
| 135 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 136 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 137 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 138 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0); |
| 139 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 140 | } |
| 141 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 142 | TEST(msgbuf, extract_one_int8) |
| 143 | { |
| 144 | struct pldm_msgbuf _ctx; |
| 145 | struct pldm_msgbuf* ctx = &_ctx; |
| 146 | int8_t buf[1] = {-1}; |
| 147 | int8_t val; |
| 148 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 149 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 150 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 151 | EXPECT_EQ(val, -1); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 152 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST(msgbuf, extract_over_int8) |
| 156 | { |
| 157 | struct pldm_msgbuf _ctx; |
| 158 | struct pldm_msgbuf* ctx = &_ctx; |
| 159 | int8_t buf[1] = {}; |
| 160 | int8_t val; |
| 161 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 162 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 163 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), 0); |
| 164 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 167 | TEST(msgbuf, extract_under_int8) |
| 168 | { |
| 169 | struct pldm_msgbuf _ctx; |
| 170 | struct pldm_msgbuf* ctx = &_ctx; |
| 171 | |
| 172 | uint8_t buf[1] = {}; |
| 173 | int8_t val; |
| 174 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 175 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 176 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 177 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), 0); |
| 178 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 179 | } |
| 180 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 181 | TEST(msgbuf, extract_one_uint16) |
| 182 | { |
| 183 | struct pldm_msgbuf _ctx; |
| 184 | struct pldm_msgbuf* ctx = &_ctx; |
| 185 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 186 | uint16_t val = {}; |
| 187 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 188 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 189 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 190 | EXPECT_EQ(val, 0x5aa5); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 191 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 192 | } |
| 193 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 194 | TEST(msgbuf, extract_under_uint16) |
| 195 | { |
| 196 | struct pldm_msgbuf _ctx; |
| 197 | struct pldm_msgbuf* ctx = &_ctx; |
| 198 | |
| 199 | uint16_t buf[1] = {}; |
| 200 | uint16_t val; |
| 201 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 202 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 203 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 204 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), 0); |
| 205 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 206 | } |
| 207 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 208 | TEST(msgbuf, extract_over_uint16) |
| 209 | { |
| 210 | struct pldm_msgbuf _ctx; |
| 211 | struct pldm_msgbuf* ctx = &_ctx; |
| 212 | uint16_t buf[1] = {}; |
| 213 | uint16_t val; |
| 214 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 215 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 216 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), 0); |
| 217 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | TEST(msgbuf, extract_one_int16) |
| 221 | { |
| 222 | struct pldm_msgbuf _ctx; |
| 223 | struct pldm_msgbuf* ctx = &_ctx; |
| 224 | int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))}; |
| 225 | int16_t val; |
| 226 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 227 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 228 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 229 | EXPECT_EQ(val, INT16_MIN); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 230 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST(msgbuf, extract_over_int16) |
| 234 | { |
| 235 | struct pldm_msgbuf _ctx; |
| 236 | struct pldm_msgbuf* ctx = &_ctx; |
| 237 | int16_t buf[1] = {}; |
| 238 | int16_t val; |
| 239 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 240 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 241 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), 0); |
| 242 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 243 | } |
| 244 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 245 | TEST(msgbuf, extract_under_int16) |
| 246 | { |
| 247 | struct pldm_msgbuf _ctx; |
| 248 | struct pldm_msgbuf* ctx = &_ctx; |
| 249 | |
| 250 | int16_t buf[1] = {}; |
| 251 | int16_t val; |
| 252 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 253 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 254 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 255 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), 0); |
| 256 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 257 | } |
| 258 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 259 | TEST(msgbuf, extract_one_uint32) |
| 260 | { |
| 261 | struct pldm_msgbuf _ctx; |
| 262 | struct pldm_msgbuf* ctx = &_ctx; |
| 263 | uint32_t buf[1] = {htole32(0x5a00ffa5)}; |
| 264 | uint32_t val; |
| 265 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 266 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 267 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 268 | EXPECT_EQ(val, 0x5a00ffa5); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 269 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST(msgbuf, extract_over_uint32) |
| 273 | { |
| 274 | struct pldm_msgbuf _ctx; |
| 275 | struct pldm_msgbuf* ctx = &_ctx; |
| 276 | uint32_t buf[1] = {}; |
| 277 | uint32_t val; |
| 278 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 279 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 280 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), 0); |
| 281 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 282 | } |
| 283 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 284 | TEST(msgbuf, extract_under_uint32) |
| 285 | { |
| 286 | struct pldm_msgbuf _ctx; |
| 287 | struct pldm_msgbuf* ctx = &_ctx; |
| 288 | |
| 289 | uint32_t buf[1] = {}; |
| 290 | uint32_t val; |
| 291 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 292 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 293 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 294 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), 0); |
| 295 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 296 | } |
| 297 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 298 | TEST(msgbuf, extract_one_int32) |
| 299 | { |
| 300 | struct pldm_msgbuf _ctx; |
| 301 | struct pldm_msgbuf* ctx = &_ctx; |
| 302 | int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))}; |
| 303 | int32_t val; |
| 304 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 305 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 306 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 307 | EXPECT_EQ(val, INT32_MIN); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 308 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(msgbuf, extract_over_int32) |
| 312 | { |
| 313 | struct pldm_msgbuf _ctx; |
| 314 | struct pldm_msgbuf* ctx = &_ctx; |
| 315 | int32_t buf[1] = {}; |
| 316 | int32_t val; |
| 317 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 318 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 319 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), 0); |
| 320 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 321 | } |
| 322 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 323 | TEST(msgbuf, extract_under_int32) |
| 324 | { |
| 325 | struct pldm_msgbuf _ctx; |
| 326 | struct pldm_msgbuf* ctx = &_ctx; |
| 327 | |
| 328 | int32_t buf[1] = {}; |
| 329 | int32_t val; |
| 330 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 331 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 332 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 333 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), 0); |
| 334 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 335 | } |
| 336 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 337 | TEST(msgbuf, extract_one_real32) |
| 338 | { |
| 339 | struct pldm_msgbuf _ctx; |
| 340 | struct pldm_msgbuf* ctx = &_ctx; |
| 341 | uint32_t buf[1] = {}; |
| 342 | uint32_t xform; |
| 343 | real32_t val; |
| 344 | |
| 345 | val = FLT_MAX; |
| 346 | memcpy(&xform, &val, sizeof(val)); |
| 347 | buf[0] = htole32(xform); |
| 348 | val = 0; |
| 349 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 350 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0); |
| 351 | EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, val), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 352 | EXPECT_EQ(val, FLT_MAX); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 353 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | TEST(msgbuf, extract_over_real32) |
| 357 | { |
| 358 | struct pldm_msgbuf _ctx; |
| 359 | struct pldm_msgbuf* ctx = &_ctx; |
| 360 | real32_t buf[1] = {}; |
| 361 | real32_t val; |
| 362 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 363 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 364 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), 0); |
| 365 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 366 | } |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 367 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 368 | TEST(msgbuf, extract_under_real32) |
| 369 | { |
| 370 | struct pldm_msgbuf _ctx; |
| 371 | struct pldm_msgbuf* ctx = &_ctx; |
| 372 | |
| 373 | real32_t buf[1] = {}; |
| 374 | real32_t val; |
| 375 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 376 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 377 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 378 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), 0); |
| 379 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 380 | } |
| 381 | |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 382 | TEST(msgbuf, extract_array_uint8_buf0_req0) |
| 383 | { |
| 384 | struct pldm_msgbuf _ctx; |
| 385 | struct pldm_msgbuf* ctx = &_ctx; |
| 386 | uint8_t buf[1] = {}; |
| 387 | uint8_t arr[1]; |
| 388 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 389 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 390 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), 0); |
| 391 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | TEST(msgbuf, extract_array_uint8_buf1_req1) |
| 395 | { |
| 396 | struct pldm_msgbuf _ctx; |
| 397 | struct pldm_msgbuf* ctx = &_ctx; |
| 398 | uint8_t buf[1] = {}; |
| 399 | uint8_t arr[1]; |
| 400 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 401 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 402 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 403 | pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 404 | EXPECT_EQ(arr[0], 0); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 405 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST(msgbuf, extract_array_uint8_buf1_req2) |
| 409 | { |
| 410 | struct pldm_msgbuf _ctx; |
| 411 | struct pldm_msgbuf* ctx = &_ctx; |
| 412 | uint8_t buf[1] = {}; |
| 413 | uint8_t arr[2]; |
| 414 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 415 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 416 | EXPECT_NE( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 417 | pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
| 418 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 419 | } |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 420 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 421 | TEST(msgbuf, extract_under_array_uint8) |
| 422 | { |
| 423 | struct pldm_msgbuf _ctx; |
| 424 | struct pldm_msgbuf* ctx = &_ctx; |
| 425 | uint8_t buf[1] = {}; |
| 426 | uint8_t arr[1]; |
| 427 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 428 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 429 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 430 | EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), 0); |
| 431 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 432 | } |
| 433 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 434 | TEST(msgbuf, extract_array_char_buf0_req0) |
| 435 | { |
| 436 | struct pldm_msgbuf _ctx; |
| 437 | struct pldm_msgbuf* ctx = &_ctx; |
| 438 | char buf[1] = {'\0'}; |
| 439 | char arr[1] = {'1'}; |
| 440 | |
| 441 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 442 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 443 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 444 | } |
| 445 | |
| 446 | TEST(msgbuf, extract_array_char_buf1_req1) |
| 447 | { |
| 448 | struct pldm_msgbuf _ctx; |
| 449 | struct pldm_msgbuf* ctx = &_ctx; |
| 450 | char buf[1] = {'\0'}; |
| 451 | char arr[1] = {'1'}; |
| 452 | |
| 453 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 454 | EXPECT_EQ( |
| 455 | pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 456 | EXPECT_EQ(arr[0], '\0'); |
| 457 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 458 | } |
| 459 | |
| 460 | TEST(msgbuf, extract_array_char_buf1_req2) |
| 461 | { |
| 462 | struct pldm_msgbuf _ctx; |
| 463 | struct pldm_msgbuf* ctx = &_ctx; |
| 464 | char buf[1] = {'\0'}; |
| 465 | char arr[2] = {'1', '2'}; |
| 466 | |
| 467 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 468 | EXPECT_NE( |
| 469 | pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 470 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 471 | } |
| 472 | |
| 473 | TEST(msgbuf, extract_under_array_char) |
| 474 | { |
| 475 | struct pldm_msgbuf _ctx; |
| 476 | struct pldm_msgbuf* ctx = &_ctx; |
| 477 | char buf[1] = {'\0'}; |
| 478 | char arr[1] = {'1'}; |
| 479 | |
| 480 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 481 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 482 | EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 483 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 484 | } |
| 485 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 486 | TEST(msgbuf, consumed_under) |
| 487 | { |
| 488 | struct pldm_msgbuf _ctx; |
| 489 | struct pldm_msgbuf* ctx = &_ctx; |
| 490 | uint8_t buf[1] = {}; |
| 491 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 492 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 493 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), -EBADMSG); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | TEST(msgbuf, consumed_exact) |
| 497 | { |
| 498 | struct pldm_msgbuf _ctx; |
| 499 | struct pldm_msgbuf* ctx = &_ctx; |
| 500 | uint8_t buf[1] = {}; |
| 501 | uint8_t val; |
| 502 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 503 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 504 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0); |
| 505 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), 0); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | TEST(msgbuf, consumed_over) |
| 509 | { |
| 510 | struct pldm_msgbuf _ctx; |
| 511 | struct pldm_msgbuf* ctx = &_ctx; |
| 512 | uint8_t buf[1] = {}; |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 513 | uint8_t valid; |
| 514 | uint8_t invalid; |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 515 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 516 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 517 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, valid), 0); |
| 518 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, invalid), 0); |
| 519 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), -EBADMSG); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 520 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 521 | |
| 522 | TEST(msgbuf, pldm_msgbuf_insert_int32_good) |
| 523 | { |
| 524 | struct pldm_msgbuf _ctx; |
| 525 | struct pldm_msgbuf* ctx = &_ctx; |
| 526 | int32_t src = -12345; |
| 527 | int32_t checkVal = 0; |
| 528 | uint8_t buf[sizeof(int32_t)] = {}; |
| 529 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 530 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 531 | EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 532 | |
| 533 | struct pldm_msgbuf _ctxExtract; |
| 534 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 535 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 536 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 537 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 538 | |
| 539 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 540 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 541 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 542 | } |
| 543 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 544 | TEST(msgbuf, insert_under_int32) |
| 545 | { |
| 546 | struct pldm_msgbuf _ctx; |
| 547 | struct pldm_msgbuf* ctx = &_ctx; |
| 548 | |
| 549 | int32_t buf[1] = {}; |
| 550 | int32_t val = 0; |
| 551 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 552 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 553 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 554 | EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), 0); |
| 555 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 556 | } |
| 557 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 558 | TEST(msgbuf, pldm_msgbuf_insert_uint32_good) |
| 559 | { |
| 560 | struct pldm_msgbuf _ctx; |
| 561 | struct pldm_msgbuf* ctx = &_ctx; |
| 562 | uint32_t src = 0xf1223344; |
| 563 | uint32_t checkVal = 0; |
| 564 | uint8_t buf[sizeof(uint32_t)] = {}; |
| 565 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 566 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 567 | EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 568 | |
| 569 | struct pldm_msgbuf _ctxExtract; |
| 570 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 571 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 572 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 573 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 574 | |
| 575 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 576 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 577 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 578 | } |
| 579 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 580 | TEST(msgbuf, insert_under_uint32) |
| 581 | { |
| 582 | struct pldm_msgbuf _ctx; |
| 583 | struct pldm_msgbuf* ctx = &_ctx; |
| 584 | |
| 585 | uint32_t buf[1] = {}; |
| 586 | uint32_t val = 0; |
| 587 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 588 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 589 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 590 | EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), 0); |
| 591 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 592 | } |
| 593 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 594 | TEST(msgbuf, pldm_msgbuf_insert_uint16_good) |
| 595 | { |
| 596 | struct pldm_msgbuf _ctx; |
| 597 | struct pldm_msgbuf* ctx = &_ctx; |
| 598 | uint16_t src = 0xf344; |
| 599 | uint16_t checkVal = 0; |
| 600 | uint8_t buf[sizeof(uint16_t)] = {}; |
| 601 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 602 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)), 0); |
| 603 | EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 604 | |
| 605 | struct pldm_msgbuf _ctxExtract; |
| 606 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 607 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 608 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 609 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 610 | |
| 611 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 612 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 613 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 614 | } |
| 615 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 616 | TEST(msgbuf, insert_under_uint16) |
| 617 | { |
| 618 | struct pldm_msgbuf _ctx; |
| 619 | struct pldm_msgbuf* ctx = &_ctx; |
| 620 | |
| 621 | uint16_t buf[1] = {}; |
| 622 | uint16_t val = 0; |
| 623 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 624 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 625 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 626 | EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), 0); |
| 627 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 628 | } |
| 629 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 630 | TEST(msgbuf, pldm_msgbuf_insert_int16_good) |
| 631 | { |
| 632 | struct pldm_msgbuf _ctx; |
| 633 | struct pldm_msgbuf* ctx = &_ctx; |
| 634 | int16_t src = -12; |
| 635 | int16_t checkVal = 0; |
| 636 | uint8_t buf[sizeof(int16_t)] = {}; |
| 637 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 638 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)), 0); |
| 639 | EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 640 | |
| 641 | struct pldm_msgbuf _ctxExtract; |
| 642 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 643 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 644 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 645 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 646 | |
| 647 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 648 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 649 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 650 | } |
| 651 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 652 | TEST(msgbuf, insert_under_int16) |
| 653 | { |
| 654 | struct pldm_msgbuf _ctx; |
| 655 | struct pldm_msgbuf* ctx = &_ctx; |
| 656 | |
| 657 | int16_t buf[1] = {}; |
| 658 | int16_t val = 0; |
| 659 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 660 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 661 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 662 | EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), 0); |
| 663 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 664 | } |
| 665 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 666 | TEST(msgbuf, pldm_msgbuf_insert_uint8_good) |
| 667 | { |
| 668 | struct pldm_msgbuf _ctx; |
| 669 | struct pldm_msgbuf* ctx = &_ctx; |
| 670 | uint8_t src = 0xf4; |
| 671 | uint8_t checkVal = 0; |
| 672 | uint8_t buf[sizeof(uint8_t)] = {}; |
| 673 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 674 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 675 | EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 676 | |
| 677 | struct pldm_msgbuf _ctxExtract; |
| 678 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 679 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 680 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 681 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 682 | |
| 683 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 684 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 685 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 686 | } |
| 687 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 688 | TEST(msgbuf, insert_under_uint8) |
| 689 | { |
| 690 | struct pldm_msgbuf _ctx; |
| 691 | struct pldm_msgbuf* ctx = &_ctx; |
| 692 | |
| 693 | uint8_t buf[1] = {}; |
| 694 | uint8_t val = 0; |
| 695 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 696 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 697 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 698 | EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), 0); |
| 699 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 700 | } |
| 701 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 702 | TEST(msgbuf, pldm_msgbuf_insert_int8_good) |
| 703 | { |
| 704 | struct pldm_msgbuf _ctx; |
| 705 | struct pldm_msgbuf* ctx = &_ctx; |
| 706 | int8_t src = -4; |
| 707 | int8_t checkVal = 0; |
| 708 | uint8_t buf[sizeof(int8_t)] = {}; |
| 709 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 710 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 711 | EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 712 | |
| 713 | struct pldm_msgbuf _ctxExtract; |
| 714 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 715 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 716 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 717 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, checkVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 718 | |
| 719 | EXPECT_EQ(src, checkVal); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 720 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 721 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 722 | } |
| 723 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 724 | TEST(msgbuf, insert_under_int8) |
| 725 | { |
| 726 | struct pldm_msgbuf _ctx; |
| 727 | struct pldm_msgbuf* ctx = &_ctx; |
| 728 | |
| 729 | int8_t buf[1] = {}; |
| 730 | int8_t val = 0; |
| 731 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 732 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 733 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 734 | EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), 0); |
| 735 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 736 | } |
| 737 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 738 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good) |
| 739 | { |
| 740 | struct pldm_msgbuf _ctx; |
| 741 | struct pldm_msgbuf* ctx = &_ctx; |
| 742 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 743 | uint8_t buf[6] = {}; |
| 744 | uint8_t retBuff[6] = {}; |
| 745 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 746 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 747 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 748 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 749 | |
| 750 | struct pldm_msgbuf _ctxExtract; |
| 751 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 752 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 753 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 754 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff), |
| 755 | retBuff, sizeof(retBuff)), |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 756 | 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 757 | |
| 758 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 759 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 760 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 761 | } |
| 762 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 763 | TEST(msgbuf, insert_under_array_uint8) |
| 764 | { |
| 765 | struct pldm_msgbuf _ctx; |
| 766 | struct pldm_msgbuf* ctx = &_ctx; |
| 767 | |
| 768 | uint8_t buf[1] = {}; |
| 769 | uint8_t val[1] = {0}; |
| 770 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 771 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 772 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 773 | EXPECT_NE( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 774 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)), 0); |
| 775 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 776 | } |
| 777 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 778 | TEST(msgbuf, pldm_msgbuf_insert_array_char_good) |
| 779 | { |
| 780 | struct pldm_msgbuf _ctx; |
| 781 | struct pldm_msgbuf* ctx = &_ctx; |
| 782 | char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 783 | char buf[6] = {}; |
| 784 | char retBuff[6] = {}; |
| 785 | |
| 786 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 787 | EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)), |
| 788 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 789 | |
| 790 | struct pldm_msgbuf _ctxExtract; |
| 791 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 792 | |
| 793 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 794 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff), |
| 795 | retBuff, sizeof(retBuff)), |
| 796 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 797 | |
| 798 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 799 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 800 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 801 | } |
| 802 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 803 | TEST(msgbuf, insert_under_array_char) |
| 804 | { |
| 805 | struct pldm_msgbuf _ctx; |
| 806 | struct pldm_msgbuf* ctx = &_ctx; |
| 807 | char buf[1] = {}; |
| 808 | char val[1] = {0}; |
| 809 | |
| 810 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 811 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 812 | EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)), |
| 813 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 814 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 815 | } |
| 816 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 817 | TEST(msgbuf, pldm_msgbuf_span_required_good) |
| 818 | { |
| 819 | struct pldm_msgbuf _ctx; |
| 820 | struct pldm_msgbuf* ctx = &_ctx; |
| 821 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 822 | uint8_t buf[6] = {0}; |
| 823 | const size_t required = 4; |
| 824 | uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77}; |
| 825 | uint16_t testVal; |
| 826 | uint8_t* retBuff = NULL; |
| 827 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 828 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 829 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 830 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 831 | |
| 832 | struct pldm_msgbuf _ctxExtract; |
| 833 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 834 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 835 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 836 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 837 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff), |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 838 | 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 839 | |
| 840 | EXPECT_EQ(memcmp(expectData, retBuff, required), 0); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 841 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 842 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | TEST(msgbuf, pldm_msgbuf_span_required_bad) |
| 846 | { |
| 847 | struct pldm_msgbuf _ctx; |
| 848 | struct pldm_msgbuf* ctx = &_ctx; |
| 849 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 850 | uint8_t buf[6] = {0}; |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 851 | const size_t required = 4; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 852 | uint16_t testVal; |
| 853 | [[maybe_unused]] uint8_t* retBuff = NULL; |
| 854 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 855 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 856 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 857 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 858 | |
| 859 | struct pldm_msgbuf _ctxExtract; |
| 860 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 861 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 862 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 863 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
| 864 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 865 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 866 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 867 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 868 | } |
| 869 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 870 | TEST(msgbuf, span_required_under) |
| 871 | { |
| 872 | struct pldm_msgbuf _ctx; |
| 873 | struct pldm_msgbuf* ctx = &_ctx; |
| 874 | |
| 875 | uint8_t buf[1] = {}; |
| 876 | void* cursor = nullptr; |
| 877 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 878 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 879 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 880 | EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), 0); |
| 881 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 882 | } |
| 883 | |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 884 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good) |
| 885 | { |
| 886 | struct pldm_msgbuf _ctxExtract; |
| 887 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 888 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 889 | constexpr size_t required = 6; |
| 890 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 891 | uint16_t testVal; |
| 892 | uint8_t testVal1; |
| 893 | char* retBuff = NULL; |
| 894 | |
| 895 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 896 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 897 | EXPECT_EQ(0x2211, testVal); |
| 898 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 899 | 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 900 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 901 | EXPECT_EQ(0x77, testVal1); |
| 902 | |
| 903 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 904 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 905 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 906 | } |
| 907 | |
| 908 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length) |
| 909 | { |
| 910 | struct pldm_msgbuf _ctxExtract; |
| 911 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 912 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 913 | constexpr size_t required = 6; |
| 914 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 915 | uint16_t testVal; |
| 916 | uint8_t testVal1; |
| 917 | char* retBuff = NULL; |
| 918 | size_t length; |
| 919 | |
| 920 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 921 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 922 | EXPECT_EQ(0x2211, testVal); |
| 923 | EXPECT_EQ( |
| 924 | pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length), |
| 925 | 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 926 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 927 | EXPECT_EQ(0x77, testVal1); |
| 928 | |
| 929 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 930 | EXPECT_EQ(length, strlen(retBuff) + 1); |
| 931 | EXPECT_EQ(required, length); |
| 932 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 933 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 934 | } |
| 935 | |
| 936 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args) |
| 937 | { |
| 938 | struct pldm_msgbuf _ctxExtract; |
| 939 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 940 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 941 | uint16_t testVal; |
| 942 | |
| 943 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 944 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 945 | EXPECT_EQ(0x2211, testVal); |
| 946 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0); |
| 947 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 948 | } |
| 949 | |
| 950 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator) |
| 951 | { |
| 952 | struct pldm_msgbuf _ctxExtract; |
| 953 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 954 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77}; |
| 955 | uint16_t testVal; |
| 956 | char* retBuff = NULL; |
| 957 | |
| 958 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 959 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 960 | EXPECT_EQ(0x2211, testVal); |
| 961 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 962 | -EOVERFLOW); |
| 963 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 964 | } |
| 965 | |
| 966 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_under) |
| 967 | { |
| 968 | struct pldm_msgbuf _ctxExtract; |
| 969 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 970 | |
| 971 | uint8_t src[1] = {}; |
| 972 | char* retBuff = NULL; |
| 973 | |
| 974 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 975 | ctxExtract->remaining = INTMAX_MIN; |
| 976 | EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 977 | 0); |
| 978 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 979 | } |
| 980 | |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 981 | static size_t str16len(char16_t* startptr) |
| 982 | { |
| 983 | char16_t* endptr = startptr; |
| 984 | while (*endptr) |
| 985 | { |
| 986 | endptr++; |
| 987 | } |
| 988 | return endptr - startptr; |
| 989 | } |
| 990 | |
| 991 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good) |
| 992 | { |
| 993 | struct pldm_msgbuf _ctxExtract; |
| 994 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 995 | uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = { |
| 996 | 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 997 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 998 | const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 999 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1000 | uint16_t testVal; |
| 1001 | uint16_t testVal1; |
| 1002 | void* retBuff = NULL; |
| 1003 | |
| 1004 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1005 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1006 | EXPECT_EQ(0x2211, testVal); |
| 1007 | |
| 1008 | ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1009 | 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1010 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1011 | EXPECT_EQ(0x1234, testVal1); |
| 1012 | |
| 1013 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1014 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1015 | EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData))); |
| 1016 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1017 | } |
| 1018 | |
| 1019 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2) |
| 1020 | { |
| 1021 | struct pldm_msgbuf _ctxExtract; |
| 1022 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1023 | uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1024 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12, |
| 1025 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 1026 | constexpr size_t required = 6; |
| 1027 | const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33, |
| 1028 | 0x6c44, 0x6f55, 0x0000}; |
| 1029 | const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000}; |
| 1030 | uint16_t testVal; |
| 1031 | uint16_t testVal1; |
| 1032 | char* retBuff = NULL; |
| 1033 | char* retBuff1 = NULL; |
| 1034 | size_t length = 0; |
| 1035 | |
| 1036 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1037 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1038 | EXPECT_EQ(0x2211, testVal); |
| 1039 | |
| 1040 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1041 | 0); |
| 1042 | |
| 1043 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1044 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1045 | EXPECT_EQ(memcmp(expectData, retBuff, |
| 1046 | sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)), |
| 1047 | 0); |
| 1048 | |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1049 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1050 | EXPECT_EQ(0x1234, testVal1); |
| 1051 | |
| 1052 | EXPECT_EQ( |
| 1053 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length), |
| 1054 | 0); |
| 1055 | |
| 1056 | EXPECT_EQ(0, length % 2); |
| 1057 | EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0); |
| 1058 | |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1059 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1060 | EXPECT_EQ(0x1234, testVal1); |
| 1061 | |
| 1062 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args) |
| 1066 | { |
| 1067 | struct pldm_msgbuf _ctxExtract; |
| 1068 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1069 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1070 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1071 | uint16_t testVal; |
| 1072 | |
| 1073 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1074 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1075 | EXPECT_EQ(0x2211, testVal); |
| 1076 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0); |
| 1077 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1078 | } |
| 1079 | |
| 1080 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator) |
| 1081 | { |
| 1082 | struct pldm_msgbuf _ctxExtract; |
| 1083 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1084 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1085 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77}; |
| 1086 | uint16_t testVal; |
| 1087 | char16_t* retBuff = NULL; |
| 1088 | |
| 1089 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1090 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1091 | EXPECT_EQ(0x2211, testVal); |
| 1092 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1093 | -EOVERFLOW); |
| 1094 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1095 | } |
| 1096 | |
| 1097 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size) |
| 1098 | { |
| 1099 | struct pldm_msgbuf _ctxExtract; |
| 1100 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1101 | uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1102 | 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00}; |
| 1103 | uint16_t testVal; |
| 1104 | char16_t* retBuff = NULL; |
| 1105 | |
| 1106 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1107 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1108 | EXPECT_EQ(0x2211, testVal); |
| 1109 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1110 | -EOVERFLOW); |
| 1111 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1112 | } |
| 1113 | |
| 1114 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix) |
| 1115 | { |
| 1116 | struct pldm_msgbuf _ctxExtract; |
| 1117 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1118 | uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en" |
| 1119 | 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, |
| 1120 | 0x58, 0x00, 0x00, // Entity Name "S0S" |
| 1121 | 0x66, 0x6e, 0x00, // Language Tag "en" |
| 1122 | 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1" |
| 1123 | 0x67, 0x6e, 0x00, // Language Tag "en" |
| 1124 | 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, |
| 1125 | 0x00, // Entity Name "RR3" |
| 1126 | 0x77, 0x88}; |
| 1127 | uint8_t name_count; |
| 1128 | uint16_t test_val; |
| 1129 | char* tag = NULL; |
| 1130 | char* name = NULL; |
| 1131 | char* tag1 = NULL; |
| 1132 | char* name1 = NULL; |
| 1133 | char* tag2 = NULL; |
| 1134 | char* name2 = NULL; |
| 1135 | const char expectTag0[3] = {0x65, 0x6e, 0x00}; |
| 1136 | const char expectTag1[3] = {0x66, 0x6e, 0x00}; |
| 1137 | const char expectTag2[3] = {0x67, 0x6e, 0x00}; |
| 1138 | |
| 1139 | const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000}; |
| 1140 | const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000}; |
| 1141 | const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000}; |
| 1142 | size_t length = 0; |
| 1143 | |
| 1144 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1145 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, name_count), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1146 | EXPECT_EQ(0x2, name_count); |
| 1147 | |
| 1148 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0); |
| 1149 | EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0); |
| 1150 | |
| 1151 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL), |
| 1152 | 0); |
| 1153 | ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1)); |
| 1154 | EXPECT_EQ(5, str16len((char16_t*)name) + 1); |
| 1155 | EXPECT_EQ(memcmp(expectName0, name, |
| 1156 | sizeof(char16_t) * (str16len((char16_t*)name) + 1)), |
| 1157 | 0); |
| 1158 | |
| 1159 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length), |
| 1160 | 0); |
| 1161 | EXPECT_EQ(strncmp(expectTag1, tag1, length), 0); |
| 1162 | EXPECT_EQ( |
| 1163 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0); |
| 1164 | EXPECT_EQ(0, length % 2); |
| 1165 | EXPECT_EQ(memcmp(expectName1, name1, length), 0); |
| 1166 | |
| 1167 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL), |
| 1168 | 0); |
| 1169 | EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0); |
| 1170 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL), |
| 1171 | 0); |
| 1172 | ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1)); |
| 1173 | EXPECT_EQ(4, str16len((char16_t*)name2) + 1); |
| 1174 | EXPECT_EQ(memcmp(expectName2, name2, |
| 1175 | sizeof(char16_t) * (str16len((char16_t*)name2) + 1)), |
| 1176 | 0); |
| 1177 | |
Andrew Jeffery | e5f1253 | 2024-10-01 12:18:49 +0930 | [diff] [blame] | 1178 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, test_val), 0); |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1179 | EXPECT_EQ(0x8877, test_val); |
| 1180 | |
| 1181 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1182 | } |
| 1183 | |
| 1184 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_under) |
| 1185 | { |
| 1186 | struct pldm_msgbuf _ctxExtract; |
| 1187 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1188 | |
| 1189 | uint8_t src[1] = {}; |
| 1190 | char* retBuff = NULL; |
| 1191 | |
| 1192 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1193 | ctxExtract->remaining = INTMAX_MIN; |
| 1194 | EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1195 | 0); |
| 1196 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1197 | } |
| 1198 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1199 | TEST(msgbuf, pldm_msgbuf_span_remaining_good) |
| 1200 | { |
| 1201 | struct pldm_msgbuf _ctx; |
| 1202 | struct pldm_msgbuf* ctx = &_ctx; |
| 1203 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1204 | uint8_t buf[8] = {0}; |
| 1205 | uint16_t testVal; |
| 1206 | uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1207 | size_t remaining; |
| 1208 | uint8_t* retBuff = NULL; |
| 1209 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1210 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1211 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1212 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1213 | |
| 1214 | struct pldm_msgbuf _ctxExtract; |
| 1215 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1216 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1217 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 1218 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1219 | EXPECT_EQ( |
| 1220 | pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining), |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1221 | 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1222 | |
| 1223 | EXPECT_EQ(remaining, sizeof(expectData)); |
| 1224 | EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0); |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1225 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1226 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | TEST(msgbuf, pldm_msgbuf_span_remaining_bad) |
| 1230 | { |
| 1231 | struct pldm_msgbuf _ctx; |
| 1232 | struct pldm_msgbuf* ctx = &_ctx; |
| 1233 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1234 | uint8_t buf[8] = {0}; |
| 1235 | uint16_t testVal; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1236 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1237 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1238 | EXPECT_EQ( |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1239 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1240 | |
| 1241 | struct pldm_msgbuf _ctxExtract; |
| 1242 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1243 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1244 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 1245 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1246 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1247 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1248 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 1249 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1250 | |
| 1251 | TEST(msgbuf, pldm_msgbuf_copy_good) |
| 1252 | { |
| 1253 | struct pldm_msgbuf _src; |
| 1254 | struct pldm_msgbuf* src = &_src; |
| 1255 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 1256 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1257 | ASSERT_EQ(pldm_msgbuf_init_errno(src, sizeof(buf), buf, sizeof(buf)), 0); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1258 | |
| 1259 | struct pldm_msgbuf _dst; |
| 1260 | struct pldm_msgbuf* dst = &_dst; |
| 1261 | uint16_t checkVal = 0; |
| 1262 | uint8_t buf1[sizeof(buf)] = {}; |
| 1263 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1264 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, sizeof(buf1), buf1, sizeof(buf1)), 0); |
| 1265 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), 0); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1266 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1267 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, sizeof(buf1), buf1, sizeof(buf1)), 0); |
| 1268 | EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, checkVal), 0); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1269 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1270 | EXPECT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1271 | EXPECT_EQ(pldm_msgbuf_destroy(dst), 0); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1272 | |
| 1273 | EXPECT_EQ(buf[0], checkVal); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | TEST(msgbuf, pldm_msgbuf_copy_bad) |
| 1277 | { |
| 1278 | struct pldm_msgbuf _src; |
| 1279 | struct pldm_msgbuf* src = &_src; |
| 1280 | struct pldm_msgbuf _dst; |
| 1281 | struct pldm_msgbuf* dst = &_dst; |
| 1282 | uint8_t buf[1] = {sizeof(uint8_t)}; |
| 1283 | uint8_t buf1[1] = {sizeof(uint16_t)}; |
| 1284 | uint16_t value = 8; |
| 1285 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1286 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, buf, sizeof(buf)), 0); |
| 1287 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf1, sizeof(buf1)), 0); |
| 1288 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), -EOVERFLOW); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1289 | |
Andrew Jeffery | 830c1eb | 2024-10-04 10:48:10 +0930 | [diff] [blame] | 1290 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, buf1, sizeof(buf1)), 0); |
| 1291 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1292 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), -EOVERFLOW); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1293 | } |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1294 | |
| 1295 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact) |
| 1296 | { |
| 1297 | const char msg[] = "this is a message"; |
| 1298 | |
| 1299 | struct pldm_msgbuf _src; |
| 1300 | struct pldm_msgbuf* src = &_src; |
| 1301 | struct pldm_msgbuf _dst; |
| 1302 | struct pldm_msgbuf* dst = &_dst; |
| 1303 | |
| 1304 | char buf[sizeof(msg)] = {}; |
| 1305 | |
| 1306 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1307 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1308 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1309 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1310 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1311 | EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf))); |
| 1312 | } |
| 1313 | |
| 1314 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src) |
| 1315 | { |
| 1316 | const char msg[] = "this is a message"; |
| 1317 | |
| 1318 | struct pldm_msgbuf _src; |
| 1319 | struct pldm_msgbuf* src = &_src; |
| 1320 | struct pldm_msgbuf _dst; |
| 1321 | struct pldm_msgbuf* dst = &_dst; |
| 1322 | |
| 1323 | char buf[sizeof(msg) + 1] = {}; |
| 1324 | |
| 1325 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1326 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1327 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1328 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1329 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1330 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1331 | } |
| 1332 | |
| 1333 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst) |
| 1334 | { |
| 1335 | const char msg[] = "this is a message"; |
| 1336 | |
| 1337 | struct pldm_msgbuf _src; |
| 1338 | struct pldm_msgbuf* src = &_src; |
| 1339 | struct pldm_msgbuf _dst; |
| 1340 | struct pldm_msgbuf* dst = &_dst; |
| 1341 | |
| 1342 | char buf[sizeof(msg) - 1] = {}; |
| 1343 | |
| 1344 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1345 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1346 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1347 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1348 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1349 | } |
| 1350 | |
| 1351 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src) |
| 1352 | { |
| 1353 | const char msg[] = {'a'}; |
| 1354 | |
| 1355 | struct pldm_msgbuf _src; |
| 1356 | struct pldm_msgbuf* src = &_src; |
| 1357 | struct pldm_msgbuf _dst; |
| 1358 | struct pldm_msgbuf* dst = &_dst; |
| 1359 | |
| 1360 | char buf[sizeof(msg)] = {}; |
| 1361 | |
| 1362 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1363 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1364 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1365 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1366 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1367 | } |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1368 | |
| 1369 | TEST(msgbuf, pldm_msgbuf_copy_utf16_exact) |
| 1370 | { |
| 1371 | const char16_t msg[] = u"this is a message"; |
| 1372 | |
| 1373 | struct pldm_msgbuf _src; |
| 1374 | struct pldm_msgbuf* src = &_src; |
| 1375 | struct pldm_msgbuf _dst; |
| 1376 | struct pldm_msgbuf* dst = &_dst; |
| 1377 | |
| 1378 | char buf[sizeof(msg)] = {}; |
| 1379 | |
| 1380 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1381 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1382 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1383 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1384 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1385 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1386 | } |
| 1387 | |
| 1388 | TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src) |
| 1389 | { |
| 1390 | const char16_t msg[] = u"this is a message"; |
| 1391 | |
| 1392 | struct pldm_msgbuf _src; |
| 1393 | struct pldm_msgbuf* src = &_src; |
| 1394 | struct pldm_msgbuf _dst; |
| 1395 | struct pldm_msgbuf* dst = &_dst; |
| 1396 | |
| 1397 | char buf[sizeof(msg) + 1] = {}; |
| 1398 | |
| 1399 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1400 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1401 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1402 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1403 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1404 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1405 | } |
| 1406 | |
| 1407 | TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst) |
| 1408 | { |
| 1409 | const char16_t msg[] = u"this is a message"; |
| 1410 | |
| 1411 | struct pldm_msgbuf _src; |
| 1412 | struct pldm_msgbuf* src = &_src; |
| 1413 | struct pldm_msgbuf _dst; |
| 1414 | struct pldm_msgbuf* dst = &_dst; |
| 1415 | |
| 1416 | char buf[sizeof(msg) - 1] = {}; |
| 1417 | |
| 1418 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1419 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1420 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1421 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1422 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1423 | } |
| 1424 | |
| 1425 | TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src) |
| 1426 | { |
| 1427 | const char16_t msg[] = {u'a'}; |
| 1428 | |
| 1429 | struct pldm_msgbuf _src; |
| 1430 | struct pldm_msgbuf* src = &_src; |
| 1431 | struct pldm_msgbuf _dst; |
| 1432 | struct pldm_msgbuf* dst = &_dst; |
| 1433 | |
| 1434 | char buf[sizeof(msg)] = {}; |
| 1435 | |
| 1436 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1437 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1438 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1439 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1440 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1441 | } |