blob: 111200a7ff04c6d4a6fc021d7f28a7e5da0a993f [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
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103015TEST(msgbuf, init_bad_minsize)
16{
17 struct pldm_msgbuf _ctx;
18 struct pldm_msgbuf* ctx = &_ctx;
19 uint8_t buf[1] = {};
20
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093021 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103022 PLDM_SUCCESS);
23}
24
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103025TEST(msgbuf, init_bad_len)
26{
27 struct pldm_msgbuf _ctx;
28 struct pldm_msgbuf* ctx = &_ctx;
29 uint8_t buf[1] = {};
30
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093031 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX),
32 PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103033}
34
35TEST(msgbuf, init_overflow)
36{
37 struct pldm_msgbuf _ctx;
38 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103039 // NOLINTNEXTLINE(performance-no-int-to-ptr)
Andrew Jeffery07febdb2024-05-17 14:17:14 +093040 void* buf = (void*)UINTPTR_MAX;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103041
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093042 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103043}
44
45TEST(msgbuf, init_success)
46{
47 struct pldm_msgbuf _ctx;
48 struct pldm_msgbuf* ctx = &_ctx;
49 uint8_t buf[1] = {};
50
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093051 EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103052 PLDM_SUCCESS);
53}
54
55TEST(msgbuf, destroy_none)
56{
57 struct pldm_msgbuf _ctx;
58 struct pldm_msgbuf* ctx = &_ctx;
59 uint8_t buf[1] = {};
60
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093061 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103062 PLDM_SUCCESS);
63 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
64}
65
66TEST(msgbuf, destroy_exact)
67{
68 struct pldm_msgbuf _ctx;
69 struct pldm_msgbuf* ctx = &_ctx;
70 uint8_t buf[1] = {0xa5};
71 uint8_t val;
72
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093073 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103074 PLDM_SUCCESS);
75 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
76 EXPECT_EQ(val, 0xa5);
77 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
78}
79
80TEST(msgbuf, destroy_over)
81{
82 struct pldm_msgbuf _ctx;
83 struct pldm_msgbuf* ctx = &_ctx;
84 uint8_t buf[1] = {0xa5};
85 uint8_t val;
86
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093087 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103088 PLDM_SUCCESS);
89 ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
90 ASSERT_EQ(val, 0xa5);
91 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
92 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
93}
94
95TEST(msgbuf, destroy_under)
96{
97 struct pldm_msgbuf _ctx;
98 struct pldm_msgbuf* ctx = &_ctx;
99 uint8_t buf[2] = {0x5a, 0xa5};
100 uint8_t val;
101
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930102 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030103 PLDM_SUCCESS);
104 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
105 EXPECT_EQ(val, 0x5a);
106 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
107}
108
109TEST(msgbuf, extract_one_uint8)
110{
111 struct pldm_msgbuf _ctx;
112 struct pldm_msgbuf* ctx = &_ctx;
113 uint8_t buf[1] = {0xa5};
114 uint8_t val;
115
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930116 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030117 PLDM_SUCCESS);
118 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
119 EXPECT_EQ(val, 0xa5);
120 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
121}
122
123TEST(msgbuf, extract_over_uint8)
124{
125 struct pldm_msgbuf _ctx;
126 struct pldm_msgbuf* ctx = &_ctx;
127 uint8_t buf[1] = {};
128 uint8_t val;
129
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930130 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030131 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
132 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
133}
134
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930135TEST(msgbuf, extract_under_uint8)
136{
137 struct pldm_msgbuf _ctx;
138 struct pldm_msgbuf* ctx = &_ctx;
139
140 uint8_t buf[1] = {};
141 uint8_t val;
142
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930143 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930144 ctx->remaining = INTMAX_MIN;
145 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
146 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
147}
148
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030149TEST(msgbuf, extract_one_int8)
150{
151 struct pldm_msgbuf _ctx;
152 struct pldm_msgbuf* ctx = &_ctx;
153 int8_t buf[1] = {-1};
154 int8_t val;
155
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930156 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030157 PLDM_SUCCESS);
158 EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
159 EXPECT_EQ(val, -1);
160 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
161}
162
163TEST(msgbuf, extract_over_int8)
164{
165 struct pldm_msgbuf _ctx;
166 struct pldm_msgbuf* ctx = &_ctx;
167 int8_t buf[1] = {};
168 int8_t val;
169
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930170 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030171 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
172 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
173}
174
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930175TEST(msgbuf, extract_under_int8)
176{
177 struct pldm_msgbuf _ctx;
178 struct pldm_msgbuf* ctx = &_ctx;
179
180 uint8_t buf[1] = {};
181 int8_t val;
182
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930183 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930184 ctx->remaining = INTMAX_MIN;
185 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
186 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
187}
188
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030189TEST(msgbuf, extract_one_uint16)
190{
191 struct pldm_msgbuf _ctx;
192 struct pldm_msgbuf* ctx = &_ctx;
193 uint16_t buf[1] = {htole16(0x5aa5)};
194 uint16_t val = {};
195
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930196 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030197 PLDM_SUCCESS);
198 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
199 EXPECT_EQ(val, 0x5aa5);
200 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
201}
202
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930203TEST(msgbuf, extract_under_uint16)
204{
205 struct pldm_msgbuf _ctx;
206 struct pldm_msgbuf* ctx = &_ctx;
207
208 uint16_t buf[1] = {};
209 uint16_t val;
210
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930211 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930212 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
213 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
214 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
215}
216
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030217TEST(msgbuf, extract_over_uint16)
218{
219 struct pldm_msgbuf _ctx;
220 struct pldm_msgbuf* ctx = &_ctx;
221 uint16_t buf[1] = {};
222 uint16_t val;
223
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930224 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030225 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
226 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
227}
228
229TEST(msgbuf, extract_one_int16)
230{
231 struct pldm_msgbuf _ctx;
232 struct pldm_msgbuf* ctx = &_ctx;
233 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
234 int16_t val;
235
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930236 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030237 PLDM_SUCCESS);
238 EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
239 EXPECT_EQ(val, INT16_MIN);
240 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
241}
242
243TEST(msgbuf, extract_over_int16)
244{
245 struct pldm_msgbuf _ctx;
246 struct pldm_msgbuf* ctx = &_ctx;
247 int16_t buf[1] = {};
248 int16_t val;
249
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930250 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030251 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
252 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
253}
254
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930255TEST(msgbuf, extract_under_int16)
256{
257 struct pldm_msgbuf _ctx;
258 struct pldm_msgbuf* ctx = &_ctx;
259
260 int16_t buf[1] = {};
261 int16_t val;
262
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930263 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930264 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
265 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
266 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
267}
268
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030269TEST(msgbuf, extract_one_uint32)
270{
271 struct pldm_msgbuf _ctx;
272 struct pldm_msgbuf* ctx = &_ctx;
273 uint32_t buf[1] = {htole32(0x5a00ffa5)};
274 uint32_t val;
275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930276 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030277 PLDM_SUCCESS);
278 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
279 EXPECT_EQ(val, 0x5a00ffa5);
280 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
281}
282
283TEST(msgbuf, extract_over_uint32)
284{
285 struct pldm_msgbuf _ctx;
286 struct pldm_msgbuf* ctx = &_ctx;
287 uint32_t buf[1] = {};
288 uint32_t val;
289
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930290 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030291 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
292 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
293}
294
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930295TEST(msgbuf, extract_under_uint32)
296{
297 struct pldm_msgbuf _ctx;
298 struct pldm_msgbuf* ctx = &_ctx;
299
300 uint32_t buf[1] = {};
301 uint32_t val;
302
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930303 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930304 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
305 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
306 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
307}
308
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030309TEST(msgbuf, extract_one_int32)
310{
311 struct pldm_msgbuf _ctx;
312 struct pldm_msgbuf* ctx = &_ctx;
313 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
314 int32_t val;
315
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930316 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030317 PLDM_SUCCESS);
318 EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
319 EXPECT_EQ(val, INT32_MIN);
320 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
321}
322
323TEST(msgbuf, extract_over_int32)
324{
325 struct pldm_msgbuf _ctx;
326 struct pldm_msgbuf* ctx = &_ctx;
327 int32_t buf[1] = {};
328 int32_t val;
329
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930330 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030331 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
332 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
333}
334
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930335TEST(msgbuf, extract_under_int32)
336{
337 struct pldm_msgbuf _ctx;
338 struct pldm_msgbuf* ctx = &_ctx;
339
340 int32_t buf[1] = {};
341 int32_t val;
342
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930343 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930344 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
345 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
346 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
347}
348
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030349TEST(msgbuf, extract_one_real32)
350{
351 struct pldm_msgbuf _ctx;
352 struct pldm_msgbuf* ctx = &_ctx;
353 uint32_t buf[1] = {};
354 uint32_t xform;
355 real32_t val;
356
357 val = FLT_MAX;
358 memcpy(&xform, &val, sizeof(val));
359 buf[0] = htole32(xform);
360 val = 0;
361
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930362 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030363 PLDM_SUCCESS);
364 EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
365 EXPECT_EQ(val, FLT_MAX);
366 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
367}
368
369TEST(msgbuf, extract_over_real32)
370{
371 struct pldm_msgbuf _ctx;
372 struct pldm_msgbuf* ctx = &_ctx;
373 real32_t buf[1] = {};
374 real32_t val;
375
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930376 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030377 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
378 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
379}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930380
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930381TEST(msgbuf, extract_under_real32)
382{
383 struct pldm_msgbuf _ctx;
384 struct pldm_msgbuf* ctx = &_ctx;
385
386 real32_t buf[1] = {};
387 real32_t val;
388
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930389 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930390 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
391 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
392 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
393}
394
Andrew Jeffery369b1212023-04-20 15:44:48 +0930395TEST(msgbuf, extract_array_uint8_buf0_req0)
396{
397 struct pldm_msgbuf _ctx;
398 struct pldm_msgbuf* ctx = &_ctx;
399 uint8_t buf[1] = {};
400 uint8_t arr[1];
401
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930402 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000403 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930404 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
405}
406
407TEST(msgbuf, extract_array_uint8_buf1_req1)
408{
409 struct pldm_msgbuf _ctx;
410 struct pldm_msgbuf* ctx = &_ctx;
411 uint8_t buf[1] = {};
412 uint8_t arr[1];
413
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930414 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000415 EXPECT_EQ(
416 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
417 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930418 EXPECT_EQ(arr[0], 0);
419 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
420}
421
422TEST(msgbuf, extract_array_uint8_buf1_req2)
423{
424 struct pldm_msgbuf _ctx;
425 struct pldm_msgbuf* ctx = &_ctx;
426 uint8_t buf[1] = {};
427 uint8_t arr[2];
428
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930429 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000430 EXPECT_NE(
431 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
432 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930433 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
434}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930435
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930436TEST(msgbuf, extract_under_array_uint8)
437{
438 struct pldm_msgbuf _ctx;
439 struct pldm_msgbuf* ctx = &_ctx;
440 uint8_t buf[1] = {};
441 uint8_t arr[1];
442
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930443 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930444 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000445 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930446 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
447}
448
Andrew Jeffery1c571442024-07-08 10:25:48 +0930449TEST(msgbuf, extract_array_char_buf0_req0)
450{
451 struct pldm_msgbuf _ctx;
452 struct pldm_msgbuf* ctx = &_ctx;
453 char buf[1] = {'\0'};
454 char arr[1] = {'1'};
455
456 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000457 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930458 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
459}
460
461TEST(msgbuf, extract_array_char_buf1_req1)
462{
463 struct pldm_msgbuf _ctx;
464 struct pldm_msgbuf* ctx = &_ctx;
465 char buf[1] = {'\0'};
466 char arr[1] = {'1'};
467
468 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000469 EXPECT_EQ(
470 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930471 EXPECT_EQ(arr[0], '\0');
472 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
473}
474
475TEST(msgbuf, extract_array_char_buf1_req2)
476{
477 struct pldm_msgbuf _ctx;
478 struct pldm_msgbuf* ctx = &_ctx;
479 char buf[1] = {'\0'};
480 char arr[2] = {'1', '2'};
481
482 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000483 EXPECT_NE(
484 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930485 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
486}
487
488TEST(msgbuf, extract_under_array_char)
489{
490 struct pldm_msgbuf _ctx;
491 struct pldm_msgbuf* ctx = &_ctx;
492 char buf[1] = {'\0'};
493 char arr[1] = {'1'};
494
495 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
496 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000497 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930498 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
499}
500
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930501TEST(msgbuf, consumed_under)
502{
503 struct pldm_msgbuf _ctx;
504 struct pldm_msgbuf* ctx = &_ctx;
505 uint8_t buf[1] = {};
506
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930507 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930508 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
509}
510
511TEST(msgbuf, consumed_exact)
512{
513 struct pldm_msgbuf _ctx;
514 struct pldm_msgbuf* ctx = &_ctx;
515 uint8_t buf[1] = {};
516 uint8_t val;
517
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930518 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930519 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
520 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
521}
522
523TEST(msgbuf, consumed_over)
524{
525 struct pldm_msgbuf _ctx;
526 struct pldm_msgbuf* ctx = &_ctx;
527 uint8_t buf[1] = {};
528 uint8_t val[2];
529
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930530 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930531 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS);
532 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
533 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
534}
Thu Nguyen062c8762023-04-22 20:45:04 +0700535
536TEST(msgbuf, pldm_msgbuf_insert_int32_good)
537{
538 struct pldm_msgbuf _ctx;
539 struct pldm_msgbuf* ctx = &_ctx;
540 int32_t src = -12345;
541 int32_t checkVal = 0;
542 uint8_t buf[sizeof(int32_t)] = {};
543
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930544 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700545 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
546
547 struct pldm_msgbuf _ctxExtract;
548 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
549
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930550 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
551 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700552 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS);
553
554 EXPECT_EQ(src, checkVal);
555 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
556 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
557}
558
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930559TEST(msgbuf, insert_under_int32)
560{
561 struct pldm_msgbuf _ctx;
562 struct pldm_msgbuf* ctx = &_ctx;
563
564 int32_t buf[1] = {};
565 int32_t val = 0;
566
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930567 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930568 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
569 EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS);
570 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
571}
572
Thu Nguyen062c8762023-04-22 20:45:04 +0700573TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
574{
575 struct pldm_msgbuf _ctx;
576 struct pldm_msgbuf* ctx = &_ctx;
577 uint32_t src = 0xf1223344;
578 uint32_t checkVal = 0;
579 uint8_t buf[sizeof(uint32_t)] = {};
580
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930581 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700582 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
583
584 struct pldm_msgbuf _ctxExtract;
585 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
586
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930587 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
588 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700589 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS);
590
591 EXPECT_EQ(src, checkVal);
592 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
593 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
594}
595
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930596TEST(msgbuf, insert_under_uint32)
597{
598 struct pldm_msgbuf _ctx;
599 struct pldm_msgbuf* ctx = &_ctx;
600
601 uint32_t buf[1] = {};
602 uint32_t val = 0;
603
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930604 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930605 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
606 EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS);
607 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
608}
609
Thu Nguyen062c8762023-04-22 20:45:04 +0700610TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
611{
612 struct pldm_msgbuf _ctx;
613 struct pldm_msgbuf* ctx = &_ctx;
614 uint16_t src = 0xf344;
615 uint16_t checkVal = 0;
616 uint8_t buf[sizeof(uint16_t)] = {};
617
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930618 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700619 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
620
621 struct pldm_msgbuf _ctxExtract;
622 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
623
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930624 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
625 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700626 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS);
627
628 EXPECT_EQ(src, checkVal);
629 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
630 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
631}
632
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930633TEST(msgbuf, insert_under_uint16)
634{
635 struct pldm_msgbuf _ctx;
636 struct pldm_msgbuf* ctx = &_ctx;
637
638 uint16_t buf[1] = {};
639 uint16_t val = 0;
640
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930641 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930642 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
643 EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS);
644 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
645}
646
Thu Nguyen062c8762023-04-22 20:45:04 +0700647TEST(msgbuf, pldm_msgbuf_insert_int16_good)
648{
649 struct pldm_msgbuf _ctx;
650 struct pldm_msgbuf* ctx = &_ctx;
651 int16_t src = -12;
652 int16_t checkVal = 0;
653 uint8_t buf[sizeof(int16_t)] = {};
654
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930655 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700656 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
657
658 struct pldm_msgbuf _ctxExtract;
659 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
660
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930661 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
662 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700663 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS);
664
665 EXPECT_EQ(src, checkVal);
666 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
667 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
668}
669
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930670TEST(msgbuf, insert_under_int16)
671{
672 struct pldm_msgbuf _ctx;
673 struct pldm_msgbuf* ctx = &_ctx;
674
675 int16_t buf[1] = {};
676 int16_t val = 0;
677
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930678 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930679 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
680 EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS);
681 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
682}
683
Thu Nguyen062c8762023-04-22 20:45:04 +0700684TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
685{
686 struct pldm_msgbuf _ctx;
687 struct pldm_msgbuf* ctx = &_ctx;
688 uint8_t src = 0xf4;
689 uint8_t checkVal = 0;
690 uint8_t buf[sizeof(uint8_t)] = {};
691
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930692 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700693 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
694
695 struct pldm_msgbuf _ctxExtract;
696 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
697
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930698 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
699 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700700 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS);
701
702 EXPECT_EQ(src, checkVal);
703 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
704 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
705}
706
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930707TEST(msgbuf, insert_under_uint8)
708{
709 struct pldm_msgbuf _ctx;
710 struct pldm_msgbuf* ctx = &_ctx;
711
712 uint8_t buf[1] = {};
713 uint8_t val = 0;
714
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930715 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930716 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
717 EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS);
718 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
719}
720
Thu Nguyen062c8762023-04-22 20:45:04 +0700721TEST(msgbuf, pldm_msgbuf_insert_int8_good)
722{
723 struct pldm_msgbuf _ctx;
724 struct pldm_msgbuf* ctx = &_ctx;
725 int8_t src = -4;
726 int8_t checkVal = 0;
727 uint8_t buf[sizeof(int8_t)] = {};
728
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930729 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700730 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
731
732 struct pldm_msgbuf _ctxExtract;
733 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
734
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930735 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
736 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700737 EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS);
738
739 EXPECT_EQ(src, checkVal);
740 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
741 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
742}
743
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930744TEST(msgbuf, insert_under_int8)
745{
746 struct pldm_msgbuf _ctx;
747 struct pldm_msgbuf* ctx = &_ctx;
748
749 int8_t buf[1] = {};
750 int8_t val = 0;
751
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930752 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930753 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
754 EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS);
755 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
756}
757
Thu Nguyen062c8762023-04-22 20:45:04 +0700758TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
759{
760 struct pldm_msgbuf _ctx;
761 struct pldm_msgbuf* ctx = &_ctx;
762 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
763 uint8_t buf[6] = {};
764 uint8_t retBuff[6] = {};
765
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930766 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000767 EXPECT_EQ(
768 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
769 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700770
771 struct pldm_msgbuf _ctxExtract;
772 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
773
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930774 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
775 PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000776 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff),
777 retBuff, sizeof(retBuff)),
778 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700779
780 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
781 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
782 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
783}
784
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930785TEST(msgbuf, insert_under_array_uint8)
786{
787 struct pldm_msgbuf _ctx;
788 struct pldm_msgbuf* ctx = &_ctx;
789
790 uint8_t buf[1] = {};
791 uint8_t val[1] = {0};
792
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930793 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930794 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000795 EXPECT_NE(
796 pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)),
797 PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930798 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
799}
800
Andrew Jeffery1c571442024-07-08 10:25:48 +0930801TEST(msgbuf, pldm_msgbuf_insert_array_char_good)
802{
803 struct pldm_msgbuf _ctx;
804 struct pldm_msgbuf* ctx = &_ctx;
805 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
806 char buf[6] = {};
807 char retBuff[6] = {};
808
809 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000810 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)),
811 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930812
813 struct pldm_msgbuf _ctxExtract;
814 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
815
816 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000817 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff),
818 retBuff, sizeof(retBuff)),
819 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930820
821 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
822 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
823 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
824}
825
Andrew Jeffery1c571442024-07-08 10:25:48 +0930826TEST(msgbuf, insert_under_array_char)
827{
828 struct pldm_msgbuf _ctx;
829 struct pldm_msgbuf* ctx = &_ctx;
830 char buf[1] = {};
831 char val[1] = {0};
832
833 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
834 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000835 EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)),
836 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930837 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
838}
839
Thu Nguyen062c8762023-04-22 20:45:04 +0700840TEST(msgbuf, pldm_msgbuf_span_required_good)
841{
842 struct pldm_msgbuf _ctx;
843 struct pldm_msgbuf* ctx = &_ctx;
844 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
845 uint8_t buf[6] = {0};
846 const size_t required = 4;
847 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
848 uint16_t testVal;
849 uint8_t* retBuff = NULL;
850
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930851 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000852 EXPECT_EQ(
853 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
854 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700855
856 struct pldm_msgbuf _ctxExtract;
857 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
858
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930859 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
860 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700861 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
862 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
863 PLDM_SUCCESS);
864
865 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
866 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
867 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
868}
869
870TEST(msgbuf, pldm_msgbuf_span_required_bad)
871{
872 struct pldm_msgbuf _ctx;
873 struct pldm_msgbuf* ctx = &_ctx;
874 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
875 uint8_t buf[6] = {0};
Thu Nguyen062c8762023-04-22 20:45:04 +0700876 uint16_t testVal;
877 [[maybe_unused]] uint8_t* retBuff = NULL;
878
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930879 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000880 EXPECT_EQ(
881 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
882 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700883
884 struct pldm_msgbuf _ctxExtract;
885 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
886
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930887 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
888 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700889 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700890
891 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
892 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
893}
894
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930895TEST(msgbuf, span_required_under)
896{
897 struct pldm_msgbuf _ctx;
898 struct pldm_msgbuf* ctx = &_ctx;
899
900 uint8_t buf[1] = {};
901 void* cursor = nullptr;
902
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930903 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930904 ctx->remaining = INTMAX_MIN;
905 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS);
906 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
907}
908
Thu Nguyen9c83d682024-07-02 08:43:09 +0000909TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
910{
911 struct pldm_msgbuf _ctxExtract;
912 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
913 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
914 constexpr size_t required = 6;
915 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
916 uint16_t testVal;
917 uint8_t testVal1;
918 char* retBuff = NULL;
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(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
924 0);
925 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
926 EXPECT_EQ(0x77, testVal1);
927
928 EXPECT_EQ(required, strlen(retBuff) + 1);
929 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
930 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
931}
932
933TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length)
934{
935 struct pldm_msgbuf _ctxExtract;
936 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
937 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
938 constexpr size_t required = 6;
939 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
940 uint16_t testVal;
941 uint8_t testVal1;
942 char* retBuff = NULL;
943 size_t length;
944
945 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
946 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
947 EXPECT_EQ(0x2211, testVal);
948 EXPECT_EQ(
949 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
950 0);
951 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
952 EXPECT_EQ(0x77, testVal1);
953
954 EXPECT_EQ(required, strlen(retBuff) + 1);
955 EXPECT_EQ(length, strlen(retBuff) + 1);
956 EXPECT_EQ(required, length);
957 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
958 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
959}
960
961TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
962{
963 struct pldm_msgbuf _ctxExtract;
964 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
965 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
966 uint16_t testVal;
967
968 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
969 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
970 EXPECT_EQ(0x2211, testVal);
971 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
972 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
973}
974
975TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
976{
977 struct pldm_msgbuf _ctxExtract;
978 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
979 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
980 uint16_t testVal;
981 char* retBuff = NULL;
982
983 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
984 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
985 EXPECT_EQ(0x2211, testVal);
986 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
987 -EOVERFLOW);
988 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
989}
990
991TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
992{
993 struct pldm_msgbuf _ctxExtract;
994 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
995
996 uint8_t src[1] = {};
997 char* retBuff = NULL;
998
999 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1000 ctxExtract->remaining = INTMAX_MIN;
1001 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1002 0);
1003 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1004}
1005
Thu Nguyen15237782024-07-02 09:30:41 +00001006static size_t str16len(char16_t* startptr)
1007{
1008 char16_t* endptr = startptr;
1009 while (*endptr)
1010 {
1011 endptr++;
1012 }
1013 return endptr - startptr;
1014}
1015
1016TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
1017{
1018 struct pldm_msgbuf _ctxExtract;
1019 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1020 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
1021 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1022 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1023 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1024 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1025 uint16_t testVal;
1026 uint16_t testVal1;
1027 void* retBuff = NULL;
1028
1029 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1030 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1031 EXPECT_EQ(0x2211, testVal);
1032
1033 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1034 0);
1035 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1036 EXPECT_EQ(0x1234, testVal1);
1037
1038 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1039 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1040 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1041 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1042}
1043
1044TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1045{
1046 struct pldm_msgbuf _ctxExtract;
1047 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1048 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1049 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1050 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1051 constexpr size_t required = 6;
1052 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1053 0x6c44, 0x6f55, 0x0000};
1054 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1055 uint16_t testVal;
1056 uint16_t testVal1;
1057 char* retBuff = NULL;
1058 char* retBuff1 = NULL;
1059 size_t length = 0;
1060
1061 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1062 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1063 EXPECT_EQ(0x2211, testVal);
1064
1065 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1066 0);
1067
1068 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1069 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1070 EXPECT_EQ(memcmp(expectData, retBuff,
1071 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1072 0);
1073
1074 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1075 EXPECT_EQ(0x1234, testVal1);
1076
1077 EXPECT_EQ(
1078 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1079 0);
1080
1081 EXPECT_EQ(0, length % 2);
1082 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1083
1084 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1085 EXPECT_EQ(0x1234, testVal1);
1086
1087 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1088}
1089
1090TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1091{
1092 struct pldm_msgbuf _ctxExtract;
1093 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1094 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1095 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1096 uint16_t testVal;
1097
1098 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1099 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1100 EXPECT_EQ(0x2211, testVal);
1101 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1102 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1103}
1104
1105TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1106{
1107 struct pldm_msgbuf _ctxExtract;
1108 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1109 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1110 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1111 uint16_t testVal;
1112 char16_t* retBuff = NULL;
1113
1114 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1115 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1116 EXPECT_EQ(0x2211, testVal);
1117 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1118 -EOVERFLOW);
1119 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1120}
1121
1122TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1123{
1124 struct pldm_msgbuf _ctxExtract;
1125 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1126 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1127 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1128 uint16_t testVal;
1129 char16_t* retBuff = NULL;
1130
1131 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1132 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1133 EXPECT_EQ(0x2211, testVal);
1134 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1135 -EOVERFLOW);
1136 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1137}
1138
1139TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1140{
1141 struct pldm_msgbuf _ctxExtract;
1142 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1143 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1144 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1145 0x58, 0x00, 0x00, // Entity Name "S0S"
1146 0x66, 0x6e, 0x00, // Language Tag "en"
1147 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1148 0x67, 0x6e, 0x00, // Language Tag "en"
1149 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1150 0x00, // Entity Name "RR3"
1151 0x77, 0x88};
1152 uint8_t name_count;
1153 uint16_t test_val;
1154 char* tag = NULL;
1155 char* name = NULL;
1156 char* tag1 = NULL;
1157 char* name1 = NULL;
1158 char* tag2 = NULL;
1159 char* name2 = NULL;
1160 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1161 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1162 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1163
1164 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1165 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1166 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1167 size_t length = 0;
1168
1169 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1170 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0);
1171 EXPECT_EQ(0x2, name_count);
1172
1173 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1174 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1175
1176 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1177 0);
1178 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1179 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1180 EXPECT_EQ(memcmp(expectName0, name,
1181 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1182 0);
1183
1184 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1185 0);
1186 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1187 EXPECT_EQ(
1188 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1189 EXPECT_EQ(0, length % 2);
1190 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1191
1192 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1193 0);
1194 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1195 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1196 0);
1197 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1198 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1199 EXPECT_EQ(memcmp(expectName2, name2,
1200 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1201 0);
1202
1203 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0);
1204 EXPECT_EQ(0x8877, test_val);
1205
1206 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1207}
1208
1209TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1210{
1211 struct pldm_msgbuf _ctxExtract;
1212 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1213
1214 uint8_t src[1] = {};
1215 char* retBuff = NULL;
1216
1217 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1218 ctxExtract->remaining = INTMAX_MIN;
1219 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1220 0);
1221 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1222}
1223
Thu Nguyen062c8762023-04-22 20:45:04 +07001224TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1225{
1226 struct pldm_msgbuf _ctx;
1227 struct pldm_msgbuf* ctx = &_ctx;
1228 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1229 uint8_t buf[8] = {0};
1230 uint16_t testVal;
1231 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1232 size_t remaining;
1233 uint8_t* retBuff = NULL;
1234
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301235 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001236 EXPECT_EQ(
1237 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1238 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001239
1240 struct pldm_msgbuf _ctxExtract;
1241 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1242
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301243 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1244 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001245 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
1246 EXPECT_EQ(
1247 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
1248 PLDM_SUCCESS);
1249
1250 EXPECT_EQ(remaining, sizeof(expectData));
1251 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
1252 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1253 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
1254}
1255
1256TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1257{
1258 struct pldm_msgbuf _ctx;
1259 struct pldm_msgbuf* ctx = &_ctx;
1260 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1261 uint8_t buf[8] = {0};
1262 uint16_t testVal;
Thu Nguyen062c8762023-04-22 20:45:04 +07001263
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301264 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001265 EXPECT_EQ(
1266 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1267 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001268
1269 struct pldm_msgbuf _ctxExtract;
1270 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1271
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301272 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1273 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001274 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001275
1276 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1277 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301278}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001279
1280TEST(msgbuf, pldm_msgbuf_copy_good)
1281{
1282 struct pldm_msgbuf _src;
1283 struct pldm_msgbuf* src = &_src;
1284 uint16_t buf[1] = {htole16(0x5aa5)};
1285
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301286 ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001287 PLDM_SUCCESS);
1288
1289 struct pldm_msgbuf _dst;
1290 struct pldm_msgbuf* dst = &_dst;
1291 uint16_t checkVal = 0;
1292 uint8_t buf1[sizeof(buf)] = {};
1293
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301294 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001295 PLDM_SUCCESS);
1296 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS);
1297
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301298 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001299 PLDM_SUCCESS);
1300 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS);
1301
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001302 EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS);
1303 EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301304
1305 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001306}
1307
1308TEST(msgbuf, pldm_msgbuf_copy_bad)
1309{
1310 struct pldm_msgbuf _src;
1311 struct pldm_msgbuf* src = &_src;
1312 struct pldm_msgbuf _dst;
1313 struct pldm_msgbuf* dst = &_dst;
1314 uint8_t buf[1] = {sizeof(uint8_t)};
1315 uint8_t buf1[1] = {sizeof(uint16_t)};
1316 uint16_t value = 8;
1317
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301318 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS);
1319 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001320 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1321 PLDM_ERROR_INVALID_LENGTH);
1322
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301323 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
1324 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001325 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1326 PLDM_ERROR_INVALID_LENGTH);
1327}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301328
1329TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
1330{
1331 const char msg[] = "this is a message";
1332
1333 struct pldm_msgbuf _src;
1334 struct pldm_msgbuf* src = &_src;
1335 struct pldm_msgbuf _dst;
1336 struct pldm_msgbuf* dst = &_dst;
1337
1338 char buf[sizeof(msg)] = {};
1339
1340 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1341 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1342 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1343 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1344 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1345 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1346}
1347
1348TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1349{
1350 const char msg[] = "this is a message";
1351
1352 struct pldm_msgbuf _src;
1353 struct pldm_msgbuf* src = &_src;
1354 struct pldm_msgbuf _dst;
1355 struct pldm_msgbuf* dst = &_dst;
1356
1357 char buf[sizeof(msg) + 1] = {};
1358
1359 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1360 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1361 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1362 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1363 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1364 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1365}
1366
1367TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1368{
1369 const char msg[] = "this is a message";
1370
1371 struct pldm_msgbuf _src;
1372 struct pldm_msgbuf* src = &_src;
1373 struct pldm_msgbuf _dst;
1374 struct pldm_msgbuf* dst = &_dst;
1375
1376 char buf[sizeof(msg) - 1] = {};
1377
1378 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1379 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1380 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1381 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1382 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1383}
1384
1385TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1386{
1387 const char msg[] = {'a'};
1388
1389 struct pldm_msgbuf _src;
1390 struct pldm_msgbuf* src = &_src;
1391 struct pldm_msgbuf _dst;
1392 struct pldm_msgbuf* dst = &_dst;
1393
1394 char buf[sizeof(msg)] = {};
1395
1396 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1397 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1398 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1399 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1400 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1401}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301402
1403TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
1404{
1405 const char16_t msg[] = u"this is a message";
1406
1407 struct pldm_msgbuf _src;
1408 struct pldm_msgbuf* src = &_src;
1409 struct pldm_msgbuf _dst;
1410 struct pldm_msgbuf* dst = &_dst;
1411
1412 char buf[sizeof(msg)] = {};
1413
1414 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1415 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1416 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1417 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1418 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1419 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1420}
1421
1422TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1423{
1424 const char16_t msg[] = u"this is a message";
1425
1426 struct pldm_msgbuf _src;
1427 struct pldm_msgbuf* src = &_src;
1428 struct pldm_msgbuf _dst;
1429 struct pldm_msgbuf* dst = &_dst;
1430
1431 char buf[sizeof(msg) + 1] = {};
1432
1433 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1434 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1435 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1436 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1437 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1438 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1439}
1440
1441TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1442{
1443 const char16_t msg[] = u"this is a message";
1444
1445 struct pldm_msgbuf _src;
1446 struct pldm_msgbuf* src = &_src;
1447 struct pldm_msgbuf _dst;
1448 struct pldm_msgbuf* dst = &_dst;
1449
1450 char buf[sizeof(msg) - 1] = {};
1451
1452 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1453 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1454 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1455 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1456 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1457}
1458
1459TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1460{
1461 const char16_t msg[] = {u'a'};
1462
1463 struct pldm_msgbuf _src;
1464 struct pldm_msgbuf* src = &_src;
1465 struct pldm_msgbuf _dst;
1466 struct pldm_msgbuf* dst = &_dst;
1467
1468 char buf[sizeof(msg)] = {};
1469
1470 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1471 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1472 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1473 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1474 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1475}