blob: 91192735600e3f3839f1be4582e6adca938c0a4a [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jefferyc63f63a2023-02-24 22:29:33 +10302/* Test pldm_msgbuf_extract() separately because we can't do _Generic() in C++
3 * code, i.e. gtest */
4
5#include <endian.h>
6#include <float.h>
7#include <stdio.h>
8#include <stdlib.h>
9
10/* We're exercising the implementation so disable the asserts for now */
11#ifndef NDEBUG
12#define NDEBUG 1
13#endif
14#include "msgbuf.h"
15
16/* Given we disabled asserts above, set up our own expectation framework */
17#define expect(cond) __expect(__func__, __LINE__, (cond))
18#define __expect(fn, line, cond) \
19 do \
20 { \
21 if (!(cond)) \
22 { \
23 fprintf(stderr, "%s:%d: failed expectation: %s\n", fn, line, \
24 #cond); \
25 exit(EXIT_FAILURE); \
26 } \
27 } while (0)
28
29static void test_msgbuf_extract_generic_uint8(void)
30{
31 struct pldm_msgbuf _ctx;
32 struct pldm_msgbuf* ctx = &_ctx;
33 uint8_t buf[1] = {0xa5};
34 uint8_t val;
35
36 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
37 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +093038 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103039 expect(val == 0xa5);
40 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
41}
42
43static void test_msgbuf_extract_generic_int8(void)
44{
45 struct pldm_msgbuf _ctx;
46 struct pldm_msgbuf* ctx = &_ctx;
47 int8_t buf[1] = {-1};
48 int8_t val;
49
50 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
51 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +093052 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103053 expect(val == -1);
54 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
55}
56
57static void test_msgbuf_extract_generic_uint16(void)
58{
59 struct pldm_msgbuf _ctx;
60 struct pldm_msgbuf* ctx = &_ctx;
61 uint16_t buf[1] = {0x5aa5};
62 uint16_t val;
63
64 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
65 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +093066 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103067 expect(val == 0x5aa5);
68 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
69}
70
71static void test_msgbuf_extract_generic_int16(void)
72{
73 struct pldm_msgbuf _ctx;
74 struct pldm_msgbuf* ctx = &_ctx;
75 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
76 int16_t val;
77
78 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
79 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +093080 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103081 expect(val == INT16_MIN);
82 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
83}
84
85static void test_msgbuf_extract_generic_uint32(void)
86{
87 struct pldm_msgbuf _ctx;
88 struct pldm_msgbuf* ctx = &_ctx;
89 uint32_t buf[1] = {0x5a00ffa5};
90 uint32_t val;
91
92 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
93 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +093094 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103095 expect(val == 0x5a00ffa5);
96 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
97}
98
99static void test_msgbuf_extract_generic_int32(void)
100{
101 struct pldm_msgbuf _ctx;
102 struct pldm_msgbuf* ctx = &_ctx;
103 int32_t buf[1] = {(int32_t)(htole32((uint32_t)INT32_MIN))};
104 int32_t val;
105
106 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
107 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930108 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030109 expect(val == INT32_MIN);
110 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
111}
112
113static void test_msgbuf_extract_generic_real32(void)
114{
115 struct pldm_msgbuf _ctx;
116 struct pldm_msgbuf* ctx = &_ctx;
117 uint32_t buf[1];
118 uint32_t xform;
119 real32_t val;
120
121 val = FLT_MAX;
122 memcpy(&xform, &val, sizeof(val));
123 buf[0] = htole32(xform);
124 val = 0;
125
126 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
127 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930128 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030129 expect(val == FLT_MAX);
130 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
131}
132
Andrew Jeffery369b1212023-04-20 15:44:48 +0930133static void test_msgbuf_extract_array_generic_uint8(void)
134{
135 struct pldm_msgbuf _ctx;
136 struct pldm_msgbuf* ctx = &_ctx;
137 uint32_t buf[1] = {0};
138 uint8_t arr[1];
139
140 expect(pldm_msgbuf_init(ctx, sizeof(buf), buf, sizeof(buf)) ==
141 PLDM_SUCCESS);
142 expect(pldm_msgbuf_extract_array(ctx, arr, 1) == PLDM_SUCCESS);
143 expect(arr[0] == 0);
144 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
145}
146
Thu Nguyen062c8762023-04-22 20:45:04 +0700147static void test_msgbuf_insert_generic_int32(void)
148{
149 struct pldm_msgbuf _ctx;
150 struct pldm_msgbuf* ctx = &_ctx;
151 int32_t src = -12345;
152 int32_t checkVal = 0;
153 uint8_t buf[sizeof(int32_t)] = {0};
154
155 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
156 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
157
158 struct pldm_msgbuf _ctxExtract;
159 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
160
161 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930162 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700163
164 expect(src == checkVal);
165 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
166 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
167}
168
169static void test_msgbuf_insert_generic_uint32(void)
170{
171 struct pldm_msgbuf _ctx;
172 struct pldm_msgbuf* ctx = &_ctx;
173 uint32_t src = 0xf1223344;
174 uint32_t checkVal = 0;
175 uint8_t buf[sizeof(uint32_t)] = {0};
176
177 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
178 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
179
180 struct pldm_msgbuf _ctxExtract;
181 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
182
183 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930184 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700185
186 expect(src == checkVal);
187 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
188 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
189}
190
191static void test_msgbuf_insert_generic_uint16(void)
192{
193 struct pldm_msgbuf _ctx;
194 struct pldm_msgbuf* ctx = &_ctx;
195 uint16_t src = 0xf344;
196 uint16_t checkVal = 0;
197 uint8_t buf[sizeof(uint16_t)] = {0};
198
199 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
200 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
201
202 struct pldm_msgbuf _ctxExtract;
203 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
204
205 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930206 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700207
208 expect(src == checkVal);
209 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
210 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
211}
212
213static void test_msgbuf_insert_generic_int16(void)
214{
215 struct pldm_msgbuf _ctx;
216 struct pldm_msgbuf* ctx = &_ctx;
217 int16_t src = -12;
218 int16_t checkVal = 0;
219 uint8_t buf[sizeof(int16_t)] = {0};
220
221 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
222 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
223
224 struct pldm_msgbuf _ctxExtract;
225 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
226
227 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930228 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700229
230 expect(src == checkVal);
231 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
232 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
233}
234
235static void test_msgbuf_insert_generic_uint8(void)
236{
237 struct pldm_msgbuf _ctx;
238 struct pldm_msgbuf* ctx = &_ctx;
239 uint8_t src = 0xf4;
240 uint8_t checkVal = 0;
241 uint8_t buf[sizeof(uint8_t)] = {0};
242
243 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
244 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
245
246 struct pldm_msgbuf _ctxExtract;
247 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
248
249 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930250 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700251
252 expect(src == checkVal);
253 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
254 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
255}
256
257static void test_msgbuf_insert_generic_int8(void)
258{
259 struct pldm_msgbuf _ctx;
260 struct pldm_msgbuf* ctx = &_ctx;
261 int8_t src = -4;
262 int8_t checkVal = 0;
263 uint8_t buf[sizeof(int8_t)] = {0};
264
265 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
266 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
267
268 struct pldm_msgbuf _ctxExtract;
269 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
270
271 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930272 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700273
274 expect(src == checkVal);
275 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
276 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
277}
278
279static void test_msgbuf_insert_array_generic_uint8(void)
280{
281 struct pldm_msgbuf _ctx;
282 struct pldm_msgbuf* ctx = &_ctx;
283 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
284 uint8_t buf[6] = {0};
285 uint8_t retBuff[6] = {0};
286
287 expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
288 expect(pldm_msgbuf_insert_array(ctx, src, sizeof(src)) == PLDM_SUCCESS);
289
290 struct pldm_msgbuf _ctxExtract;
291 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
292
293 expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
294 expect(pldm_msgbuf_extract_array(ctxExtract, retBuff, sizeof(retBuff)) ==
295 PLDM_SUCCESS);
296
297 expect(memcmp(src, retBuff, sizeof(retBuff)) == 0);
298 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
299 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
300}
301
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030302typedef void (*testfn)(void);
303
Andrew Jeffery369b1212023-04-20 15:44:48 +0930304static const testfn tests[] = {test_msgbuf_extract_generic_uint8,
305 test_msgbuf_extract_generic_int8,
306 test_msgbuf_extract_generic_uint16,
307 test_msgbuf_extract_generic_int16,
308 test_msgbuf_extract_generic_uint32,
309 test_msgbuf_extract_generic_int32,
310 test_msgbuf_extract_generic_real32,
311 test_msgbuf_extract_array_generic_uint8,
Thu Nguyen062c8762023-04-22 20:45:04 +0700312 test_msgbuf_insert_generic_uint8,
313 test_msgbuf_insert_generic_int8,
314 test_msgbuf_insert_generic_uint16,
315 test_msgbuf_insert_generic_int16,
316 test_msgbuf_insert_generic_uint32,
317 test_msgbuf_insert_generic_int32,
318 test_msgbuf_insert_array_generic_uint8,
Andrew Jeffery369b1212023-04-20 15:44:48 +0930319 NULL};
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030320
321int main(void)
322{
323 testfn const* testp = &tests[0];
324
325 while (*testp)
326 {
327 (*testp)();
328 testp++;
329 }
330
331 return 0;
332}