Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1 | #include <endian.h> |
| 2 | |
| 3 | #include <cfloat> |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | /* We're exercising the implementation so disable the asserts for now */ |
| 8 | #ifndef NDEBUG |
| 9 | #define NDEBUG 1 |
| 10 | #endif |
| 11 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 12 | #include "msgbuf.h" |
| 13 | |
| 14 | TEST(msgbuf, init_bad_ctx) |
| 15 | { |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 16 | EXPECT_NE(pldm_msgbuf_init_cc(NULL, 0, NULL, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | TEST(msgbuf, init_bad_minsize) |
| 20 | { |
| 21 | struct pldm_msgbuf _ctx; |
| 22 | struct pldm_msgbuf* ctx = &_ctx; |
| 23 | uint8_t buf[1] = {}; |
| 24 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 25 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf) + 1U, buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 26 | PLDM_SUCCESS); |
| 27 | } |
| 28 | |
| 29 | TEST(msgbuf, init_bad_buf) |
| 30 | { |
| 31 | struct pldm_msgbuf _ctx; |
| 32 | struct pldm_msgbuf* ctx = &_ctx; |
| 33 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 34 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, NULL, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST(msgbuf, init_bad_len) |
| 38 | { |
| 39 | struct pldm_msgbuf _ctx; |
| 40 | struct pldm_msgbuf* ctx = &_ctx; |
| 41 | uint8_t buf[1] = {}; |
| 42 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 43 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX), |
| 44 | PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(msgbuf, init_overflow) |
| 48 | { |
| 49 | struct pldm_msgbuf _ctx; |
| 50 | struct pldm_msgbuf* ctx = &_ctx; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 51 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 52 | void* buf = (void*)UINTPTR_MAX; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 53 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 54 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST(msgbuf, init_success) |
| 58 | { |
| 59 | struct pldm_msgbuf _ctx; |
| 60 | struct pldm_msgbuf* ctx = &_ctx; |
| 61 | uint8_t buf[1] = {}; |
| 62 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 63 | EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 64 | PLDM_SUCCESS); |
| 65 | } |
| 66 | |
| 67 | TEST(msgbuf, destroy_none) |
| 68 | { |
| 69 | struct pldm_msgbuf _ctx; |
| 70 | struct pldm_msgbuf* ctx = &_ctx; |
| 71 | uint8_t buf[1] = {}; |
| 72 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 73 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 74 | PLDM_SUCCESS); |
| 75 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 76 | } |
| 77 | |
| 78 | TEST(msgbuf, destroy_exact) |
| 79 | { |
| 80 | struct pldm_msgbuf _ctx; |
| 81 | struct pldm_msgbuf* ctx = &_ctx; |
| 82 | uint8_t buf[1] = {0xa5}; |
| 83 | uint8_t val; |
| 84 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 85 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 86 | PLDM_SUCCESS); |
| 87 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 88 | EXPECT_EQ(val, 0xa5); |
| 89 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 90 | } |
| 91 | |
| 92 | TEST(msgbuf, destroy_over) |
| 93 | { |
| 94 | struct pldm_msgbuf _ctx; |
| 95 | struct pldm_msgbuf* ctx = &_ctx; |
| 96 | uint8_t buf[1] = {0xa5}; |
| 97 | uint8_t val; |
| 98 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 99 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 100 | PLDM_SUCCESS); |
| 101 | ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 102 | ASSERT_EQ(val, 0xa5); |
| 103 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 104 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 105 | } |
| 106 | |
| 107 | TEST(msgbuf, destroy_under) |
| 108 | { |
| 109 | struct pldm_msgbuf _ctx; |
| 110 | struct pldm_msgbuf* ctx = &_ctx; |
| 111 | uint8_t buf[2] = {0x5a, 0xa5}; |
| 112 | uint8_t val; |
| 113 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 114 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 115 | PLDM_SUCCESS); |
| 116 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 117 | EXPECT_EQ(val, 0x5a); |
| 118 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 119 | } |
| 120 | |
| 121 | TEST(msgbuf, extract_one_uint8) |
| 122 | { |
| 123 | struct pldm_msgbuf _ctx; |
| 124 | struct pldm_msgbuf* ctx = &_ctx; |
| 125 | uint8_t buf[1] = {0xa5}; |
| 126 | uint8_t val; |
| 127 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 128 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 129 | PLDM_SUCCESS); |
| 130 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 131 | EXPECT_EQ(val, 0xa5); |
| 132 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 133 | } |
| 134 | |
| 135 | TEST(msgbuf, extract_over_uint8) |
| 136 | { |
| 137 | struct pldm_msgbuf _ctx; |
| 138 | struct pldm_msgbuf* ctx = &_ctx; |
| 139 | uint8_t buf[1] = {}; |
| 140 | uint8_t val; |
| 141 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 142 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 143 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 144 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 145 | } |
| 146 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 147 | TEST(msgbuf, extract_under_uint8) |
| 148 | { |
| 149 | struct pldm_msgbuf _ctx; |
| 150 | struct pldm_msgbuf* ctx = &_ctx; |
| 151 | |
| 152 | uint8_t buf[1] = {}; |
| 153 | uint8_t val; |
| 154 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 155 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 156 | ctx->remaining = INTMAX_MIN; |
| 157 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 158 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 159 | } |
| 160 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 161 | TEST(msgbuf, extract_one_int8) |
| 162 | { |
| 163 | struct pldm_msgbuf _ctx; |
| 164 | struct pldm_msgbuf* ctx = &_ctx; |
| 165 | int8_t buf[1] = {-1}; |
| 166 | int8_t val; |
| 167 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 168 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 169 | PLDM_SUCCESS); |
| 170 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 171 | EXPECT_EQ(val, -1); |
| 172 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 173 | } |
| 174 | |
| 175 | TEST(msgbuf, extract_over_int8) |
| 176 | { |
| 177 | struct pldm_msgbuf _ctx; |
| 178 | struct pldm_msgbuf* ctx = &_ctx; |
| 179 | int8_t buf[1] = {}; |
| 180 | int8_t val; |
| 181 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 182 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 183 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 184 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 185 | } |
| 186 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 187 | TEST(msgbuf, extract_under_int8) |
| 188 | { |
| 189 | struct pldm_msgbuf _ctx; |
| 190 | struct pldm_msgbuf* ctx = &_ctx; |
| 191 | |
| 192 | uint8_t buf[1] = {}; |
| 193 | int8_t val; |
| 194 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 195 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 196 | ctx->remaining = INTMAX_MIN; |
| 197 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 198 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 199 | } |
| 200 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 201 | TEST(msgbuf, extract_one_uint16) |
| 202 | { |
| 203 | struct pldm_msgbuf _ctx; |
| 204 | struct pldm_msgbuf* ctx = &_ctx; |
| 205 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 206 | uint16_t val = {}; |
| 207 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 208 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 209 | PLDM_SUCCESS); |
| 210 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 211 | EXPECT_EQ(val, 0x5aa5); |
| 212 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 213 | } |
| 214 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 215 | TEST(msgbuf, extract_under_uint16) |
| 216 | { |
| 217 | struct pldm_msgbuf _ctx; |
| 218 | struct pldm_msgbuf* ctx = &_ctx; |
| 219 | |
| 220 | uint16_t buf[1] = {}; |
| 221 | uint16_t val; |
| 222 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 223 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 224 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 225 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 226 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 227 | } |
| 228 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 229 | TEST(msgbuf, extract_over_uint16) |
| 230 | { |
| 231 | struct pldm_msgbuf _ctx; |
| 232 | struct pldm_msgbuf* ctx = &_ctx; |
| 233 | uint16_t buf[1] = {}; |
| 234 | uint16_t val; |
| 235 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 236 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 237 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 238 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 239 | } |
| 240 | |
| 241 | TEST(msgbuf, extract_one_int16) |
| 242 | { |
| 243 | struct pldm_msgbuf _ctx; |
| 244 | struct pldm_msgbuf* ctx = &_ctx; |
| 245 | int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))}; |
| 246 | int16_t val; |
| 247 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 248 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 249 | PLDM_SUCCESS); |
| 250 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 251 | EXPECT_EQ(val, INT16_MIN); |
| 252 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 253 | } |
| 254 | |
| 255 | TEST(msgbuf, extract_over_int16) |
| 256 | { |
| 257 | struct pldm_msgbuf _ctx; |
| 258 | struct pldm_msgbuf* ctx = &_ctx; |
| 259 | int16_t buf[1] = {}; |
| 260 | int16_t val; |
| 261 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 262 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 263 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 264 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 265 | } |
| 266 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 267 | TEST(msgbuf, extract_under_int16) |
| 268 | { |
| 269 | struct pldm_msgbuf _ctx; |
| 270 | struct pldm_msgbuf* ctx = &_ctx; |
| 271 | |
| 272 | int16_t buf[1] = {}; |
| 273 | int16_t val; |
| 274 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 275 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 276 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 277 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 278 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 279 | } |
| 280 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 281 | TEST(msgbuf, extract_one_uint32) |
| 282 | { |
| 283 | struct pldm_msgbuf _ctx; |
| 284 | struct pldm_msgbuf* ctx = &_ctx; |
| 285 | uint32_t buf[1] = {htole32(0x5a00ffa5)}; |
| 286 | uint32_t val; |
| 287 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 288 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 289 | PLDM_SUCCESS); |
| 290 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 291 | EXPECT_EQ(val, 0x5a00ffa5); |
| 292 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 293 | } |
| 294 | |
| 295 | TEST(msgbuf, extract_over_uint32) |
| 296 | { |
| 297 | struct pldm_msgbuf _ctx; |
| 298 | struct pldm_msgbuf* ctx = &_ctx; |
| 299 | uint32_t buf[1] = {}; |
| 300 | uint32_t val; |
| 301 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 302 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 303 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 304 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 305 | } |
| 306 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 307 | TEST(msgbuf, extract_under_uint32) |
| 308 | { |
| 309 | struct pldm_msgbuf _ctx; |
| 310 | struct pldm_msgbuf* ctx = &_ctx; |
| 311 | |
| 312 | uint32_t buf[1] = {}; |
| 313 | uint32_t val; |
| 314 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 315 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 316 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 317 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 318 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 319 | } |
| 320 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 321 | TEST(msgbuf, extract_one_int32) |
| 322 | { |
| 323 | struct pldm_msgbuf _ctx; |
| 324 | struct pldm_msgbuf* ctx = &_ctx; |
| 325 | int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))}; |
| 326 | int32_t val; |
| 327 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 328 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 329 | PLDM_SUCCESS); |
| 330 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 331 | EXPECT_EQ(val, INT32_MIN); |
| 332 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 333 | } |
| 334 | |
| 335 | TEST(msgbuf, extract_over_int32) |
| 336 | { |
| 337 | struct pldm_msgbuf _ctx; |
| 338 | struct pldm_msgbuf* ctx = &_ctx; |
| 339 | int32_t buf[1] = {}; |
| 340 | int32_t val; |
| 341 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 342 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 343 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 344 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 345 | } |
| 346 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 347 | TEST(msgbuf, extract_under_int32) |
| 348 | { |
| 349 | struct pldm_msgbuf _ctx; |
| 350 | struct pldm_msgbuf* ctx = &_ctx; |
| 351 | |
| 352 | int32_t buf[1] = {}; |
| 353 | int32_t val; |
| 354 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 355 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 356 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 357 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 358 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 359 | } |
| 360 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 361 | TEST(msgbuf, extract_one_real32) |
| 362 | { |
| 363 | struct pldm_msgbuf _ctx; |
| 364 | struct pldm_msgbuf* ctx = &_ctx; |
| 365 | uint32_t buf[1] = {}; |
| 366 | uint32_t xform; |
| 367 | real32_t val; |
| 368 | |
| 369 | val = FLT_MAX; |
| 370 | memcpy(&xform, &val, sizeof(val)); |
| 371 | buf[0] = htole32(xform); |
| 372 | val = 0; |
| 373 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 374 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 375 | PLDM_SUCCESS); |
| 376 | EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 377 | EXPECT_EQ(val, FLT_MAX); |
| 378 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 379 | } |
| 380 | |
| 381 | TEST(msgbuf, extract_over_real32) |
| 382 | { |
| 383 | struct pldm_msgbuf _ctx; |
| 384 | struct pldm_msgbuf* ctx = &_ctx; |
| 385 | real32_t buf[1] = {}; |
| 386 | real32_t val; |
| 387 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 388 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 389 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 390 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 391 | } |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 392 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 393 | TEST(msgbuf, extract_under_real32) |
| 394 | { |
| 395 | struct pldm_msgbuf _ctx; |
| 396 | struct pldm_msgbuf* ctx = &_ctx; |
| 397 | |
| 398 | real32_t buf[1] = {}; |
| 399 | real32_t val; |
| 400 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 401 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 402 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 403 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 404 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 405 | } |
| 406 | |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 407 | TEST(msgbuf, extract_array_uint8_buf0_req0) |
| 408 | { |
| 409 | struct pldm_msgbuf _ctx; |
| 410 | struct pldm_msgbuf* ctx = &_ctx; |
| 411 | uint8_t buf[1] = {}; |
| 412 | uint8_t arr[1]; |
| 413 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 414 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 415 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, 0), PLDM_SUCCESS); |
| 416 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 417 | } |
| 418 | |
| 419 | TEST(msgbuf, extract_array_uint8_buf1_req1) |
| 420 | { |
| 421 | struct pldm_msgbuf _ctx; |
| 422 | struct pldm_msgbuf* ctx = &_ctx; |
| 423 | uint8_t buf[1] = {}; |
| 424 | uint8_t arr[1]; |
| 425 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 426 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 427 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)), |
| 428 | PLDM_SUCCESS); |
| 429 | EXPECT_EQ(arr[0], 0); |
| 430 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 431 | } |
| 432 | |
| 433 | TEST(msgbuf, extract_array_uint8_buf1_req2) |
| 434 | { |
| 435 | struct pldm_msgbuf _ctx; |
| 436 | struct pldm_msgbuf* ctx = &_ctx; |
| 437 | uint8_t buf[1] = {}; |
| 438 | uint8_t arr[2]; |
| 439 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 440 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 441 | EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, arr, sizeof(arr)), |
| 442 | PLDM_SUCCESS); |
| 443 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 444 | } |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 445 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 446 | TEST(msgbuf, extract_under_array_uint8) |
| 447 | { |
| 448 | struct pldm_msgbuf _ctx; |
| 449 | struct pldm_msgbuf* ctx = &_ctx; |
| 450 | uint8_t buf[1] = {}; |
| 451 | uint8_t arr[1]; |
| 452 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 453 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 454 | ctx->remaining = INTMAX_MIN; |
| 455 | EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, arr, 1), PLDM_SUCCESS); |
| 456 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 457 | } |
| 458 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 459 | TEST(msgbuf, consumed_under) |
| 460 | { |
| 461 | struct pldm_msgbuf _ctx; |
| 462 | struct pldm_msgbuf* ctx = &_ctx; |
| 463 | uint8_t buf[1] = {}; |
| 464 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 465 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 466 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 467 | } |
| 468 | |
| 469 | TEST(msgbuf, consumed_exact) |
| 470 | { |
| 471 | struct pldm_msgbuf _ctx; |
| 472 | struct pldm_msgbuf* ctx = &_ctx; |
| 473 | uint8_t buf[1] = {}; |
| 474 | uint8_t val; |
| 475 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 476 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 477 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 478 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS); |
| 479 | } |
| 480 | |
| 481 | TEST(msgbuf, consumed_over) |
| 482 | { |
| 483 | struct pldm_msgbuf _ctx; |
| 484 | struct pldm_msgbuf* ctx = &_ctx; |
| 485 | uint8_t buf[1] = {}; |
| 486 | uint8_t val[2]; |
| 487 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 488 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 489 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS); |
| 490 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS); |
| 491 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 492 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 493 | |
| 494 | TEST(msgbuf, pldm_msgbuf_insert_int32_good) |
| 495 | { |
| 496 | struct pldm_msgbuf _ctx; |
| 497 | struct pldm_msgbuf* ctx = &_ctx; |
| 498 | int32_t src = -12345; |
| 499 | int32_t checkVal = 0; |
| 500 | uint8_t buf[sizeof(int32_t)] = {}; |
| 501 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 502 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 503 | EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS); |
| 504 | |
| 505 | struct pldm_msgbuf _ctxExtract; |
| 506 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 507 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 508 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 509 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 510 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 511 | |
| 512 | EXPECT_EQ(src, checkVal); |
| 513 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 514 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 515 | } |
| 516 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 517 | TEST(msgbuf, insert_under_int32) |
| 518 | { |
| 519 | struct pldm_msgbuf _ctx; |
| 520 | struct pldm_msgbuf* ctx = &_ctx; |
| 521 | |
| 522 | int32_t buf[1] = {}; |
| 523 | int32_t val = 0; |
| 524 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 525 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 526 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 527 | EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS); |
| 528 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 529 | } |
| 530 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 531 | TEST(msgbuf, pldm_msgbuf_insert_uint32_good) |
| 532 | { |
| 533 | struct pldm_msgbuf _ctx; |
| 534 | struct pldm_msgbuf* ctx = &_ctx; |
| 535 | uint32_t src = 0xf1223344; |
| 536 | uint32_t checkVal = 0; |
| 537 | uint8_t buf[sizeof(uint32_t)] = {}; |
| 538 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 539 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 540 | EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS); |
| 541 | |
| 542 | struct pldm_msgbuf _ctxExtract; |
| 543 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 544 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 545 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 546 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 547 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 548 | |
| 549 | EXPECT_EQ(src, checkVal); |
| 550 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 551 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 552 | } |
| 553 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 554 | TEST(msgbuf, insert_under_uint32) |
| 555 | { |
| 556 | struct pldm_msgbuf _ctx; |
| 557 | struct pldm_msgbuf* ctx = &_ctx; |
| 558 | |
| 559 | uint32_t buf[1] = {}; |
| 560 | uint32_t val = 0; |
| 561 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 562 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 563 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 564 | EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS); |
| 565 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 566 | } |
| 567 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 568 | TEST(msgbuf, pldm_msgbuf_insert_uint16_good) |
| 569 | { |
| 570 | struct pldm_msgbuf _ctx; |
| 571 | struct pldm_msgbuf* ctx = &_ctx; |
| 572 | uint16_t src = 0xf344; |
| 573 | uint16_t checkVal = 0; |
| 574 | uint8_t buf[sizeof(uint16_t)] = {}; |
| 575 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 576 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 577 | EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS); |
| 578 | |
| 579 | struct pldm_msgbuf _ctxExtract; |
| 580 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 581 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 582 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 583 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 584 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 585 | |
| 586 | EXPECT_EQ(src, checkVal); |
| 587 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 588 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 589 | } |
| 590 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 591 | TEST(msgbuf, insert_under_uint16) |
| 592 | { |
| 593 | struct pldm_msgbuf _ctx; |
| 594 | struct pldm_msgbuf* ctx = &_ctx; |
| 595 | |
| 596 | uint16_t buf[1] = {}; |
| 597 | uint16_t val = 0; |
| 598 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 599 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 600 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 601 | EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS); |
| 602 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 603 | } |
| 604 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 605 | TEST(msgbuf, pldm_msgbuf_insert_int16_good) |
| 606 | { |
| 607 | struct pldm_msgbuf _ctx; |
| 608 | struct pldm_msgbuf* ctx = &_ctx; |
| 609 | int16_t src = -12; |
| 610 | int16_t checkVal = 0; |
| 611 | uint8_t buf[sizeof(int16_t)] = {}; |
| 612 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 613 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 614 | EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS); |
| 615 | |
| 616 | struct pldm_msgbuf _ctxExtract; |
| 617 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 618 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 619 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 620 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 621 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 622 | |
| 623 | EXPECT_EQ(src, checkVal); |
| 624 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 625 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 626 | } |
| 627 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 628 | TEST(msgbuf, insert_under_int16) |
| 629 | { |
| 630 | struct pldm_msgbuf _ctx; |
| 631 | struct pldm_msgbuf* ctx = &_ctx; |
| 632 | |
| 633 | int16_t buf[1] = {}; |
| 634 | int16_t val = 0; |
| 635 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 636 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 637 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 638 | EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS); |
| 639 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 640 | } |
| 641 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 642 | TEST(msgbuf, pldm_msgbuf_insert_uint8_good) |
| 643 | { |
| 644 | struct pldm_msgbuf _ctx; |
| 645 | struct pldm_msgbuf* ctx = &_ctx; |
| 646 | uint8_t src = 0xf4; |
| 647 | uint8_t checkVal = 0; |
| 648 | uint8_t buf[sizeof(uint8_t)] = {}; |
| 649 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 650 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 651 | EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS); |
| 652 | |
| 653 | struct pldm_msgbuf _ctxExtract; |
| 654 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 655 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 656 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 657 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 658 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 659 | |
| 660 | EXPECT_EQ(src, checkVal); |
| 661 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 662 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 663 | } |
| 664 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 665 | TEST(msgbuf, insert_under_uint8) |
| 666 | { |
| 667 | struct pldm_msgbuf _ctx; |
| 668 | struct pldm_msgbuf* ctx = &_ctx; |
| 669 | |
| 670 | uint8_t buf[1] = {}; |
| 671 | uint8_t val = 0; |
| 672 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 673 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 674 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 675 | EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS); |
| 676 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 677 | } |
| 678 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 679 | TEST(msgbuf, pldm_msgbuf_insert_int8_good) |
| 680 | { |
| 681 | struct pldm_msgbuf _ctx; |
| 682 | struct pldm_msgbuf* ctx = &_ctx; |
| 683 | int8_t src = -4; |
| 684 | int8_t checkVal = 0; |
| 685 | uint8_t buf[sizeof(int8_t)] = {}; |
| 686 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 687 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 688 | EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS); |
| 689 | |
| 690 | struct pldm_msgbuf _ctxExtract; |
| 691 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 692 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 693 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 694 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 695 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 696 | |
| 697 | EXPECT_EQ(src, checkVal); |
| 698 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 699 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 700 | } |
| 701 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 702 | TEST(msgbuf, insert_under_int8) |
| 703 | { |
| 704 | struct pldm_msgbuf _ctx; |
| 705 | struct pldm_msgbuf* ctx = &_ctx; |
| 706 | |
| 707 | int8_t buf[1] = {}; |
| 708 | int8_t val = 0; |
| 709 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 710 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 711 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 712 | EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS); |
| 713 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 714 | } |
| 715 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 716 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good) |
| 717 | { |
| 718 | struct pldm_msgbuf _ctx; |
| 719 | struct pldm_msgbuf* ctx = &_ctx; |
| 720 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 721 | uint8_t buf[6] = {}; |
| 722 | uint8_t retBuff[6] = {}; |
| 723 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 724 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 725 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 726 | PLDM_SUCCESS); |
| 727 | |
| 728 | struct pldm_msgbuf _ctxExtract; |
| 729 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 730 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 731 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 732 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 733 | EXPECT_EQ( |
| 734 | pldm_msgbuf_extract_array_uint8(ctxExtract, retBuff, sizeof(retBuff)), |
| 735 | PLDM_SUCCESS); |
| 736 | |
| 737 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 738 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 739 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 740 | } |
| 741 | |
| 742 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad) |
| 743 | { |
| 744 | struct pldm_msgbuf _ctx; |
| 745 | struct pldm_msgbuf* ctx = &_ctx; |
| 746 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 747 | uint8_t buf[6] = {}; |
| 748 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 749 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 750 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, NULL, sizeof(src)), |
| 751 | PLDM_ERROR_INVALID_DATA); |
| 752 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 753 | } |
| 754 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 755 | TEST(msgbuf, insert_under_array_uint8) |
| 756 | { |
| 757 | struct pldm_msgbuf _ctx; |
| 758 | struct pldm_msgbuf* ctx = &_ctx; |
| 759 | |
| 760 | uint8_t buf[1] = {}; |
| 761 | uint8_t val[1] = {0}; |
| 762 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 763 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 764 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 765 | EXPECT_NE(pldm_msgbuf_insert_array_uint8(ctx, val, sizeof(val)), |
| 766 | PLDM_SUCCESS); |
| 767 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 768 | } |
| 769 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 770 | TEST(msgbuf, pldm_msgbuf_span_required_good) |
| 771 | { |
| 772 | struct pldm_msgbuf _ctx; |
| 773 | struct pldm_msgbuf* ctx = &_ctx; |
| 774 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 775 | uint8_t buf[6] = {0}; |
| 776 | const size_t required = 4; |
| 777 | uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77}; |
| 778 | uint16_t testVal; |
| 779 | uint8_t* retBuff = NULL; |
| 780 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 781 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 782 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 783 | PLDM_SUCCESS); |
| 784 | |
| 785 | struct pldm_msgbuf _ctxExtract; |
| 786 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 787 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 788 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 789 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 790 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 791 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff), |
| 792 | PLDM_SUCCESS); |
| 793 | |
| 794 | EXPECT_EQ(memcmp(expectData, retBuff, required), 0); |
| 795 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 796 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 797 | } |
| 798 | |
| 799 | TEST(msgbuf, pldm_msgbuf_span_required_bad) |
| 800 | { |
| 801 | struct pldm_msgbuf _ctx; |
| 802 | struct pldm_msgbuf* ctx = &_ctx; |
| 803 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 804 | uint8_t buf[6] = {0}; |
| 805 | const size_t required = 4; |
| 806 | uint16_t testVal; |
| 807 | [[maybe_unused]] uint8_t* retBuff = NULL; |
| 808 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 809 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 810 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 811 | PLDM_SUCCESS); |
| 812 | |
| 813 | struct pldm_msgbuf _ctxExtract; |
| 814 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 815 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 816 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 817 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 818 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 819 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL), |
| 820 | PLDM_ERROR_INVALID_DATA); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 821 | |
| 822 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 823 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 824 | } |
| 825 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 826 | TEST(msgbuf, span_required_under) |
| 827 | { |
| 828 | struct pldm_msgbuf _ctx; |
| 829 | struct pldm_msgbuf* ctx = &_ctx; |
| 830 | |
| 831 | uint8_t buf[1] = {}; |
| 832 | void* cursor = nullptr; |
| 833 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 834 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 835 | ctx->remaining = INTMAX_MIN; |
| 836 | EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS); |
| 837 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 838 | } |
| 839 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 840 | TEST(msgbuf, pldm_msgbuf_span_remaining_good) |
| 841 | { |
| 842 | struct pldm_msgbuf _ctx; |
| 843 | struct pldm_msgbuf* ctx = &_ctx; |
| 844 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 845 | uint8_t buf[8] = {0}; |
| 846 | uint16_t testVal; |
| 847 | uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 848 | size_t remaining; |
| 849 | uint8_t* retBuff = NULL; |
| 850 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 851 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 852 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 853 | PLDM_SUCCESS); |
| 854 | |
| 855 | struct pldm_msgbuf _ctxExtract; |
| 856 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 857 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 858 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 859 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 860 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 861 | EXPECT_EQ( |
| 862 | pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining), |
| 863 | PLDM_SUCCESS); |
| 864 | |
| 865 | EXPECT_EQ(remaining, sizeof(expectData)); |
| 866 | EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0); |
| 867 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 868 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 869 | } |
| 870 | |
| 871 | TEST(msgbuf, pldm_msgbuf_span_remaining_bad) |
| 872 | { |
| 873 | struct pldm_msgbuf _ctx; |
| 874 | struct pldm_msgbuf* ctx = &_ctx; |
| 875 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 876 | uint8_t buf[8] = {0}; |
| 877 | uint16_t testVal; |
| 878 | size_t remaining; |
| 879 | uint8_t* retBuff = NULL; |
| 880 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 881 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 882 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 883 | PLDM_SUCCESS); |
| 884 | |
| 885 | struct pldm_msgbuf _ctxExtract; |
| 886 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 887 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 888 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 889 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 890 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 891 | EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining), |
| 892 | PLDM_ERROR_INVALID_DATA); |
| 893 | EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL), |
| 894 | PLDM_ERROR_INVALID_DATA); |
| 895 | |
| 896 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 897 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 898 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 899 | |
| 900 | TEST(msgbuf, pldm_msgbuf_copy_good) |
| 901 | { |
| 902 | struct pldm_msgbuf _src; |
| 903 | struct pldm_msgbuf* src = &_src; |
| 904 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 905 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 906 | ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 907 | PLDM_SUCCESS); |
| 908 | |
| 909 | struct pldm_msgbuf _dst; |
| 910 | struct pldm_msgbuf* dst = &_dst; |
| 911 | uint16_t checkVal = 0; |
| 912 | uint8_t buf1[sizeof(buf)] = {}; |
| 913 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 914 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 915 | PLDM_SUCCESS); |
| 916 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS); |
| 917 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 918 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 919 | PLDM_SUCCESS); |
| 920 | EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS); |
| 921 | |
| 922 | EXPECT_EQ(buf[0], checkVal); |
| 923 | EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS); |
| 924 | EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS); |
| 925 | } |
| 926 | |
| 927 | TEST(msgbuf, pldm_msgbuf_copy_bad) |
| 928 | { |
| 929 | struct pldm_msgbuf _src; |
| 930 | struct pldm_msgbuf* src = &_src; |
| 931 | struct pldm_msgbuf _dst; |
| 932 | struct pldm_msgbuf* dst = &_dst; |
| 933 | uint8_t buf[1] = {sizeof(uint8_t)}; |
| 934 | uint8_t buf1[1] = {sizeof(uint16_t)}; |
| 935 | uint16_t value = 8; |
| 936 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 937 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
| 938 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf1, sizeof(buf1)), PLDM_SUCCESS); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 939 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 940 | PLDM_ERROR_INVALID_LENGTH); |
| 941 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 942 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS); |
| 943 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 944 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 945 | PLDM_ERROR_INVALID_LENGTH); |
| 946 | } |