blob: 4ae9a2fc75c50e833297fb24cff04353f11aa43e [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 Jeffery830c1eb2024-10-04 10:48:10 +093036 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
37 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103038 expect(val == 0xa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093039 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103040}
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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093049 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
50 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103051 expect(val == -1);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093052 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103053}
54
55static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093062 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
63 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103064 expect(val == 0x5aa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093065 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103066}
67
68static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093075 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
76 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103077 expect(val == INT16_MIN);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093078 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103079}
80
81static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093088 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
89 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103090 expect(val == 0x5a00ffa5);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +093091 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +103092}
93
94static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930101 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
102 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030103 expect(val == INT32_MIN);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930104 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030105}
106
107static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930120 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
121 expect(pldm_msgbuf_extract(ctx, val) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030122 expect(val == FLT_MAX);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930123 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030124}
125
Andrew Jeffery369b1212023-04-20 15:44:48 +0930126static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930133 expect(pldm_msgbuf_init_errno(ctx, sizeof(buf), buf, sizeof(buf)) == 0);
134 expect(pldm_msgbuf_extract_array(ctx, 1, arr, 1) == 0);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930135 expect(arr[0] == 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930136 expect(pldm_msgbuf_destroy(ctx) == 0);
Andrew Jeffery369b1212023-04-20 15:44:48 +0930137}
138
Thu Nguyen062c8762023-04-22 20:45:04 +0700139static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930147 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
148 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700149
150 struct pldm_msgbuf _ctxExtract;
151 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
152
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930153 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
154 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700155
156 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930157 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
158 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700159}
160
161static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930169 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
170 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700171
172 struct pldm_msgbuf _ctxExtract;
173 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
174
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930175 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
176 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700177
178 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930179 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
180 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700181}
182
183static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930191 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
192 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700193
194 struct pldm_msgbuf _ctxExtract;
195 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
196
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930197 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
198 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700199
200 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930201 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
202 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700203}
204
205static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930213 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(uint16_t)) == 0);
214 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700215
216 struct pldm_msgbuf _ctxExtract;
217 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
218
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930219 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
220 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700221
222 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930223 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
224 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700225}
226
227static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930235 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
236 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700237
238 struct pldm_msgbuf _ctxExtract;
239 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
240
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930241 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
242 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700243
244 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930245 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
246 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700247}
248
249static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930257 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
258 expect(pldm_msgbuf_insert(ctx, src) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700259
260 struct pldm_msgbuf _ctxExtract;
261 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
262
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930263 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
264 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700265
266 expect(src == checkVal);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930267 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
268 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700269}
270
271static 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
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930279 expect(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)) == 0);
280 expect(pldm_msgbuf_insert_array(ctx, sizeof(src), src, sizeof(src)) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700281
282 struct pldm_msgbuf _ctxExtract;
283 struct pldm_msgbuf* ctxExtract = &_ctxExtract;
284
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930285 expect(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)) == 0);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000286 expect(pldm_msgbuf_extract_array(ctxExtract, sizeof(retBuff), retBuff,
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930287 sizeof(retBuff)) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700288
289 expect(memcmp(src, retBuff, sizeof(retBuff)) == 0);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +0930290 expect(pldm_msgbuf_destroy(ctxExtract) == 0);
291 expect(pldm_msgbuf_destroy(ctx) == 0);
Thu Nguyen062c8762023-04-22 20:45:04 +0700292}
293
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030294typedef void (*testfn)(void);
295
Andrew Jeffery369b1212023-04-20 15:44:48 +0930296static 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,
Thu Nguyen062c8762023-04-22 20:45:04 +0700304 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,
Andrew Jeffery369b1212023-04-20 15:44:48 +0930311 NULL};
Andrew Jefferyc63f63a2023-02-24 22:29:33 +1030312
313int main(void)
314{
315 testfn const* testp = &tests[0];
316
317 while (*testp)
318 {
319 (*testp)();
320 testp++;
321 }
322
323 return 0;
324}