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