blob: fc3c0c872edf4f9f94bea41b778c86f00b50282c [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 Jeffery830c1eb2024-10-04 10:48:10 +093021 EXPECT_NE(pldm_msgbuf_init_errno(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
22 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103023}
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 Jeffery830c1eb2024-10-04 10:48:10 +093031 EXPECT_NE(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, SIZE_MAX), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103032}
33
34TEST(msgbuf, init_overflow)
35{
36 struct pldm_msgbuf _ctx;
37 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103038 // NOLINTNEXTLINE(performance-no-int-to-ptr)
Andrew Jeffery07febdb2024-05-17 14:17:14 +093039 void* buf = (void*)UINTPTR_MAX;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103040
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093041 EXPECT_NE(pldm_msgbuf_init_errno(ctx, 0, buf, 2), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103042}
43
44TEST(msgbuf, init_success)
45{
46 struct pldm_msgbuf _ctx;
47 struct pldm_msgbuf* ctx = &_ctx;
48 uint8_t buf[1] = {};
49
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093050 EXPECT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103051}
52
53TEST(msgbuf, destroy_none)
54{
55 struct pldm_msgbuf _ctx;
56 struct pldm_msgbuf* ctx = &_ctx;
57 uint8_t buf[1] = {};
58
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093059 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
60 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103061}
62
63TEST(msgbuf, destroy_exact)
64{
65 struct pldm_msgbuf _ctx;
66 struct pldm_msgbuf* ctx = &_ctx;
67 uint8_t buf[1] = {0xa5};
68 uint8_t val;
69
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093070 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
71 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103072 EXPECT_EQ(val, 0xa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093073 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103074}
75
76TEST(msgbuf, destroy_over)
77{
78 struct pldm_msgbuf _ctx;
79 struct pldm_msgbuf* ctx = &_ctx;
80 uint8_t buf[1] = {0xa5};
81 uint8_t val;
82
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093083 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
84 ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103085 ASSERT_EQ(val, 0xa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093086 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0);
87 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103088}
89
90TEST(msgbuf, destroy_under)
91{
92 struct pldm_msgbuf _ctx;
93 struct pldm_msgbuf* ctx = &_ctx;
94 uint8_t buf[2] = {0x5a, 0xa5};
95 uint8_t val;
96
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093097 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
98 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103099 EXPECT_EQ(val, 0x5a);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930100 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030101}
102
103TEST(msgbuf, extract_one_uint8)
104{
105 struct pldm_msgbuf _ctx;
106 struct pldm_msgbuf* ctx = &_ctx;
107 uint8_t buf[1] = {0xa5};
108 uint8_t val;
109
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930110 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
111 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030112 EXPECT_EQ(val, 0xa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930113 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030114}
115
116TEST(msgbuf, extract_over_uint8)
117{
118 struct pldm_msgbuf _ctx;
119 struct pldm_msgbuf* ctx = &_ctx;
120 uint8_t buf[1] = {};
121 uint8_t val;
122
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930123 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
124 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0);
125 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030126}
127
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930128TEST(msgbuf, extract_under_uint8)
129{
130 struct pldm_msgbuf _ctx;
131 struct pldm_msgbuf* ctx = &_ctx;
132
133 uint8_t buf[1] = {};
134 uint8_t val;
135
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930136 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930137 ctx->remaining = INTMAX_MIN;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930138 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), 0);
139 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930140}
141
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030142TEST(msgbuf, extract_one_int8)
143{
144 struct pldm_msgbuf _ctx;
145 struct pldm_msgbuf* ctx = &_ctx;
146 int8_t buf[1] = {-1};
147 int8_t val;
148
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930149 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
150 EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030151 EXPECT_EQ(val, -1);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930152 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030153}
154
155TEST(msgbuf, extract_over_int8)
156{
157 struct pldm_msgbuf _ctx;
158 struct pldm_msgbuf* ctx = &_ctx;
159 int8_t buf[1] = {};
160 int8_t val;
161
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930162 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
163 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), 0);
164 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030165}
166
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930167TEST(msgbuf, extract_under_int8)
168{
169 struct pldm_msgbuf _ctx;
170 struct pldm_msgbuf* ctx = &_ctx;
171
172 uint8_t buf[1] = {};
173 int8_t val;
174
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930175 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930176 ctx->remaining = INTMAX_MIN;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930177 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), 0);
178 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930179}
180
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030181TEST(msgbuf, extract_one_uint16)
182{
183 struct pldm_msgbuf _ctx;
184 struct pldm_msgbuf* ctx = &_ctx;
185 uint16_t buf[1] = {htole16(0x5aa5)};
186 uint16_t val = {};
187
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930188 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
189 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030190 EXPECT_EQ(val, 0x5aa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930191 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030192}
193
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930194TEST(msgbuf, extract_under_uint16)
195{
196 struct pldm_msgbuf _ctx;
197 struct pldm_msgbuf* ctx = &_ctx;
198
199 uint16_t buf[1] = {};
200 uint16_t val;
201
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930202 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930203 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930204 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), 0);
205 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930206}
207
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030208TEST(msgbuf, extract_over_uint16)
209{
210 struct pldm_msgbuf _ctx;
211 struct pldm_msgbuf* ctx = &_ctx;
212 uint16_t buf[1] = {};
213 uint16_t val;
214
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930215 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
216 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), 0);
217 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030218}
219
220TEST(msgbuf, extract_one_int16)
221{
222 struct pldm_msgbuf _ctx;
223 struct pldm_msgbuf* ctx = &_ctx;
224 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
225 int16_t val;
226
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930227 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
228 EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030229 EXPECT_EQ(val, INT16_MIN);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930230 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030231}
232
233TEST(msgbuf, extract_over_int16)
234{
235 struct pldm_msgbuf _ctx;
236 struct pldm_msgbuf* ctx = &_ctx;
237 int16_t buf[1] = {};
238 int16_t val;
239
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930240 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
241 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), 0);
242 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030243}
244
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930245TEST(msgbuf, extract_under_int16)
246{
247 struct pldm_msgbuf _ctx;
248 struct pldm_msgbuf* ctx = &_ctx;
249
250 int16_t buf[1] = {};
251 int16_t val;
252
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930253 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930254 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930255 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), 0);
256 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930257}
258
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030259TEST(msgbuf, extract_one_uint32)
260{
261 struct pldm_msgbuf _ctx;
262 struct pldm_msgbuf* ctx = &_ctx;
263 uint32_t buf[1] = {htole32(0x5a00ffa5)};
264 uint32_t val;
265
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930266 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
267 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030268 EXPECT_EQ(val, 0x5a00ffa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930269 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030270}
271
272TEST(msgbuf, extract_over_uint32)
273{
274 struct pldm_msgbuf _ctx;
275 struct pldm_msgbuf* ctx = &_ctx;
276 uint32_t buf[1] = {};
277 uint32_t val;
278
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930279 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
280 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), 0);
281 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030282}
283
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930284TEST(msgbuf, extract_under_uint32)
285{
286 struct pldm_msgbuf _ctx;
287 struct pldm_msgbuf* ctx = &_ctx;
288
289 uint32_t buf[1] = {};
290 uint32_t val;
291
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930292 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930293 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930294 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), 0);
295 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930296}
297
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030298TEST(msgbuf, extract_one_int32)
299{
300 struct pldm_msgbuf _ctx;
301 struct pldm_msgbuf* ctx = &_ctx;
302 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
303 int32_t val;
304
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930305 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
306 EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030307 EXPECT_EQ(val, INT32_MIN);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930308 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030309}
310
311TEST(msgbuf, extract_over_int32)
312{
313 struct pldm_msgbuf _ctx;
314 struct pldm_msgbuf* ctx = &_ctx;
315 int32_t buf[1] = {};
316 int32_t val;
317
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930318 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
319 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), 0);
320 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030321}
322
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930323TEST(msgbuf, extract_under_int32)
324{
325 struct pldm_msgbuf _ctx;
326 struct pldm_msgbuf* ctx = &_ctx;
327
328 int32_t buf[1] = {};
329 int32_t val;
330
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930331 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930332 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930333 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), 0);
334 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930335}
336
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030337TEST(msgbuf, extract_one_real32)
338{
339 struct pldm_msgbuf _ctx;
340 struct pldm_msgbuf* ctx = &_ctx;
341 uint32_t buf[1] = {};
342 uint32_t xform;
343 real32_t val;
344
345 val = FLT_MAX;
346 memcpy(&xform, &val, sizeof(val));
347 buf[0] = htole32(xform);
348 val = 0;
349
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930350 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)), 0);
351 EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, val), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030352 EXPECT_EQ(val, FLT_MAX);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930353 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030354}
355
356TEST(msgbuf, extract_over_real32)
357{
358 struct pldm_msgbuf _ctx;
359 struct pldm_msgbuf* ctx = &_ctx;
360 real32_t buf[1] = {};
361 real32_t val;
362
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930363 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
364 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), 0);
365 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030366}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930367
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930368TEST(msgbuf, extract_under_real32)
369{
370 struct pldm_msgbuf _ctx;
371 struct pldm_msgbuf* ctx = &_ctx;
372
373 real32_t buf[1] = {};
374 real32_t val;
375
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930376 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930377 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930378 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), 0);
379 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930380}
381
Andrew Jeffery369b1212023-04-20 15:44:48 +0930382TEST(msgbuf, extract_array_uint8_buf0_req0)
383{
384 struct pldm_msgbuf _ctx;
385 struct pldm_msgbuf* ctx = &_ctx;
386 uint8_t buf[1] = {};
387 uint8_t arr[1];
388
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930389 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
390 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), 0);
391 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930392}
393
394TEST(msgbuf, extract_array_uint8_buf1_req1)
395{
396 struct pldm_msgbuf _ctx;
397 struct pldm_msgbuf* ctx = &_ctx;
398 uint8_t buf[1] = {};
399 uint8_t arr[1];
400
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930401 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000402 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930403 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930404 EXPECT_EQ(arr[0], 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930405 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930406}
407
408TEST(msgbuf, extract_array_uint8_buf1_req2)
409{
410 struct pldm_msgbuf _ctx;
411 struct pldm_msgbuf* ctx = &_ctx;
412 uint8_t buf[1] = {};
413 uint8_t arr[2];
414
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930415 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000416 EXPECT_NE(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930417 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), 0);
418 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930419}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930420
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930421TEST(msgbuf, extract_under_array_uint8)
422{
423 struct pldm_msgbuf _ctx;
424 struct pldm_msgbuf* ctx = &_ctx;
425 uint8_t buf[1] = {};
426 uint8_t arr[1];
427
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930428 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930429 ctx->remaining = INTMAX_MIN;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930430 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), 0);
431 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930432}
433
Andrew Jeffery1c571442024-07-08 10:25:48 +0930434TEST(msgbuf, extract_array_char_buf0_req0)
435{
436 struct pldm_msgbuf _ctx;
437 struct pldm_msgbuf* ctx = &_ctx;
438 char buf[1] = {'\0'};
439 char arr[1] = {'1'};
440
441 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000442 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930443 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
444}
445
446TEST(msgbuf, extract_array_char_buf1_req1)
447{
448 struct pldm_msgbuf _ctx;
449 struct pldm_msgbuf* ctx = &_ctx;
450 char buf[1] = {'\0'};
451 char arr[1] = {'1'};
452
453 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000454 EXPECT_EQ(
455 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930456 EXPECT_EQ(arr[0], '\0');
457 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
458}
459
460TEST(msgbuf, extract_array_char_buf1_req2)
461{
462 struct pldm_msgbuf _ctx;
463 struct pldm_msgbuf* ctx = &_ctx;
464 char buf[1] = {'\0'};
465 char arr[2] = {'1', '2'};
466
467 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000468 EXPECT_NE(
469 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930470 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
471}
472
473TEST(msgbuf, extract_under_array_char)
474{
475 struct pldm_msgbuf _ctx;
476 struct pldm_msgbuf* ctx = &_ctx;
477 char buf[1] = {'\0'};
478 char arr[1] = {'1'};
479
480 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
481 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000482 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930483 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
484}
485
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930486TEST(msgbuf, consumed_under)
487{
488 struct pldm_msgbuf _ctx;
489 struct pldm_msgbuf* ctx = &_ctx;
490 uint8_t buf[1] = {};
491
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930492 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
493 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), -EBADMSG);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930494}
495
496TEST(msgbuf, consumed_exact)
497{
498 struct pldm_msgbuf _ctx;
499 struct pldm_msgbuf* ctx = &_ctx;
500 uint8_t buf[1] = {};
501 uint8_t val;
502
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930503 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
504 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), 0);
505 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), 0);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930506}
507
508TEST(msgbuf, consumed_over)
509{
510 struct pldm_msgbuf _ctx;
511 struct pldm_msgbuf* ctx = &_ctx;
512 uint8_t buf[1] = {};
Andrew Jefferye5f12532024-10-01 12:18:49 +0930513 uint8_t valid;
514 uint8_t invalid;
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930515
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930516 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
517 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, valid), 0);
518 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, invalid), 0);
519 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), -EBADMSG);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930520}
Thu Nguyen062c8762023-04-22 20:45:04 +0700521
522TEST(msgbuf, pldm_msgbuf_insert_int32_good)
523{
524 struct pldm_msgbuf _ctx;
525 struct pldm_msgbuf* ctx = &_ctx;
526 int32_t src = -12345;
527 int32_t checkVal = 0;
528 uint8_t buf[sizeof(int32_t)] = {};
529
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930530 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
531 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700532
533 struct pldm_msgbuf _ctxExtract;
534 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
535
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930536 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
537 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700538
539 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930540 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
541 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700542}
543
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930544TEST(msgbuf, insert_under_int32)
545{
546 struct pldm_msgbuf _ctx;
547 struct pldm_msgbuf* ctx = &_ctx;
548
549 int32_t buf[1] = {};
550 int32_t val = 0;
551
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930552 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930553 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930554 EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), 0);
555 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930556}
557
Thu Nguyen062c8762023-04-22 20:45:04 +0700558TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
559{
560 struct pldm_msgbuf _ctx;
561 struct pldm_msgbuf* ctx = &_ctx;
562 uint32_t src = 0xf1223344;
563 uint32_t checkVal = 0;
564 uint8_t buf[sizeof(uint32_t)] = {};
565
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930566 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
567 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700568
569 struct pldm_msgbuf _ctxExtract;
570 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
571
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930572 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
573 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700574
575 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930576 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
577 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700578}
579
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930580TEST(msgbuf, insert_under_uint32)
581{
582 struct pldm_msgbuf _ctx;
583 struct pldm_msgbuf* ctx = &_ctx;
584
585 uint32_t buf[1] = {};
586 uint32_t val = 0;
587
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930588 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930589 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930590 EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), 0);
591 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930592}
593
Thu Nguyen062c8762023-04-22 20:45:04 +0700594TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
595{
596 struct pldm_msgbuf _ctx;
597 struct pldm_msgbuf* ctx = &_ctx;
598 uint16_t src = 0xf344;
599 uint16_t checkVal = 0;
600 uint8_t buf[sizeof(uint16_t)] = {};
601
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930602 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)), 0);
603 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700604
605 struct pldm_msgbuf _ctxExtract;
606 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
607
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930608 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
609 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700610
611 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930612 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
613 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700614}
615
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930616TEST(msgbuf, insert_under_uint16)
617{
618 struct pldm_msgbuf _ctx;
619 struct pldm_msgbuf* ctx = &_ctx;
620
621 uint16_t buf[1] = {};
622 uint16_t val = 0;
623
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930624 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930625 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930626 EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), 0);
627 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930628}
629
Thu Nguyen062c8762023-04-22 20:45:04 +0700630TEST(msgbuf, pldm_msgbuf_insert_int16_good)
631{
632 struct pldm_msgbuf _ctx;
633 struct pldm_msgbuf* ctx = &_ctx;
634 int16_t src = -12;
635 int16_t checkVal = 0;
636 uint8_t buf[sizeof(int16_t)] = {};
637
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930638 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)), 0);
639 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700640
641 struct pldm_msgbuf _ctxExtract;
642 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
643
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930644 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
645 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700646
647 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930648 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
649 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700650}
651
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930652TEST(msgbuf, insert_under_int16)
653{
654 struct pldm_msgbuf _ctx;
655 struct pldm_msgbuf* ctx = &_ctx;
656
657 int16_t buf[1] = {};
658 int16_t val = 0;
659
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930660 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930661 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930662 EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), 0);
663 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930664}
665
Thu Nguyen062c8762023-04-22 20:45:04 +0700666TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
667{
668 struct pldm_msgbuf _ctx;
669 struct pldm_msgbuf* ctx = &_ctx;
670 uint8_t src = 0xf4;
671 uint8_t checkVal = 0;
672 uint8_t buf[sizeof(uint8_t)] = {};
673
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930674 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
675 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700676
677 struct pldm_msgbuf _ctxExtract;
678 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
679
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930680 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
681 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700682
683 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930684 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
685 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700686}
687
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930688TEST(msgbuf, insert_under_uint8)
689{
690 struct pldm_msgbuf _ctx;
691 struct pldm_msgbuf* ctx = &_ctx;
692
693 uint8_t buf[1] = {};
694 uint8_t val = 0;
695
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930696 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930697 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930698 EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), 0);
699 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930700}
701
Thu Nguyen062c8762023-04-22 20:45:04 +0700702TEST(msgbuf, pldm_msgbuf_insert_int8_good)
703{
704 struct pldm_msgbuf _ctx;
705 struct pldm_msgbuf* ctx = &_ctx;
706 int8_t src = -4;
707 int8_t checkVal = 0;
708 uint8_t buf[sizeof(int8_t)] = {};
709
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930710 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
711 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700712
713 struct pldm_msgbuf _ctxExtract;
714 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
715
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930716 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
717 EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, checkVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700718
719 EXPECT_EQ(src, checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930720 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
721 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700722}
723
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930724TEST(msgbuf, insert_under_int8)
725{
726 struct pldm_msgbuf _ctx;
727 struct pldm_msgbuf* ctx = &_ctx;
728
729 int8_t buf[1] = {};
730 int8_t val = 0;
731
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930732 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930733 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930734 EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), 0);
735 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930736}
737
Thu Nguyen062c8762023-04-22 20:45:04 +0700738TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
739{
740 struct pldm_msgbuf _ctx;
741 struct pldm_msgbuf* ctx = &_ctx;
742 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
743 uint8_t buf[6] = {};
744 uint8_t retBuff[6] = {};
745
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930746 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000747 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930748 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700749
750 struct pldm_msgbuf _ctxExtract;
751 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
752
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930753 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000754 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff),
755 retBuff, sizeof(retBuff)),
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930756 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700757
758 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930759 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
760 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700761}
762
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930763TEST(msgbuf, insert_under_array_uint8)
764{
765 struct pldm_msgbuf _ctx;
766 struct pldm_msgbuf* ctx = &_ctx;
767
768 uint8_t buf[1] = {};
769 uint8_t val[1] = {0};
770
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930771 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930772 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000773 EXPECT_NE(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930774 pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)), 0);
775 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930776}
777
Andrew Jeffery1c571442024-07-08 10:25:48 +0930778TEST(msgbuf, pldm_msgbuf_insert_array_char_good)
779{
780 struct pldm_msgbuf _ctx;
781 struct pldm_msgbuf* ctx = &_ctx;
782 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
783 char buf[6] = {};
784 char retBuff[6] = {};
785
786 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000787 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)),
788 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930789
790 struct pldm_msgbuf _ctxExtract;
791 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
792
793 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000794 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff),
795 retBuff, sizeof(retBuff)),
796 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930797
798 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
799 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
800 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
801}
802
Andrew Jeffery1c571442024-07-08 10:25:48 +0930803TEST(msgbuf, insert_under_array_char)
804{
805 struct pldm_msgbuf _ctx;
806 struct pldm_msgbuf* ctx = &_ctx;
807 char buf[1] = {};
808 char val[1] = {0};
809
810 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
811 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000812 EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)),
813 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930814 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
815}
816
Thu Nguyen062c8762023-04-22 20:45:04 +0700817TEST(msgbuf, pldm_msgbuf_span_required_good)
818{
819 struct pldm_msgbuf _ctx;
820 struct pldm_msgbuf* ctx = &_ctx;
821 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
822 uint8_t buf[6] = {0};
823 const size_t required = 4;
824 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
825 uint16_t testVal;
826 uint8_t* retBuff = NULL;
827
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930828 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000829 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930830 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700831
832 struct pldm_msgbuf _ctxExtract;
833 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
834
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930835 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
836 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700837 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930838 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700839
840 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930841 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
842 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700843}
844
845TEST(msgbuf, pldm_msgbuf_span_required_bad)
846{
847 struct pldm_msgbuf _ctx;
848 struct pldm_msgbuf* ctx = &_ctx;
849 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
850 uint8_t buf[6] = {0};
Varsha Kaverappa79393822024-08-07 00:40:13 -0500851 const size_t required = 4;
Thu Nguyen062c8762023-04-22 20:45:04 +0700852 uint16_t testVal;
853 [[maybe_unused]] uint8_t* retBuff = NULL;
854
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930855 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000856 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930857 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700858
859 struct pldm_msgbuf _ctxExtract;
860 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
861
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930862 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
863 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
864 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700865
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930866 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
867 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700868}
869
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930870TEST(msgbuf, span_required_under)
871{
872 struct pldm_msgbuf _ctx;
873 struct pldm_msgbuf* ctx = &_ctx;
874
875 uint8_t buf[1] = {};
876 void* cursor = nullptr;
877
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930878 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930879 ctx->remaining = INTMAX_MIN;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930880 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), 0);
881 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930882}
883
Thu Nguyen9c83d682024-07-02 08:43:09 +0000884TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
885{
886 struct pldm_msgbuf _ctxExtract;
887 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
888 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
889 constexpr size_t required = 6;
890 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
891 uint16_t testVal;
892 uint8_t testVal1;
893 char* retBuff = NULL;
894
895 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930896 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000897 EXPECT_EQ(0x2211, testVal);
898 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
899 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930900 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000901 EXPECT_EQ(0x77, testVal1);
902
903 EXPECT_EQ(required, strlen(retBuff) + 1);
904 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
905 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
906}
907
908TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length)
909{
910 struct pldm_msgbuf _ctxExtract;
911 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
912 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
913 constexpr size_t required = 6;
914 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
915 uint16_t testVal;
916 uint8_t testVal1;
917 char* retBuff = NULL;
918 size_t length;
919
920 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930921 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000922 EXPECT_EQ(0x2211, testVal);
923 EXPECT_EQ(
924 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
925 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930926 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000927 EXPECT_EQ(0x77, testVal1);
928
929 EXPECT_EQ(required, strlen(retBuff) + 1);
930 EXPECT_EQ(length, strlen(retBuff) + 1);
931 EXPECT_EQ(required, length);
932 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
933 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
934}
935
936TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
937{
938 struct pldm_msgbuf _ctxExtract;
939 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
940 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
941 uint16_t testVal;
942
943 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930944 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000945 EXPECT_EQ(0x2211, testVal);
946 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
947 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
948}
949
950TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
951{
952 struct pldm_msgbuf _ctxExtract;
953 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
954 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
955 uint16_t testVal;
956 char* retBuff = NULL;
957
958 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930959 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000960 EXPECT_EQ(0x2211, testVal);
961 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
962 -EOVERFLOW);
963 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
964}
965
966TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
967{
968 struct pldm_msgbuf _ctxExtract;
969 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
970
971 uint8_t src[1] = {};
972 char* retBuff = NULL;
973
974 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
975 ctxExtract->remaining = INTMAX_MIN;
976 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
977 0);
978 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
979}
980
Thu Nguyen15237782024-07-02 09:30:41 +0000981static size_t str16len(char16_t* startptr)
982{
983 char16_t* endptr = startptr;
984 while (*endptr)
985 {
986 endptr++;
987 }
988 return endptr - startptr;
989}
990
991TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
992{
993 struct pldm_msgbuf _ctxExtract;
994 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
995 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
996 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
997 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
998 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
999 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1000 uint16_t testVal;
1001 uint16_t testVal1;
1002 void* retBuff = NULL;
1003
1004 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301005 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001006 EXPECT_EQ(0x2211, testVal);
1007
1008 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1009 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301010 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001011 EXPECT_EQ(0x1234, testVal1);
1012
1013 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1014 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1015 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1016 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1017}
1018
1019TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1020{
1021 struct pldm_msgbuf _ctxExtract;
1022 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1023 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1024 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1025 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1026 constexpr size_t required = 6;
1027 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1028 0x6c44, 0x6f55, 0x0000};
1029 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1030 uint16_t testVal;
1031 uint16_t testVal1;
1032 char* retBuff = NULL;
1033 char* retBuff1 = NULL;
1034 size_t length = 0;
1035
1036 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301037 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001038 EXPECT_EQ(0x2211, testVal);
1039
1040 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1041 0);
1042
1043 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1044 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1045 EXPECT_EQ(memcmp(expectData, retBuff,
1046 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1047 0);
1048
Andrew Jefferye5f12532024-10-01 12:18:49 +09301049 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001050 EXPECT_EQ(0x1234, testVal1);
1051
1052 EXPECT_EQ(
1053 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1054 0);
1055
1056 EXPECT_EQ(0, length % 2);
1057 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1058
Andrew Jefferye5f12532024-10-01 12:18:49 +09301059 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001060 EXPECT_EQ(0x1234, testVal1);
1061
1062 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1063}
1064
1065TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1066{
1067 struct pldm_msgbuf _ctxExtract;
1068 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1069 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1070 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1071 uint16_t testVal;
1072
1073 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301074 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001075 EXPECT_EQ(0x2211, testVal);
1076 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1077 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1078}
1079
1080TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1081{
1082 struct pldm_msgbuf _ctxExtract;
1083 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1084 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1085 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1086 uint16_t testVal;
1087 char16_t* retBuff = NULL;
1088
1089 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301090 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001091 EXPECT_EQ(0x2211, testVal);
1092 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1093 -EOVERFLOW);
1094 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1095}
1096
1097TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1098{
1099 struct pldm_msgbuf _ctxExtract;
1100 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1101 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1102 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1103 uint16_t testVal;
1104 char16_t* retBuff = NULL;
1105
1106 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301107 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001108 EXPECT_EQ(0x2211, testVal);
1109 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1110 -EOVERFLOW);
1111 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1112}
1113
1114TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1115{
1116 struct pldm_msgbuf _ctxExtract;
1117 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1118 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1119 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1120 0x58, 0x00, 0x00, // Entity Name "S0S"
1121 0x66, 0x6e, 0x00, // Language Tag "en"
1122 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1123 0x67, 0x6e, 0x00, // Language Tag "en"
1124 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1125 0x00, // Entity Name "RR3"
1126 0x77, 0x88};
1127 uint8_t name_count;
1128 uint16_t test_val;
1129 char* tag = NULL;
1130 char* name = NULL;
1131 char* tag1 = NULL;
1132 char* name1 = NULL;
1133 char* tag2 = NULL;
1134 char* name2 = NULL;
1135 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1136 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1137 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1138
1139 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1140 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1141 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1142 size_t length = 0;
1143
1144 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301145 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, name_count), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001146 EXPECT_EQ(0x2, name_count);
1147
1148 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1149 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1150
1151 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1152 0);
1153 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1154 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1155 EXPECT_EQ(memcmp(expectName0, name,
1156 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1157 0);
1158
1159 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1160 0);
1161 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1162 EXPECT_EQ(
1163 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1164 EXPECT_EQ(0, length % 2);
1165 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1166
1167 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1168 0);
1169 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1170 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1171 0);
1172 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1173 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1174 EXPECT_EQ(memcmp(expectName2, name2,
1175 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1176 0);
1177
Andrew Jefferye5f12532024-10-01 12:18:49 +09301178 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, test_val), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001179 EXPECT_EQ(0x8877, test_val);
1180
1181 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1182}
1183
1184TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1185{
1186 struct pldm_msgbuf _ctxExtract;
1187 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1188
1189 uint8_t src[1] = {};
1190 char* retBuff = NULL;
1191
1192 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1193 ctxExtract->remaining = INTMAX_MIN;
1194 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1195 0);
1196 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1197}
1198
Thu Nguyen062c8762023-04-22 20:45:04 +07001199TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1200{
1201 struct pldm_msgbuf _ctx;
1202 struct pldm_msgbuf* ctx = &_ctx;
1203 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1204 uint8_t buf[8] = {0};
1205 uint16_t testVal;
1206 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1207 size_t remaining;
1208 uint8_t* retBuff = NULL;
1209
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301210 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001211 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301212 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001213
1214 struct pldm_msgbuf _ctxExtract;
1215 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1216
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301217 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
1218 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001219 EXPECT_EQ(
1220 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301221 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001222
1223 EXPECT_EQ(remaining, sizeof(expectData));
1224 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301225 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1226 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001227}
1228
1229TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1230{
1231 struct pldm_msgbuf _ctx;
1232 struct pldm_msgbuf* ctx = &_ctx;
1233 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1234 uint8_t buf[8] = {0};
1235 uint16_t testVal;
Thu Nguyen062c8762023-04-22 20:45:04 +07001236
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301237 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001238 EXPECT_EQ(
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301239 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001240
1241 struct pldm_msgbuf _ctxExtract;
1242 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1243
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301244 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
1245 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen062c8762023-04-22 20:45:04 +07001246
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301247 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1248 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301249}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001250
1251TEST(msgbuf, pldm_msgbuf_copy_good)
1252{
1253 struct pldm_msgbuf _src;
1254 struct pldm_msgbuf* src = &_src;
1255 uint16_t buf[1] = {htole16(0x5aa5)};
1256
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301257 ASSERT_EQ(pldm_msgbuf_init_errno(src, sizeof(buf), buf, sizeof(buf)), 0);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001258
1259 struct pldm_msgbuf _dst;
1260 struct pldm_msgbuf* dst = &_dst;
1261 uint16_t checkVal = 0;
1262 uint8_t buf1[sizeof(buf)] = {};
1263
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301264 ASSERT_EQ(pldm_msgbuf_init_errno(dst, sizeof(buf1), buf1, sizeof(buf1)), 0);
1265 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), 0);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001266
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301267 ASSERT_EQ(pldm_msgbuf_init_errno(dst, sizeof(buf1), buf1, sizeof(buf1)), 0);
1268 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, checkVal), 0);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001269
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301270 EXPECT_EQ(pldm_msgbuf_destroy(src), 0);
1271 EXPECT_EQ(pldm_msgbuf_destroy(dst), 0);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301272
1273 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001274}
1275
1276TEST(msgbuf, pldm_msgbuf_copy_bad)
1277{
1278 struct pldm_msgbuf _src;
1279 struct pldm_msgbuf* src = &_src;
1280 struct pldm_msgbuf _dst;
1281 struct pldm_msgbuf* dst = &_dst;
1282 uint8_t buf[1] = {sizeof(uint8_t)};
1283 uint8_t buf1[1] = {sizeof(uint16_t)};
1284 uint16_t value = 8;
1285
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301286 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, buf, sizeof(buf)), 0);
1287 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf1, sizeof(buf1)), 0);
1288 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), -EOVERFLOW);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001289
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301290 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, buf1, sizeof(buf1)), 0);
1291 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1292 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), -EOVERFLOW);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001293}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301294
1295TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
1296{
1297 const char msg[] = "this is a message";
1298
1299 struct pldm_msgbuf _src;
1300 struct pldm_msgbuf* src = &_src;
1301 struct pldm_msgbuf _dst;
1302 struct pldm_msgbuf* dst = &_dst;
1303
1304 char buf[sizeof(msg)] = {};
1305
1306 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1307 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1308 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1309 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1310 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1311 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1312}
1313
1314TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1315{
1316 const char msg[] = "this is a message";
1317
1318 struct pldm_msgbuf _src;
1319 struct pldm_msgbuf* src = &_src;
1320 struct pldm_msgbuf _dst;
1321 struct pldm_msgbuf* dst = &_dst;
1322
1323 char buf[sizeof(msg) + 1] = {};
1324
1325 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1326 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1327 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1328 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1329 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1330 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1331}
1332
1333TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1334{
1335 const char msg[] = "this is a message";
1336
1337 struct pldm_msgbuf _src;
1338 struct pldm_msgbuf* src = &_src;
1339 struct pldm_msgbuf _dst;
1340 struct pldm_msgbuf* dst = &_dst;
1341
1342 char buf[sizeof(msg) - 1] = {};
1343
1344 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1345 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1346 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1347 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1348 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1349}
1350
1351TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1352{
1353 const char msg[] = {'a'};
1354
1355 struct pldm_msgbuf _src;
1356 struct pldm_msgbuf* src = &_src;
1357 struct pldm_msgbuf _dst;
1358 struct pldm_msgbuf* dst = &_dst;
1359
1360 char buf[sizeof(msg)] = {};
1361
1362 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1363 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1364 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1365 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1366 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1367}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301368
1369TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
1370{
1371 const char16_t msg[] = u"this is a message";
1372
1373 struct pldm_msgbuf _src;
1374 struct pldm_msgbuf* src = &_src;
1375 struct pldm_msgbuf _dst;
1376 struct pldm_msgbuf* dst = &_dst;
1377
1378 char buf[sizeof(msg)] = {};
1379
1380 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1381 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1382 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1383 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1384 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1385 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1386}
1387
1388TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1389{
1390 const char16_t msg[] = u"this is a message";
1391
1392 struct pldm_msgbuf _src;
1393 struct pldm_msgbuf* src = &_src;
1394 struct pldm_msgbuf _dst;
1395 struct pldm_msgbuf* dst = &_dst;
1396
1397 char buf[sizeof(msg) + 1] = {};
1398
1399 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1400 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1401 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1402 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1403 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1404 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1405}
1406
1407TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1408{
1409 const char16_t msg[] = u"this is a message";
1410
1411 struct pldm_msgbuf _src;
1412 struct pldm_msgbuf* src = &_src;
1413 struct pldm_msgbuf _dst;
1414 struct pldm_msgbuf* dst = &_dst;
1415
1416 char buf[sizeof(msg) - 1] = {};
1417
1418 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1419 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1420 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1421 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1422 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1423}
1424
1425TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1426{
1427 const char16_t msg[] = {u'a'};
1428
1429 struct pldm_msgbuf _src;
1430 struct pldm_msgbuf* src = &_src;
1431 struct pldm_msgbuf _dst;
1432 struct pldm_msgbuf* dst = &_dst;
1433
1434 char buf[sizeof(msg)] = {};
1435
1436 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1437 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1438 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1439 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1440 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1441}