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