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