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 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_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 37 PLDM_SUCCESS); 38 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 39 expect(val == 0xa5); 40 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 41 } 42 43 static 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 50 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 51 PLDM_SUCCESS); 52 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 53 expect(val == -1); 54 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 55 } 56 57 static 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 64 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 65 PLDM_SUCCESS); 66 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 67 expect(val == 0x5aa5); 68 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 69 } 70 71 static 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 78 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 79 PLDM_SUCCESS); 80 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 81 expect(val == INT16_MIN); 82 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 83 } 84 85 static 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 92 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 93 PLDM_SUCCESS); 94 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 95 expect(val == 0x5a00ffa5); 96 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 97 } 98 99 static 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 106 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 107 PLDM_SUCCESS); 108 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 109 expect(val == INT32_MIN); 110 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 111 } 112 113 static 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 126 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 127 PLDM_SUCCESS); 128 expect(pldm_msgbuf_extract(ctx, val) == PLDM_SUCCESS); 129 expect(val == FLT_MAX); 130 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 131 } 132 133 static 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 140 expect(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)) == 141 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 147 static 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 155 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS); 156 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 157 158 struct pldm_msgbuf _ctxExtract; 159 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 160 161 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 162 PLDM_SUCCESS); 163 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 164 165 expect(src == checkVal); 166 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 167 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 168 } 169 170 static 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 178 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS); 179 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 180 181 struct pldm_msgbuf _ctxExtract; 182 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 183 184 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 185 PLDM_SUCCESS); 186 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 187 188 expect(src == checkVal); 189 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 190 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 191 } 192 193 static 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 201 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS); 202 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 203 204 struct pldm_msgbuf _ctxExtract; 205 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 206 207 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 208 PLDM_SUCCESS); 209 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 210 211 expect(src == checkVal); 212 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 213 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 214 } 215 216 static 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 224 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS); 225 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 226 227 struct pldm_msgbuf _ctxExtract; 228 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 229 230 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 231 PLDM_SUCCESS); 232 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 233 234 expect(src == checkVal); 235 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 236 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 237 } 238 239 static 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 247 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS); 248 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 249 250 struct pldm_msgbuf _ctxExtract; 251 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 252 253 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 254 PLDM_SUCCESS); 255 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 256 257 expect(src == checkVal); 258 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 259 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 260 } 261 262 static 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 270 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS); 271 expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS); 272 273 struct pldm_msgbuf _ctxExtract; 274 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 275 276 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 277 PLDM_SUCCESS); 278 expect(pldm_msgbuf_extract(ctxExtract, checkVal) == PLDM_SUCCESS); 279 280 expect(src == checkVal); 281 expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS); 282 expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS); 283 } 284 285 static 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 293 expect(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS); 294 expect(pldm_msgbuf_insert_array(ctx, src, sizeof(src)) == PLDM_SUCCESS); 295 296 struct pldm_msgbuf _ctxExtract; 297 struct pldm_msgbuf* ctxExtract = &_ctxExtract; 298 299 expect(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)) == 300 PLDM_SUCCESS); 301 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 309 typedef void (*testfn)(void); 310 311 static 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, 319 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, 326 NULL}; 327 328 int main(void) 329 { 330 testfn const* testp = &tests[0]; 331 332 while (*testp) 333 { 334 (*testp)(); 335 testp++; 336 } 337 338 return 0; 339 } 340