1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 /* 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
test_msgbuf_extract_generic_uint8(void)29 static 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_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
37 expect(pldm_msgbuf_extract(ctx, val) == 0);
38 expect(val == 0xa5);
39 expect(pldm_msgbuf_destroy(ctx) == 0);
40 }
41
test_msgbuf_extract_generic_int8(void)42 static 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_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
50 expect(pldm_msgbuf_extract(ctx, val) == 0);
51 expect(val == -1);
52 expect(pldm_msgbuf_destroy(ctx) == 0);
53 }
54
test_msgbuf_extract_generic_uint16(void)55 static void test_msgbuf_extract_generic_uint16(void)
56 {
57 struct pldm_msgbuf _ctx;
58 struct pldm_msgbuf* ctx = &_ctx;
59 uint16_t buf[1] = {0x5aa5};
60 uint16_t val;
61
62 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
63 expect(pldm_msgbuf_extract(ctx, val) == 0);
64 expect(val == 0x5aa5);
65 expect(pldm_msgbuf_destroy(ctx) == 0);
66 }
67
test_msgbuf_extract_generic_int16(void)68 static void test_msgbuf_extract_generic_int16(void)
69 {
70 struct pldm_msgbuf _ctx;
71 struct pldm_msgbuf* ctx = &_ctx;
72 int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))};
73 int16_t val;
74
75 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
76 expect(pldm_msgbuf_extract(ctx, val) == 0);
77 expect(val == INT16_MIN);
78 expect(pldm_msgbuf_destroy(ctx) == 0);
79 }
80
test_msgbuf_extract_generic_uint32(void)81 static void test_msgbuf_extract_generic_uint32(void)
82 {
83 struct pldm_msgbuf _ctx;
84 struct pldm_msgbuf* ctx = &_ctx;
85 uint32_t buf[1] = {0x5a00ffa5};
86 uint32_t val;
87
88 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
89 expect(pldm_msgbuf_extract(ctx, val) == 0);
90 expect(val == 0x5a00ffa5);
91 expect(pldm_msgbuf_destroy(ctx) == 0);
92 }
93
test_msgbuf_extract_generic_int32(void)94 static void test_msgbuf_extract_generic_int32(void)
95 {
96 struct pldm_msgbuf _ctx;
97 struct pldm_msgbuf* ctx = &_ctx;
98 int32_t buf[1] = {(int32_t)(htole32((uint32_t)INT32_MIN))};
99 int32_t val;
100
101 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
102 expect(pldm_msgbuf_extract(ctx, val) == 0);
103 expect(val == INT32_MIN);
104 expect(pldm_msgbuf_destroy(ctx) == 0);
105 }
106
test_msgbuf_extract_generic_real32(void)107 static void test_msgbuf_extract_generic_real32(void)
108 {
109 struct pldm_msgbuf _ctx;
110 struct pldm_msgbuf* ctx = &_ctx;
111 uint32_t buf[1];
112 uint32_t xform;
113 real32_t val;
114
115 val = FLT_MAX;
116 memcpy(&xform, &val, sizeof(val));
117 buf[0] = htole32(xform);
118 val = 0;
119
120 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
121 expect(pldm_msgbuf_extract(ctx, val) == 0);
122 expect(val == FLT_MAX);
123 expect(pldm_msgbuf_destroy(ctx) == 0);
124 }
125
test_msgbuf_extract_array_generic_uint8(void)126 static void test_msgbuf_extract_array_generic_uint8(void)
127 {
128 struct pldm_msgbuf _ctx;
129 struct pldm_msgbuf* ctx = &_ctx;
130 uint32_t buf[1] = {0};
131 uint8_t arr[1];
132
133 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
134 expect(pldm_msgbuf_extract_array(ctx, 1, arr, 1) == 0);
135 expect(arr[0] == 0);
136 expect(pldm_msgbuf_destroy(ctx) == 0);
137 }
138
test_msgbuf_insert_generic_int32(void)139 static void test_msgbuf_insert_generic_int32(void)
140 {
141 struct pldm_msgbuf _ctx;
142 struct pldm_msgbuf* ctx = &_ctx;
143 int32_t src = -12345;
144 int32_t checkVal = 0;
145 uint8_t buf[sizeof(int32_t)] = {0};
146
147 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
148 expect(pldm_msgbuf_insert(ctx, src) == 0);
149
150 struct pldm_msgbuf _ctxExtract;
151 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
152
153 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
154 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
155
156 expect(src == checkVal);
157 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
158 expect(pldm_msgbuf_destroy(ctx) == 0);
159 }
160
test_msgbuf_insert_generic_uint32(void)161 static void test_msgbuf_insert_generic_uint32(void)
162 {
163 struct pldm_msgbuf _ctx;
164 struct pldm_msgbuf* ctx = &_ctx;
165 uint32_t src = 0xf1223344;
166 uint32_t checkVal = 0;
167 uint8_t buf[sizeof(uint32_t)] = {0};
168
169 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
170 expect(pldm_msgbuf_insert(ctx, src) == 0);
171
172 struct pldm_msgbuf _ctxExtract;
173 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
174
175 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
176 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
177
178 expect(src == checkVal);
179 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
180 expect(pldm_msgbuf_destroy(ctx) == 0);
181 }
182
test_msgbuf_insert_generic_uint16(void)183 static void test_msgbuf_insert_generic_uint16(void)
184 {
185 struct pldm_msgbuf _ctx;
186 struct pldm_msgbuf* ctx = &_ctx;
187 uint16_t src = 0xf344;
188 uint16_t checkVal = 0;
189 uint8_t buf[sizeof(uint16_t)] = {0};
190
191 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
192 expect(pldm_msgbuf_insert(ctx, src) == 0);
193
194 struct pldm_msgbuf _ctxExtract;
195 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
196
197 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
198 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
199
200 expect(src == checkVal);
201 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
202 expect(pldm_msgbuf_destroy(ctx) == 0);
203 }
204
test_msgbuf_insert_generic_int16(void)205 static void test_msgbuf_insert_generic_int16(void)
206 {
207 struct pldm_msgbuf _ctx;
208 struct pldm_msgbuf* ctx = &_ctx;
209 int16_t src = -12;
210 int16_t checkVal = 0;
211 uint8_t buf[sizeof(int16_t)] = {0};
212
213 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
214 expect(pldm_msgbuf_insert(ctx, src) == 0);
215
216 struct pldm_msgbuf _ctxExtract;
217 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
218
219 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
220 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
221
222 expect(src == checkVal);
223 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
224 expect(pldm_msgbuf_destroy(ctx) == 0);
225 }
226
test_msgbuf_insert_generic_uint8(void)227 static void test_msgbuf_insert_generic_uint8(void)
228 {
229 struct pldm_msgbuf _ctx;
230 struct pldm_msgbuf* ctx = &_ctx;
231 uint8_t src = 0xf4;
232 uint8_t checkVal = 0;
233 uint8_t buf[sizeof(uint8_t)] = {0};
234
235 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
236 expect(pldm_msgbuf_insert(ctx, src) == 0);
237
238 struct pldm_msgbuf _ctxExtract;
239 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
240
241 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
242 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
243
244 expect(src == checkVal);
245 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
246 expect(pldm_msgbuf_destroy(ctx) == 0);
247 }
248
test_msgbuf_insert_generic_int8(void)249 static void test_msgbuf_insert_generic_int8(void)
250 {
251 struct pldm_msgbuf _ctx;
252 struct pldm_msgbuf* ctx = &_ctx;
253 int8_t src = -4;
254 int8_t checkVal = 0;
255 uint8_t buf[sizeof(int8_t)] = {0};
256
257 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
258 expect(pldm_msgbuf_insert(ctx, src) == 0);
259
260 struct pldm_msgbuf _ctxExtract;
261 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
262
263 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
264 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
265
266 expect(src == checkVal);
267 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
268 expect(pldm_msgbuf_destroy(ctx) == 0);
269 }
270
test_msgbuf_insert_array_generic_uint8(void)271 static void test_msgbuf_insert_array_generic_uint8(void)
272 {
273 struct pldm_msgbuf _ctx;
274 struct pldm_msgbuf* ctx = &_ctx;
275 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
276 uint8_t buf[6] = {0};
277 uint8_t retBuff[6] = {0};
278
279 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
280 expect(pldm_msgbuf_insert_array(ctx, sizeof(src), src, sizeof(src)) == 0);
281
282 struct pldm_msgbuf _ctxExtract;
283 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
284
285 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
286 expect(pldm_msgbuf_extract_array(ctxExtract, sizeof(retBuff), retBuff,
287 sizeof(retBuff)) == 0);
288
289 expect(memcmp(src, retBuff, sizeof(retBuff)) == 0);
290 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
291 expect(pldm_msgbuf_destroy(ctx) == 0);
292 }
293
294 typedef void (*testfn)(void);
295
296 static const testfn tests[] = {test_msgbuf_extract_generic_uint8,
297 test_msgbuf_extract_generic_int8,
298 test_msgbuf_extract_generic_uint16,
299 test_msgbuf_extract_generic_int16,
300 test_msgbuf_extract_generic_uint32,
301 test_msgbuf_extract_generic_int32,
302 test_msgbuf_extract_generic_real32,
303 test_msgbuf_extract_array_generic_uint8,
304 test_msgbuf_insert_generic_uint8,
305 test_msgbuf_insert_generic_int8,
306 test_msgbuf_insert_generic_uint16,
307 test_msgbuf_insert_generic_int16,
308 test_msgbuf_insert_generic_uint32,
309 test_msgbuf_insert_generic_int32,
310 test_msgbuf_insert_array_generic_uint8,
311 NULL};
312
main(void)313 int main(void)
314 {
315 testfn const* testp = &tests[0];
316
317 while (*testp)
318 {
319 (*testp)();
320 testp++;
321 }
322
323 return 0;
324 }
325