blob: 7ddd2fb2dcf6d018a05c7a541aaa4a8944533a27 [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};
Varsha Kaverappa79393822024-08-07 00:40:13 -0500876 const size_t required = 4;
Thu Nguyen062c8762023-04-22 20:45:04 +0700877 uint16_t testVal;
878 [[maybe_unused]] uint8_t* retBuff = NULL;
879
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930880 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000881 EXPECT_EQ(
882 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
883 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700884
885 struct pldm_msgbuf _ctxExtract;
886 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
887
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930888 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
889 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700890 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Varsha Kaverappa79393822024-08-07 00:40:13 -0500891 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
892 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700893
894 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
895 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
896}
897
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930898TEST(msgbuf, span_required_under)
899{
900 struct pldm_msgbuf _ctx;
901 struct pldm_msgbuf* ctx = &_ctx;
902
903 uint8_t buf[1] = {};
904 void* cursor = nullptr;
905
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930906 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930907 ctx->remaining = INTMAX_MIN;
908 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS);
909 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
910}
911
Thu Nguyen9c83d682024-07-02 08:43:09 +0000912TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
913{
914 struct pldm_msgbuf _ctxExtract;
915 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
916 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
917 constexpr size_t required = 6;
918 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
919 uint16_t testVal;
920 uint8_t testVal1;
921 char* retBuff = NULL;
922
923 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
924 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
925 EXPECT_EQ(0x2211, testVal);
926 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
927 0);
928 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
929 EXPECT_EQ(0x77, testVal1);
930
931 EXPECT_EQ(required, strlen(retBuff) + 1);
932 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
933 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
934}
935
936TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length)
937{
938 struct pldm_msgbuf _ctxExtract;
939 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
940 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
941 constexpr size_t required = 6;
942 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
943 uint16_t testVal;
944 uint8_t testVal1;
945 char* retBuff = NULL;
946 size_t length;
947
948 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
949 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
950 EXPECT_EQ(0x2211, testVal);
951 EXPECT_EQ(
952 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
953 0);
954 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
955 EXPECT_EQ(0x77, testVal1);
956
957 EXPECT_EQ(required, strlen(retBuff) + 1);
958 EXPECT_EQ(length, strlen(retBuff) + 1);
959 EXPECT_EQ(required, length);
960 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
961 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
962}
963
964TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
965{
966 struct pldm_msgbuf _ctxExtract;
967 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
968 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
969 uint16_t testVal;
970
971 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
972 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
973 EXPECT_EQ(0x2211, testVal);
974 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
975 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
976}
977
978TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
979{
980 struct pldm_msgbuf _ctxExtract;
981 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
982 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
983 uint16_t testVal;
984 char* retBuff = NULL;
985
986 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
987 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
988 EXPECT_EQ(0x2211, testVal);
989 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
990 -EOVERFLOW);
991 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
992}
993
994TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
995{
996 struct pldm_msgbuf _ctxExtract;
997 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
998
999 uint8_t src[1] = {};
1000 char* retBuff = NULL;
1001
1002 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1003 ctxExtract->remaining = INTMAX_MIN;
1004 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1005 0);
1006 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1007}
1008
Thu Nguyen15237782024-07-02 09:30:41 +00001009static size_t str16len(char16_t* startptr)
1010{
1011 char16_t* endptr = startptr;
1012 while (*endptr)
1013 {
1014 endptr++;
1015 }
1016 return endptr - startptr;
1017}
1018
1019TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
1020{
1021 struct pldm_msgbuf _ctxExtract;
1022 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1023 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
1024 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1025 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1026 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1027 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1028 uint16_t testVal;
1029 uint16_t testVal1;
1030 void* retBuff = NULL;
1031
1032 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1033 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1034 EXPECT_EQ(0x2211, testVal);
1035
1036 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1037 0);
1038 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1039 EXPECT_EQ(0x1234, testVal1);
1040
1041 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1042 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1043 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1044 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1045}
1046
1047TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1048{
1049 struct pldm_msgbuf _ctxExtract;
1050 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1051 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1052 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1053 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1054 constexpr size_t required = 6;
1055 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1056 0x6c44, 0x6f55, 0x0000};
1057 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1058 uint16_t testVal;
1059 uint16_t testVal1;
1060 char* retBuff = NULL;
1061 char* retBuff1 = NULL;
1062 size_t length = 0;
1063
1064 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1065 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1066 EXPECT_EQ(0x2211, testVal);
1067
1068 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1069 0);
1070
1071 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1072 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1073 EXPECT_EQ(memcmp(expectData, retBuff,
1074 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1075 0);
1076
1077 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1078 EXPECT_EQ(0x1234, testVal1);
1079
1080 EXPECT_EQ(
1081 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1082 0);
1083
1084 EXPECT_EQ(0, length % 2);
1085 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1086
1087 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1088 EXPECT_EQ(0x1234, testVal1);
1089
1090 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1091}
1092
1093TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1094{
1095 struct pldm_msgbuf _ctxExtract;
1096 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1097 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1098 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1099 uint16_t testVal;
1100
1101 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1102 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1103 EXPECT_EQ(0x2211, testVal);
1104 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1105 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1106}
1107
1108TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1109{
1110 struct pldm_msgbuf _ctxExtract;
1111 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1112 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1113 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1114 uint16_t testVal;
1115 char16_t* retBuff = NULL;
1116
1117 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1118 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1119 EXPECT_EQ(0x2211, testVal);
1120 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1121 -EOVERFLOW);
1122 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1123}
1124
1125TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1126{
1127 struct pldm_msgbuf _ctxExtract;
1128 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1129 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1130 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1131 uint16_t testVal;
1132 char16_t* retBuff = NULL;
1133
1134 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1135 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1136 EXPECT_EQ(0x2211, testVal);
1137 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1138 -EOVERFLOW);
1139 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1140}
1141
1142TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1143{
1144 struct pldm_msgbuf _ctxExtract;
1145 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1146 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1147 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1148 0x58, 0x00, 0x00, // Entity Name "S0S"
1149 0x66, 0x6e, 0x00, // Language Tag "en"
1150 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1151 0x67, 0x6e, 0x00, // Language Tag "en"
1152 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1153 0x00, // Entity Name "RR3"
1154 0x77, 0x88};
1155 uint8_t name_count;
1156 uint16_t test_val;
1157 char* tag = NULL;
1158 char* name = NULL;
1159 char* tag1 = NULL;
1160 char* name1 = NULL;
1161 char* tag2 = NULL;
1162 char* name2 = NULL;
1163 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1164 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1165 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1166
1167 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1168 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1169 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1170 size_t length = 0;
1171
1172 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1173 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0);
1174 EXPECT_EQ(0x2, name_count);
1175
1176 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1177 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1178
1179 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1180 0);
1181 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1182 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1183 EXPECT_EQ(memcmp(expectName0, name,
1184 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1185 0);
1186
1187 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1188 0);
1189 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1190 EXPECT_EQ(
1191 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1192 EXPECT_EQ(0, length % 2);
1193 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1194
1195 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1196 0);
1197 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1198 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1199 0);
1200 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1201 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1202 EXPECT_EQ(memcmp(expectName2, name2,
1203 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1204 0);
1205
1206 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0);
1207 EXPECT_EQ(0x8877, test_val);
1208
1209 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1210}
1211
1212TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1213{
1214 struct pldm_msgbuf _ctxExtract;
1215 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1216
1217 uint8_t src[1] = {};
1218 char* retBuff = NULL;
1219
1220 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1221 ctxExtract->remaining = INTMAX_MIN;
1222 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1223 0);
1224 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1225}
1226
Thu Nguyen062c8762023-04-22 20:45:04 +07001227TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1228{
1229 struct pldm_msgbuf _ctx;
1230 struct pldm_msgbuf* ctx = &_ctx;
1231 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1232 uint8_t buf[8] = {0};
1233 uint16_t testVal;
1234 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1235 size_t remaining;
1236 uint8_t* retBuff = NULL;
1237
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301238 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001239 EXPECT_EQ(
1240 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1241 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001242
1243 struct pldm_msgbuf _ctxExtract;
1244 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1245
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301246 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1247 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001248 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
1249 EXPECT_EQ(
1250 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
1251 PLDM_SUCCESS);
1252
1253 EXPECT_EQ(remaining, sizeof(expectData));
1254 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
1255 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1256 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
1257}
1258
1259TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1260{
1261 struct pldm_msgbuf _ctx;
1262 struct pldm_msgbuf* ctx = &_ctx;
1263 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1264 uint8_t buf[8] = {0};
1265 uint16_t testVal;
Thu Nguyen062c8762023-04-22 20:45:04 +07001266
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301267 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001268 EXPECT_EQ(
1269 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1270 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001271
1272 struct pldm_msgbuf _ctxExtract;
1273 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1274
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301275 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1276 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001277 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001278
1279 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1280 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301281}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001282
1283TEST(msgbuf, pldm_msgbuf_copy_good)
1284{
1285 struct pldm_msgbuf _src;
1286 struct pldm_msgbuf* src = &_src;
1287 uint16_t buf[1] = {htole16(0x5aa5)};
1288
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301289 ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001290 PLDM_SUCCESS);
1291
1292 struct pldm_msgbuf _dst;
1293 struct pldm_msgbuf* dst = &_dst;
1294 uint16_t checkVal = 0;
1295 uint8_t buf1[sizeof(buf)] = {};
1296
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301297 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001298 PLDM_SUCCESS);
1299 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS);
1300
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301301 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001302 PLDM_SUCCESS);
1303 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS);
1304
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001305 EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS);
1306 EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301307
1308 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001309}
1310
1311TEST(msgbuf, pldm_msgbuf_copy_bad)
1312{
1313 struct pldm_msgbuf _src;
1314 struct pldm_msgbuf* src = &_src;
1315 struct pldm_msgbuf _dst;
1316 struct pldm_msgbuf* dst = &_dst;
1317 uint8_t buf[1] = {sizeof(uint8_t)};
1318 uint8_t buf1[1] = {sizeof(uint16_t)};
1319 uint16_t value = 8;
1320
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301321 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS);
1322 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001323 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1324 PLDM_ERROR_INVALID_LENGTH);
1325
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301326 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
1327 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001328 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1329 PLDM_ERROR_INVALID_LENGTH);
1330}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301331
1332TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
1333{
1334 const char msg[] = "this is a message";
1335
1336 struct pldm_msgbuf _src;
1337 struct pldm_msgbuf* src = &_src;
1338 struct pldm_msgbuf _dst;
1339 struct pldm_msgbuf* dst = &_dst;
1340
1341 char buf[sizeof(msg)] = {};
1342
1343 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1344 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1345 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1346 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1347 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1348 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1349}
1350
1351TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1352{
1353 const char msg[] = "this is a message";
1354
1355 struct pldm_msgbuf _src;
1356 struct pldm_msgbuf* src = &_src;
1357 struct pldm_msgbuf _dst;
1358 struct pldm_msgbuf* dst = &_dst;
1359
1360 char buf[sizeof(msg) + 1] = {};
1361
1362 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1363 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1364 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1365 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1366 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1367 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1368}
1369
1370TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1371{
1372 const char msg[] = "this is a message";
1373
1374 struct pldm_msgbuf _src;
1375 struct pldm_msgbuf* src = &_src;
1376 struct pldm_msgbuf _dst;
1377 struct pldm_msgbuf* dst = &_dst;
1378
1379 char buf[sizeof(msg) - 1] = {};
1380
1381 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1382 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1383 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1384 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1385 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1386}
1387
1388TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1389{
1390 const char msg[] = {'a'};
1391
1392 struct pldm_msgbuf _src;
1393 struct pldm_msgbuf* src = &_src;
1394 struct pldm_msgbuf _dst;
1395 struct pldm_msgbuf* dst = &_dst;
1396
1397 char buf[sizeof(msg)] = {};
1398
1399 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1400 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1401 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1402 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1403 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1404}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301405
1406TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
1407{
1408 const char16_t msg[] = u"this is a message";
1409
1410 struct pldm_msgbuf _src;
1411 struct pldm_msgbuf* src = &_src;
1412 struct pldm_msgbuf _dst;
1413 struct pldm_msgbuf* dst = &_dst;
1414
1415 char buf[sizeof(msg)] = {};
1416
1417 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1418 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1419 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1420 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1421 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1422 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1423}
1424
1425TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1426{
1427 const char16_t msg[] = u"this is a message";
1428
1429 struct pldm_msgbuf _src;
1430 struct pldm_msgbuf* src = &_src;
1431 struct pldm_msgbuf _dst;
1432 struct pldm_msgbuf* dst = &_dst;
1433
1434 char buf[sizeof(msg) + 1] = {};
1435
1436 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1437 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1438 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1439 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1440 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1441 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1442}
1443
1444TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1445{
1446 const char16_t msg[] = u"this is a message";
1447
1448 struct pldm_msgbuf _src;
1449 struct pldm_msgbuf* src = &_src;
1450 struct pldm_msgbuf _dst;
1451 struct pldm_msgbuf* dst = &_dst;
1452
1453 char buf[sizeof(msg) - 1] = {};
1454
1455 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1456 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1457 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1458 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1459 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1460}
1461
1462TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1463{
1464 const char16_t msg[] = {u'a'};
1465
1466 struct pldm_msgbuf _src;
1467 struct pldm_msgbuf* src = &_src;
1468 struct pldm_msgbuf _dst;
1469 struct pldm_msgbuf* dst = &_dst;
1470
1471 char buf[sizeof(msg)] = {};
1472
1473 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1474 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1475 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1476 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1477 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1478}