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