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 | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 459 | TEST(msgbuf, extract_array_char_buf0_req0) |
| 460 | { |
| 461 | struct pldm_msgbuf _ctx; |
| 462 | struct pldm_msgbuf* ctx = &_ctx; |
| 463 | char buf[1] = {'\0'}; |
| 464 | char arr[1] = {'1'}; |
| 465 | |
| 466 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 467 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, arr, 0), 0); |
| 468 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 469 | } |
| 470 | |
| 471 | TEST(msgbuf, extract_array_char_buf1_req1) |
| 472 | { |
| 473 | struct pldm_msgbuf _ctx; |
| 474 | struct pldm_msgbuf* ctx = &_ctx; |
| 475 | char buf[1] = {'\0'}; |
| 476 | char arr[1] = {'1'}; |
| 477 | |
| 478 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 479 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, arr, sizeof(arr)), 0); |
| 480 | EXPECT_EQ(arr[0], '\0'); |
| 481 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 482 | } |
| 483 | |
| 484 | TEST(msgbuf, extract_array_char_buf1_req2) |
| 485 | { |
| 486 | struct pldm_msgbuf _ctx; |
| 487 | struct pldm_msgbuf* ctx = &_ctx; |
| 488 | char buf[1] = {'\0'}; |
| 489 | char arr[2] = {'1', '2'}; |
| 490 | |
| 491 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 492 | EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, arr, sizeof(arr)), 0); |
| 493 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 494 | } |
| 495 | |
| 496 | TEST(msgbuf, extract_under_array_char) |
| 497 | { |
| 498 | struct pldm_msgbuf _ctx; |
| 499 | struct pldm_msgbuf* ctx = &_ctx; |
| 500 | char buf[1] = {'\0'}; |
| 501 | char arr[1] = {'1'}; |
| 502 | |
| 503 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 504 | ctx->remaining = INTMAX_MIN; |
| 505 | EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, arr, 1), 0); |
| 506 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 507 | } |
| 508 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 509 | TEST(msgbuf, consumed_under) |
| 510 | { |
| 511 | struct pldm_msgbuf _ctx; |
| 512 | struct pldm_msgbuf* ctx = &_ctx; |
| 513 | uint8_t buf[1] = {}; |
| 514 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 515 | 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] | 516 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 517 | } |
| 518 | |
| 519 | TEST(msgbuf, consumed_exact) |
| 520 | { |
| 521 | struct pldm_msgbuf _ctx; |
| 522 | struct pldm_msgbuf* ctx = &_ctx; |
| 523 | uint8_t buf[1] = {}; |
| 524 | uint8_t val; |
| 525 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 526 | 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] | 527 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 528 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS); |
| 529 | } |
| 530 | |
| 531 | TEST(msgbuf, consumed_over) |
| 532 | { |
| 533 | struct pldm_msgbuf _ctx; |
| 534 | struct pldm_msgbuf* ctx = &_ctx; |
| 535 | uint8_t buf[1] = {}; |
| 536 | uint8_t val[2]; |
| 537 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 538 | 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] | 539 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS); |
| 540 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS); |
| 541 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 542 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 543 | |
| 544 | TEST(msgbuf, pldm_msgbuf_insert_int32_good) |
| 545 | { |
| 546 | struct pldm_msgbuf _ctx; |
| 547 | struct pldm_msgbuf* ctx = &_ctx; |
| 548 | int32_t src = -12345; |
| 549 | int32_t checkVal = 0; |
| 550 | uint8_t buf[sizeof(int32_t)] = {}; |
| 551 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 552 | 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] | 553 | EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS); |
| 554 | |
| 555 | struct pldm_msgbuf _ctxExtract; |
| 556 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 557 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 558 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 559 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 560 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 561 | |
| 562 | EXPECT_EQ(src, checkVal); |
| 563 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 564 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 565 | } |
| 566 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 567 | TEST(msgbuf, insert_under_int32) |
| 568 | { |
| 569 | struct pldm_msgbuf _ctx; |
| 570 | struct pldm_msgbuf* ctx = &_ctx; |
| 571 | |
| 572 | int32_t buf[1] = {}; |
| 573 | int32_t val = 0; |
| 574 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 575 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 576 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 577 | EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS); |
| 578 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 579 | } |
| 580 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 581 | TEST(msgbuf, pldm_msgbuf_insert_uint32_good) |
| 582 | { |
| 583 | struct pldm_msgbuf _ctx; |
| 584 | struct pldm_msgbuf* ctx = &_ctx; |
| 585 | uint32_t src = 0xf1223344; |
| 586 | uint32_t checkVal = 0; |
| 587 | uint8_t buf[sizeof(uint32_t)] = {}; |
| 588 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 589 | 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] | 590 | EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS); |
| 591 | |
| 592 | struct pldm_msgbuf _ctxExtract; |
| 593 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 594 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 595 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 596 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 597 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 598 | |
| 599 | EXPECT_EQ(src, checkVal); |
| 600 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 601 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 602 | } |
| 603 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 604 | TEST(msgbuf, insert_under_uint32) |
| 605 | { |
| 606 | struct pldm_msgbuf _ctx; |
| 607 | struct pldm_msgbuf* ctx = &_ctx; |
| 608 | |
| 609 | uint32_t buf[1] = {}; |
| 610 | uint32_t val = 0; |
| 611 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 612 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 613 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 614 | EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS); |
| 615 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 616 | } |
| 617 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 618 | TEST(msgbuf, pldm_msgbuf_insert_uint16_good) |
| 619 | { |
| 620 | struct pldm_msgbuf _ctx; |
| 621 | struct pldm_msgbuf* ctx = &_ctx; |
| 622 | uint16_t src = 0xf344; |
| 623 | uint16_t checkVal = 0; |
| 624 | uint8_t buf[sizeof(uint16_t)] = {}; |
| 625 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 626 | 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] | 627 | EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS); |
| 628 | |
| 629 | struct pldm_msgbuf _ctxExtract; |
| 630 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 631 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 632 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 633 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 634 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 635 | |
| 636 | EXPECT_EQ(src, checkVal); |
| 637 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 638 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 639 | } |
| 640 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 641 | TEST(msgbuf, insert_under_uint16) |
| 642 | { |
| 643 | struct pldm_msgbuf _ctx; |
| 644 | struct pldm_msgbuf* ctx = &_ctx; |
| 645 | |
| 646 | uint16_t buf[1] = {}; |
| 647 | uint16_t val = 0; |
| 648 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 649 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 650 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 651 | EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS); |
| 652 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 653 | } |
| 654 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 655 | TEST(msgbuf, pldm_msgbuf_insert_int16_good) |
| 656 | { |
| 657 | struct pldm_msgbuf _ctx; |
| 658 | struct pldm_msgbuf* ctx = &_ctx; |
| 659 | int16_t src = -12; |
| 660 | int16_t checkVal = 0; |
| 661 | uint8_t buf[sizeof(int16_t)] = {}; |
| 662 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 663 | 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] | 664 | EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS); |
| 665 | |
| 666 | struct pldm_msgbuf _ctxExtract; |
| 667 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 668 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 669 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 670 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 671 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 672 | |
| 673 | EXPECT_EQ(src, checkVal); |
| 674 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 675 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 676 | } |
| 677 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 678 | TEST(msgbuf, insert_under_int16) |
| 679 | { |
| 680 | struct pldm_msgbuf _ctx; |
| 681 | struct pldm_msgbuf* ctx = &_ctx; |
| 682 | |
| 683 | int16_t buf[1] = {}; |
| 684 | int16_t val = 0; |
| 685 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 686 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 687 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 688 | EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS); |
| 689 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 690 | } |
| 691 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 692 | TEST(msgbuf, pldm_msgbuf_insert_uint8_good) |
| 693 | { |
| 694 | struct pldm_msgbuf _ctx; |
| 695 | struct pldm_msgbuf* ctx = &_ctx; |
| 696 | uint8_t src = 0xf4; |
| 697 | uint8_t checkVal = 0; |
| 698 | uint8_t buf[sizeof(uint8_t)] = {}; |
| 699 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 700 | 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] | 701 | EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS); |
| 702 | |
| 703 | struct pldm_msgbuf _ctxExtract; |
| 704 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 705 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 706 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 707 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 708 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 709 | |
| 710 | EXPECT_EQ(src, checkVal); |
| 711 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 712 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 713 | } |
| 714 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 715 | TEST(msgbuf, insert_under_uint8) |
| 716 | { |
| 717 | struct pldm_msgbuf _ctx; |
| 718 | struct pldm_msgbuf* ctx = &_ctx; |
| 719 | |
| 720 | uint8_t buf[1] = {}; |
| 721 | uint8_t val = 0; |
| 722 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 723 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 724 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 725 | EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS); |
| 726 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 727 | } |
| 728 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 729 | TEST(msgbuf, pldm_msgbuf_insert_int8_good) |
| 730 | { |
| 731 | struct pldm_msgbuf _ctx; |
| 732 | struct pldm_msgbuf* ctx = &_ctx; |
| 733 | int8_t src = -4; |
| 734 | int8_t checkVal = 0; |
| 735 | uint8_t buf[sizeof(int8_t)] = {}; |
| 736 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 737 | 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] | 738 | EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS); |
| 739 | |
| 740 | struct pldm_msgbuf _ctxExtract; |
| 741 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 742 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 743 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 744 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 745 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 746 | |
| 747 | EXPECT_EQ(src, checkVal); |
| 748 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 749 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 750 | } |
| 751 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 752 | TEST(msgbuf, insert_under_int8) |
| 753 | { |
| 754 | struct pldm_msgbuf _ctx; |
| 755 | struct pldm_msgbuf* ctx = &_ctx; |
| 756 | |
| 757 | int8_t buf[1] = {}; |
| 758 | int8_t val = 0; |
| 759 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 760 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 761 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 762 | EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS); |
| 763 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 764 | } |
| 765 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 766 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good) |
| 767 | { |
| 768 | struct pldm_msgbuf _ctx; |
| 769 | struct pldm_msgbuf* ctx = &_ctx; |
| 770 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 771 | uint8_t buf[6] = {}; |
| 772 | uint8_t retBuff[6] = {}; |
| 773 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 774 | 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] | 775 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 776 | PLDM_SUCCESS); |
| 777 | |
| 778 | struct pldm_msgbuf _ctxExtract; |
| 779 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 780 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 781 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 782 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 783 | EXPECT_EQ( |
| 784 | pldm_msgbuf_extract_array_uint8(ctxExtract, retBuff, sizeof(retBuff)), |
| 785 | PLDM_SUCCESS); |
| 786 | |
| 787 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 788 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 789 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 790 | } |
| 791 | |
| 792 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad) |
| 793 | { |
| 794 | struct pldm_msgbuf _ctx; |
| 795 | struct pldm_msgbuf* ctx = &_ctx; |
| 796 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 797 | uint8_t buf[6] = {}; |
| 798 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 799 | 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] | 800 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, NULL, sizeof(src)), |
| 801 | PLDM_ERROR_INVALID_DATA); |
| 802 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 803 | } |
| 804 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 805 | TEST(msgbuf, insert_under_array_uint8) |
| 806 | { |
| 807 | struct pldm_msgbuf _ctx; |
| 808 | struct pldm_msgbuf* ctx = &_ctx; |
| 809 | |
| 810 | uint8_t buf[1] = {}; |
| 811 | uint8_t val[1] = {0}; |
| 812 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 813 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 814 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 815 | EXPECT_NE(pldm_msgbuf_insert_array_uint8(ctx, val, sizeof(val)), |
| 816 | PLDM_SUCCESS); |
| 817 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 818 | } |
| 819 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 820 | TEST(msgbuf, pldm_msgbuf_insert_array_char_good) |
| 821 | { |
| 822 | struct pldm_msgbuf _ctx; |
| 823 | struct pldm_msgbuf* ctx = &_ctx; |
| 824 | char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 825 | char buf[6] = {}; |
| 826 | char retBuff[6] = {}; |
| 827 | |
| 828 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 829 | EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, src, sizeof(src)), 0); |
| 830 | |
| 831 | struct pldm_msgbuf _ctxExtract; |
| 832 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 833 | |
| 834 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
| 835 | EXPECT_EQ( |
| 836 | pldm_msgbuf_extract_array_char(ctxExtract, retBuff, sizeof(retBuff)), |
| 837 | 0); |
| 838 | |
| 839 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 840 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 841 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 842 | } |
| 843 | |
| 844 | TEST(msgbuf, pldm_msgbuf_insert_array_char_bad) |
| 845 | { |
| 846 | struct pldm_msgbuf _ctx; |
| 847 | struct pldm_msgbuf* ctx = &_ctx; |
| 848 | char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 849 | char buf[6] = {}; |
| 850 | |
| 851 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
| 852 | EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, NULL, sizeof(src)), -EINVAL); |
| 853 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 854 | } |
| 855 | |
| 856 | TEST(msgbuf, insert_under_array_char) |
| 857 | { |
| 858 | struct pldm_msgbuf _ctx; |
| 859 | struct pldm_msgbuf* ctx = &_ctx; |
| 860 | char buf[1] = {}; |
| 861 | char val[1] = {0}; |
| 862 | |
| 863 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 864 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 865 | EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, val, sizeof(val)), 0); |
| 866 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 867 | } |
| 868 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 869 | TEST(msgbuf, pldm_msgbuf_span_required_good) |
| 870 | { |
| 871 | struct pldm_msgbuf _ctx; |
| 872 | struct pldm_msgbuf* ctx = &_ctx; |
| 873 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 874 | uint8_t buf[6] = {0}; |
| 875 | const size_t required = 4; |
| 876 | uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77}; |
| 877 | uint16_t testVal; |
| 878 | uint8_t* retBuff = NULL; |
| 879 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 880 | 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] | 881 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 882 | PLDM_SUCCESS); |
| 883 | |
| 884 | struct pldm_msgbuf _ctxExtract; |
| 885 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 886 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 887 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 888 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 889 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 890 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff), |
| 891 | PLDM_SUCCESS); |
| 892 | |
| 893 | EXPECT_EQ(memcmp(expectData, retBuff, required), 0); |
| 894 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 895 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 896 | } |
| 897 | |
| 898 | TEST(msgbuf, pldm_msgbuf_span_required_bad) |
| 899 | { |
| 900 | struct pldm_msgbuf _ctx; |
| 901 | struct pldm_msgbuf* ctx = &_ctx; |
| 902 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 903 | uint8_t buf[6] = {0}; |
| 904 | const size_t required = 4; |
| 905 | uint16_t testVal; |
| 906 | [[maybe_unused]] uint8_t* retBuff = NULL; |
| 907 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 908 | 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] | 909 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 910 | PLDM_SUCCESS); |
| 911 | |
| 912 | struct pldm_msgbuf _ctxExtract; |
| 913 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 914 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 915 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 916 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 917 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 918 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL), |
| 919 | PLDM_ERROR_INVALID_DATA); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 920 | |
| 921 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 922 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 923 | } |
| 924 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 925 | TEST(msgbuf, span_required_under) |
| 926 | { |
| 927 | struct pldm_msgbuf _ctx; |
| 928 | struct pldm_msgbuf* ctx = &_ctx; |
| 929 | |
| 930 | uint8_t buf[1] = {}; |
| 931 | void* cursor = nullptr; |
| 932 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 933 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 934 | ctx->remaining = INTMAX_MIN; |
| 935 | EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS); |
| 936 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 937 | } |
| 938 | |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 939 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good) |
| 940 | { |
| 941 | struct pldm_msgbuf _ctxExtract; |
| 942 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 943 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 944 | constexpr size_t required = 6; |
| 945 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 946 | uint16_t testVal; |
| 947 | uint8_t testVal1; |
| 948 | char* retBuff = NULL; |
| 949 | |
| 950 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 951 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 952 | EXPECT_EQ(0x2211, testVal); |
| 953 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 954 | 0); |
| 955 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 956 | EXPECT_EQ(0x77, testVal1); |
| 957 | |
| 958 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 959 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 960 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 961 | } |
| 962 | |
| 963 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length) |
| 964 | { |
| 965 | struct pldm_msgbuf _ctxExtract; |
| 966 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 967 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 968 | constexpr size_t required = 6; |
| 969 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 970 | uint16_t testVal; |
| 971 | uint8_t testVal1; |
| 972 | char* retBuff = NULL; |
| 973 | size_t length; |
| 974 | |
| 975 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 976 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 977 | EXPECT_EQ(0x2211, testVal); |
| 978 | EXPECT_EQ( |
| 979 | pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length), |
| 980 | 0); |
| 981 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 982 | EXPECT_EQ(0x77, testVal1); |
| 983 | |
| 984 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 985 | EXPECT_EQ(length, strlen(retBuff) + 1); |
| 986 | EXPECT_EQ(required, length); |
| 987 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 988 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 989 | } |
| 990 | |
| 991 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args) |
| 992 | { |
| 993 | struct pldm_msgbuf _ctxExtract; |
| 994 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 995 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 996 | uint16_t testVal; |
| 997 | |
| 998 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 999 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1000 | EXPECT_EQ(0x2211, testVal); |
| 1001 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0); |
| 1002 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1003 | } |
| 1004 | |
| 1005 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator) |
| 1006 | { |
| 1007 | struct pldm_msgbuf _ctxExtract; |
| 1008 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1009 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77}; |
| 1010 | uint16_t testVal; |
| 1011 | char* retBuff = NULL; |
| 1012 | |
| 1013 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1014 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1015 | EXPECT_EQ(0x2211, testVal); |
| 1016 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 1017 | -EOVERFLOW); |
| 1018 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1019 | } |
| 1020 | |
| 1021 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_under) |
| 1022 | { |
| 1023 | struct pldm_msgbuf _ctxExtract; |
| 1024 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1025 | |
| 1026 | uint8_t src[1] = {}; |
| 1027 | char* retBuff = NULL; |
| 1028 | |
| 1029 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1030 | ctxExtract->remaining = INTMAX_MIN; |
| 1031 | EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 1032 | 0); |
| 1033 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1034 | } |
| 1035 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1036 | TEST(msgbuf, pldm_msgbuf_span_remaining_good) |
| 1037 | { |
| 1038 | struct pldm_msgbuf _ctx; |
| 1039 | struct pldm_msgbuf* ctx = &_ctx; |
| 1040 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1041 | uint8_t buf[8] = {0}; |
| 1042 | uint16_t testVal; |
| 1043 | uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1044 | size_t remaining; |
| 1045 | uint8_t* retBuff = NULL; |
| 1046 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1047 | 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] | 1048 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 1049 | PLDM_SUCCESS); |
| 1050 | |
| 1051 | struct pldm_msgbuf _ctxExtract; |
| 1052 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1053 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1054 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1055 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1056 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 1057 | EXPECT_EQ( |
| 1058 | pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining), |
| 1059 | PLDM_SUCCESS); |
| 1060 | |
| 1061 | EXPECT_EQ(remaining, sizeof(expectData)); |
| 1062 | EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0); |
| 1063 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1064 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 1065 | } |
| 1066 | |
| 1067 | TEST(msgbuf, pldm_msgbuf_span_remaining_bad) |
| 1068 | { |
| 1069 | struct pldm_msgbuf _ctx; |
| 1070 | struct pldm_msgbuf* ctx = &_ctx; |
| 1071 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1072 | uint8_t buf[8] = {0}; |
| 1073 | uint16_t testVal; |
| 1074 | size_t remaining; |
| 1075 | uint8_t* retBuff = NULL; |
| 1076 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1077 | 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] | 1078 | EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)), |
| 1079 | PLDM_SUCCESS); |
| 1080 | |
| 1081 | struct pldm_msgbuf _ctxExtract; |
| 1082 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1083 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1084 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1085 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1086 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1087 | EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining), |
| 1088 | PLDM_ERROR_INVALID_DATA); |
| 1089 | EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL), |
| 1090 | PLDM_ERROR_INVALID_DATA); |
| 1091 | |
| 1092 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1093 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 1094 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1095 | |
| 1096 | TEST(msgbuf, pldm_msgbuf_copy_good) |
| 1097 | { |
| 1098 | struct pldm_msgbuf _src; |
| 1099 | struct pldm_msgbuf* src = &_src; |
| 1100 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 1101 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1102 | ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1103 | PLDM_SUCCESS); |
| 1104 | |
| 1105 | struct pldm_msgbuf _dst; |
| 1106 | struct pldm_msgbuf* dst = &_dst; |
| 1107 | uint16_t checkVal = 0; |
| 1108 | uint8_t buf1[sizeof(buf)] = {}; |
| 1109 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1110 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1111 | PLDM_SUCCESS); |
| 1112 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS); |
| 1113 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1114 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1115 | PLDM_SUCCESS); |
| 1116 | EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS); |
| 1117 | |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1118 | EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS); |
| 1119 | EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame^] | 1120 | |
| 1121 | EXPECT_EQ(buf[0], checkVal); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST(msgbuf, pldm_msgbuf_copy_bad) |
| 1125 | { |
| 1126 | struct pldm_msgbuf _src; |
| 1127 | struct pldm_msgbuf* src = &_src; |
| 1128 | struct pldm_msgbuf _dst; |
| 1129 | struct pldm_msgbuf* dst = &_dst; |
| 1130 | uint8_t buf[1] = {sizeof(uint8_t)}; |
| 1131 | uint8_t buf1[1] = {sizeof(uint16_t)}; |
| 1132 | uint16_t value = 8; |
| 1133 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1134 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
| 1135 | 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] | 1136 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1137 | PLDM_ERROR_INVALID_LENGTH); |
| 1138 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1139 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS); |
| 1140 | 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] | 1141 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1142 | PLDM_ERROR_INVALID_LENGTH); |
| 1143 | } |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame^] | 1144 | |
| 1145 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact) |
| 1146 | { |
| 1147 | const char msg[] = "this is a message"; |
| 1148 | |
| 1149 | struct pldm_msgbuf _src; |
| 1150 | struct pldm_msgbuf* src = &_src; |
| 1151 | struct pldm_msgbuf _dst; |
| 1152 | struct pldm_msgbuf* dst = &_dst; |
| 1153 | |
| 1154 | char buf[sizeof(msg)] = {}; |
| 1155 | |
| 1156 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1157 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1158 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1159 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1160 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1161 | EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf))); |
| 1162 | } |
| 1163 | |
| 1164 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src) |
| 1165 | { |
| 1166 | const char msg[] = "this is a message"; |
| 1167 | |
| 1168 | struct pldm_msgbuf _src; |
| 1169 | struct pldm_msgbuf* src = &_src; |
| 1170 | struct pldm_msgbuf _dst; |
| 1171 | struct pldm_msgbuf* dst = &_dst; |
| 1172 | |
| 1173 | char buf[sizeof(msg) + 1] = {}; |
| 1174 | |
| 1175 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1176 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1177 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1178 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1179 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1180 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1181 | } |
| 1182 | |
| 1183 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst) |
| 1184 | { |
| 1185 | const char msg[] = "this is a message"; |
| 1186 | |
| 1187 | struct pldm_msgbuf _src; |
| 1188 | struct pldm_msgbuf* src = &_src; |
| 1189 | struct pldm_msgbuf _dst; |
| 1190 | struct pldm_msgbuf* dst = &_dst; |
| 1191 | |
| 1192 | char buf[sizeof(msg) - 1] = {}; |
| 1193 | |
| 1194 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1195 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1196 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1197 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1198 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1199 | } |
| 1200 | |
| 1201 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src) |
| 1202 | { |
| 1203 | const char msg[] = {'a'}; |
| 1204 | |
| 1205 | struct pldm_msgbuf _src; |
| 1206 | struct pldm_msgbuf* src = &_src; |
| 1207 | struct pldm_msgbuf _dst; |
| 1208 | struct pldm_msgbuf* dst = &_dst; |
| 1209 | |
| 1210 | char buf[sizeof(msg)] = {}; |
| 1211 | |
| 1212 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1213 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1214 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1215 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1216 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1217 | } |