blob: c84acce2be6bb8f90d0e886d29a0264976872f1e [file] [log] [blame]
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301#include <endian.h>
Thu Nguyen15237782024-07-02 09:30:41 +00002#include <libpldm/utils.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10303
4#include <cfloat>
5
6#include <gtest/gtest.h>
7
8/* We're exercising the implementation so disable the asserts for now */
9#ifndef NDEBUG
10#define NDEBUG 1
11#endif
12
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103013#include "msgbuf.h"
14
15TEST(msgbuf, init_bad_ctx)
16{
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093017 EXPECT_NE(pldm_msgbuf_init_cc(NULL, 0, NULL, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103018}
19
20TEST(msgbuf, init_bad_minsize)
21{
22 struct pldm_msgbuf _ctx;
23 struct pldm_msgbuf* ctx = &_ctx;
24 uint8_t buf[1] = {};
25
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093026 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103027 PLDM_SUCCESS);
28}
29
30TEST(msgbuf, init_bad_buf)
31{
32 struct pldm_msgbuf _ctx;
33 struct pldm_msgbuf* ctx = &_ctx;
34
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093035 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, NULL, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103036}
37
38TEST(msgbuf, init_bad_len)
39{
40 struct pldm_msgbuf _ctx;
41 struct pldm_msgbuf* ctx = &_ctx;
42 uint8_t buf[1] = {};
43
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093044 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX),
45 PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103046}
47
48TEST(msgbuf, init_overflow)
49{
50 struct pldm_msgbuf _ctx;
51 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103052 // NOLINTNEXTLINE(performance-no-int-to-ptr)
Andrew Jeffery07febdb2024-05-17 14:17:14 +093053 void* buf = (void*)UINTPTR_MAX;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103054
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093055 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103056}
57
58TEST(msgbuf, init_success)
59{
60 struct pldm_msgbuf _ctx;
61 struct pldm_msgbuf* ctx = &_ctx;
62 uint8_t buf[1] = {};
63
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093064 EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103065 PLDM_SUCCESS);
66}
67
68TEST(msgbuf, destroy_none)
69{
70 struct pldm_msgbuf _ctx;
71 struct pldm_msgbuf* ctx = &_ctx;
72 uint8_t buf[1] = {};
73
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093074 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103075 PLDM_SUCCESS);
76 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
77}
78
79TEST(msgbuf, destroy_exact)
80{
81 struct pldm_msgbuf _ctx;
82 struct pldm_msgbuf* ctx = &_ctx;
83 uint8_t buf[1] = {0xa5};
84 uint8_t val;
85
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093086 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103087 PLDM_SUCCESS);
88 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
89 EXPECT_EQ(val, 0xa5);
90 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
91}
92
93TEST(msgbuf, destroy_over)
94{
95 struct pldm_msgbuf _ctx;
96 struct pldm_msgbuf* ctx = &_ctx;
97 uint8_t buf[1] = {0xa5};
98 uint8_t val;
99
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930100 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030101 PLDM_SUCCESS);
102 ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
103 ASSERT_EQ(val, 0xa5);
104 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
105 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
106}
107
108TEST(msgbuf, destroy_under)
109{
110 struct pldm_msgbuf _ctx;
111 struct pldm_msgbuf* ctx = &_ctx;
112 uint8_t buf[2] = {0x5a, 0xa5};
113 uint8_t val;
114
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930115 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030116 PLDM_SUCCESS);
117 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
118 EXPECT_EQ(val, 0x5a);
119 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
120}
121
122TEST(msgbuf, extract_one_uint8)
123{
124 struct pldm_msgbuf _ctx;
125 struct pldm_msgbuf* ctx = &_ctx;
126 uint8_t buf[1] = {0xa5};
127 uint8_t val;
128
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930129 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030130 PLDM_SUCCESS);
131 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
132 EXPECT_EQ(val, 0xa5);
133 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
134}
135
136TEST(msgbuf, extract_over_uint8)
137{
138 struct pldm_msgbuf _ctx;
139 struct pldm_msgbuf* ctx = &_ctx;
140 uint8_t buf[1] = {};
141 uint8_t val;
142
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930143 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030144 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
145 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
146}
147
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930148TEST(msgbuf, extract_under_uint8)
149{
150 struct pldm_msgbuf _ctx;
151 struct pldm_msgbuf* ctx = &_ctx;
152
153 uint8_t buf[1] = {};
154 uint8_t val;
155
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930156 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930157 ctx->remaining = INTMAX_MIN;
158 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
159 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
160}
161
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030162TEST(msgbuf, extract_one_int8)
163{
164 struct pldm_msgbuf _ctx;
165 struct pldm_msgbuf* ctx = &_ctx;
166 int8_t buf[1] = {-1};
167 int8_t val;
168
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930169 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030170 PLDM_SUCCESS);
171 EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
172 EXPECT_EQ(val, -1);
173 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
174}
175
176TEST(msgbuf, extract_over_int8)
177{
178 struct pldm_msgbuf _ctx;
179 struct pldm_msgbuf* ctx = &_ctx;
180 int8_t buf[1] = {};
181 int8_t val;
182
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930183 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030184 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
185 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
186}
187
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930188TEST(msgbuf, extract_under_int8)
189{
190 struct pldm_msgbuf _ctx;
191 struct pldm_msgbuf* ctx = &_ctx;
192
193 uint8_t buf[1] = {};
194 int8_t val;
195
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930196 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930197 ctx->remaining = INTMAX_MIN;
198 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS);
199 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
200}
201
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030202TEST(msgbuf, extract_one_uint16)
203{
204 struct pldm_msgbuf _ctx;
205 struct pldm_msgbuf* ctx = &_ctx;
206 uint16_t buf[1] = {htole16(0x5aa5)};
207 uint16_t val = {};
208
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930209 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030210 PLDM_SUCCESS);
211 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
212 EXPECT_EQ(val, 0x5aa5);
213 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
214}
215
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930216TEST(msgbuf, extract_under_uint16)
217{
218 struct pldm_msgbuf _ctx;
219 struct pldm_msgbuf* ctx = &_ctx;
220
221 uint16_t buf[1] = {};
222 uint16_t val;
223
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930224 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930225 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
226 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
227 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
228}
229
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030230TEST(msgbuf, extract_over_uint16)
231{
232 struct pldm_msgbuf _ctx;
233 struct pldm_msgbuf* ctx = &_ctx;
234 uint16_t buf[1] = {};
235 uint16_t val;
236
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930237 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030238 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS);
239 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
240}
241
242TEST(msgbuf, extract_one_int16)
243{
244 struct pldm_msgbuf _ctx;
245 struct pldm_msgbuf* ctx = &_ctx;
246 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
247 int16_t val;
248
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930249 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030250 PLDM_SUCCESS);
251 EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
252 EXPECT_EQ(val, INT16_MIN);
253 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
254}
255
256TEST(msgbuf, extract_over_int16)
257{
258 struct pldm_msgbuf _ctx;
259 struct pldm_msgbuf* ctx = &_ctx;
260 int16_t buf[1] = {};
261 int16_t val;
262
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930263 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030264 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
265 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
266}
267
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930268TEST(msgbuf, extract_under_int16)
269{
270 struct pldm_msgbuf _ctx;
271 struct pldm_msgbuf* ctx = &_ctx;
272
273 int16_t buf[1] = {};
274 int16_t val;
275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930276 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930277 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
278 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS);
279 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
280}
281
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030282TEST(msgbuf, extract_one_uint32)
283{
284 struct pldm_msgbuf _ctx;
285 struct pldm_msgbuf* ctx = &_ctx;
286 uint32_t buf[1] = {htole32(0x5a00ffa5)};
287 uint32_t val;
288
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930289 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030290 PLDM_SUCCESS);
291 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
292 EXPECT_EQ(val, 0x5a00ffa5);
293 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
294}
295
296TEST(msgbuf, extract_over_uint32)
297{
298 struct pldm_msgbuf _ctx;
299 struct pldm_msgbuf* ctx = &_ctx;
300 uint32_t buf[1] = {};
301 uint32_t val;
302
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930303 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030304 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
305 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
306}
307
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930308TEST(msgbuf, extract_under_uint32)
309{
310 struct pldm_msgbuf _ctx;
311 struct pldm_msgbuf* ctx = &_ctx;
312
313 uint32_t buf[1] = {};
314 uint32_t val;
315
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930316 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930317 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
318 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS);
319 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
320}
321
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030322TEST(msgbuf, extract_one_int32)
323{
324 struct pldm_msgbuf _ctx;
325 struct pldm_msgbuf* ctx = &_ctx;
326 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
327 int32_t val;
328
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930329 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030330 PLDM_SUCCESS);
331 EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
332 EXPECT_EQ(val, INT32_MIN);
333 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
334}
335
336TEST(msgbuf, extract_over_int32)
337{
338 struct pldm_msgbuf _ctx;
339 struct pldm_msgbuf* ctx = &_ctx;
340 int32_t buf[1] = {};
341 int32_t val;
342
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930343 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030344 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
345 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
346}
347
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930348TEST(msgbuf, extract_under_int32)
349{
350 struct pldm_msgbuf _ctx;
351 struct pldm_msgbuf* ctx = &_ctx;
352
353 int32_t buf[1] = {};
354 int32_t val;
355
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930356 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930357 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
358 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS);
359 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
360}
361
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030362TEST(msgbuf, extract_one_real32)
363{
364 struct pldm_msgbuf _ctx;
365 struct pldm_msgbuf* ctx = &_ctx;
366 uint32_t buf[1] = {};
367 uint32_t xform;
368 real32_t val;
369
370 val = FLT_MAX;
371 memcpy(&xform, &val, sizeof(val));
372 buf[0] = htole32(xform);
373 val = 0;
374
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930375 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030376 PLDM_SUCCESS);
377 EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
378 EXPECT_EQ(val, FLT_MAX);
379 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
380}
381
382TEST(msgbuf, extract_over_real32)
383{
384 struct pldm_msgbuf _ctx;
385 struct pldm_msgbuf* ctx = &_ctx;
386 real32_t buf[1] = {};
387 real32_t val;
388
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930389 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030390 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
391 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
392}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930393
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930394TEST(msgbuf, extract_under_real32)
395{
396 struct pldm_msgbuf _ctx;
397 struct pldm_msgbuf* ctx = &_ctx;
398
399 real32_t buf[1] = {};
400 real32_t val;
401
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930402 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930403 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
404 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS);
405 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
406}
407
Andrew Jeffery369b1212023-04-20 15:44:48 +0930408TEST(msgbuf, extract_array_uint8_buf0_req0)
409{
410 struct pldm_msgbuf _ctx;
411 struct pldm_msgbuf* ctx = &_ctx;
412 uint8_t buf[1] = {};
413 uint8_t arr[1];
414
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930415 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930416 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, 0), PLDM_SUCCESS);
417 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
418}
419
420TEST(msgbuf, extract_array_uint8_buf1_req1)
421{
422 struct pldm_msgbuf _ctx;
423 struct pldm_msgbuf* ctx = &_ctx;
424 uint8_t buf[1] = {};
425 uint8_t arr[1];
426
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930427 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930428 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)),
429 PLDM_SUCCESS);
430 EXPECT_EQ(arr[0], 0);
431 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
432}
433
434TEST(msgbuf, extract_array_uint8_buf1_req2)
435{
436 struct pldm_msgbuf _ctx;
437 struct pldm_msgbuf* ctx = &_ctx;
438 uint8_t buf[1] = {};
439 uint8_t arr[2];
440
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930441 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930442 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)),
443 PLDM_SUCCESS);
444 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
445}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930446
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930447TEST(msgbuf, extract_under_array_uint8)
448{
449 struct pldm_msgbuf _ctx;
450 struct pldm_msgbuf* ctx = &_ctx;
451 uint8_t buf[1] = {};
452 uint8_t arr[1];
453
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930454 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930455 ctx->remaining = INTMAX_MIN;
456 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, arr, 1), PLDM_SUCCESS);
457 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
458}
459
Andrew Jeffery1c571442024-07-08 10:25:48 +0930460TEST(msgbuf, extract_array_char_buf0_req0)
461{
462 struct pldm_msgbuf _ctx;
463 struct pldm_msgbuf* ctx = &_ctx;
464 char buf[1] = {'\0'};
465 char arr[1] = {'1'};
466
467 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
468 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, arr, 0), 0);
469 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
470}
471
472TEST(msgbuf, extract_array_char_buf1_req1)
473{
474 struct pldm_msgbuf _ctx;
475 struct pldm_msgbuf* ctx = &_ctx;
476 char buf[1] = {'\0'};
477 char arr[1] = {'1'};
478
479 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
480 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, arr, sizeof(arr)), 0);
481 EXPECT_EQ(arr[0], '\0');
482 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
483}
484
485TEST(msgbuf, extract_array_char_buf1_req2)
486{
487 struct pldm_msgbuf _ctx;
488 struct pldm_msgbuf* ctx = &_ctx;
489 char buf[1] = {'\0'};
490 char arr[2] = {'1', '2'};
491
492 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
493 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, arr, sizeof(arr)), 0);
494 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
495}
496
497TEST(msgbuf, extract_under_array_char)
498{
499 struct pldm_msgbuf _ctx;
500 struct pldm_msgbuf* ctx = &_ctx;
501 char buf[1] = {'\0'};
502 char arr[1] = {'1'};
503
504 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
505 ctx->remaining = INTMAX_MIN;
506 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, arr, 1), 0);
507 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
508}
509
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930510TEST(msgbuf, consumed_under)
511{
512 struct pldm_msgbuf _ctx;
513 struct pldm_msgbuf* ctx = &_ctx;
514 uint8_t buf[1] = {};
515
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930516 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930517 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
518}
519
520TEST(msgbuf, consumed_exact)
521{
522 struct pldm_msgbuf _ctx;
523 struct pldm_msgbuf* ctx = &_ctx;
524 uint8_t buf[1] = {};
525 uint8_t val;
526
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930527 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930528 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
529 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
530}
531
532TEST(msgbuf, consumed_over)
533{
534 struct pldm_msgbuf _ctx;
535 struct pldm_msgbuf* ctx = &_ctx;
536 uint8_t buf[1] = {};
537 uint8_t val[2];
538
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930539 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930540 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS);
541 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
542 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
543}
Thu Nguyen062c8762023-04-22 20:45:04 +0700544
545TEST(msgbuf, pldm_msgbuf_insert_int32_good)
546{
547 struct pldm_msgbuf _ctx;
548 struct pldm_msgbuf* ctx = &_ctx;
549 int32_t src = -12345;
550 int32_t checkVal = 0;
551 uint8_t buf[sizeof(int32_t)] = {};
552
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930553 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700554 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
555
556 struct pldm_msgbuf _ctxExtract;
557 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
558
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930559 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
560 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700561 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS);
562
563 EXPECT_EQ(src, checkVal);
564 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
565 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
566}
567
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930568TEST(msgbuf, insert_under_int32)
569{
570 struct pldm_msgbuf _ctx;
571 struct pldm_msgbuf* ctx = &_ctx;
572
573 int32_t buf[1] = {};
574 int32_t val = 0;
575
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930576 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930577 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
578 EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS);
579 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
580}
581
Thu Nguyen062c8762023-04-22 20:45:04 +0700582TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
583{
584 struct pldm_msgbuf _ctx;
585 struct pldm_msgbuf* ctx = &_ctx;
586 uint32_t src = 0xf1223344;
587 uint32_t checkVal = 0;
588 uint8_t buf[sizeof(uint32_t)] = {};
589
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930590 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700591 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
592
593 struct pldm_msgbuf _ctxExtract;
594 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
595
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930596 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
597 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700598 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS);
599
600 EXPECT_EQ(src, checkVal);
601 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
602 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
603}
604
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930605TEST(msgbuf, insert_under_uint32)
606{
607 struct pldm_msgbuf _ctx;
608 struct pldm_msgbuf* ctx = &_ctx;
609
610 uint32_t buf[1] = {};
611 uint32_t val = 0;
612
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930613 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930614 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
615 EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS);
616 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
617}
618
Thu Nguyen062c8762023-04-22 20:45:04 +0700619TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
620{
621 struct pldm_msgbuf _ctx;
622 struct pldm_msgbuf* ctx = &_ctx;
623 uint16_t src = 0xf344;
624 uint16_t checkVal = 0;
625 uint8_t buf[sizeof(uint16_t)] = {};
626
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930627 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700628 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
629
630 struct pldm_msgbuf _ctxExtract;
631 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
632
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930633 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
634 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700635 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS);
636
637 EXPECT_EQ(src, checkVal);
638 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
639 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
640}
641
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930642TEST(msgbuf, insert_under_uint16)
643{
644 struct pldm_msgbuf _ctx;
645 struct pldm_msgbuf* ctx = &_ctx;
646
647 uint16_t buf[1] = {};
648 uint16_t val = 0;
649
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930650 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930651 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
652 EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS);
653 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
654}
655
Thu Nguyen062c8762023-04-22 20:45:04 +0700656TEST(msgbuf, pldm_msgbuf_insert_int16_good)
657{
658 struct pldm_msgbuf _ctx;
659 struct pldm_msgbuf* ctx = &_ctx;
660 int16_t src = -12;
661 int16_t checkVal = 0;
662 uint8_t buf[sizeof(int16_t)] = {};
663
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930664 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700665 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
666
667 struct pldm_msgbuf _ctxExtract;
668 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
669
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930670 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
671 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700672 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS);
673
674 EXPECT_EQ(src, checkVal);
675 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
676 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
677}
678
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930679TEST(msgbuf, insert_under_int16)
680{
681 struct pldm_msgbuf _ctx;
682 struct pldm_msgbuf* ctx = &_ctx;
683
684 int16_t buf[1] = {};
685 int16_t val = 0;
686
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930687 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930688 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
689 EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS);
690 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
691}
692
Thu Nguyen062c8762023-04-22 20:45:04 +0700693TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
694{
695 struct pldm_msgbuf _ctx;
696 struct pldm_msgbuf* ctx = &_ctx;
697 uint8_t src = 0xf4;
698 uint8_t checkVal = 0;
699 uint8_t buf[sizeof(uint8_t)] = {};
700
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930701 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700702 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
703
704 struct pldm_msgbuf _ctxExtract;
705 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
706
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930707 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
708 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700709 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS);
710
711 EXPECT_EQ(src, checkVal);
712 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
713 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
714}
715
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930716TEST(msgbuf, insert_under_uint8)
717{
718 struct pldm_msgbuf _ctx;
719 struct pldm_msgbuf* ctx = &_ctx;
720
721 uint8_t buf[1] = {};
722 uint8_t val = 0;
723
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930724 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930725 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
726 EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS);
727 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
728}
729
Thu Nguyen062c8762023-04-22 20:45:04 +0700730TEST(msgbuf, pldm_msgbuf_insert_int8_good)
731{
732 struct pldm_msgbuf _ctx;
733 struct pldm_msgbuf* ctx = &_ctx;
734 int8_t src = -4;
735 int8_t checkVal = 0;
736 uint8_t buf[sizeof(int8_t)] = {};
737
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930738 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700739 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
740
741 struct pldm_msgbuf _ctxExtract;
742 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
743
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930744 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
745 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700746 EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS);
747
748 EXPECT_EQ(src, checkVal);
749 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
750 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
751}
752
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930753TEST(msgbuf, insert_under_int8)
754{
755 struct pldm_msgbuf _ctx;
756 struct pldm_msgbuf* ctx = &_ctx;
757
758 int8_t buf[1] = {};
759 int8_t val = 0;
760
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930761 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930762 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
763 EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS);
764 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
765}
766
Thu Nguyen062c8762023-04-22 20:45:04 +0700767TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
768{
769 struct pldm_msgbuf _ctx;
770 struct pldm_msgbuf* ctx = &_ctx;
771 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
772 uint8_t buf[6] = {};
773 uint8_t retBuff[6] = {};
774
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930775 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700776 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
777 PLDM_SUCCESS);
778
779 struct pldm_msgbuf _ctxExtract;
780 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
781
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930782 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
783 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700784 EXPECT_EQ(
785 pldm_msgbuf_extract_array_uint8(ctxExtract, retBuff, sizeof(retBuff)),
786 PLDM_SUCCESS);
787
788 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
789 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
790 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
791}
792
793TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad)
794{
795 struct pldm_msgbuf _ctx;
796 struct pldm_msgbuf* ctx = &_ctx;
797 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
798 uint8_t buf[6] = {};
799
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930800 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700801 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, NULL, sizeof(src)),
802 PLDM_ERROR_INVALID_DATA);
803 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
804}
805
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930806TEST(msgbuf, insert_under_array_uint8)
807{
808 struct pldm_msgbuf _ctx;
809 struct pldm_msgbuf* ctx = &_ctx;
810
811 uint8_t buf[1] = {};
812 uint8_t val[1] = {0};
813
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930814 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930815 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
816 EXPECT_NE(pldm_msgbuf_insert_array_uint8(ctx, val, sizeof(val)),
817 PLDM_SUCCESS);
818 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
819}
820
Andrew Jeffery1c571442024-07-08 10:25:48 +0930821TEST(msgbuf, pldm_msgbuf_insert_array_char_good)
822{
823 struct pldm_msgbuf _ctx;
824 struct pldm_msgbuf* ctx = &_ctx;
825 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
826 char buf[6] = {};
827 char retBuff[6] = {};
828
829 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
830 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, src, sizeof(src)), 0);
831
832 struct pldm_msgbuf _ctxExtract;
833 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
834
835 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
836 EXPECT_EQ(
837 pldm_msgbuf_extract_array_char(ctxExtract, retBuff, sizeof(retBuff)),
838 0);
839
840 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
841 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
842 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
843}
844
845TEST(msgbuf, pldm_msgbuf_insert_array_char_bad)
846{
847 struct pldm_msgbuf _ctx;
848 struct pldm_msgbuf* ctx = &_ctx;
849 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
850 char buf[6] = {};
851
852 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
853 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, NULL, sizeof(src)), -EINVAL);
854 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
855}
856
857TEST(msgbuf, insert_under_array_char)
858{
859 struct pldm_msgbuf _ctx;
860 struct pldm_msgbuf* ctx = &_ctx;
861 char buf[1] = {};
862 char val[1] = {0};
863
864 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
865 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
866 EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, val, sizeof(val)), 0);
867 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
868}
869
Thu Nguyen062c8762023-04-22 20:45:04 +0700870TEST(msgbuf, pldm_msgbuf_span_required_good)
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};
876 const size_t required = 4;
877 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
878 uint16_t testVal;
879 uint8_t* retBuff = NULL;
880
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930881 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700882 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
883 PLDM_SUCCESS);
884
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);
891 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
892 PLDM_SUCCESS);
893
894 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
895 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
896 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
897}
898
899TEST(msgbuf, pldm_msgbuf_span_required_bad)
900{
901 struct pldm_msgbuf _ctx;
902 struct pldm_msgbuf* ctx = &_ctx;
903 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
904 uint8_t buf[6] = {0};
905 const size_t required = 4;
906 uint16_t testVal;
907 [[maybe_unused]] uint8_t* retBuff = NULL;
908
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930909 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700910 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
911 PLDM_SUCCESS);
912
913 struct pldm_msgbuf _ctxExtract;
914 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
915
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930916 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
917 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700918 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
919 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
920 PLDM_ERROR_INVALID_DATA);
Thu Nguyen062c8762023-04-22 20:45:04 +0700921
922 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
923 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
924}
925
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930926TEST(msgbuf, span_required_under)
927{
928 struct pldm_msgbuf _ctx;
929 struct pldm_msgbuf* ctx = &_ctx;
930
931 uint8_t buf[1] = {};
932 void* cursor = nullptr;
933
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930934 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930935 ctx->remaining = INTMAX_MIN;
936 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS);
937 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
938}
939
Thu Nguyen9c83d682024-07-02 08:43:09 +0000940TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
941{
942 struct pldm_msgbuf _ctxExtract;
943 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
944 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
945 constexpr size_t required = 6;
946 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
947 uint16_t testVal;
948 uint8_t testVal1;
949 char* retBuff = NULL;
950
951 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
952 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
953 EXPECT_EQ(0x2211, testVal);
954 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
955 0);
956 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
957 EXPECT_EQ(0x77, testVal1);
958
959 EXPECT_EQ(required, strlen(retBuff) + 1);
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_good_with_length)
965{
966 struct pldm_msgbuf _ctxExtract;
967 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
968 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
969 constexpr size_t required = 6;
970 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
971 uint16_t testVal;
972 uint8_t testVal1;
973 char* retBuff = NULL;
974 size_t length;
975
976 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
977 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
978 EXPECT_EQ(0x2211, testVal);
979 EXPECT_EQ(
980 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
981 0);
982 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0);
983 EXPECT_EQ(0x77, testVal1);
984
985 EXPECT_EQ(required, strlen(retBuff) + 1);
986 EXPECT_EQ(length, strlen(retBuff) + 1);
987 EXPECT_EQ(required, length);
988 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
989 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
990}
991
992TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
993{
994 struct pldm_msgbuf _ctxExtract;
995 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
996 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
997 uint16_t testVal;
998
999 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1000 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1001 EXPECT_EQ(0x2211, testVal);
1002 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
1003 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1004}
1005
1006TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
1007{
1008 struct pldm_msgbuf _ctxExtract;
1009 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1010 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
1011 uint16_t testVal;
1012 char* retBuff = NULL;
1013
1014 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1015 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1016 EXPECT_EQ(0x2211, testVal);
1017 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1018 -EOVERFLOW);
1019 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1020}
1021
1022TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
1023{
1024 struct pldm_msgbuf _ctxExtract;
1025 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1026
1027 uint8_t src[1] = {};
1028 char* retBuff = NULL;
1029
1030 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1031 ctxExtract->remaining = INTMAX_MIN;
1032 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1033 0);
1034 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1035}
1036
Thu Nguyen15237782024-07-02 09:30:41 +00001037static size_t str16len(char16_t* startptr)
1038{
1039 char16_t* endptr = startptr;
1040 while (*endptr)
1041 {
1042 endptr++;
1043 }
1044 return endptr - startptr;
1045}
1046
1047TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
1048{
1049 struct pldm_msgbuf _ctxExtract;
1050 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1051 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
1052 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1053 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1054 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1055 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1056 uint16_t testVal;
1057 uint16_t testVal1;
1058 void* retBuff = NULL;
1059
1060 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1061 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1062 EXPECT_EQ(0x2211, testVal);
1063
1064 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1065 0);
1066 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1067 EXPECT_EQ(0x1234, testVal1);
1068
1069 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1070 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1071 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1072 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1073}
1074
1075TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1076{
1077 struct pldm_msgbuf _ctxExtract;
1078 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1079 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1080 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1081 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1082 constexpr size_t required = 6;
1083 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1084 0x6c44, 0x6f55, 0x0000};
1085 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1086 uint16_t testVal;
1087 uint16_t testVal1;
1088 char* retBuff = NULL;
1089 char* retBuff1 = NULL;
1090 size_t length = 0;
1091
1092 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1093 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1094 EXPECT_EQ(0x2211, testVal);
1095
1096 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1097 0);
1098
1099 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1100 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1101 EXPECT_EQ(memcmp(expectData, retBuff,
1102 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1103 0);
1104
1105 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1106 EXPECT_EQ(0x1234, testVal1);
1107
1108 EXPECT_EQ(
1109 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1110 0);
1111
1112 EXPECT_EQ(0, length % 2);
1113 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1114
1115 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0);
1116 EXPECT_EQ(0x1234, testVal1);
1117
1118 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1119}
1120
1121TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1122{
1123 struct pldm_msgbuf _ctxExtract;
1124 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1125 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1126 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1127 uint16_t testVal;
1128
1129 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1130 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1131 EXPECT_EQ(0x2211, testVal);
1132 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1133 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1134}
1135
1136TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1137{
1138 struct pldm_msgbuf _ctxExtract;
1139 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1140 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1141 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1142 uint16_t testVal;
1143 char16_t* retBuff = NULL;
1144
1145 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1146 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1147 EXPECT_EQ(0x2211, testVal);
1148 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1149 -EOVERFLOW);
1150 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1151}
1152
1153TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1154{
1155 struct pldm_msgbuf _ctxExtract;
1156 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1157 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1158 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1159 uint16_t testVal;
1160 char16_t* retBuff = NULL;
1161
1162 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1163 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0);
1164 EXPECT_EQ(0x2211, testVal);
1165 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1166 -EOVERFLOW);
1167 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1168}
1169
1170TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1171{
1172 struct pldm_msgbuf _ctxExtract;
1173 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1174 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1175 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1176 0x58, 0x00, 0x00, // Entity Name "S0S"
1177 0x66, 0x6e, 0x00, // Language Tag "en"
1178 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1179 0x67, 0x6e, 0x00, // Language Tag "en"
1180 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1181 0x00, // Entity Name "RR3"
1182 0x77, 0x88};
1183 uint8_t name_count;
1184 uint16_t test_val;
1185 char* tag = NULL;
1186 char* name = NULL;
1187 char* tag1 = NULL;
1188 char* name1 = NULL;
1189 char* tag2 = NULL;
1190 char* name2 = NULL;
1191 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1192 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1193 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1194
1195 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1196 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1197 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1198 size_t length = 0;
1199
1200 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
1201 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0);
1202 EXPECT_EQ(0x2, name_count);
1203
1204 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1205 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1206
1207 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1208 0);
1209 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1210 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1211 EXPECT_EQ(memcmp(expectName0, name,
1212 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1213 0);
1214
1215 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1216 0);
1217 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1218 EXPECT_EQ(
1219 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1220 EXPECT_EQ(0, length % 2);
1221 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1222
1223 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1224 0);
1225 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1226 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1227 0);
1228 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1229 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1230 EXPECT_EQ(memcmp(expectName2, name2,
1231 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1232 0);
1233
1234 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0);
1235 EXPECT_EQ(0x8877, test_val);
1236
1237 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1238}
1239
1240TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1241{
1242 struct pldm_msgbuf _ctxExtract;
1243 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1244
1245 uint8_t src[1] = {};
1246 char* retBuff = NULL;
1247
1248 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1249 ctxExtract->remaining = INTMAX_MIN;
1250 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1251 0);
1252 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1253}
1254
Thu Nguyen062c8762023-04-22 20:45:04 +07001255TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1256{
1257 struct pldm_msgbuf _ctx;
1258 struct pldm_msgbuf* ctx = &_ctx;
1259 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1260 uint8_t buf[8] = {0};
1261 uint16_t testVal;
1262 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1263 size_t remaining;
1264 uint8_t* retBuff = NULL;
1265
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301266 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001267 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
1268 PLDM_SUCCESS);
1269
1270 struct pldm_msgbuf _ctxExtract;
1271 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1272
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301273 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1274 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001275 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
1276 EXPECT_EQ(
1277 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
1278 PLDM_SUCCESS);
1279
1280 EXPECT_EQ(remaining, sizeof(expectData));
1281 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
1282 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1283 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
1284}
1285
1286TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1287{
1288 struct pldm_msgbuf _ctx;
1289 struct pldm_msgbuf* ctx = &_ctx;
1290 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1291 uint8_t buf[8] = {0};
1292 uint16_t testVal;
1293 size_t remaining;
1294 uint8_t* retBuff = NULL;
1295
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301296 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001297 EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
1298 PLDM_SUCCESS);
1299
1300 struct pldm_msgbuf _ctxExtract;
1301 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1302
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301303 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1304 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001305 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001306 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining),
1307 PLDM_ERROR_INVALID_DATA);
1308 EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL),
1309 PLDM_ERROR_INVALID_DATA);
1310
1311 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1312 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301313}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001314
1315TEST(msgbuf, pldm_msgbuf_copy_good)
1316{
1317 struct pldm_msgbuf _src;
1318 struct pldm_msgbuf* src = &_src;
1319 uint16_t buf[1] = {htole16(0x5aa5)};
1320
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301321 ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001322 PLDM_SUCCESS);
1323
1324 struct pldm_msgbuf _dst;
1325 struct pldm_msgbuf* dst = &_dst;
1326 uint16_t checkVal = 0;
1327 uint8_t buf1[sizeof(buf)] = {};
1328
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301329 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001330 PLDM_SUCCESS);
1331 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS);
1332
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301333 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001334 PLDM_SUCCESS);
1335 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS);
1336
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001337 EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS);
1338 EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301339
1340 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001341}
1342
1343TEST(msgbuf, pldm_msgbuf_copy_bad)
1344{
1345 struct pldm_msgbuf _src;
1346 struct pldm_msgbuf* src = &_src;
1347 struct pldm_msgbuf _dst;
1348 struct pldm_msgbuf* dst = &_dst;
1349 uint8_t buf[1] = {sizeof(uint8_t)};
1350 uint8_t buf1[1] = {sizeof(uint16_t)};
1351 uint16_t value = 8;
1352
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301353 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS);
1354 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001355 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1356 PLDM_ERROR_INVALID_LENGTH);
1357
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301358 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
1359 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001360 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1361 PLDM_ERROR_INVALID_LENGTH);
1362}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301363
1364TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
1365{
1366 const char msg[] = "this is a message";
1367
1368 struct pldm_msgbuf _src;
1369 struct pldm_msgbuf* src = &_src;
1370 struct pldm_msgbuf _dst;
1371 struct pldm_msgbuf* dst = &_dst;
1372
1373 char buf[sizeof(msg)] = {};
1374
1375 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1376 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1377 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1378 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1379 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1380 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1381}
1382
1383TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1384{
1385 const char msg[] = "this is a message";
1386
1387 struct pldm_msgbuf _src;
1388 struct pldm_msgbuf* src = &_src;
1389 struct pldm_msgbuf _dst;
1390 struct pldm_msgbuf* dst = &_dst;
1391
1392 char buf[sizeof(msg) + 1] = {};
1393
1394 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1395 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1396 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1397 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1398 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1399 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1400}
1401
1402TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1403{
1404 const char msg[] = "this is a message";
1405
1406 struct pldm_msgbuf _src;
1407 struct pldm_msgbuf* src = &_src;
1408 struct pldm_msgbuf _dst;
1409 struct pldm_msgbuf* dst = &_dst;
1410
1411 char buf[sizeof(msg) - 1] = {};
1412
1413 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1414 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1415 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1416 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1417 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1418}
1419
1420TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1421{
1422 const char msg[] = {'a'};
1423
1424 struct pldm_msgbuf _src;
1425 struct pldm_msgbuf* src = &_src;
1426 struct pldm_msgbuf _dst;
1427 struct pldm_msgbuf* dst = &_dst;
1428
1429 char buf[sizeof(msg)] = {};
1430
1431 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1432 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1433 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1434 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1435 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1436}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301437
1438TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
1439{
1440 const char16_t msg[] = u"this is a message";
1441
1442 struct pldm_msgbuf _src;
1443 struct pldm_msgbuf* src = &_src;
1444 struct pldm_msgbuf _dst;
1445 struct pldm_msgbuf* dst = &_dst;
1446
1447 char buf[sizeof(msg)] = {};
1448
1449 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1450 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1451 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1452 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1453 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1454 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1455}
1456
1457TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1458{
1459 const char16_t msg[] = u"this is a message";
1460
1461 struct pldm_msgbuf _src;
1462 struct pldm_msgbuf* src = &_src;
1463 struct pldm_msgbuf _dst;
1464 struct pldm_msgbuf* dst = &_dst;
1465
1466 char buf[sizeof(msg) + 1] = {};
1467
1468 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1469 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1470 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1471 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1472 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1473 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1474}
1475
1476TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1477{
1478 const char16_t msg[] = u"this is a message";
1479
1480 struct pldm_msgbuf _src;
1481 struct pldm_msgbuf* src = &_src;
1482 struct pldm_msgbuf _dst;
1483 struct pldm_msgbuf* dst = &_dst;
1484
1485 char buf[sizeof(msg) - 1] = {};
1486
1487 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1488 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1489 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1490 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1491 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1492}
1493
1494TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1495{
1496 const char16_t msg[] = {u'a'};
1497
1498 struct pldm_msgbuf _src;
1499 struct pldm_msgbuf* src = &_src;
1500 struct pldm_msgbuf _dst;
1501 struct pldm_msgbuf* dst = &_dst;
1502
1503 char buf[sizeof(msg)] = {};
1504
1505 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1506 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1507 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1508 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1509 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1510}