xref: /openbmc/libpldm/tests/msgbuf_generic.c (revision 36af84cd)
1 /* 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 
28 static 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 
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(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 
56 static 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 
70 static 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 
84 static 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 
98 static 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 
112 static 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 
132 typedef void (*testfn)(void);
133 
134 static const testfn tests[] = {
135     test_msgbuf_extract_generic_uint8,  test_msgbuf_extract_generic_int8,
136     test_msgbuf_extract_generic_uint16, test_msgbuf_extract_generic_int16,
137     test_msgbuf_extract_generic_uint32, test_msgbuf_extract_generic_int32,
138     test_msgbuf_extract_generic_real32, NULL};
139 
140 int main(void)
141 {
142     testfn const* testp = &tests[0];
143 
144     while (*testp)
145     {
146         (*testp)();
147         testp++;
148     }
149 
150     return 0;
151 }
152