blob: 96b3bb587f1c9e136fc7490640e40c043bbf8e84 [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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093036 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103037 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093050 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103051 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093064 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103065 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093078 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103079 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +093092 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103093 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930106 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030107 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930126 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030127 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930140 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) ==
Andrew Jeffery369b1212023-04-20 15:44:48 +0930141 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
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930155 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700156 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
157
158 struct pldm_msgbuf _ctxExtract;
159 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
160
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930161 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
162 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930163 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700164
165 expect(src == checkVal);
166 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
167 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
168}
169
170static void test_msgbuf_insert_generic_uint32(void)
171{
172 struct pldm_msgbuf _ctx;
173 struct pldm_msgbuf* ctx = &_ctx;
174 uint32_t src = 0xf1223344;
175 uint32_t checkVal = 0;
176 uint8_t buf[sizeof(uint32_t)] = {0};
177
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930178 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700179 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
180
181 struct pldm_msgbuf _ctxExtract;
182 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
183
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930184 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
185 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930186 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700187
188 expect(src == checkVal);
189 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
190 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
191}
192
193static void test_msgbuf_insert_generic_uint16(void)
194{
195 struct pldm_msgbuf _ctx;
196 struct pldm_msgbuf* ctx = &_ctx;
197 uint16_t src = 0xf344;
198 uint16_t checkVal = 0;
199 uint8_t buf[sizeof(uint16_t)] = {0};
200
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930201 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700202 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
203
204 struct pldm_msgbuf _ctxExtract;
205 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
206
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930207 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
208 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930209 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700210
211 expect(src == checkVal);
212 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
213 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
214}
215
216static void test_msgbuf_insert_generic_int16(void)
217{
218 struct pldm_msgbuf _ctx;
219 struct pldm_msgbuf* ctx = &_ctx;
220 int16_t src = -12;
221 int16_t checkVal = 0;
222 uint8_t buf[sizeof(int16_t)] = {0};
223
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930224 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700225 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
226
227 struct pldm_msgbuf _ctxExtract;
228 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
229
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930230 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
231 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930232 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700233
234 expect(src == checkVal);
235 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
236 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
237}
238
239static void test_msgbuf_insert_generic_uint8(void)
240{
241 struct pldm_msgbuf _ctx;
242 struct pldm_msgbuf* ctx = &_ctx;
243 uint8_t src = 0xf4;
244 uint8_t checkVal = 0;
245 uint8_t buf[sizeof(uint8_t)] = {0};
246
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930247 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700248 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
249
250 struct pldm_msgbuf _ctxExtract;
251 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
252
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930253 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
254 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930255 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700256
257 expect(src == checkVal);
258 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
259 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
260}
261
262static void test_msgbuf_insert_generic_int8(void)
263{
264 struct pldm_msgbuf _ctx;
265 struct pldm_msgbuf* ctx = &_ctx;
266 int8_t src = -4;
267 int8_t checkVal = 0;
268 uint8_t buf[sizeof(int8_t)] = {0};
269
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930270 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700271 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
272
273 struct pldm_msgbuf _ctxExtract;
274 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
275
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930276 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
277 PLDM_SUCCESS);
Andrew Jeffery66c77232024-04-24 11:42:02 +0930278 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700279
280 expect(src == checkVal);
281 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
282 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
283}
284
285static void test_msgbuf_insert_array_generic_uint8(void)
286{
287 struct pldm_msgbuf _ctx;
288 struct pldm_msgbuf* ctx = &_ctx;
289 uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
290 uint8_t buf[6] = {0};
291 uint8_t retBuff[6] = {0};
292
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930293 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700294 expect(pldm_msgbuf_insert_array(ctx, src, sizeof(src)) == PLDM_SUCCESS);
295
296 struct pldm_msgbuf _ctxExtract;
297 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
298
Andrew Jefferyc8df31c2024-05-21 16:47:43 +0930299 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) ==
300 PLDM_SUCCESS);
Thu Nguyen062c8762023-04-22 20:45:04 +0700301 expect(pldm_msgbuf_extract_array(ctxExtract, retBuff, sizeof(retBuff)) ==
302 PLDM_SUCCESS);
303
304 expect(memcmp(src, retBuff, sizeof(retBuff)) == 0);
305 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
306 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
307}
308
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030309typedef void (*testfn)(void);
310
Andrew Jeffery369b1212023-04-20 15:44:48 +0930311static const testfn tests[] = {test_msgbuf_extract_generic_uint8,
312 test_msgbuf_extract_generic_int8,
313 test_msgbuf_extract_generic_uint16,
314 test_msgbuf_extract_generic_int16,
315 test_msgbuf_extract_generic_uint32,
316 test_msgbuf_extract_generic_int32,
317 test_msgbuf_extract_generic_real32,
318 test_msgbuf_extract_array_generic_uint8,
Thu Nguyen062c8762023-04-22 20:45:04 +0700319 test_msgbuf_insert_generic_uint8,
320 test_msgbuf_insert_generic_int8,
321 test_msgbuf_insert_generic_uint16,
322 test_msgbuf_insert_generic_int16,
323 test_msgbuf_insert_generic_uint32,
324 test_msgbuf_insert_generic_int32,
325 test_msgbuf_insert_array_generic_uint8,
Andrew Jeffery369b1212023-04-20 15:44:48 +0930326 NULL};
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030327
328int main(void)
329{
330 testfn const* testp = &tests[0];
331
332 while (*testp)
333 {
334 (*testp)();
335 testp++;
336 }
337
338 return 0;
339}