blob: a65f6bb0c0e549b58ff8c8b04f59d2234d2cf82e [file] [log] [blame]
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10301#include <endian.h>
Thu Nguyen15237782024-07-02 09:30:41 +00002#include <libpldm/utils.h>
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10303
4#include <cfloat>
5
6#include <gtest/gtest.h>
7
8/* We're exercising the implementation so disable the asserts for now */
9#ifndef NDEBUG
10#define NDEBUG 1
11#endif
12
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103013#include "msgbuf.h"
14
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103015TEST(msgbuf, init_bad_minsize)
16{
17 struct pldm_msgbuf _ctx;
18 struct pldm_msgbuf* ctx = &_ctx;
19 uint8_t buf[1] = {};
20
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093021 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf) + 1U, buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103022 PLDM_SUCCESS);
23}
24
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103025TEST(msgbuf, init_bad_len)
26{
27 struct pldm_msgbuf _ctx;
28 struct pldm_msgbuf* ctx = &_ctx;
29 uint8_t buf[1] = {};
30
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093031 EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX),
32 PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103033}
34
35TEST(msgbuf, init_overflow)
36{
37 struct pldm_msgbuf _ctx;
38 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103039 // NOLINTNEXTLINE(performance-no-int-to-ptr)
Andrew Jeffery07febdb2024-05-17 14:17:14 +093040 void* buf = (void*)UINTPTR_MAX;
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103041
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093042 EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103043}
44
45TEST(msgbuf, init_success)
46{
47 struct pldm_msgbuf _ctx;
48 struct pldm_msgbuf* ctx = &_ctx;
49 uint8_t buf[1] = {};
50
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093051 EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103052 PLDM_SUCCESS);
53}
54
55TEST(msgbuf, destroy_none)
56{
57 struct pldm_msgbuf _ctx;
58 struct pldm_msgbuf* ctx = &_ctx;
59 uint8_t buf[1] = {};
60
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093061 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103062 PLDM_SUCCESS);
63 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
64}
65
66TEST(msgbuf, destroy_exact)
67{
68 struct pldm_msgbuf _ctx;
69 struct pldm_msgbuf* ctx = &_ctx;
70 uint8_t buf[1] = {0xa5};
71 uint8_t val;
72
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093073 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103074 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +093075 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103076 EXPECT_EQ(val, 0xa5);
77 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
78}
79
80TEST(msgbuf, destroy_over)
81{
82 struct pldm_msgbuf _ctx;
83 struct pldm_msgbuf* ctx = &_ctx;
84 uint8_t buf[1] = {0xa5};
85 uint8_t val;
86
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093087 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103088 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +093089 ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103090 ASSERT_EQ(val, 0xa5);
Andrew Jefferye5f12532024-10-01 12:18:49 +093091 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103092 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
93}
94
95TEST(msgbuf, destroy_under)
96{
97 struct pldm_msgbuf _ctx;
98 struct pldm_msgbuf* ctx = &_ctx;
99 uint8_t buf[2] = {0x5a, 0xa5};
100 uint8_t val;
101
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930102 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030103 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930104 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030105 EXPECT_EQ(val, 0x5a);
106 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
107}
108
109TEST(msgbuf, extract_one_uint8)
110{
111 struct pldm_msgbuf _ctx;
112 struct pldm_msgbuf* ctx = &_ctx;
113 uint8_t buf[1] = {0xa5};
114 uint8_t val;
115
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930116 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030117 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930118 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030119 EXPECT_EQ(val, 0xa5);
120 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
121}
122
123TEST(msgbuf, extract_over_uint8)
124{
125 struct pldm_msgbuf _ctx;
126 struct pldm_msgbuf* ctx = &_ctx;
127 uint8_t buf[1] = {};
128 uint8_t val;
129
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930130 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930131 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030132 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
133}
134
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930135TEST(msgbuf, extract_under_uint8)
136{
137 struct pldm_msgbuf _ctx;
138 struct pldm_msgbuf* ctx = &_ctx;
139
140 uint8_t buf[1] = {};
141 uint8_t val;
142
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930143 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930144 ctx->remaining = INTMAX_MIN;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930145 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930146 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
147}
148
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030149TEST(msgbuf, extract_one_int8)
150{
151 struct pldm_msgbuf _ctx;
152 struct pldm_msgbuf* ctx = &_ctx;
153 int8_t buf[1] = {-1};
154 int8_t val;
155
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930156 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030157 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930158 EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030159 EXPECT_EQ(val, -1);
160 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
161}
162
163TEST(msgbuf, extract_over_int8)
164{
165 struct pldm_msgbuf _ctx;
166 struct pldm_msgbuf* ctx = &_ctx;
167 int8_t buf[1] = {};
168 int8_t val;
169
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930170 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930171 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030172 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
173}
174
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930175TEST(msgbuf, extract_under_int8)
176{
177 struct pldm_msgbuf _ctx;
178 struct pldm_msgbuf* ctx = &_ctx;
179
180 uint8_t buf[1] = {};
181 int8_t val;
182
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930183 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930184 ctx->remaining = INTMAX_MIN;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930185 EXPECT_NE(pldm_msgbuf_extract_int8(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930186 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
187}
188
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030189TEST(msgbuf, extract_one_uint16)
190{
191 struct pldm_msgbuf _ctx;
192 struct pldm_msgbuf* ctx = &_ctx;
193 uint16_t buf[1] = {htole16(0x5aa5)};
194 uint16_t val = {};
195
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930196 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030197 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930198 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030199 EXPECT_EQ(val, 0x5aa5);
200 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
201}
202
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930203TEST(msgbuf, extract_under_uint16)
204{
205 struct pldm_msgbuf _ctx;
206 struct pldm_msgbuf* ctx = &_ctx;
207
208 uint16_t buf[1] = {};
209 uint16_t val;
210
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930211 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930212 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930213 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930214 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
215}
216
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030217TEST(msgbuf, extract_over_uint16)
218{
219 struct pldm_msgbuf _ctx;
220 struct pldm_msgbuf* ctx = &_ctx;
221 uint16_t buf[1] = {};
222 uint16_t val;
223
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930224 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930225 EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030226 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
227}
228
229TEST(msgbuf, extract_one_int16)
230{
231 struct pldm_msgbuf _ctx;
232 struct pldm_msgbuf* ctx = &_ctx;
233 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
234 int16_t val;
235
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930236 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030237 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930238 EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030239 EXPECT_EQ(val, INT16_MIN);
240 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
241}
242
243TEST(msgbuf, extract_over_int16)
244{
245 struct pldm_msgbuf _ctx;
246 struct pldm_msgbuf* ctx = &_ctx;
247 int16_t buf[1] = {};
248 int16_t val;
249
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930250 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930251 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030252 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
253}
254
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930255TEST(msgbuf, extract_under_int16)
256{
257 struct pldm_msgbuf _ctx;
258 struct pldm_msgbuf* ctx = &_ctx;
259
260 int16_t buf[1] = {};
261 int16_t val;
262
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930263 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930264 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930265 EXPECT_NE(pldm_msgbuf_extract_int16(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930266 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
267}
268
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030269TEST(msgbuf, extract_one_uint32)
270{
271 struct pldm_msgbuf _ctx;
272 struct pldm_msgbuf* ctx = &_ctx;
273 uint32_t buf[1] = {htole32(0x5a00ffa5)};
274 uint32_t val;
275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930276 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030277 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930278 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030279 EXPECT_EQ(val, 0x5a00ffa5);
280 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
281}
282
283TEST(msgbuf, extract_over_uint32)
284{
285 struct pldm_msgbuf _ctx;
286 struct pldm_msgbuf* ctx = &_ctx;
287 uint32_t buf[1] = {};
288 uint32_t val;
289
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930290 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930291 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030292 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
293}
294
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930295TEST(msgbuf, extract_under_uint32)
296{
297 struct pldm_msgbuf _ctx;
298 struct pldm_msgbuf* ctx = &_ctx;
299
300 uint32_t buf[1] = {};
301 uint32_t val;
302
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930303 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930304 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930305 EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930306 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
307}
308
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030309TEST(msgbuf, extract_one_int32)
310{
311 struct pldm_msgbuf _ctx;
312 struct pldm_msgbuf* ctx = &_ctx;
313 int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))};
314 int32_t val;
315
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930316 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030317 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930318 EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030319 EXPECT_EQ(val, INT32_MIN);
320 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
321}
322
323TEST(msgbuf, extract_over_int32)
324{
325 struct pldm_msgbuf _ctx;
326 struct pldm_msgbuf* ctx = &_ctx;
327 int32_t buf[1] = {};
328 int32_t val;
329
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930330 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930331 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030332 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
333}
334
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930335TEST(msgbuf, extract_under_int32)
336{
337 struct pldm_msgbuf _ctx;
338 struct pldm_msgbuf* ctx = &_ctx;
339
340 int32_t buf[1] = {};
341 int32_t val;
342
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930343 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930344 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930345 EXPECT_NE(pldm_msgbuf_extract_int32(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930346 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
347}
348
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030349TEST(msgbuf, extract_one_real32)
350{
351 struct pldm_msgbuf _ctx;
352 struct pldm_msgbuf* ctx = &_ctx;
353 uint32_t buf[1] = {};
354 uint32_t xform;
355 real32_t val;
356
357 val = FLT_MAX;
358 memcpy(&xform, &val, sizeof(val));
359 buf[0] = htole32(xform);
360 val = 0;
361
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930362 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)),
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030363 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930364 EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030365 EXPECT_EQ(val, FLT_MAX);
366 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
367}
368
369TEST(msgbuf, extract_over_real32)
370{
371 struct pldm_msgbuf _ctx;
372 struct pldm_msgbuf* ctx = &_ctx;
373 real32_t buf[1] = {};
374 real32_t val;
375
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930376 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930377 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030378 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
379}
Andrew Jeffery369b1212023-04-20 15:44:48 +0930380
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930381TEST(msgbuf, extract_under_real32)
382{
383 struct pldm_msgbuf _ctx;
384 struct pldm_msgbuf* ctx = &_ctx;
385
386 real32_t buf[1] = {};
387 real32_t val;
388
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930389 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930390 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jefferye5f12532024-10-01 12:18:49 +0930391 EXPECT_NE(pldm_msgbuf_extract_real32(ctx, val), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930392 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
393}
394
Andrew Jeffery369b1212023-04-20 15:44:48 +0930395TEST(msgbuf, extract_array_uint8_buf0_req0)
396{
397 struct pldm_msgbuf _ctx;
398 struct pldm_msgbuf* ctx = &_ctx;
399 uint8_t buf[1] = {};
400 uint8_t arr[1];
401
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930402 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000403 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930404 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
405}
406
407TEST(msgbuf, extract_array_uint8_buf1_req1)
408{
409 struct pldm_msgbuf _ctx;
410 struct pldm_msgbuf* ctx = &_ctx;
411 uint8_t buf[1] = {};
412 uint8_t arr[1];
413
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930414 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000415 EXPECT_EQ(
416 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
417 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930418 EXPECT_EQ(arr[0], 0);
419 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
420}
421
422TEST(msgbuf, extract_array_uint8_buf1_req2)
423{
424 struct pldm_msgbuf _ctx;
425 struct pldm_msgbuf* ctx = &_ctx;
426 uint8_t buf[1] = {};
427 uint8_t arr[2];
428
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930429 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000430 EXPECT_NE(
431 pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)),
432 PLDM_SUCCESS);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930433 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
434}
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930435
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930436TEST(msgbuf, extract_under_array_uint8)
437{
438 struct pldm_msgbuf _ctx;
439 struct pldm_msgbuf* ctx = &_ctx;
440 uint8_t buf[1] = {};
441 uint8_t arr[1];
442
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930443 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930444 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000445 EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930446 ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
447}
448
Andrew Jeffery1c571442024-07-08 10:25:48 +0930449TEST(msgbuf, extract_array_char_buf0_req0)
450{
451 struct pldm_msgbuf _ctx;
452 struct pldm_msgbuf* ctx = &_ctx;
453 char buf[1] = {'\0'};
454 char arr[1] = {'1'};
455
456 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000457 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930458 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
459}
460
461TEST(msgbuf, extract_array_char_buf1_req1)
462{
463 struct pldm_msgbuf _ctx;
464 struct pldm_msgbuf* ctx = &_ctx;
465 char buf[1] = {'\0'};
466 char arr[1] = {'1'};
467
468 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000469 EXPECT_EQ(
470 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930471 EXPECT_EQ(arr[0], '\0');
472 ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0);
473}
474
475TEST(msgbuf, extract_array_char_buf1_req2)
476{
477 struct pldm_msgbuf _ctx;
478 struct pldm_msgbuf* ctx = &_ctx;
479 char buf[1] = {'\0'};
480 char arr[2] = {'1', '2'};
481
482 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000483 EXPECT_NE(
484 pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930485 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
486}
487
488TEST(msgbuf, extract_under_array_char)
489{
490 struct pldm_msgbuf _ctx;
491 struct pldm_msgbuf* ctx = &_ctx;
492 char buf[1] = {'\0'};
493 char arr[1] = {'1'};
494
495 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
496 ctx->remaining = INTMAX_MIN;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000497 EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930498 ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
499}
500
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930501TEST(msgbuf, consumed_under)
502{
503 struct pldm_msgbuf _ctx;
504 struct pldm_msgbuf* ctx = &_ctx;
505 uint8_t buf[1] = {};
506
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930507 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930508 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
509}
510
511TEST(msgbuf, consumed_exact)
512{
513 struct pldm_msgbuf _ctx;
514 struct pldm_msgbuf* ctx = &_ctx;
515 uint8_t buf[1] = {};
516 uint8_t val;
517
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930518 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930519 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, val), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930520 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
521}
522
523TEST(msgbuf, consumed_over)
524{
525 struct pldm_msgbuf _ctx;
526 struct pldm_msgbuf* ctx = &_ctx;
527 uint8_t buf[1] = {};
Andrew Jefferye5f12532024-10-01 12:18:49 +0930528 uint8_t valid;
529 uint8_t invalid;
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930530
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930531 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930532 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, valid), PLDM_SUCCESS);
533 EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, invalid), PLDM_SUCCESS);
Andrew Jefferydb7b8322023-04-12 23:05:21 +0930534 EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
535}
Thu Nguyen062c8762023-04-22 20:45:04 +0700536
537TEST(msgbuf, pldm_msgbuf_insert_int32_good)
538{
539 struct pldm_msgbuf _ctx;
540 struct pldm_msgbuf* ctx = &_ctx;
541 int32_t src = -12345;
542 int32_t checkVal = 0;
543 uint8_t buf[sizeof(int32_t)] = {};
544
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930545 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700546 EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
547
548 struct pldm_msgbuf _ctxExtract;
549 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
550
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930551 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
552 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930553 EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700554
555 EXPECT_EQ(src, checkVal);
556 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
557 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
558}
559
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930560TEST(msgbuf, insert_under_int32)
561{
562 struct pldm_msgbuf _ctx;
563 struct pldm_msgbuf* ctx = &_ctx;
564
565 int32_t buf[1] = {};
566 int32_t val = 0;
567
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930568 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930569 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
570 EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS);
571 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
572}
573
Thu Nguyen062c8762023-04-22 20:45:04 +0700574TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
575{
576 struct pldm_msgbuf _ctx;
577 struct pldm_msgbuf* ctx = &_ctx;
578 uint32_t src = 0xf1223344;
579 uint32_t checkVal = 0;
580 uint8_t buf[sizeof(uint32_t)] = {};
581
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930582 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700583 EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
584
585 struct pldm_msgbuf _ctxExtract;
586 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
587
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930588 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
589 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930590 EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700591
592 EXPECT_EQ(src, checkVal);
593 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
594 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
595}
596
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930597TEST(msgbuf, insert_under_uint32)
598{
599 struct pldm_msgbuf _ctx;
600 struct pldm_msgbuf* ctx = &_ctx;
601
602 uint32_t buf[1] = {};
603 uint32_t val = 0;
604
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930605 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930606 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
607 EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS);
608 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
609}
610
Thu Nguyen062c8762023-04-22 20:45:04 +0700611TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
612{
613 struct pldm_msgbuf _ctx;
614 struct pldm_msgbuf* ctx = &_ctx;
615 uint16_t src = 0xf344;
616 uint16_t checkVal = 0;
617 uint8_t buf[sizeof(uint16_t)] = {};
618
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930619 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700620 EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
621
622 struct pldm_msgbuf _ctxExtract;
623 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
624
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930625 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
626 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930627 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700628
629 EXPECT_EQ(src, checkVal);
630 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
631 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
632}
633
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930634TEST(msgbuf, insert_under_uint16)
635{
636 struct pldm_msgbuf _ctx;
637 struct pldm_msgbuf* ctx = &_ctx;
638
639 uint16_t buf[1] = {};
640 uint16_t val = 0;
641
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930642 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930643 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
644 EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS);
645 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
646}
647
Thu Nguyen062c8762023-04-22 20:45:04 +0700648TEST(msgbuf, pldm_msgbuf_insert_int16_good)
649{
650 struct pldm_msgbuf _ctx;
651 struct pldm_msgbuf* ctx = &_ctx;
652 int16_t src = -12;
653 int16_t checkVal = 0;
654 uint8_t buf[sizeof(int16_t)] = {};
655
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930656 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700657 EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
658
659 struct pldm_msgbuf _ctxExtract;
660 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
661
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930662 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
663 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930664 EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700665
666 EXPECT_EQ(src, checkVal);
667 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
668 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
669}
670
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930671TEST(msgbuf, insert_under_int16)
672{
673 struct pldm_msgbuf _ctx;
674 struct pldm_msgbuf* ctx = &_ctx;
675
676 int16_t buf[1] = {};
677 int16_t val = 0;
678
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930679 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930680 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
681 EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS);
682 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
683}
684
Thu Nguyen062c8762023-04-22 20:45:04 +0700685TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
686{
687 struct pldm_msgbuf _ctx;
688 struct pldm_msgbuf* ctx = &_ctx;
689 uint8_t src = 0xf4;
690 uint8_t checkVal = 0;
691 uint8_t buf[sizeof(uint8_t)] = {};
692
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930693 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700694 EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
695
696 struct pldm_msgbuf _ctxExtract;
697 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
698
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930699 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
700 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930701 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700702
703 EXPECT_EQ(src, checkVal);
704 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
705 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
706}
707
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930708TEST(msgbuf, insert_under_uint8)
709{
710 struct pldm_msgbuf _ctx;
711 struct pldm_msgbuf* ctx = &_ctx;
712
713 uint8_t buf[1] = {};
714 uint8_t val = 0;
715
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930716 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930717 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
718 EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS);
719 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
720}
721
Thu Nguyen062c8762023-04-22 20:45:04 +0700722TEST(msgbuf, pldm_msgbuf_insert_int8_good)
723{
724 struct pldm_msgbuf _ctx;
725 struct pldm_msgbuf* ctx = &_ctx;
726 int8_t src = -4;
727 int8_t checkVal = 0;
728 uint8_t buf[sizeof(int8_t)] = {};
729
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930730 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700731 EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
732
733 struct pldm_msgbuf _ctxExtract;
734 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
735
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930736 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
737 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930738 EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, checkVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700739
740 EXPECT_EQ(src, checkVal);
741 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
742 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
743}
744
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930745TEST(msgbuf, insert_under_int8)
746{
747 struct pldm_msgbuf _ctx;
748 struct pldm_msgbuf* ctx = &_ctx;
749
750 int8_t buf[1] = {};
751 int8_t val = 0;
752
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930753 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930754 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
755 EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS);
756 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
757}
758
Thu Nguyen062c8762023-04-22 20:45:04 +0700759TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
760{
761 struct pldm_msgbuf _ctx;
762 struct pldm_msgbuf* ctx = &_ctx;
763 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
764 uint8_t buf[6] = {};
765 uint8_t retBuff[6] = {};
766
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930767 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000768 EXPECT_EQ(
769 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
770 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700771
772 struct pldm_msgbuf _ctxExtract;
773 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
774
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930775 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
776 PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000777 EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff),
778 retBuff, sizeof(retBuff)),
779 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700780
781 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
782 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
783 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
784}
785
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930786TEST(msgbuf, insert_under_array_uint8)
787{
788 struct pldm_msgbuf _ctx;
789 struct pldm_msgbuf* ctx = &_ctx;
790
791 uint8_t buf[1] = {};
792 uint8_t val[1] = {0};
793
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930794 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930795 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000796 EXPECT_NE(
797 pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)),
798 PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930799 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
800}
801
Andrew Jeffery1c571442024-07-08 10:25:48 +0930802TEST(msgbuf, pldm_msgbuf_insert_array_char_good)
803{
804 struct pldm_msgbuf _ctx;
805 struct pldm_msgbuf* ctx = &_ctx;
806 char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
807 char buf[6] = {};
808 char retBuff[6] = {};
809
810 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000811 EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)),
812 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930813
814 struct pldm_msgbuf _ctxExtract;
815 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
816
817 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000818 EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff),
819 retBuff, sizeof(retBuff)),
820 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930821
822 EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
823 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
824 EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0);
825}
826
Andrew Jeffery1c571442024-07-08 10:25:48 +0930827TEST(msgbuf, insert_under_array_char)
828{
829 struct pldm_msgbuf _ctx;
830 struct pldm_msgbuf* ctx = &_ctx;
831 char buf[1] = {};
832 char val[1] = {0};
833
834 ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0);
835 ctx->remaining = INTMAX_MIN + sizeof(val) - 1;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000836 EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)),
837 0);
Andrew Jeffery1c571442024-07-08 10:25:48 +0930838 EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW);
839}
840
Thu Nguyen062c8762023-04-22 20:45:04 +0700841TEST(msgbuf, pldm_msgbuf_span_required_good)
842{
843 struct pldm_msgbuf _ctx;
844 struct pldm_msgbuf* ctx = &_ctx;
845 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
846 uint8_t buf[6] = {0};
847 const size_t required = 4;
848 uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
849 uint16_t testVal;
850 uint8_t* retBuff = NULL;
851
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930852 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000853 EXPECT_EQ(
854 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
855 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700856
857 struct pldm_msgbuf _ctxExtract;
858 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
859
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930860 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
861 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930862 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700863 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
864 PLDM_SUCCESS);
865
866 EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
867 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
868 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
869}
870
871TEST(msgbuf, pldm_msgbuf_span_required_bad)
872{
873 struct pldm_msgbuf _ctx;
874 struct pldm_msgbuf* ctx = &_ctx;
875 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
876 uint8_t buf[6] = {0};
Varsha Kaverappa79393822024-08-07 00:40:13 -0500877 const size_t required = 4;
Thu Nguyen062c8762023-04-22 20:45:04 +0700878 uint16_t testVal;
879 [[maybe_unused]] 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);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000882 EXPECT_EQ(
883 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
884 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700885
886 struct pldm_msgbuf _ctxExtract;
887 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
888
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930889 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
890 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930891 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), PLDM_SUCCESS);
Varsha Kaverappa79393822024-08-07 00:40:13 -0500892 EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
893 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700894
895 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
896 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
897}
898
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930899TEST(msgbuf, span_required_under)
900{
901 struct pldm_msgbuf _ctx;
902 struct pldm_msgbuf* ctx = &_ctx;
903
904 uint8_t buf[1] = {};
905 void* cursor = nullptr;
906
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930907 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS);
Andrew Jeffery2ff8cf82024-05-17 15:20:46 +0930908 ctx->remaining = INTMAX_MIN;
909 EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS);
910 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
911}
912
Thu Nguyen9c83d682024-07-02 08:43:09 +0000913TEST(msgbuf, pldm_msgbuf_span_string_ascii_good)
914{
915 struct pldm_msgbuf _ctxExtract;
916 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
917 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
918 constexpr size_t required = 6;
919 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
920 uint16_t testVal;
921 uint8_t testVal1;
922 char* retBuff = NULL;
923
924 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930925 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000926 EXPECT_EQ(0x2211, testVal);
927 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
928 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930929 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000930 EXPECT_EQ(0x77, testVal1);
931
932 EXPECT_EQ(required, strlen(retBuff) + 1);
933 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
934 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
935}
936
937TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length)
938{
939 struct pldm_msgbuf _ctxExtract;
940 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
941 uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77};
942 constexpr size_t required = 6;
943 const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
944 uint16_t testVal;
945 uint8_t testVal1;
946 char* retBuff = NULL;
947 size_t length;
948
949 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930950 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000951 EXPECT_EQ(0x2211, testVal);
952 EXPECT_EQ(
953 pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length),
954 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930955 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, testVal1), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000956 EXPECT_EQ(0x77, testVal1);
957
958 EXPECT_EQ(required, strlen(retBuff) + 1);
959 EXPECT_EQ(length, strlen(retBuff) + 1);
960 EXPECT_EQ(required, length);
961 EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0);
962 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
963}
964
965TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args)
966{
967 struct pldm_msgbuf _ctxExtract;
968 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
969 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00};
970 uint16_t testVal;
971
972 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930973 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000974 EXPECT_EQ(0x2211, testVal);
975 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0);
976 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
977}
978
979TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator)
980{
981 struct pldm_msgbuf _ctxExtract;
982 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
983 uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77};
984 uint16_t testVal;
985 char* retBuff = NULL;
986
987 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +0930988 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen9c83d682024-07-02 08:43:09 +0000989 EXPECT_EQ(0x2211, testVal);
990 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
991 -EOVERFLOW);
992 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
993}
994
995TEST(msgbuf, pldm_msgbuf_span_string_ascii_under)
996{
997 struct pldm_msgbuf _ctxExtract;
998 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
999
1000 uint8_t src[1] = {};
1001 char* retBuff = NULL;
1002
1003 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1004 ctxExtract->remaining = INTMAX_MIN;
1005 EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL),
1006 0);
1007 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1008}
1009
Thu Nguyen15237782024-07-02 09:30:41 +00001010static size_t str16len(char16_t* startptr)
1011{
1012 char16_t* endptr = startptr;
1013 while (*endptr)
1014 {
1015 endptr++;
1016 }
1017 return endptr - startptr;
1018}
1019
1020TEST(msgbuf, pldm_msgbuf_span_string_utf16_good)
1021{
1022 struct pldm_msgbuf _ctxExtract;
1023 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1024 uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = {
1025 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1026 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1027 const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1028 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1029 uint16_t testVal;
1030 uint16_t testVal1;
1031 void* retBuff = NULL;
1032
1033 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301034 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001035 EXPECT_EQ(0x2211, testVal);
1036
1037 ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1038 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301039 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001040 EXPECT_EQ(0x1234, testVal1);
1041
1042 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1043 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1044 EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData)));
1045 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1046}
1047
1048TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2)
1049{
1050 struct pldm_msgbuf _ctxExtract;
1051 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1052 uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c,
1053 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12,
1054 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12};
1055 constexpr size_t required = 6;
1056 const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33,
1057 0x6c44, 0x6f55, 0x0000};
1058 const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000};
1059 uint16_t testVal;
1060 uint16_t testVal1;
1061 char* retBuff = NULL;
1062 char* retBuff1 = NULL;
1063 size_t length = 0;
1064
1065 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301066 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001067 EXPECT_EQ(0x2211, testVal);
1068
1069 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1070 0);
1071
1072 ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1));
1073 EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1);
1074 EXPECT_EQ(memcmp(expectData, retBuff,
1075 sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)),
1076 0);
1077
Andrew Jefferye5f12532024-10-01 12:18:49 +09301078 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001079 EXPECT_EQ(0x1234, testVal1);
1080
1081 EXPECT_EQ(
1082 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length),
1083 0);
1084
1085 EXPECT_EQ(0, length % 2);
1086 EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0);
1087
Andrew Jefferye5f12532024-10-01 12:18:49 +09301088 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal1), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001089 EXPECT_EQ(0x1234, testVal1);
1090
1091 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1092}
1093
1094TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args)
1095{
1096 struct pldm_msgbuf _ctxExtract;
1097 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1098 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1099 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00};
1100 uint16_t testVal;
1101
1102 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301103 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001104 EXPECT_EQ(0x2211, testVal);
1105 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0);
1106 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1107}
1108
1109TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator)
1110{
1111 struct pldm_msgbuf _ctxExtract;
1112 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1113 uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1114 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77};
1115 uint16_t testVal;
1116 char16_t* retBuff = NULL;
1117
1118 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301119 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001120 EXPECT_EQ(0x2211, testVal);
1121 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1122 -EOVERFLOW);
1123 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1124}
1125
1126TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size)
1127{
1128 struct pldm_msgbuf _ctxExtract;
1129 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1130 uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33,
1131 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00};
1132 uint16_t testVal;
1133 char16_t* retBuff = NULL;
1134
1135 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301136 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001137 EXPECT_EQ(0x2211, testVal);
1138 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1139 -EOVERFLOW);
1140 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1141}
1142
1143TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix)
1144{
1145 struct pldm_msgbuf _ctxExtract;
1146 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1147 uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en"
1148 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00,
1149 0x58, 0x00, 0x00, // Entity Name "S0S"
1150 0x66, 0x6e, 0x00, // Language Tag "en"
1151 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
1152 0x67, 0x6e, 0x00, // Language Tag "en"
1153 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00,
1154 0x00, // Entity Name "RR3"
1155 0x77, 0x88};
1156 uint8_t name_count;
1157 uint16_t test_val;
1158 char* tag = NULL;
1159 char* name = NULL;
1160 char* tag1 = NULL;
1161 char* name1 = NULL;
1162 char* tag2 = NULL;
1163 char* name2 = NULL;
1164 const char expectTag0[3] = {0x65, 0x6e, 0x00};
1165 const char expectTag1[3] = {0x66, 0x6e, 0x00};
1166 const char expectTag2[3] = {0x67, 0x6e, 0x00};
1167
1168 const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000};
1169 const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000};
1170 const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000};
1171 size_t length = 0;
1172
1173 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301174 EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, name_count), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001175 EXPECT_EQ(0x2, name_count);
1176
1177 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0);
1178 EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0);
1179
1180 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL),
1181 0);
1182 ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1));
1183 EXPECT_EQ(5, str16len((char16_t*)name) + 1);
1184 EXPECT_EQ(memcmp(expectName0, name,
1185 sizeof(char16_t) * (str16len((char16_t*)name) + 1)),
1186 0);
1187
1188 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length),
1189 0);
1190 EXPECT_EQ(strncmp(expectTag1, tag1, length), 0);
1191 EXPECT_EQ(
1192 pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0);
1193 EXPECT_EQ(0, length % 2);
1194 EXPECT_EQ(memcmp(expectName1, name1, length), 0);
1195
1196 EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL),
1197 0);
1198 EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0);
1199 EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL),
1200 0);
1201 ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1));
1202 EXPECT_EQ(4, str16len((char16_t*)name2) + 1);
1203 EXPECT_EQ(memcmp(expectName2, name2,
1204 sizeof(char16_t) * (str16len((char16_t*)name2) + 1)),
1205 0);
1206
Andrew Jefferye5f12532024-10-01 12:18:49 +09301207 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, test_val), 0);
Thu Nguyen15237782024-07-02 09:30:41 +00001208 EXPECT_EQ(0x8877, test_val);
1209
1210 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0);
1211}
1212
1213TEST(msgbuf, pldm_msgbuf_span_string_utf16_under)
1214{
1215 struct pldm_msgbuf _ctxExtract;
1216 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1217
1218 uint8_t src[1] = {};
1219 char* retBuff = NULL;
1220
1221 ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0);
1222 ctxExtract->remaining = INTMAX_MIN;
1223 EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL),
1224 0);
1225 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW);
1226}
1227
Thu Nguyen062c8762023-04-22 20:45:04 +07001228TEST(msgbuf, pldm_msgbuf_span_remaining_good)
1229{
1230 struct pldm_msgbuf _ctx;
1231 struct pldm_msgbuf* ctx = &_ctx;
1232 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1233 uint8_t buf[8] = {0};
1234 uint16_t testVal;
1235 uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1236 size_t remaining;
1237 uint8_t* retBuff = NULL;
1238
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301239 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001240 EXPECT_EQ(
1241 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1242 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001243
1244 struct pldm_msgbuf _ctxExtract;
1245 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1246
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301247 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1248 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301249 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001250 EXPECT_EQ(
1251 pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
1252 PLDM_SUCCESS);
1253
1254 EXPECT_EQ(remaining, sizeof(expectData));
1255 EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
1256 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1257 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
1258}
1259
1260TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
1261{
1262 struct pldm_msgbuf _ctx;
1263 struct pldm_msgbuf* ctx = &_ctx;
1264 uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
1265 uint8_t buf[8] = {0};
1266 uint16_t testVal;
Thu Nguyen062c8762023-04-22 20:45:04 +07001267
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301268 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001269 EXPECT_EQ(
1270 pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)),
1271 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001272
1273 struct pldm_msgbuf _ctxExtract;
1274 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
1275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301276 ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)),
1277 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301278 EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, testVal), PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +07001279
1280 EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
1281 EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
Andrew Jeffery07febdb2024-05-17 14:17:14 +09301282}
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001283
1284TEST(msgbuf, pldm_msgbuf_copy_good)
1285{
1286 struct pldm_msgbuf _src;
1287 struct pldm_msgbuf* src = &_src;
1288 uint16_t buf[1] = {htole16(0x5aa5)};
1289
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301290 ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001291 PLDM_SUCCESS);
1292
1293 struct pldm_msgbuf _dst;
1294 struct pldm_msgbuf* dst = &_dst;
1295 uint16_t checkVal = 0;
1296 uint8_t buf1[sizeof(buf)] = {};
1297
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301298 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001299 PLDM_SUCCESS);
1300 EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS);
1301
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301302 ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)),
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001303 PLDM_SUCCESS);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301304 EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, checkVal), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001305
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001306 EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS);
1307 EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS);
Andrew Jeffery8b879602024-07-08 12:50:19 +09301308
1309 EXPECT_EQ(buf[0], checkVal);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001310}
1311
1312TEST(msgbuf, pldm_msgbuf_copy_bad)
1313{
1314 struct pldm_msgbuf _src;
1315 struct pldm_msgbuf* src = &_src;
1316 struct pldm_msgbuf _dst;
1317 struct pldm_msgbuf* dst = &_dst;
1318 uint8_t buf[1] = {sizeof(uint8_t)};
1319 uint8_t buf1[1] = {sizeof(uint16_t)};
1320 uint16_t value = 8;
1321
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301322 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS);
1323 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001324 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1325 PLDM_ERROR_INVALID_LENGTH);
1326
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301327 ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS);
1328 ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS);
Varsha Kaverappa909bf7c2024-05-03 06:18:42 -05001329 EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name),
1330 PLDM_ERROR_INVALID_LENGTH);
1331}
Andrew Jeffery8b879602024-07-08 12:50:19 +09301332
1333TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact)
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)] = {};
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), 0);
1347 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1348 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1349 EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf)));
1350}
1351
1352TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src)
1353{
1354 const char msg[] = "this is a message";
1355
1356 struct pldm_msgbuf _src;
1357 struct pldm_msgbuf* src = &_src;
1358 struct pldm_msgbuf _dst;
1359 struct pldm_msgbuf* dst = &_dst;
1360
1361 char buf[sizeof(msg) + 1] = {};
1362
1363 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1364 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1365 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0);
1366 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1367 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1368 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1369}
1370
1371TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst)
1372{
1373 const char msg[] = "this is a message";
1374
1375 struct pldm_msgbuf _src;
1376 struct pldm_msgbuf* src = &_src;
1377 struct pldm_msgbuf _dst;
1378 struct pldm_msgbuf* dst = &_dst;
1379
1380 char buf[sizeof(msg) - 1] = {};
1381
1382 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1383 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1384 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1385 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1386 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1387}
1388
1389TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src)
1390{
1391 const char msg[] = {'a'};
1392
1393 struct pldm_msgbuf _src;
1394 struct pldm_msgbuf* src = &_src;
1395 struct pldm_msgbuf _dst;
1396 struct pldm_msgbuf* dst = &_dst;
1397
1398 char buf[sizeof(msg)] = {};
1399
1400 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1401 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1402 EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW);
1403 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1404 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1405}
Andrew Jeffery56f73f92024-07-08 12:50:28 +09301406
1407TEST(msgbuf, pldm_msgbuf_copy_utf16_exact)
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)] = {};
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), 0);
1421 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1422 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1423 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1424}
1425
1426TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src)
1427{
1428 const char16_t msg[] = u"this is a message";
1429
1430 struct pldm_msgbuf _src;
1431 struct pldm_msgbuf* src = &_src;
1432 struct pldm_msgbuf _dst;
1433 struct pldm_msgbuf* dst = &_dst;
1434
1435 char buf[sizeof(msg) + 1] = {};
1436
1437 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1438 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1439 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0);
1440 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1441 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1442 EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg)));
1443}
1444
1445TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst)
1446{
1447 const char16_t msg[] = u"this is a message";
1448
1449 struct pldm_msgbuf _src;
1450 struct pldm_msgbuf* src = &_src;
1451 struct pldm_msgbuf _dst;
1452 struct pldm_msgbuf* dst = &_dst;
1453
1454 char buf[sizeof(msg) - 1] = {};
1455
1456 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1457 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1458 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1459 ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW);
1460 ASSERT_EQ(pldm_msgbuf_destroy(src), 0);
1461}
1462
1463TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src)
1464{
1465 const char16_t msg[] = {u'a'};
1466
1467 struct pldm_msgbuf _src;
1468 struct pldm_msgbuf* src = &_src;
1469 struct pldm_msgbuf _dst;
1470 struct pldm_msgbuf* dst = &_dst;
1471
1472 char buf[sizeof(msg)] = {};
1473
1474 ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0);
1475 ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0);
1476 EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW);
1477 ASSERT_EQ(pldm_msgbuf_destroy(dst), 0);
1478 ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW);
1479}