blob: 6623fe00c29d85a9071e44d4e756538ca9b7472f [file] [log] [blame]
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301#include <endian.h>
2
3#include <cfloat>
4
5#include <gtest/gtest.h>
6
7/* We're exercising the implementation so disable the asserts for now */
8#ifndef NDEBUG
9#define NDEBUG 1
10#endif
11
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103012#include "msgbuf.h"
13
14TEST(msgbuf, init_bad_ctx)
15{
16 EXPECT_NE(pldm_msgbuf_init(NULL, 0, NULL, 0), PLDM_SUCCESS);
17}
18
19TEST(msgbuf, init_bad_minsize)
20{
21 struct pldm_msgbuf _ctx;
22 struct pldm_msgbuf* ctx = &_ctx;
23 uint8_t buf[1] = {};
24
25 EXPECT_NE(pldm_msgbuf_init(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
26 PLDM_SUCCESS);
27}
28
29TEST(msgbuf, init_bad_buf)
30{
31 struct pldm_msgbuf _ctx;
32 struct pldm_msgbuf* ctx = &_ctx;
33
34 EXPECT_NE(pldm_msgbuf_init(ctx, 0, NULL, 0), PLDM_SUCCESS);
35}
36
37TEST(msgbuf, init_bad_len)
38{
39 struct pldm_msgbuf _ctx;
40 struct pldm_msgbuf* ctx = &_ctx;
41 uint8_t buf[1] = {};
42
43 EXPECT_NE(pldm_msgbuf_init(ctx, sizeof(buf), buf, SIZE_MAX), PLDM_SUCCESS);
44}
45
46TEST(msgbuf, init_overflow)
47{
48 struct pldm_msgbuf _ctx;
49 struct pldm_msgbuf* ctx = &_ctx;
50 // This is an intrinsic part of the test.
51 // NOLINTNEXTLINE(performance-no-int-to-ptr)
52 uint8_t* buf = (uint8_t*)SIZE_MAX;
53
54 EXPECT_NE(pldm_msgbuf_init(ctx, 0, buf, 2), PLDM_SUCCESS);
55}
56
57TEST(msgbuf, init_success)
58{
59 struct pldm_msgbuf _ctx;
60 struct pldm_msgbuf* ctx = &_ctx;
61 uint8_t buf[1] = {};
62
63 EXPECT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
64 PLDM_SUCCESS);
65}
66
67TEST(msgbuf, destroy_none)
68{
69 struct pldm_msgbuf _ctx;
70 struct pldm_msgbuf* ctx = &_ctx;
71 uint8_t buf[1] = {};
72
73 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
74 PLDM_SUCCESS);
75 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
76}
77
78TEST(msgbuf, destroy_exact)
79{
80 struct pldm_msgbuf _ctx;
81 struct pldm_msgbuf* ctx = &_ctx;
82 uint8_t buf[1] = {0xa5};
83 uint8_t val;
84
85 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
86 PLDM_SUCCESS);
87 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
88 EXPECT_EQ(val, 0xa5);
89 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
90}
91
92TEST(msgbuf, destroy_over)
93{
94 struct pldm_msgbuf _ctx;
95 struct pldm_msgbuf* ctx = &_ctx;
96 uint8_t buf[1] = {0xa5};
97 uint8_t val;
98
99 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
100 PLDM_SUCCESS);
101 ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
102 ASSERT_EQ(val, 0xa5);
103 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
104 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
105}
106
107TEST(msgbuf, destroy_under)
108{
109 struct pldm_msgbuf _ctx;
110 struct pldm_msgbuf* ctx = &_ctx;
111 uint8_t buf[2] = {0x5a, 0xa5};
112 uint8_t val;
113
114 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
115 PLDM_SUCCESS);
116 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
117 EXPECT_EQ(val, 0x5a);
118 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
119}
120
121TEST(msgbuf, extract_one_uint8)
122{
123 struct pldm_msgbuf _ctx;
124 struct pldm_msgbuf* ctx = &_ctx;
125 uint8_t buf[1] = {0xa5};
126 uint8_t val;
127
128 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
129 PLDM_SUCCESS);
130 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
131 EXPECT_EQ(val, 0xa5);
132 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
133}
134
135TEST(msgbuf, extract_over_uint8)
136{
137 struct pldm_msgbuf _ctx;
138 struct pldm_msgbuf* ctx = &_ctx;
139 uint8_t buf[1] = {};
140 uint8_t val;
141
142 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
143 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
144 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
145}
146
147TEST(msgbuf, extract_one_int8)
148{
149 struct pldm_msgbuf _ctx;
150 struct pldm_msgbuf* ctx = &_ctx;
151 int8_t buf[1] = {-1};
152 int8_t val;
153
154 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
155 PLDM_SUCCESS);
156 EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
157 EXPECT_EQ(val, -1);
158 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
159}
160
161TEST(msgbuf, extract_over_int8)
162{
163 struct pldm_msgbuf _ctx;
164 struct pldm_msgbuf* ctx = &_ctx;
165 int8_t buf[1] = {};
166 int8_t val;
167
168 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
169 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
170 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
171}
172
173TEST(msgbuf, extract_one_uint16)
174{
175 struct pldm_msgbuf _ctx;
176 struct pldm_msgbuf* ctx = &_ctx;
177 uint16_t buf[1] = {htole16(0x5aa5)};
178 uint16_t val = {};
179
180 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
181 PLDM_SUCCESS);
182 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
183 EXPECT_EQ(val, 0x5aa5);
184 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
185}
186
187TEST(msgbuf, extract_over_uint16)
188{
189 struct pldm_msgbuf _ctx;
190 struct pldm_msgbuf* ctx = &_ctx;
191 uint16_t buf[1] = {};
192 uint16_t val;
193
194 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
195 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
196 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
197}
198
199TEST(msgbuf, extract_one_int16)
200{
201 struct pldm_msgbuf _ctx;
202 struct pldm_msgbuf* ctx = &_ctx;
203 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
204 int16_t val;
205
206 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
207 PLDM_SUCCESS);
208 EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
209 EXPECT_EQ(val, INT16_MIN);
210 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
211}
212
213TEST(msgbuf, extract_over_int16)
214{
215 struct pldm_msgbuf _ctx;
216 struct pldm_msgbuf* ctx = &_ctx;
217 int16_t buf[1] = {};
218 int16_t val;
219
220 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
221 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
222 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
223}
224
225TEST(msgbuf, extract_one_uint32)
226{
227 struct pldm_msgbuf _ctx;
228 struct pldm_msgbuf* ctx = &_ctx;
229 uint32_t buf[1] = {htole32(0x5a00ffa5)};
230 uint32_t val;
231
232 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
233 PLDM_SUCCESS);
234 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
235 EXPECT_EQ(val, 0x5a00ffa5);
236 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
237}
238
239TEST(msgbuf, extract_over_uint32)
240{
241 struct pldm_msgbuf _ctx;
242 struct pldm_msgbuf* ctx = &_ctx;
243 uint32_t buf[1] = {};
244 uint32_t val;
245
246 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
247 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
248 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
249}
250
251TEST(msgbuf, extract_one_int32)
252{
253 struct pldm_msgbuf _ctx;
254 struct pldm_msgbuf* ctx = &_ctx;
255 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
256 int32_t val;
257
258 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
259 PLDM_SUCCESS);
260 EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
261 EXPECT_EQ(val, INT32_MIN);
262 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
263}
264
265TEST(msgbuf, extract_over_int32)
266{
267 struct pldm_msgbuf _ctx;
268 struct pldm_msgbuf* ctx = &_ctx;
269 int32_t buf[1] = {};
270 int32_t val;
271
272 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
273 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
274 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
275}
276
277TEST(msgbuf, extract_one_real32)
278{
279 struct pldm_msgbuf _ctx;
280 struct pldm_msgbuf* ctx = &_ctx;
281 uint32_t buf[1] = {};
282 uint32_t xform;
283 real32_t val;
284
285 val = FLT_MAX;
286 memcpy(&xform, &val, sizeof(val));
287 buf[0] = htole32(xform);
288 val = 0;
289
290 ASSERT_EQ(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)),
291 PLDM_SUCCESS);
292 EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
293 EXPECT_EQ(val, FLT_MAX);
294 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
295}
296
297TEST(msgbuf, extract_over_real32)
298{
299 struct pldm_msgbuf _ctx;
300 struct pldm_msgbuf* ctx = &_ctx;
301 real32_t buf[1] = {};
302 real32_t val;
303
304 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
305 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
306 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
307}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930308
309TEST(msgbuf, extract_array_uint8_buf0_req0)
310{
311 struct pldm_msgbuf _ctx;
312 struct pldm_msgbuf* ctx = &_ctx;
313 uint8_t buf[1] = {};
314 uint8_t arr[1];
315
316 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, 0), PLDM_SUCCESS);
317 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, 0), PLDM_SUCCESS);
318 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
319}
320
321TEST(msgbuf, extract_array_uint8_buf1_req1)
322{
323 struct pldm_msgbuf _ctx;
324 struct pldm_msgbuf* ctx = &_ctx;
325 uint8_t buf[1] = {};
326 uint8_t arr[1];
327
328 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
329 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)),
330 PLDM_SUCCESS);
331 EXPECT_EQ(arr[0], 0);
332 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
333}
334
335TEST(msgbuf, extract_array_uint8_buf1_req2)
336{
337 struct pldm_msgbuf _ctx;
338 struct pldm_msgbuf* ctx = &_ctx;
339 uint8_t buf[1] = {};
340 uint8_t arr[2];
341
342 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
343 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)),
344 PLDM_SUCCESS);
345 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
346}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930347
348TEST(msgbuf, consumed_under)
349{
350 struct pldm_msgbuf _ctx;
351 struct pldm_msgbuf* ctx = &_ctx;
352 uint8_t buf[1] = {};
353
354 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
355 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
356}
357
358TEST(msgbuf, consumed_exact)
359{
360 struct pldm_msgbuf _ctx;
361 struct pldm_msgbuf* ctx = &_ctx;
362 uint8_t buf[1] = {};
363 uint8_t val;
364
365 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
366 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
367 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
368}
369
370TEST(msgbuf, consumed_over)
371{
372 struct pldm_msgbuf _ctx;
373 struct pldm_msgbuf* ctx = &_ctx;
374 uint8_t buf[1] = {};
375 uint8_t val[2];
376
377 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
378 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS);
379 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
380 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
381}
Thu Nguyen062c8762023-04-22 20:45:04 +0700382
383TEST(msgbuf, pldm_msgbuf_insert_int32_good)
384{
385 struct pldm_msgbuf _ctx;
386 struct pldm_msgbuf* ctx = &_ctx;
387 int32_t src = -12345;
388 int32_t checkVal = 0;
389 uint8_t buf[sizeof(int32_t)] = {};
390
391 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
392 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
393
394 struct pldm_msgbuf _ctxExtract;
395 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
396
397 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
398 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS);
399
400 EXPECT_EQ(src, checkVal);
401 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
402 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
403}
404
405TEST(msgbuf, pldm_msgbuf_insert_int32_bad)
406{
407 int32_t src = -12345;
408
409 auto rc = pldm_msgbuf_insert_int32(NULL, src);
410
411 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
412}
413
414TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
415{
416 struct pldm_msgbuf _ctx;
417 struct pldm_msgbuf* ctx = &_ctx;
418 uint32_t src = 0xf1223344;
419 uint32_t checkVal = 0;
420 uint8_t buf[sizeof(uint32_t)] = {};
421
422 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
423 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
424
425 struct pldm_msgbuf _ctxExtract;
426 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
427
428 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
429 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS);
430
431 EXPECT_EQ(src, checkVal);
432 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
433 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
434}
435
436TEST(msgbuf, pldm_msgbuf_insert_uint32_bad)
437{
438 uint32_t src = 0xf1223344;
439
440 auto rc = pldm_msgbuf_insert_uint32(NULL, src);
441
442 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
443}
444
445TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
446{
447 struct pldm_msgbuf _ctx;
448 struct pldm_msgbuf* ctx = &_ctx;
449 uint16_t src = 0xf344;
450 uint16_t checkVal = 0;
451 uint8_t buf[sizeof(uint16_t)] = {};
452
453 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
454 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
455
456 struct pldm_msgbuf _ctxExtract;
457 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
458
459 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
460 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS);
461
462 EXPECT_EQ(src, checkVal);
463 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
464 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
465}
466
467TEST(msgbuf, pldm_msgbuf_insert_uint16_bad)
468{
469 uint16_t src = 0xf344;
470
471 auto rc = pldm_msgbuf_insert_uint16(NULL, src);
472
473 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
474}
475
476TEST(msgbuf, pldm_msgbuf_insert_int16_good)
477{
478 struct pldm_msgbuf _ctx;
479 struct pldm_msgbuf* ctx = &_ctx;
480 int16_t src = -12;
481 int16_t checkVal = 0;
482 uint8_t buf[sizeof(int16_t)] = {};
483
484 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
485 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
486
487 struct pldm_msgbuf _ctxExtract;
488 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
489
490 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
491 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS);
492
493 EXPECT_EQ(src, checkVal);
494 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
495 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
496}
497
498TEST(msgbuf, pldm_msgbuf_insert_int16_bad)
499{
500 int16_t src = -12;
501
502 auto rc = pldm_msgbuf_insert_int16(NULL, src);
503
504 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
505}
506
507TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
508{
509 struct pldm_msgbuf _ctx;
510 struct pldm_msgbuf* ctx = &_ctx;
511 uint8_t src = 0xf4;
512 uint8_t checkVal = 0;
513 uint8_t buf[sizeof(uint8_t)] = {};
514
515 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
516 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
517
518 struct pldm_msgbuf _ctxExtract;
519 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
520
521 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
522 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS);
523
524 EXPECT_EQ(src, checkVal);
525 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
526 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
527}
528
529TEST(msgbuf, pldm_msgbuf_insert_uint8_bad)
530{
531 uint8_t src = 0xf4;
532
533 auto rc = pldm_msgbuf_insert_uint8(NULL, src);
534
535 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
536}
537
538TEST(msgbuf, pldm_msgbuf_insert_int8_good)
539{
540 struct pldm_msgbuf _ctx;
541 struct pldm_msgbuf* ctx = &_ctx;
542 int8_t src = -4;
543 int8_t checkVal = 0;
544 uint8_t buf[sizeof(int8_t)] = {};
545
546 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
547 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
548
549 struct pldm_msgbuf _ctxExtract;
550 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
551
552 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
553 EXPECT_EQ(pldm_msgbuf_extract_int8(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
560TEST(msgbuf, pldm_msgbuf_insert_int8_bad)
561{
562 int8_t src = -4;
563
564 auto rc = pldm_msgbuf_insert_int8(NULL, src);
565
566 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
567}
568
569TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
570{
571 struct pldm_msgbuf _ctx;
572 struct pldm_msgbuf* ctx = &_ctx;
573 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
574 uint8_t buf[6] = {};
575 uint8_t retBuff[6] = {};
576
577 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
578 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
579 PLDM_SUCCESS);
580
581 struct pldm_msgbuf _ctxExtract;
582 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
583
584 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
585 EXPECT_EQ(
586 pldm_msgbuf_extract_array_uint8(ctxExtract, retBuff, sizeof(retBuff)),
587 PLDM_SUCCESS);
588
589 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
590 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
591 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
592}
593
594TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad)
595{
596 struct pldm_msgbuf _ctx;
597 struct pldm_msgbuf* ctx = &_ctx;
598 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
599 uint8_t buf[6] = {};
600
601 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
602 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(NULL, src, sizeof(src)),
603 PLDM_ERROR_INVALID_DATA);
604 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, NULL, sizeof(src)),
605 PLDM_ERROR_INVALID_DATA);
606 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
607}
608
609TEST(msgbuf, pldm_msgbuf_span_required_good)
610{
611 struct pldm_msgbuf _ctx;
612 struct pldm_msgbuf* ctx = &_ctx;
613 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
614 uint8_t buf[6] = {0};
615 const size_t required = 4;
616 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
617 uint16_t testVal;
618 uint8_t* retBuff = NULL;
619
620 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
621 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
622 PLDM_SUCCESS);
623
624 struct pldm_msgbuf _ctxExtract;
625 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
626
627 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
628 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
629 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
630 PLDM_SUCCESS);
631
632 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
633 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
634 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
635}
636
637TEST(msgbuf, pldm_msgbuf_span_required_bad)
638{
639 struct pldm_msgbuf _ctx;
640 struct pldm_msgbuf* ctx = &_ctx;
641 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
642 uint8_t buf[6] = {0};
643 const size_t required = 4;
644 uint16_t testVal;
645 [[maybe_unused]] uint8_t* retBuff = NULL;
646
647 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
648 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
649 PLDM_SUCCESS);
650
651 struct pldm_msgbuf _ctxExtract;
652 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
653
654 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
655 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
656 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
657 PLDM_ERROR_INVALID_DATA);
658 EXPECT_EQ(pldm_msgbuf_span_required(NULL, required, (void**)&retBuff),
659 PLDM_ERROR_INVALID_DATA);
660
661 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
662 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
663}
664
665TEST(msgbuf, pldm_msgbuf_span_remaining_good)
666{
667 struct pldm_msgbuf _ctx;
668 struct pldm_msgbuf* ctx = &_ctx;
669 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
670 uint8_t buf[8] = {0};
671 uint16_t testVal;
672 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
673 size_t remaining;
674 uint8_t* retBuff = NULL;
675
676 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
677 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
678 PLDM_SUCCESS);
679
680 struct pldm_msgbuf _ctxExtract;
681 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
682
683 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
684 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
685 EXPECT_EQ(
686 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
687 PLDM_SUCCESS);
688
689 EXPECT_EQ(remaining, sizeof(expectData));
690 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
691 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
692 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
693}
694
695TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
696{
697 struct pldm_msgbuf _ctx;
698 struct pldm_msgbuf* ctx = &_ctx;
699 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
700 uint8_t buf[8] = {0};
701 uint16_t testVal;
702 size_t remaining;
703 uint8_t* retBuff = NULL;
704
705 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
706 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
707 PLDM_SUCCESS);
708
709 struct pldm_msgbuf _ctxExtract;
710 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
711
712 ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
713 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
714 EXPECT_EQ(pldm_msgbuf_span_remaining(NULL, (void**)&retBuff, &remaining),
715 PLDM_ERROR_INVALID_DATA);
716 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining),
717 PLDM_ERROR_INVALID_DATA);
718 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL),
719 PLDM_ERROR_INVALID_DATA);
720
721 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
722 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
723}