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