blob: e7f9e624b1ffac425f800a34cb6d26cc81c5315c [file] [log] [blame]
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301#include <endian.h>
Thu Nguyen15237782024-07-02 09:30:41 +00002#include <libpldm/utils.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10303
4#include <cfloat>
5
6#include <gtest/gtest.h>
7
8/* We're exercising the implementation so disable the asserts for now */
9#ifndef NDEBUG
10#define NDEBUG 1
11#endif
12
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103013#include "msgbuf.h"
14
15TEST(msgbuf, init_bad_ctx)
16{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093017 EXPECT_NE(pldm_msgbuf_init_cc(NULL, 0, NULL, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103018}
19
20TEST(msgbuf, init_bad_minsize)
21{
22 struct pldm_msgbuf _ctx;
23 struct pldm_msgbuf* ctx = &_ctx;
24 uint8_t buf[1] = {};
25
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093026 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103027 PLDM_SUCCESS);
28}
29
30TEST(msgbuf, init_bad_buf)
31{
32 struct pldm_msgbuf _ctx;
33 struct pldm_msgbuf* ctx = &_ctx;
34
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093035 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, NULL, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103036}
37
38TEST(msgbuf, init_bad_len)
39{
40 struct pldm_msgbuf _ctx;
41 struct pldm_msgbuf* ctx = &_ctx;
42 uint8_t buf[1] = {};
43
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093044 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX),
45 PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103046}
47
48TEST(msgbuf, init_overflow)
49{
50 struct pldm_msgbuf _ctx;
51 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103052 // NOLINTNEXTLINE(performance-no-int-to-ptr)
Andrew Jeffery07febdb2024-05-17 14:17:14 +093053 void* buf = (void*)UINTPTR_MAX;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103054
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093055 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103056}
57
58TEST(msgbuf, init_success)
59{
60 struct pldm_msgbuf _ctx;
61 struct pldm_msgbuf* ctx = &_ctx;
62 uint8_t buf[1] = {};
63
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093064 EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103065 PLDM_SUCCESS);
66}
67
68TEST(msgbuf, destroy_none)
69{
70 struct pldm_msgbuf _ctx;
71 struct pldm_msgbuf* ctx = &_ctx;
72 uint8_t buf[1] = {};
73
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093074 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103075 PLDM_SUCCESS);
76 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
77}
78
79TEST(msgbuf, destroy_exact)
80{
81 struct pldm_msgbuf _ctx;
82 struct pldm_msgbuf* ctx = &_ctx;
83 uint8_t buf[1] = {0xa5};
84 uint8_t val;
85
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093086 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103087 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
93TEST(msgbuf, destroy_over)
94{
95 struct pldm_msgbuf _ctx;
96 struct pldm_msgbuf* ctx = &_ctx;
97 uint8_t buf[1] = {0xa5};
98 uint8_t val;
99
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930100 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030101 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
108TEST(msgbuf, destroy_under)
109{
110 struct pldm_msgbuf _ctx;
111 struct pldm_msgbuf* ctx = &_ctx;
112 uint8_t buf[2] = {0x5a, 0xa5};
113 uint8_t val;
114
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930115 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030116 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
122TEST(msgbuf, extract_one_uint8)
123{
124 struct pldm_msgbuf _ctx;
125 struct pldm_msgbuf* ctx = &_ctx;
126 uint8_t buf[1] = {0xa5};
127 uint8_t val;
128
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930129 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030130 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
136TEST(msgbuf, extract_over_uint8)
137{
138 struct pldm_msgbuf _ctx;
139 struct pldm_msgbuf* ctx = &_ctx;
140 uint8_t buf[1] = {};
141 uint8_t val;
142
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930143 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030144 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
145 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
146}
147
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930148TEST(msgbuf, extract_under_uint8)
149{
150 struct pldm_msgbuf _ctx;
151 struct pldm_msgbuf* ctx = &_ctx;
152
153 uint8_t buf[1] = {};
154 uint8_t val;
155
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930156 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930157 ctx->remaining = INTMAX_MIN;
158 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
159 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
160}
161
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030162TEST(msgbuf, extract_one_int8)
163{
164 struct pldm_msgbuf _ctx;
165 struct pldm_msgbuf* ctx = &_ctx;
166 int8_t buf[1] = {-1};
167 int8_t val;
168
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930169 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030170 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
176TEST(msgbuf, extract_over_int8)
177{
178 struct pldm_msgbuf _ctx;
179 struct pldm_msgbuf* ctx = &_ctx;
180 int8_t buf[1] = {};
181 int8_t val;
182
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930183 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030184 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
185 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
186}
187
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930188TEST(msgbuf, extract_under_int8)
189{
190 struct pldm_msgbuf _ctx;
191 struct pldm_msgbuf* ctx = &_ctx;
192
193 uint8_t buf[1] = {};
194 int8_t val;
195
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930196 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930197 ctx->remaining = INTMAX_MIN;
198 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
199 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
200}
201
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030202TEST(msgbuf, extract_one_uint16)
203{
204 struct pldm_msgbuf _ctx;
205 struct pldm_msgbuf* ctx = &_ctx;
206 uint16_t buf[1] = {htole16(0x5aa5)};
207 uint16_t val = {};
208
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930209 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030210 PLDM_SUCCESS);
211 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
212 EXPECT_EQ(val, 0x5aa5);
213 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
214}
215
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930216TEST(msgbuf, extract_under_uint16)
217{
218 struct pldm_msgbuf _ctx;
219 struct pldm_msgbuf* ctx = &_ctx;
220
221 uint16_t buf[1] = {};
222 uint16_t val;
223
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930224 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930225 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
226 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
227 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
228}
229
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030230TEST(msgbuf, extract_over_uint16)
231{
232 struct pldm_msgbuf _ctx;
233 struct pldm_msgbuf* ctx = &_ctx;
234 uint16_t buf[1] = {};
235 uint16_t val;
236
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930237 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030238 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
239 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
240}
241
242TEST(msgbuf, extract_one_int16)
243{
244 struct pldm_msgbuf _ctx;
245 struct pldm_msgbuf* ctx = &_ctx;
246 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
247 int16_t val;
248
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930249 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030250 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
256TEST(msgbuf, extract_over_int16)
257{
258 struct pldm_msgbuf _ctx;
259 struct pldm_msgbuf* ctx = &_ctx;
260 int16_t buf[1] = {};
261 int16_t val;
262
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930263 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030264 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
265 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
266}
267
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930268TEST(msgbuf, extract_under_int16)
269{
270 struct pldm_msgbuf _ctx;
271 struct pldm_msgbuf* ctx = &_ctx;
272
273 int16_t buf[1] = {};
274 int16_t val;
275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930276 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930277 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
278 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
279 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
280}
281
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030282TEST(msgbuf, extract_one_uint32)
283{
284 struct pldm_msgbuf _ctx;
285 struct pldm_msgbuf* ctx = &_ctx;
286 uint32_t buf[1] = {htole32(0x5a00ffa5)};
287 uint32_t val;
288
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930289 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030290 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
296TEST(msgbuf, extract_over_uint32)
297{
298 struct pldm_msgbuf _ctx;
299 struct pldm_msgbuf* ctx = &_ctx;
300 uint32_t buf[1] = {};
301 uint32_t val;
302
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930303 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030304 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
305 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
306}
307
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930308TEST(msgbuf, extract_under_uint32)
309{
310 struct pldm_msgbuf _ctx;
311 struct pldm_msgbuf* ctx = &_ctx;
312
313 uint32_t buf[1] = {};
314 uint32_t val;
315
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930316 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930317 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
318 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
319 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
320}
321
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030322TEST(msgbuf, extract_one_int32)
323{
324 struct pldm_msgbuf _ctx;
325 struct pldm_msgbuf* ctx = &_ctx;
326 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
327 int32_t val;
328
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930329 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030330 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
336TEST(msgbuf, extract_over_int32)
337{
338 struct pldm_msgbuf _ctx;
339 struct pldm_msgbuf* ctx = &_ctx;
340 int32_t buf[1] = {};
341 int32_t val;
342
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930343 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030344 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
345 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
346}
347
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930348TEST(msgbuf, extract_under_int32)
349{
350 struct pldm_msgbuf _ctx;
351 struct pldm_msgbuf* ctx = &_ctx;
352
353 int32_t buf[1] = {};
354 int32_t val;
355
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930356 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930357 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
358 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
359 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
360}
361
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030362TEST(msgbuf, extract_one_real32)
363{
364 struct pldm_msgbuf _ctx;
365 struct pldm_msgbuf* ctx = &_ctx;
366 uint32_t buf[1] = {};
367 uint32_t xform;
368 real32_t val;
369
370 val = FLT_MAX;
371 memcpy(&xform, &val, sizeof(val));
372 buf[0] = htole32(xform);
373 val = 0;
374
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930375 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030376 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
382TEST(msgbuf, extract_over_real32)
383{
384 struct pldm_msgbuf _ctx;
385 struct pldm_msgbuf* ctx = &_ctx;
386 real32_t buf[1] = {};
387 real32_t val;
388
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930389 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030390 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
391 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
392}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930393
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930394TEST(msgbuf, extract_under_real32)
395{
396 struct pldm_msgbuf _ctx;
397 struct pldm_msgbuf* ctx = &_ctx;
398
399 real32_t buf[1] = {};
400 real32_t val;
401
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930402 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930403 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
404 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
405 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
406}
407
Andrew Jeffery369b1212023-04-20 15:44:48 +0930408TEST(msgbuf, extract_array_uint8_buf0_req0)
409{
410 struct pldm_msgbuf _ctx;
411 struct pldm_msgbuf* ctx = &_ctx;
412 uint8_t buf[1] = {};
413 uint8_t arr[1];
414
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930415 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000416 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930417 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
418}
419
420TEST(msgbuf, extract_array_uint8_buf1_req1)
421{
422 struct pldm_msgbuf _ctx;
423 struct pldm_msgbuf* ctx = &_ctx;
424 uint8_t buf[1] = {};
425 uint8_t arr[1];
426
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930427 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000428 EXPECT_EQ(
429 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
430 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930431 EXPECT_EQ(arr[0], 0);
432 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
433}
434
435TEST(msgbuf, extract_array_uint8_buf1_req2)
436{
437 struct pldm_msgbuf _ctx;
438 struct pldm_msgbuf* ctx = &_ctx;
439 uint8_t buf[1] = {};
440 uint8_t arr[2];
441
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930442 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000443 EXPECT_NE(
444 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
445 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930446 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
447}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930448
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930449TEST(msgbuf, extract_under_array_uint8)
450{
451 struct pldm_msgbuf _ctx;
452 struct pldm_msgbuf* ctx = &_ctx;
453 uint8_t buf[1] = {};
454 uint8_t arr[1];
455
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930456 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930457 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000458 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930459 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
460}
461
Andrew Jeffery1c571442024-07-08 10:25:48 +0930462TEST(msgbuf, extract_array_char_buf0_req0)
463{
464 struct pldm_msgbuf _ctx;
465 struct pldm_msgbuf* ctx = &_ctx;
466 char buf[1] = {'\0'};
467 char arr[1] = {'1'};
468
469 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000470 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930471 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
472}
473
474TEST(msgbuf, extract_array_char_buf1_req1)
475{
476 struct pldm_msgbuf _ctx;
477 struct pldm_msgbuf* ctx = &_ctx;
478 char buf[1] = {'\0'};
479 char arr[1] = {'1'};
480
481 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000482 EXPECT_EQ(
483 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930484 EXPECT_EQ(arr[0], '\0');
485 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
486}
487
488TEST(msgbuf, extract_array_char_buf1_req2)
489{
490 struct pldm_msgbuf _ctx;
491 struct pldm_msgbuf* ctx = &_ctx;
492 char buf[1] = {'\0'};
493 char arr[2] = {'1', '2'};
494
495 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000496 EXPECT_NE(
497 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930498 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
499}
500
501TEST(msgbuf, extract_under_array_char)
502{
503 struct pldm_msgbuf _ctx;
504 struct pldm_msgbuf* ctx = &_ctx;
505 char buf[1] = {'\0'};
506 char arr[1] = {'1'};
507
508 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
509 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000510 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930511 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
512}
513
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930514TEST(msgbuf, consumed_under)
515{
516 struct pldm_msgbuf _ctx;
517 struct pldm_msgbuf* ctx = &_ctx;
518 uint8_t buf[1] = {};
519
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930520 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930521 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
522}
523
524TEST(msgbuf, consumed_exact)
525{
526 struct pldm_msgbuf _ctx;
527 struct pldm_msgbuf* ctx = &_ctx;
528 uint8_t buf[1] = {};
529 uint8_t val;
530
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930531 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930532 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
533 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
534}
535
536TEST(msgbuf, consumed_over)
537{
538 struct pldm_msgbuf _ctx;
539 struct pldm_msgbuf* ctx = &_ctx;
540 uint8_t buf[1] = {};
541 uint8_t val[2];
542
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930543 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930544 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS);
545 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
546 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
547}
Thu Nguyen062c8762023-04-22 20:45:04 +0700548
549TEST(msgbuf, pldm_msgbuf_insert_int32_good)
550{
551 struct pldm_msgbuf _ctx;
552 struct pldm_msgbuf* ctx = &_ctx;
553 int32_t src = -12345;
554 int32_t checkVal = 0;
555 uint8_t buf[sizeof(int32_t)] = {};
556
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930557 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700558 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
559
560 struct pldm_msgbuf _ctxExtract;
561 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
562
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930563 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
564 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700565 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS);
566
567 EXPECT_EQ(src, checkVal);
568 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
569 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
570}
571
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930572TEST(msgbuf, insert_under_int32)
573{
574 struct pldm_msgbuf _ctx;
575 struct pldm_msgbuf* ctx = &_ctx;
576
577 int32_t buf[1] = {};
578 int32_t val = 0;
579
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930580 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930581 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
582 EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS);
583 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
584}
585
Thu Nguyen062c8762023-04-22 20:45:04 +0700586TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
587{
588 struct pldm_msgbuf _ctx;
589 struct pldm_msgbuf* ctx = &_ctx;
590 uint32_t src = 0xf1223344;
591 uint32_t checkVal = 0;
592 uint8_t buf[sizeof(uint32_t)] = {};
593
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930594 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700595 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
596
597 struct pldm_msgbuf _ctxExtract;
598 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
599
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930600 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
601 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700602 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS);
603
604 EXPECT_EQ(src, checkVal);
605 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
606 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
607}
608
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930609TEST(msgbuf, insert_under_uint32)
610{
611 struct pldm_msgbuf _ctx;
612 struct pldm_msgbuf* ctx = &_ctx;
613
614 uint32_t buf[1] = {};
615 uint32_t val = 0;
616
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930617 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930618 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
619 EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS);
620 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
621}
622
Thu Nguyen062c8762023-04-22 20:45:04 +0700623TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
624{
625 struct pldm_msgbuf _ctx;
626 struct pldm_msgbuf* ctx = &_ctx;
627 uint16_t src = 0xf344;
628 uint16_t checkVal = 0;
629 uint8_t buf[sizeof(uint16_t)] = {};
630
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930631 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700632 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
633
634 struct pldm_msgbuf _ctxExtract;
635 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
636
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930637 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
638 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700639 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS);
640
641 EXPECT_EQ(src, checkVal);
642 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
643 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
644}
645
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930646TEST(msgbuf, insert_under_uint16)
647{
648 struct pldm_msgbuf _ctx;
649 struct pldm_msgbuf* ctx = &_ctx;
650
651 uint16_t buf[1] = {};
652 uint16_t val = 0;
653
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930654 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930655 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
656 EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS);
657 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
658}
659
Thu Nguyen062c8762023-04-22 20:45:04 +0700660TEST(msgbuf, pldm_msgbuf_insert_int16_good)
661{
662 struct pldm_msgbuf _ctx;
663 struct pldm_msgbuf* ctx = &_ctx;
664 int16_t src = -12;
665 int16_t checkVal = 0;
666 uint8_t buf[sizeof(int16_t)] = {};
667
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930668 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700669 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
670
671 struct pldm_msgbuf _ctxExtract;
672 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
673
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930674 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
675 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700676 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS);
677
678 EXPECT_EQ(src, checkVal);
679 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
680 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
681}
682
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930683TEST(msgbuf, insert_under_int16)
684{
685 struct pldm_msgbuf _ctx;
686 struct pldm_msgbuf* ctx = &_ctx;
687
688 int16_t buf[1] = {};
689 int16_t val = 0;
690
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930691 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930692 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
693 EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS);
694 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
695}
696
Thu Nguyen062c8762023-04-22 20:45:04 +0700697TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
698{
699 struct pldm_msgbuf _ctx;
700 struct pldm_msgbuf* ctx = &_ctx;
701 uint8_t src = 0xf4;
702 uint8_t checkVal = 0;
703 uint8_t buf[sizeof(uint8_t)] = {};
704
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930705 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700706 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
707
708 struct pldm_msgbuf _ctxExtract;
709 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
710
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930711 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
712 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700713 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS);
714
715 EXPECT_EQ(src, checkVal);
716 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
717 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
718}
719
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930720TEST(msgbuf, insert_under_uint8)
721{
722 struct pldm_msgbuf _ctx;
723 struct pldm_msgbuf* ctx = &_ctx;
724
725 uint8_t buf[1] = {};
726 uint8_t val = 0;
727
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930728 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930729 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
730 EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS);
731 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
732}
733
Thu Nguyen062c8762023-04-22 20:45:04 +0700734TEST(msgbuf, pldm_msgbuf_insert_int8_good)
735{
736 struct pldm_msgbuf _ctx;
737 struct pldm_msgbuf* ctx = &_ctx;
738 int8_t src = -4;
739 int8_t checkVal = 0;
740 uint8_t buf[sizeof(int8_t)] = {};
741
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930742 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700743 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
744
745 struct pldm_msgbuf _ctxExtract;
746 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
747
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930748 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
749 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700750 EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS);
751
752 EXPECT_EQ(src, checkVal);
753 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
754 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
755}
756
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930757TEST(msgbuf, insert_under_int8)
758{
759 struct pldm_msgbuf _ctx;
760 struct pldm_msgbuf* ctx = &_ctx;
761
762 int8_t buf[1] = {};
763 int8_t val = 0;
764
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930765 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930766 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
767 EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS);
768 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
769}
770
Thu Nguyen062c8762023-04-22 20:45:04 +0700771TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
772{
773 struct pldm_msgbuf _ctx;
774 struct pldm_msgbuf* ctx = &_ctx;
775 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
776 uint8_t buf[6] = {};
777 uint8_t retBuff[6] = {};
778
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930779 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000780 EXPECT_EQ(
781 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
782 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700783
784 struct pldm_msgbuf _ctxExtract;
785 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
786
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930787 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
788 PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000789 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff),
790 retBuff, sizeof(retBuff)),
791 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700792
793 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
794 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
795 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
796}
797
798TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad)
799{
800 struct pldm_msgbuf _ctx;
801 struct pldm_msgbuf* ctx = &_ctx;
802 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
803 uint8_t buf[6] = {};
804
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930805 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000806 EXPECT_EQ(
807 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), NULL, sizeof(src)),
808 PLDM_ERROR_INVALID_DATA);
Thu Nguyen062c8762023-04-22 20:45:04 +0700809 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
810}
811
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930812TEST(msgbuf, insert_under_array_uint8)
813{
814 struct pldm_msgbuf _ctx;
815 struct pldm_msgbuf* ctx = &_ctx;
816
817 uint8_t buf[1] = {};
818 uint8_t val[1] = {0};
819
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930820 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930821 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000822 EXPECT_NE(
823 pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)),
824 PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930825 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
826}
827
Andrew Jeffery1c571442024-07-08 10:25:48 +0930828TEST(msgbuf, pldm_msgbuf_insert_array_char_good)
829{
830 struct pldm_msgbuf _ctx;
831 struct pldm_msgbuf* ctx = &_ctx;
832 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
833 char buf[6] = {};
834 char retBuff[6] = {};
835
836 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000837 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)),
838 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930839
840 struct pldm_msgbuf _ctxExtract;
841 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
842
843 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000844 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff),
845 retBuff, sizeof(retBuff)),
846 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930847
848 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
849 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
850 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
851}
852
853TEST(msgbuf, pldm_msgbuf_insert_array_char_bad)
854{
855 struct pldm_msgbuf _ctx;
856 struct pldm_msgbuf* ctx = &_ctx;
857 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
858 char buf[6] = {};
859
860 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000861 EXPECT_EQ(
862 pldm_msgbuf_insert_array_char(ctx, sizeof(src), NULL, sizeof(src)),
863 -EINVAL);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930864 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
865}
866
867TEST(msgbuf, insert_under_array_char)
868{
869 struct pldm_msgbuf _ctx;
870 struct pldm_msgbuf* ctx = &_ctx;
871 char buf[1] = {};
872 char val[1] = {0};
873
874 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
875 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000876 EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)),
877 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930878 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
879}
880
Thu Nguyen062c8762023-04-22 20:45:04 +0700881TEST(msgbuf, pldm_msgbuf_span_required_good)
882{
883 struct pldm_msgbuf _ctx;
884 struct pldm_msgbuf* ctx = &_ctx;
885 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
886 uint8_t buf[6] = {0};
887 const size_t required = 4;
888 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
889 uint16_t testVal;
890 uint8_t* retBuff = NULL;
891
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930892 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000893 EXPECT_EQ(
894 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
895 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700896
897 struct pldm_msgbuf _ctxExtract;
898 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
899
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930900 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
901 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700902 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
903 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
904 PLDM_SUCCESS);
905
906 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
907 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
908 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
909}
910
911TEST(msgbuf, pldm_msgbuf_span_required_bad)
912{
913 struct pldm_msgbuf _ctx;
914 struct pldm_msgbuf* ctx = &_ctx;
915 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
916 uint8_t buf[6] = {0};
917 const size_t required = 4;
918 uint16_t testVal;
919 [[maybe_unused]] uint8_t* retBuff = NULL;
920
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930921 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000922 EXPECT_EQ(
923 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
924 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700925
926 struct pldm_msgbuf _ctxExtract;
927 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
928
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930929 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
930 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700931 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
932 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
933 PLDM_ERROR_INVALID_DATA);
Thu Nguyen062c8762023-04-22 20:45:04 +0700934
935 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
936 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
937}
938
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930939TEST(msgbuf, span_required_under)
940{
941 struct pldm_msgbuf _ctx;
942 struct pldm_msgbuf* ctx = &_ctx;
943
944 uint8_t buf[1] = {};
945 void* cursor = nullptr;
946
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930947 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930948 ctx->remaining = INTMAX_MIN;
949 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS);
950 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
951}
952
Thu Nguyen9c83d682024-07-02 08:43:09 +0000953TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
954{
955 struct pldm_msgbuf _ctxExtract;
956 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
957 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
958 constexpr size_t required = 6;
959 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
960 uint16_t testVal;
961 uint8_t testVal1;
962 char* retBuff = NULL;
963
964 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
965 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
966 EXPECT_EQ(0x2211, testVal);
967 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
968 0);
969 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
970 EXPECT_EQ(0x77, testVal1);
971
972 EXPECT_EQ(required, strlen(retBuff) + 1);
973 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
974 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
975}
976
977TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length)
978{
979 struct pldm_msgbuf _ctxExtract;
980 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
981 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
982 constexpr size_t required = 6;
983 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
984 uint16_t testVal;
985 uint8_t testVal1;
986 char* retBuff = NULL;
987 size_t length;
988
989 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
990 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
991 EXPECT_EQ(0x2211, testVal);
992 EXPECT_EQ(
993 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
994 0);
995 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
996 EXPECT_EQ(0x77, testVal1);
997
998 EXPECT_EQ(required, strlen(retBuff) + 1);
999 EXPECT_EQ(length, strlen(retBuff) + 1);
1000 EXPECT_EQ(required, length);
1001 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
1002 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1003}
1004
1005TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
1006{
1007 struct pldm_msgbuf _ctxExtract;
1008 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1009 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
1010 uint16_t testVal;
1011
1012 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1013 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1014 EXPECT_EQ(0x2211, testVal);
1015 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
1016 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1017}
1018
1019TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
1020{
1021 struct pldm_msgbuf _ctxExtract;
1022 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1023 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
1024 uint16_t testVal;
1025 char* retBuff = NULL;
1026
1027 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1028 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1029 EXPECT_EQ(0x2211, testVal);
1030 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1031 -EOVERFLOW);
1032 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1033}
1034
1035TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
1036{
1037 struct pldm_msgbuf _ctxExtract;
1038 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1039
1040 uint8_t src[1] = {};
1041 char* retBuff = NULL;
1042
1043 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1044 ctxExtract->remaining = INTMAX_MIN;
1045 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1046 0);
1047 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1048}
1049
Thu Nguyen15237782024-07-02 09:30:41 +00001050static size_t str16len(char16_t* startptr)
1051{
1052 char16_t* endptr = startptr;
1053 while (*endptr)
1054 {
1055 endptr++;
1056 }
1057 return endptr - startptr;
1058}
1059
1060TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
1061{
1062 struct pldm_msgbuf _ctxExtract;
1063 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1064 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
1065 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1066 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1067 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1068 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1069 uint16_t testVal;
1070 uint16_t testVal1;
1071 void* retBuff = NULL;
1072
1073 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1074 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1075 EXPECT_EQ(0x2211, testVal);
1076
1077 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1078 0);
1079 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1080 EXPECT_EQ(0x1234, testVal1);
1081
1082 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1083 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1084 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1085 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1086}
1087
1088TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1089{
1090 struct pldm_msgbuf _ctxExtract;
1091 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1092 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1093 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1094 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1095 constexpr size_t required = 6;
1096 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1097 0x6c44, 0x6f55, 0x0000};
1098 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1099 uint16_t testVal;
1100 uint16_t testVal1;
1101 char* retBuff = NULL;
1102 char* retBuff1 = NULL;
1103 size_t length = 0;
1104
1105 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1106 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1107 EXPECT_EQ(0x2211, testVal);
1108
1109 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1110 0);
1111
1112 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1113 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1114 EXPECT_EQ(memcmp(expectData, retBuff,
1115 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1116 0);
1117
1118 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1119 EXPECT_EQ(0x1234, testVal1);
1120
1121 EXPECT_EQ(
1122 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1123 0);
1124
1125 EXPECT_EQ(0, length % 2);
1126 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1127
1128 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1129 EXPECT_EQ(0x1234, testVal1);
1130
1131 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1132}
1133
1134TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1135{
1136 struct pldm_msgbuf _ctxExtract;
1137 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1138 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1139 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1140 uint16_t testVal;
1141
1142 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1143 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1144 EXPECT_EQ(0x2211, testVal);
1145 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1146 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1147}
1148
1149TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1150{
1151 struct pldm_msgbuf _ctxExtract;
1152 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1153 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1154 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1155 uint16_t testVal;
1156 char16_t* retBuff = NULL;
1157
1158 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1159 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1160 EXPECT_EQ(0x2211, testVal);
1161 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1162 -EOVERFLOW);
1163 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1164}
1165
1166TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1167{
1168 struct pldm_msgbuf _ctxExtract;
1169 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1170 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1171 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1172 uint16_t testVal;
1173 char16_t* retBuff = NULL;
1174
1175 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1176 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1177 EXPECT_EQ(0x2211, testVal);
1178 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1179 -EOVERFLOW);
1180 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1181}
1182
1183TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1184{
1185 struct pldm_msgbuf _ctxExtract;
1186 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1187 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1188 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1189 0x58, 0x00, 0x00, // Entity Name "S0S"
1190 0x66, 0x6e, 0x00, // Language Tag "en"
1191 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1192 0x67, 0x6e, 0x00, // Language Tag "en"
1193 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1194 0x00, // Entity Name "RR3"
1195 0x77, 0x88};
1196 uint8_t name_count;
1197 uint16_t test_val;
1198 char* tag = NULL;
1199 char* name = NULL;
1200 char* tag1 = NULL;
1201 char* name1 = NULL;
1202 char* tag2 = NULL;
1203 char* name2 = NULL;
1204 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1205 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1206 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1207
1208 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1209 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1210 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1211 size_t length = 0;
1212
1213 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1214 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0);
1215 EXPECT_EQ(0x2, name_count);
1216
1217 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1218 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1219
1220 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1221 0);
1222 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1223 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1224 EXPECT_EQ(memcmp(expectName0, name,
1225 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1226 0);
1227
1228 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1229 0);
1230 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1231 EXPECT_EQ(
1232 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1233 EXPECT_EQ(0, length % 2);
1234 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1235
1236 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1237 0);
1238 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1239 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1240 0);
1241 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1242 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1243 EXPECT_EQ(memcmp(expectName2, name2,
1244 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1245 0);
1246
1247 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0);
1248 EXPECT_EQ(0x8877, test_val);
1249
1250 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1251}
1252
1253TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1254{
1255 struct pldm_msgbuf _ctxExtract;
1256 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1257
1258 uint8_t src[1] = {};
1259 char* retBuff = NULL;
1260
1261 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1262 ctxExtract->remaining = INTMAX_MIN;
1263 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1264 0);
1265 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1266}
1267
Thu Nguyen062c8762023-04-22 20:45:04 +07001268TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1269{
1270 struct pldm_msgbuf _ctx;
1271 struct pldm_msgbuf* ctx = &_ctx;
1272 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1273 uint8_t buf[8] = {0};
1274 uint16_t testVal;
1275 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1276 size_t remaining;
1277 uint8_t* retBuff = NULL;
1278
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301279 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001280 EXPECT_EQ(
1281 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1282 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001283
1284 struct pldm_msgbuf _ctxExtract;
1285 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1286
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301287 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1288 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001289 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
1290 EXPECT_EQ(
1291 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
1292 PLDM_SUCCESS);
1293
1294 EXPECT_EQ(remaining, sizeof(expectData));
1295 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
1296 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1297 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
1298}
1299
1300TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1301{
1302 struct pldm_msgbuf _ctx;
1303 struct pldm_msgbuf* ctx = &_ctx;
1304 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1305 uint8_t buf[8] = {0};
1306 uint16_t testVal;
1307 size_t remaining;
1308 uint8_t* retBuff = NULL;
1309
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301310 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001311 EXPECT_EQ(
1312 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1313 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001314
1315 struct pldm_msgbuf _ctxExtract;
1316 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1317
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301318 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1319 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001320 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001321 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining),
1322 PLDM_ERROR_INVALID_DATA);
1323 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL),
1324 PLDM_ERROR_INVALID_DATA);
1325
1326 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1327 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301328}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001329
1330TEST(msgbuf, pldm_msgbuf_copy_good)
1331{
1332 struct pldm_msgbuf _src;
1333 struct pldm_msgbuf* src = &_src;
1334 uint16_t buf[1] = {htole16(0x5aa5)};
1335
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301336 ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001337 PLDM_SUCCESS);
1338
1339 struct pldm_msgbuf _dst;
1340 struct pldm_msgbuf* dst = &_dst;
1341 uint16_t checkVal = 0;
1342 uint8_t buf1[sizeof(buf)] = {};
1343
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301344 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001345 PLDM_SUCCESS);
1346 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS);
1347
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301348 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001349 PLDM_SUCCESS);
1350 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS);
1351
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001352 EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS);
1353 EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301354
1355 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001356}
1357
1358TEST(msgbuf, pldm_msgbuf_copy_bad)
1359{
1360 struct pldm_msgbuf _src;
1361 struct pldm_msgbuf* src = &_src;
1362 struct pldm_msgbuf _dst;
1363 struct pldm_msgbuf* dst = &_dst;
1364 uint8_t buf[1] = {sizeof(uint8_t)};
1365 uint8_t buf1[1] = {sizeof(uint16_t)};
1366 uint16_t value = 8;
1367
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301368 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS);
1369 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001370 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1371 PLDM_ERROR_INVALID_LENGTH);
1372
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301373 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
1374 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001375 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1376 PLDM_ERROR_INVALID_LENGTH);
1377}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301378
1379TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
1380{
1381 const char msg[] = "this is a message";
1382
1383 struct pldm_msgbuf _src;
1384 struct pldm_msgbuf* src = &_src;
1385 struct pldm_msgbuf _dst;
1386 struct pldm_msgbuf* dst = &_dst;
1387
1388 char buf[sizeof(msg)] = {};
1389
1390 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1391 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1392 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1393 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1394 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1395 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1396}
1397
1398TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1399{
1400 const char msg[] = "this is a message";
1401
1402 struct pldm_msgbuf _src;
1403 struct pldm_msgbuf* src = &_src;
1404 struct pldm_msgbuf _dst;
1405 struct pldm_msgbuf* dst = &_dst;
1406
1407 char buf[sizeof(msg) + 1] = {};
1408
1409 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1410 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1411 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1412 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1413 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1414 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1415}
1416
1417TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1418{
1419 const char msg[] = "this is a message";
1420
1421 struct pldm_msgbuf _src;
1422 struct pldm_msgbuf* src = &_src;
1423 struct pldm_msgbuf _dst;
1424 struct pldm_msgbuf* dst = &_dst;
1425
1426 char buf[sizeof(msg) - 1] = {};
1427
1428 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1429 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1430 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1431 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1432 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1433}
1434
1435TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1436{
1437 const char msg[] = {'a'};
1438
1439 struct pldm_msgbuf _src;
1440 struct pldm_msgbuf* src = &_src;
1441 struct pldm_msgbuf _dst;
1442 struct pldm_msgbuf* dst = &_dst;
1443
1444 char buf[sizeof(msg)] = {};
1445
1446 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1447 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1448 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1449 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1450 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1451}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301452
1453TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
1454{
1455 const char16_t msg[] = u"this is a message";
1456
1457 struct pldm_msgbuf _src;
1458 struct pldm_msgbuf* src = &_src;
1459 struct pldm_msgbuf _dst;
1460 struct pldm_msgbuf* dst = &_dst;
1461
1462 char buf[sizeof(msg)] = {};
1463
1464 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1465 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1466 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1467 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1468 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1469 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1470}
1471
1472TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1473{
1474 const char16_t msg[] = u"this is a message";
1475
1476 struct pldm_msgbuf _src;
1477 struct pldm_msgbuf* src = &_src;
1478 struct pldm_msgbuf _dst;
1479 struct pldm_msgbuf* dst = &_dst;
1480
1481 char buf[sizeof(msg) + 1] = {};
1482
1483 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1484 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1485 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1486 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1487 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1488 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1489}
1490
1491TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1492{
1493 const char16_t msg[] = u"this is a message";
1494
1495 struct pldm_msgbuf _src;
1496 struct pldm_msgbuf* src = &_src;
1497 struct pldm_msgbuf _dst;
1498 struct pldm_msgbuf* dst = &_dst;
1499
1500 char buf[sizeof(msg) - 1] = {};
1501
1502 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1503 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1504 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1505 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1506 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1507}
1508
1509TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1510{
1511 const char16_t msg[] = {u'a'};
1512
1513 struct pldm_msgbuf _src;
1514 struct pldm_msgbuf* src = &_src;
1515 struct pldm_msgbuf _dst;
1516 struct pldm_msgbuf* dst = &_dst;
1517
1518 char buf[sizeof(msg)] = {};
1519
1520 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1521 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1522 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1523 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1524 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1525}