Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 1 | #include <endian.h> |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 2 | #include <libpldm/utils.h> |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 3 | |
| 4 | #include <cfloat> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | /* We're exercising the implementation so disable the asserts for now */ |
| 9 | #ifndef NDEBUG |
| 10 | #define NDEBUG 1 |
| 11 | #endif |
| 12 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 13 | #include "msgbuf.h" |
| 14 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 15 | TEST(msgbuf, init_bad_minsize) |
| 16 | { |
| 17 | struct pldm_msgbuf _ctx; |
| 18 | struct pldm_msgbuf* ctx = &_ctx; |
| 19 | uint8_t buf[1] = {}; |
| 20 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 21 | 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] | 22 | PLDM_SUCCESS); |
| 23 | } |
| 24 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 25 | TEST(msgbuf, init_bad_len) |
| 26 | { |
| 27 | struct pldm_msgbuf _ctx; |
| 28 | struct pldm_msgbuf* ctx = &_ctx; |
| 29 | uint8_t buf[1] = {}; |
| 30 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 31 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, SIZE_MAX), |
| 32 | PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(msgbuf, init_overflow) |
| 36 | { |
| 37 | struct pldm_msgbuf _ctx; |
| 38 | struct pldm_msgbuf* ctx = &_ctx; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 39 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 40 | void* buf = (void*)UINTPTR_MAX; |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 41 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 42 | EXPECT_NE(pldm_msgbuf_init_cc(ctx, 0, buf, 2), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(msgbuf, init_success) |
| 46 | { |
| 47 | struct pldm_msgbuf _ctx; |
| 48 | struct pldm_msgbuf* ctx = &_ctx; |
| 49 | uint8_t buf[1] = {}; |
| 50 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 51 | EXPECT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 52 | PLDM_SUCCESS); |
| 53 | } |
| 54 | |
| 55 | TEST(msgbuf, destroy_none) |
| 56 | { |
| 57 | struct pldm_msgbuf _ctx; |
| 58 | struct pldm_msgbuf* ctx = &_ctx; |
| 59 | uint8_t buf[1] = {}; |
| 60 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 61 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 62 | PLDM_SUCCESS); |
| 63 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 64 | } |
| 65 | |
| 66 | TEST(msgbuf, destroy_exact) |
| 67 | { |
| 68 | struct pldm_msgbuf _ctx; |
| 69 | struct pldm_msgbuf* ctx = &_ctx; |
| 70 | uint8_t buf[1] = {0xa5}; |
| 71 | uint8_t val; |
| 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_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 76 | EXPECT_EQ(val, 0xa5); |
| 77 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 78 | } |
| 79 | |
| 80 | TEST(msgbuf, destroy_over) |
| 81 | { |
| 82 | struct pldm_msgbuf _ctx; |
| 83 | struct pldm_msgbuf* ctx = &_ctx; |
| 84 | uint8_t buf[1] = {0xa5}; |
| 85 | uint8_t val; |
| 86 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 87 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 88 | PLDM_SUCCESS); |
| 89 | ASSERT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 90 | ASSERT_EQ(val, 0xa5); |
| 91 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 92 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 93 | } |
| 94 | |
| 95 | TEST(msgbuf, destroy_under) |
| 96 | { |
| 97 | struct pldm_msgbuf _ctx; |
| 98 | struct pldm_msgbuf* ctx = &_ctx; |
| 99 | uint8_t buf[2] = {0x5a, 0xa5}; |
| 100 | uint8_t val; |
| 101 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 102 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 103 | PLDM_SUCCESS); |
| 104 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 105 | EXPECT_EQ(val, 0x5a); |
| 106 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 107 | } |
| 108 | |
| 109 | TEST(msgbuf, extract_one_uint8) |
| 110 | { |
| 111 | struct pldm_msgbuf _ctx; |
| 112 | struct pldm_msgbuf* ctx = &_ctx; |
| 113 | uint8_t buf[1] = {0xa5}; |
| 114 | uint8_t val; |
| 115 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 116 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 117 | PLDM_SUCCESS); |
| 118 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 119 | EXPECT_EQ(val, 0xa5); |
| 120 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 121 | } |
| 122 | |
| 123 | TEST(msgbuf, extract_over_uint8) |
| 124 | { |
| 125 | struct pldm_msgbuf _ctx; |
| 126 | struct pldm_msgbuf* ctx = &_ctx; |
| 127 | uint8_t buf[1] = {}; |
| 128 | uint8_t val; |
| 129 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 130 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 131 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 132 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 133 | } |
| 134 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 135 | TEST(msgbuf, extract_under_uint8) |
| 136 | { |
| 137 | struct pldm_msgbuf _ctx; |
| 138 | struct pldm_msgbuf* ctx = &_ctx; |
| 139 | |
| 140 | uint8_t buf[1] = {}; |
| 141 | uint8_t val; |
| 142 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 143 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 144 | ctx->remaining = INTMAX_MIN; |
| 145 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 146 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 147 | } |
| 148 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 149 | TEST(msgbuf, extract_one_int8) |
| 150 | { |
| 151 | struct pldm_msgbuf _ctx; |
| 152 | struct pldm_msgbuf* ctx = &_ctx; |
| 153 | int8_t buf[1] = {-1}; |
| 154 | int8_t val; |
| 155 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 156 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 157 | PLDM_SUCCESS); |
| 158 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 159 | EXPECT_EQ(val, -1); |
| 160 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 161 | } |
| 162 | |
| 163 | TEST(msgbuf, extract_over_int8) |
| 164 | { |
| 165 | struct pldm_msgbuf _ctx; |
| 166 | struct pldm_msgbuf* ctx = &_ctx; |
| 167 | int8_t buf[1] = {}; |
| 168 | int8_t val; |
| 169 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 170 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 171 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 172 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 173 | } |
| 174 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 175 | TEST(msgbuf, extract_under_int8) |
| 176 | { |
| 177 | struct pldm_msgbuf _ctx; |
| 178 | struct pldm_msgbuf* ctx = &_ctx; |
| 179 | |
| 180 | uint8_t buf[1] = {}; |
| 181 | int8_t val; |
| 182 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 183 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 184 | ctx->remaining = INTMAX_MIN; |
| 185 | EXPECT_NE(pldm_msgbuf_extract_int8(ctx, &val), PLDM_SUCCESS); |
| 186 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 187 | } |
| 188 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 189 | TEST(msgbuf, extract_one_uint16) |
| 190 | { |
| 191 | struct pldm_msgbuf _ctx; |
| 192 | struct pldm_msgbuf* ctx = &_ctx; |
| 193 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 194 | uint16_t val = {}; |
| 195 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 196 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 197 | PLDM_SUCCESS); |
| 198 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 199 | EXPECT_EQ(val, 0x5aa5); |
| 200 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 201 | } |
| 202 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 203 | TEST(msgbuf, extract_under_uint16) |
| 204 | { |
| 205 | struct pldm_msgbuf _ctx; |
| 206 | struct pldm_msgbuf* ctx = &_ctx; |
| 207 | |
| 208 | uint16_t buf[1] = {}; |
| 209 | uint16_t val; |
| 210 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 211 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 212 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 213 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 214 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 215 | } |
| 216 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 217 | TEST(msgbuf, extract_over_uint16) |
| 218 | { |
| 219 | struct pldm_msgbuf _ctx; |
| 220 | struct pldm_msgbuf* ctx = &_ctx; |
| 221 | uint16_t buf[1] = {}; |
| 222 | uint16_t val; |
| 223 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 224 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 225 | EXPECT_NE(pldm_msgbuf_extract_uint16(ctx, &val), PLDM_SUCCESS); |
| 226 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 227 | } |
| 228 | |
| 229 | TEST(msgbuf, extract_one_int16) |
| 230 | { |
| 231 | struct pldm_msgbuf _ctx; |
| 232 | struct pldm_msgbuf* ctx = &_ctx; |
| 233 | int16_t buf[1] = {(int16_t)(htole16((uint16_t)INT16_MIN))}; |
| 234 | int16_t val; |
| 235 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 236 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 237 | PLDM_SUCCESS); |
| 238 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 239 | EXPECT_EQ(val, INT16_MIN); |
| 240 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 241 | } |
| 242 | |
| 243 | TEST(msgbuf, extract_over_int16) |
| 244 | { |
| 245 | struct pldm_msgbuf _ctx; |
| 246 | struct pldm_msgbuf* ctx = &_ctx; |
| 247 | int16_t buf[1] = {}; |
| 248 | int16_t val; |
| 249 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 250 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 251 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 252 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 253 | } |
| 254 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 255 | TEST(msgbuf, extract_under_int16) |
| 256 | { |
| 257 | struct pldm_msgbuf _ctx; |
| 258 | struct pldm_msgbuf* ctx = &_ctx; |
| 259 | |
| 260 | int16_t buf[1] = {}; |
| 261 | int16_t val; |
| 262 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 263 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 264 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 265 | EXPECT_NE(pldm_msgbuf_extract_int16(ctx, &val), PLDM_SUCCESS); |
| 266 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 267 | } |
| 268 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 269 | TEST(msgbuf, extract_one_uint32) |
| 270 | { |
| 271 | struct pldm_msgbuf _ctx; |
| 272 | struct pldm_msgbuf* ctx = &_ctx; |
| 273 | uint32_t buf[1] = {htole32(0x5a00ffa5)}; |
| 274 | uint32_t val; |
| 275 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 276 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 277 | PLDM_SUCCESS); |
| 278 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 279 | EXPECT_EQ(val, 0x5a00ffa5); |
| 280 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 281 | } |
| 282 | |
| 283 | TEST(msgbuf, extract_over_uint32) |
| 284 | { |
| 285 | struct pldm_msgbuf _ctx; |
| 286 | struct pldm_msgbuf* ctx = &_ctx; |
| 287 | uint32_t buf[1] = {}; |
| 288 | uint32_t val; |
| 289 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 290 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 291 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 292 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 293 | } |
| 294 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 295 | TEST(msgbuf, extract_under_uint32) |
| 296 | { |
| 297 | struct pldm_msgbuf _ctx; |
| 298 | struct pldm_msgbuf* ctx = &_ctx; |
| 299 | |
| 300 | uint32_t buf[1] = {}; |
| 301 | uint32_t val; |
| 302 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 303 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 304 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 305 | EXPECT_NE(pldm_msgbuf_extract_uint32(ctx, &val), PLDM_SUCCESS); |
| 306 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 307 | } |
| 308 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 309 | TEST(msgbuf, extract_one_int32) |
| 310 | { |
| 311 | struct pldm_msgbuf _ctx; |
| 312 | struct pldm_msgbuf* ctx = &_ctx; |
| 313 | int32_t buf[1] = {(int32_t)(htole32((uint32_t)(INT32_MIN)))}; |
| 314 | int32_t val; |
| 315 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 316 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 317 | PLDM_SUCCESS); |
| 318 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 319 | EXPECT_EQ(val, INT32_MIN); |
| 320 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 321 | } |
| 322 | |
| 323 | TEST(msgbuf, extract_over_int32) |
| 324 | { |
| 325 | struct pldm_msgbuf _ctx; |
| 326 | struct pldm_msgbuf* ctx = &_ctx; |
| 327 | int32_t buf[1] = {}; |
| 328 | int32_t val; |
| 329 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 330 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 331 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 332 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 333 | } |
| 334 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 335 | TEST(msgbuf, extract_under_int32) |
| 336 | { |
| 337 | struct pldm_msgbuf _ctx; |
| 338 | struct pldm_msgbuf* ctx = &_ctx; |
| 339 | |
| 340 | int32_t buf[1] = {}; |
| 341 | int32_t val; |
| 342 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 343 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 344 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 345 | EXPECT_NE(pldm_msgbuf_extract_int32(ctx, &val), PLDM_SUCCESS); |
| 346 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 347 | } |
| 348 | |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 349 | TEST(msgbuf, extract_one_real32) |
| 350 | { |
| 351 | struct pldm_msgbuf _ctx; |
| 352 | struct pldm_msgbuf* ctx = &_ctx; |
| 353 | uint32_t buf[1] = {}; |
| 354 | uint32_t xform; |
| 355 | real32_t val; |
| 356 | |
| 357 | val = FLT_MAX; |
| 358 | memcpy(&xform, &val, sizeof(val)); |
| 359 | buf[0] = htole32(xform); |
| 360 | val = 0; |
| 361 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 362 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, sizeof(buf), buf, sizeof(buf)), |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 363 | PLDM_SUCCESS); |
| 364 | EXPECT_EQ(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 365 | EXPECT_EQ(val, FLT_MAX); |
| 366 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 367 | } |
| 368 | |
| 369 | TEST(msgbuf, extract_over_real32) |
| 370 | { |
| 371 | struct pldm_msgbuf _ctx; |
| 372 | struct pldm_msgbuf* ctx = &_ctx; |
| 373 | real32_t buf[1] = {}; |
| 374 | real32_t val; |
| 375 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 376 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | c63f63a | 2023-02-24 22:29:33 +1030 | [diff] [blame] | 377 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 378 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 379 | } |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 380 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 381 | TEST(msgbuf, extract_under_real32) |
| 382 | { |
| 383 | struct pldm_msgbuf _ctx; |
| 384 | struct pldm_msgbuf* ctx = &_ctx; |
| 385 | |
| 386 | real32_t buf[1] = {}; |
| 387 | real32_t val; |
| 388 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 389 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 390 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 391 | EXPECT_NE(pldm_msgbuf_extract_real32(ctx, &val), PLDM_SUCCESS); |
| 392 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 393 | } |
| 394 | |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 395 | TEST(msgbuf, extract_array_uint8_buf0_req0) |
| 396 | { |
| 397 | struct pldm_msgbuf _ctx; |
| 398 | struct pldm_msgbuf* ctx = &_ctx; |
| 399 | uint8_t buf[1] = {}; |
| 400 | uint8_t arr[1]; |
| 401 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 402 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 403 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctx, 0, arr, 0), PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 404 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 405 | } |
| 406 | |
| 407 | TEST(msgbuf, extract_array_uint8_buf1_req1) |
| 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, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 415 | EXPECT_EQ( |
| 416 | pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), |
| 417 | PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 418 | EXPECT_EQ(arr[0], 0); |
| 419 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 420 | } |
| 421 | |
| 422 | TEST(msgbuf, extract_array_uint8_buf1_req2) |
| 423 | { |
| 424 | struct pldm_msgbuf _ctx; |
| 425 | struct pldm_msgbuf* ctx = &_ctx; |
| 426 | uint8_t buf[1] = {}; |
| 427 | uint8_t arr[2]; |
| 428 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 429 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 430 | EXPECT_NE( |
| 431 | pldm_msgbuf_extract_array_uint8(ctx, sizeof(arr), arr, sizeof(arr)), |
| 432 | PLDM_SUCCESS); |
Andrew Jeffery | 369b121 | 2023-04-20 15:44:48 +0930 | [diff] [blame] | 433 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 434 | } |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 435 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 436 | TEST(msgbuf, extract_under_array_uint8) |
| 437 | { |
| 438 | struct pldm_msgbuf _ctx; |
| 439 | struct pldm_msgbuf* ctx = &_ctx; |
| 440 | uint8_t buf[1] = {}; |
| 441 | uint8_t arr[1]; |
| 442 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 443 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 444 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 445 | EXPECT_NE(pldm_msgbuf_extract_array_uint8(ctx, 1, arr, 1), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 446 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 447 | } |
| 448 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 449 | TEST(msgbuf, extract_array_char_buf0_req0) |
| 450 | { |
| 451 | struct pldm_msgbuf _ctx; |
| 452 | struct pldm_msgbuf* ctx = &_ctx; |
| 453 | char buf[1] = {'\0'}; |
| 454 | char arr[1] = {'1'}; |
| 455 | |
| 456 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 457 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctx, 0, arr, 0), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 458 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 459 | } |
| 460 | |
| 461 | TEST(msgbuf, extract_array_char_buf1_req1) |
| 462 | { |
| 463 | struct pldm_msgbuf _ctx; |
| 464 | struct pldm_msgbuf* ctx = &_ctx; |
| 465 | char buf[1] = {'\0'}; |
| 466 | char arr[1] = {'1'}; |
| 467 | |
| 468 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 469 | EXPECT_EQ( |
| 470 | pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 471 | EXPECT_EQ(arr[0], '\0'); |
| 472 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 473 | } |
| 474 | |
| 475 | TEST(msgbuf, extract_array_char_buf1_req2) |
| 476 | { |
| 477 | struct pldm_msgbuf _ctx; |
| 478 | struct pldm_msgbuf* ctx = &_ctx; |
| 479 | char buf[1] = {'\0'}; |
| 480 | char arr[2] = {'1', '2'}; |
| 481 | |
| 482 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 483 | EXPECT_NE( |
| 484 | pldm_msgbuf_extract_array_char(ctx, sizeof(arr), arr, sizeof(arr)), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 485 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 486 | } |
| 487 | |
| 488 | TEST(msgbuf, extract_under_array_char) |
| 489 | { |
| 490 | struct pldm_msgbuf _ctx; |
| 491 | struct pldm_msgbuf* ctx = &_ctx; |
| 492 | char buf[1] = {'\0'}; |
| 493 | char arr[1] = {'1'}; |
| 494 | |
| 495 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 496 | ctx->remaining = INTMAX_MIN; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 497 | EXPECT_NE(pldm_msgbuf_extract_array_char(ctx, 1, arr, 1), 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 498 | ASSERT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 499 | } |
| 500 | |
Andrew Jeffery | db7b832 | 2023-04-12 23:05:21 +0930 | [diff] [blame] | 501 | TEST(msgbuf, consumed_under) |
| 502 | { |
| 503 | struct pldm_msgbuf _ctx; |
| 504 | struct pldm_msgbuf* ctx = &_ctx; |
| 505 | uint8_t buf[1] = {}; |
| 506 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 507 | 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] | 508 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 509 | } |
| 510 | |
| 511 | TEST(msgbuf, consumed_exact) |
| 512 | { |
| 513 | struct pldm_msgbuf _ctx; |
| 514 | struct pldm_msgbuf* ctx = &_ctx; |
| 515 | uint8_t buf[1] = {}; |
| 516 | uint8_t val; |
| 517 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 518 | 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] | 519 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS); |
| 520 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS); |
| 521 | } |
| 522 | |
| 523 | TEST(msgbuf, consumed_over) |
| 524 | { |
| 525 | struct pldm_msgbuf _ctx; |
| 526 | struct pldm_msgbuf* ctx = &_ctx; |
| 527 | uint8_t buf[1] = {}; |
| 528 | uint8_t val[2]; |
| 529 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 530 | 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] | 531 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS); |
| 532 | EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS); |
| 533 | EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 534 | } |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 535 | |
| 536 | TEST(msgbuf, pldm_msgbuf_insert_int32_good) |
| 537 | { |
| 538 | struct pldm_msgbuf _ctx; |
| 539 | struct pldm_msgbuf* ctx = &_ctx; |
| 540 | int32_t src = -12345; |
| 541 | int32_t checkVal = 0; |
| 542 | uint8_t buf[sizeof(int32_t)] = {}; |
| 543 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 544 | 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] | 545 | EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS); |
| 546 | |
| 547 | struct pldm_msgbuf _ctxExtract; |
| 548 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 549 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 550 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 551 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 552 | EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 553 | |
| 554 | EXPECT_EQ(src, checkVal); |
| 555 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 556 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 557 | } |
| 558 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 559 | TEST(msgbuf, insert_under_int32) |
| 560 | { |
| 561 | struct pldm_msgbuf _ctx; |
| 562 | struct pldm_msgbuf* ctx = &_ctx; |
| 563 | |
| 564 | int32_t buf[1] = {}; |
| 565 | int32_t val = 0; |
| 566 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 567 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 568 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 569 | EXPECT_NE(pldm_msgbuf_insert_int32(ctx, val), PLDM_SUCCESS); |
| 570 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 571 | } |
| 572 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 573 | TEST(msgbuf, pldm_msgbuf_insert_uint32_good) |
| 574 | { |
| 575 | struct pldm_msgbuf _ctx; |
| 576 | struct pldm_msgbuf* ctx = &_ctx; |
| 577 | uint32_t src = 0xf1223344; |
| 578 | uint32_t checkVal = 0; |
| 579 | uint8_t buf[sizeof(uint32_t)] = {}; |
| 580 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 581 | 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] | 582 | EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS); |
| 583 | |
| 584 | struct pldm_msgbuf _ctxExtract; |
| 585 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 586 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 587 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 588 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 589 | EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 590 | |
| 591 | EXPECT_EQ(src, checkVal); |
| 592 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 593 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 594 | } |
| 595 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 596 | TEST(msgbuf, insert_under_uint32) |
| 597 | { |
| 598 | struct pldm_msgbuf _ctx; |
| 599 | struct pldm_msgbuf* ctx = &_ctx; |
| 600 | |
| 601 | uint32_t buf[1] = {}; |
| 602 | uint32_t val = 0; |
| 603 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 604 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 605 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 606 | EXPECT_NE(pldm_msgbuf_insert_uint32(ctx, val), PLDM_SUCCESS); |
| 607 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 608 | } |
| 609 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 610 | TEST(msgbuf, pldm_msgbuf_insert_uint16_good) |
| 611 | { |
| 612 | struct pldm_msgbuf _ctx; |
| 613 | struct pldm_msgbuf* ctx = &_ctx; |
| 614 | uint16_t src = 0xf344; |
| 615 | uint16_t checkVal = 0; |
| 616 | uint8_t buf[sizeof(uint16_t)] = {}; |
| 617 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 618 | 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] | 619 | EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS); |
| 620 | |
| 621 | struct pldm_msgbuf _ctxExtract; |
| 622 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 623 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 624 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 625 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 626 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 627 | |
| 628 | EXPECT_EQ(src, checkVal); |
| 629 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 630 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 631 | } |
| 632 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 633 | TEST(msgbuf, insert_under_uint16) |
| 634 | { |
| 635 | struct pldm_msgbuf _ctx; |
| 636 | struct pldm_msgbuf* ctx = &_ctx; |
| 637 | |
| 638 | uint16_t buf[1] = {}; |
| 639 | uint16_t val = 0; |
| 640 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 641 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 642 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 643 | EXPECT_NE(pldm_msgbuf_insert_uint16(ctx, val), PLDM_SUCCESS); |
| 644 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 645 | } |
| 646 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 647 | TEST(msgbuf, pldm_msgbuf_insert_int16_good) |
| 648 | { |
| 649 | struct pldm_msgbuf _ctx; |
| 650 | struct pldm_msgbuf* ctx = &_ctx; |
| 651 | int16_t src = -12; |
| 652 | int16_t checkVal = 0; |
| 653 | uint8_t buf[sizeof(int16_t)] = {}; |
| 654 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 655 | 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] | 656 | EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS); |
| 657 | |
| 658 | struct pldm_msgbuf _ctxExtract; |
| 659 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 660 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 661 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 662 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 663 | EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 664 | |
| 665 | EXPECT_EQ(src, checkVal); |
| 666 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 667 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 668 | } |
| 669 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 670 | TEST(msgbuf, insert_under_int16) |
| 671 | { |
| 672 | struct pldm_msgbuf _ctx; |
| 673 | struct pldm_msgbuf* ctx = &_ctx; |
| 674 | |
| 675 | int16_t buf[1] = {}; |
| 676 | int16_t val = 0; |
| 677 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 678 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 679 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 680 | EXPECT_NE(pldm_msgbuf_insert_int16(ctx, val), PLDM_SUCCESS); |
| 681 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 682 | } |
| 683 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 684 | TEST(msgbuf, pldm_msgbuf_insert_uint8_good) |
| 685 | { |
| 686 | struct pldm_msgbuf _ctx; |
| 687 | struct pldm_msgbuf* ctx = &_ctx; |
| 688 | uint8_t src = 0xf4; |
| 689 | uint8_t checkVal = 0; |
| 690 | uint8_t buf[sizeof(uint8_t)] = {}; |
| 691 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 692 | 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] | 693 | EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS); |
| 694 | |
| 695 | struct pldm_msgbuf _ctxExtract; |
| 696 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 697 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 698 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 699 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 700 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 701 | |
| 702 | EXPECT_EQ(src, checkVal); |
| 703 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 704 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 705 | } |
| 706 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 707 | TEST(msgbuf, insert_under_uint8) |
| 708 | { |
| 709 | struct pldm_msgbuf _ctx; |
| 710 | struct pldm_msgbuf* ctx = &_ctx; |
| 711 | |
| 712 | uint8_t buf[1] = {}; |
| 713 | uint8_t val = 0; |
| 714 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 715 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 716 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 717 | EXPECT_NE(pldm_msgbuf_insert_uint8(ctx, val), PLDM_SUCCESS); |
| 718 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 719 | } |
| 720 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 721 | TEST(msgbuf, pldm_msgbuf_insert_int8_good) |
| 722 | { |
| 723 | struct pldm_msgbuf _ctx; |
| 724 | struct pldm_msgbuf* ctx = &_ctx; |
| 725 | int8_t src = -4; |
| 726 | int8_t checkVal = 0; |
| 727 | uint8_t buf[sizeof(int8_t)] = {}; |
| 728 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 729 | 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] | 730 | EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS); |
| 731 | |
| 732 | struct pldm_msgbuf _ctxExtract; |
| 733 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 734 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 735 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 736 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 737 | EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS); |
| 738 | |
| 739 | EXPECT_EQ(src, checkVal); |
| 740 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 741 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 742 | } |
| 743 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 744 | TEST(msgbuf, insert_under_int8) |
| 745 | { |
| 746 | struct pldm_msgbuf _ctx; |
| 747 | struct pldm_msgbuf* ctx = &_ctx; |
| 748 | |
| 749 | int8_t buf[1] = {}; |
| 750 | int8_t val = 0; |
| 751 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 752 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 753 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
| 754 | EXPECT_NE(pldm_msgbuf_insert_int8(ctx, val), PLDM_SUCCESS); |
| 755 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 756 | } |
| 757 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 758 | TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good) |
| 759 | { |
| 760 | struct pldm_msgbuf _ctx; |
| 761 | struct pldm_msgbuf* ctx = &_ctx; |
| 762 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 763 | uint8_t buf[6] = {}; |
| 764 | uint8_t retBuff[6] = {}; |
| 765 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 766 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 767 | EXPECT_EQ( |
| 768 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 769 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 770 | |
| 771 | struct pldm_msgbuf _ctxExtract; |
| 772 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 773 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 774 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 775 | PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 776 | EXPECT_EQ(pldm_msgbuf_extract_array_uint8(ctxExtract, sizeof(retBuff), |
| 777 | retBuff, sizeof(retBuff)), |
| 778 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 779 | |
| 780 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 781 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 782 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 783 | } |
| 784 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 785 | TEST(msgbuf, insert_under_array_uint8) |
| 786 | { |
| 787 | struct pldm_msgbuf _ctx; |
| 788 | struct pldm_msgbuf* ctx = &_ctx; |
| 789 | |
| 790 | uint8_t buf[1] = {}; |
| 791 | uint8_t val[1] = {0}; |
| 792 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 793 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 794 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 795 | EXPECT_NE( |
| 796 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(val), val, sizeof(val)), |
| 797 | PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 798 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 799 | } |
| 800 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 801 | TEST(msgbuf, pldm_msgbuf_insert_array_char_good) |
| 802 | { |
| 803 | struct pldm_msgbuf _ctx; |
| 804 | struct pldm_msgbuf* ctx = &_ctx; |
| 805 | char src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 806 | char buf[6] = {}; |
| 807 | char retBuff[6] = {}; |
| 808 | |
| 809 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 810 | EXPECT_EQ(pldm_msgbuf_insert_array_char(ctx, sizeof(src), src, sizeof(src)), |
| 811 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 812 | |
| 813 | struct pldm_msgbuf _ctxExtract; |
| 814 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 815 | |
| 816 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, buf, sizeof(buf)), 0); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 817 | EXPECT_EQ(pldm_msgbuf_extract_array_char(ctxExtract, sizeof(retBuff), |
| 818 | retBuff, sizeof(retBuff)), |
| 819 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 820 | |
| 821 | EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0); |
| 822 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 823 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), 0); |
| 824 | } |
| 825 | |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 826 | TEST(msgbuf, insert_under_array_char) |
| 827 | { |
| 828 | struct pldm_msgbuf _ctx; |
| 829 | struct pldm_msgbuf* ctx = &_ctx; |
| 830 | char buf[1] = {}; |
| 831 | char val[1] = {0}; |
| 832 | |
| 833 | ASSERT_EQ(pldm_msgbuf_init_errno(ctx, 0, buf, 0), 0); |
| 834 | ctx->remaining = INTMAX_MIN + sizeof(val) - 1; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 835 | EXPECT_NE(pldm_msgbuf_insert_array_char(ctx, sizeof(val), val, sizeof(val)), |
| 836 | 0); |
Andrew Jeffery | 1c57144 | 2024-07-08 10:25:48 +0930 | [diff] [blame] | 837 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), -EOVERFLOW); |
| 838 | } |
| 839 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 840 | TEST(msgbuf, pldm_msgbuf_span_required_good) |
| 841 | { |
| 842 | struct pldm_msgbuf _ctx; |
| 843 | struct pldm_msgbuf* ctx = &_ctx; |
| 844 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 845 | uint8_t buf[6] = {0}; |
| 846 | const size_t required = 4; |
| 847 | uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77}; |
| 848 | uint16_t testVal; |
| 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); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 852 | EXPECT_EQ( |
| 853 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 854 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 855 | |
| 856 | struct pldm_msgbuf _ctxExtract; |
| 857 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 858 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 859 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 860 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 861 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 862 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff), |
| 863 | PLDM_SUCCESS); |
| 864 | |
| 865 | EXPECT_EQ(memcmp(expectData, retBuff, required), 0); |
| 866 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 867 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 868 | } |
| 869 | |
| 870 | TEST(msgbuf, pldm_msgbuf_span_required_bad) |
| 871 | { |
| 872 | struct pldm_msgbuf _ctx; |
| 873 | struct pldm_msgbuf* ctx = &_ctx; |
| 874 | uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; |
| 875 | uint8_t buf[6] = {0}; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 876 | uint16_t testVal; |
| 877 | [[maybe_unused]] uint8_t* retBuff = NULL; |
| 878 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 879 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 880 | EXPECT_EQ( |
| 881 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 882 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 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); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 890 | |
| 891 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 892 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 893 | } |
| 894 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 895 | TEST(msgbuf, span_required_under) |
| 896 | { |
| 897 | struct pldm_msgbuf _ctx; |
| 898 | struct pldm_msgbuf* ctx = &_ctx; |
| 899 | |
| 900 | uint8_t buf[1] = {}; |
| 901 | void* cursor = nullptr; |
| 902 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 903 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 904 | ctx->remaining = INTMAX_MIN; |
| 905 | EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS); |
| 906 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 907 | } |
| 908 | |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 909 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good) |
| 910 | { |
| 911 | struct pldm_msgbuf _ctxExtract; |
| 912 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 913 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 914 | constexpr size_t required = 6; |
| 915 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 916 | uint16_t testVal; |
| 917 | uint8_t testVal1; |
| 918 | char* retBuff = NULL; |
| 919 | |
| 920 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 921 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 922 | EXPECT_EQ(0x2211, testVal); |
| 923 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 924 | 0); |
| 925 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 926 | EXPECT_EQ(0x77, testVal1); |
| 927 | |
| 928 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 929 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 930 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 931 | } |
| 932 | |
| 933 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length) |
| 934 | { |
| 935 | struct pldm_msgbuf _ctxExtract; |
| 936 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 937 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 938 | constexpr size_t required = 6; |
| 939 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 940 | uint16_t testVal; |
| 941 | uint8_t testVal1; |
| 942 | char* retBuff = NULL; |
| 943 | size_t length; |
| 944 | |
| 945 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 946 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 947 | EXPECT_EQ(0x2211, testVal); |
| 948 | EXPECT_EQ( |
| 949 | pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length), |
| 950 | 0); |
| 951 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 952 | EXPECT_EQ(0x77, testVal1); |
| 953 | |
| 954 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 955 | EXPECT_EQ(length, strlen(retBuff) + 1); |
| 956 | EXPECT_EQ(required, length); |
| 957 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 958 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 959 | } |
| 960 | |
| 961 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args) |
| 962 | { |
| 963 | struct pldm_msgbuf _ctxExtract; |
| 964 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 965 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 966 | uint16_t testVal; |
| 967 | |
| 968 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 969 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 970 | EXPECT_EQ(0x2211, testVal); |
| 971 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0); |
| 972 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 973 | } |
| 974 | |
| 975 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator) |
| 976 | { |
| 977 | struct pldm_msgbuf _ctxExtract; |
| 978 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 979 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77}; |
| 980 | uint16_t testVal; |
| 981 | char* retBuff = NULL; |
| 982 | |
| 983 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 984 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 985 | EXPECT_EQ(0x2211, testVal); |
| 986 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 987 | -EOVERFLOW); |
| 988 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 989 | } |
| 990 | |
| 991 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_under) |
| 992 | { |
| 993 | struct pldm_msgbuf _ctxExtract; |
| 994 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 995 | |
| 996 | uint8_t src[1] = {}; |
| 997 | char* retBuff = NULL; |
| 998 | |
| 999 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1000 | ctxExtract->remaining = INTMAX_MIN; |
| 1001 | EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 1002 | 0); |
| 1003 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1004 | } |
| 1005 | |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1006 | static size_t str16len(char16_t* startptr) |
| 1007 | { |
| 1008 | char16_t* endptr = startptr; |
| 1009 | while (*endptr) |
| 1010 | { |
| 1011 | endptr++; |
| 1012 | } |
| 1013 | return endptr - startptr; |
| 1014 | } |
| 1015 | |
| 1016 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good) |
| 1017 | { |
| 1018 | struct pldm_msgbuf _ctxExtract; |
| 1019 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1020 | uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = { |
| 1021 | 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1022 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 1023 | const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1024 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1025 | uint16_t testVal; |
| 1026 | uint16_t testVal1; |
| 1027 | void* retBuff = NULL; |
| 1028 | |
| 1029 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1030 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1031 | EXPECT_EQ(0x2211, testVal); |
| 1032 | |
| 1033 | ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1034 | 0); |
| 1035 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1036 | EXPECT_EQ(0x1234, testVal1); |
| 1037 | |
| 1038 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1039 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1040 | EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData))); |
| 1041 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1042 | } |
| 1043 | |
| 1044 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2) |
| 1045 | { |
| 1046 | struct pldm_msgbuf _ctxExtract; |
| 1047 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1048 | uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1049 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12, |
| 1050 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 1051 | constexpr size_t required = 6; |
| 1052 | const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33, |
| 1053 | 0x6c44, 0x6f55, 0x0000}; |
| 1054 | const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000}; |
| 1055 | uint16_t testVal; |
| 1056 | uint16_t testVal1; |
| 1057 | char* retBuff = NULL; |
| 1058 | char* retBuff1 = NULL; |
| 1059 | size_t length = 0; |
| 1060 | |
| 1061 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1062 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1063 | EXPECT_EQ(0x2211, testVal); |
| 1064 | |
| 1065 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1066 | 0); |
| 1067 | |
| 1068 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1069 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1070 | EXPECT_EQ(memcmp(expectData, retBuff, |
| 1071 | sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)), |
| 1072 | 0); |
| 1073 | |
| 1074 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1075 | EXPECT_EQ(0x1234, testVal1); |
| 1076 | |
| 1077 | EXPECT_EQ( |
| 1078 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length), |
| 1079 | 0); |
| 1080 | |
| 1081 | EXPECT_EQ(0, length % 2); |
| 1082 | EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0); |
| 1083 | |
| 1084 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1085 | EXPECT_EQ(0x1234, testVal1); |
| 1086 | |
| 1087 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1088 | } |
| 1089 | |
| 1090 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args) |
| 1091 | { |
| 1092 | struct pldm_msgbuf _ctxExtract; |
| 1093 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1094 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1095 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1096 | uint16_t testVal; |
| 1097 | |
| 1098 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1099 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1100 | EXPECT_EQ(0x2211, testVal); |
| 1101 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0); |
| 1102 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1103 | } |
| 1104 | |
| 1105 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator) |
| 1106 | { |
| 1107 | struct pldm_msgbuf _ctxExtract; |
| 1108 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1109 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1110 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77}; |
| 1111 | uint16_t testVal; |
| 1112 | char16_t* retBuff = NULL; |
| 1113 | |
| 1114 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1115 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1116 | EXPECT_EQ(0x2211, testVal); |
| 1117 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1118 | -EOVERFLOW); |
| 1119 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1120 | } |
| 1121 | |
| 1122 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size) |
| 1123 | { |
| 1124 | struct pldm_msgbuf _ctxExtract; |
| 1125 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1126 | uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1127 | 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00}; |
| 1128 | uint16_t testVal; |
| 1129 | char16_t* retBuff = NULL; |
| 1130 | |
| 1131 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1132 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1133 | EXPECT_EQ(0x2211, testVal); |
| 1134 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1135 | -EOVERFLOW); |
| 1136 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1137 | } |
| 1138 | |
| 1139 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix) |
| 1140 | { |
| 1141 | struct pldm_msgbuf _ctxExtract; |
| 1142 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1143 | uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en" |
| 1144 | 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, |
| 1145 | 0x58, 0x00, 0x00, // Entity Name "S0S" |
| 1146 | 0x66, 0x6e, 0x00, // Language Tag "en" |
| 1147 | 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1" |
| 1148 | 0x67, 0x6e, 0x00, // Language Tag "en" |
| 1149 | 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, |
| 1150 | 0x00, // Entity Name "RR3" |
| 1151 | 0x77, 0x88}; |
| 1152 | uint8_t name_count; |
| 1153 | uint16_t test_val; |
| 1154 | char* tag = NULL; |
| 1155 | char* name = NULL; |
| 1156 | char* tag1 = NULL; |
| 1157 | char* name1 = NULL; |
| 1158 | char* tag2 = NULL; |
| 1159 | char* name2 = NULL; |
| 1160 | const char expectTag0[3] = {0x65, 0x6e, 0x00}; |
| 1161 | const char expectTag1[3] = {0x66, 0x6e, 0x00}; |
| 1162 | const char expectTag2[3] = {0x67, 0x6e, 0x00}; |
| 1163 | |
| 1164 | const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000}; |
| 1165 | const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000}; |
| 1166 | const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000}; |
| 1167 | size_t length = 0; |
| 1168 | |
| 1169 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1170 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0); |
| 1171 | EXPECT_EQ(0x2, name_count); |
| 1172 | |
| 1173 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0); |
| 1174 | EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0); |
| 1175 | |
| 1176 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL), |
| 1177 | 0); |
| 1178 | ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1)); |
| 1179 | EXPECT_EQ(5, str16len((char16_t*)name) + 1); |
| 1180 | EXPECT_EQ(memcmp(expectName0, name, |
| 1181 | sizeof(char16_t) * (str16len((char16_t*)name) + 1)), |
| 1182 | 0); |
| 1183 | |
| 1184 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length), |
| 1185 | 0); |
| 1186 | EXPECT_EQ(strncmp(expectTag1, tag1, length), 0); |
| 1187 | EXPECT_EQ( |
| 1188 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0); |
| 1189 | EXPECT_EQ(0, length % 2); |
| 1190 | EXPECT_EQ(memcmp(expectName1, name1, length), 0); |
| 1191 | |
| 1192 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL), |
| 1193 | 0); |
| 1194 | EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0); |
| 1195 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL), |
| 1196 | 0); |
| 1197 | ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1)); |
| 1198 | EXPECT_EQ(4, str16len((char16_t*)name2) + 1); |
| 1199 | EXPECT_EQ(memcmp(expectName2, name2, |
| 1200 | sizeof(char16_t) * (str16len((char16_t*)name2) + 1)), |
| 1201 | 0); |
| 1202 | |
| 1203 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0); |
| 1204 | EXPECT_EQ(0x8877, test_val); |
| 1205 | |
| 1206 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1207 | } |
| 1208 | |
| 1209 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_under) |
| 1210 | { |
| 1211 | struct pldm_msgbuf _ctxExtract; |
| 1212 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1213 | |
| 1214 | uint8_t src[1] = {}; |
| 1215 | char* retBuff = NULL; |
| 1216 | |
| 1217 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1218 | ctxExtract->remaining = INTMAX_MIN; |
| 1219 | EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1220 | 0); |
| 1221 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1222 | } |
| 1223 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1224 | TEST(msgbuf, pldm_msgbuf_span_remaining_good) |
| 1225 | { |
| 1226 | struct pldm_msgbuf _ctx; |
| 1227 | struct pldm_msgbuf* ctx = &_ctx; |
| 1228 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1229 | uint8_t buf[8] = {0}; |
| 1230 | uint16_t testVal; |
| 1231 | uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1232 | size_t remaining; |
| 1233 | uint8_t* retBuff = NULL; |
| 1234 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1235 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1236 | EXPECT_EQ( |
| 1237 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 1238 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1239 | |
| 1240 | struct pldm_msgbuf _ctxExtract; |
| 1241 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1242 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1243 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1244 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1245 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 1246 | EXPECT_EQ( |
| 1247 | pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining), |
| 1248 | PLDM_SUCCESS); |
| 1249 | |
| 1250 | EXPECT_EQ(remaining, sizeof(expectData)); |
| 1251 | EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0); |
| 1252 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1253 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 1254 | } |
| 1255 | |
| 1256 | TEST(msgbuf, pldm_msgbuf_span_remaining_bad) |
| 1257 | { |
| 1258 | struct pldm_msgbuf _ctx; |
| 1259 | struct pldm_msgbuf* ctx = &_ctx; |
| 1260 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1261 | uint8_t buf[8] = {0}; |
| 1262 | uint16_t testVal; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1263 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1264 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 1265 | EXPECT_EQ( |
| 1266 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 1267 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1268 | |
| 1269 | struct pldm_msgbuf _ctxExtract; |
| 1270 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1271 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1272 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1273 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1274 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1275 | |
| 1276 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1277 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 1278 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1279 | |
| 1280 | TEST(msgbuf, pldm_msgbuf_copy_good) |
| 1281 | { |
| 1282 | struct pldm_msgbuf _src; |
| 1283 | struct pldm_msgbuf* src = &_src; |
| 1284 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 1285 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1286 | ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1287 | PLDM_SUCCESS); |
| 1288 | |
| 1289 | struct pldm_msgbuf _dst; |
| 1290 | struct pldm_msgbuf* dst = &_dst; |
| 1291 | uint16_t checkVal = 0; |
| 1292 | uint8_t buf1[sizeof(buf)] = {}; |
| 1293 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1294 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1295 | PLDM_SUCCESS); |
| 1296 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS); |
| 1297 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1298 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1299 | PLDM_SUCCESS); |
| 1300 | EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS); |
| 1301 | |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1302 | EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS); |
| 1303 | EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1304 | |
| 1305 | EXPECT_EQ(buf[0], checkVal); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | TEST(msgbuf, pldm_msgbuf_copy_bad) |
| 1309 | { |
| 1310 | struct pldm_msgbuf _src; |
| 1311 | struct pldm_msgbuf* src = &_src; |
| 1312 | struct pldm_msgbuf _dst; |
| 1313 | struct pldm_msgbuf* dst = &_dst; |
| 1314 | uint8_t buf[1] = {sizeof(uint8_t)}; |
| 1315 | uint8_t buf1[1] = {sizeof(uint16_t)}; |
| 1316 | uint16_t value = 8; |
| 1317 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1318 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
| 1319 | 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] | 1320 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1321 | PLDM_ERROR_INVALID_LENGTH); |
| 1322 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1323 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS); |
| 1324 | 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] | 1325 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1326 | PLDM_ERROR_INVALID_LENGTH); |
| 1327 | } |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1328 | |
| 1329 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact) |
| 1330 | { |
| 1331 | const char msg[] = "this is a message"; |
| 1332 | |
| 1333 | struct pldm_msgbuf _src; |
| 1334 | struct pldm_msgbuf* src = &_src; |
| 1335 | struct pldm_msgbuf _dst; |
| 1336 | struct pldm_msgbuf* dst = &_dst; |
| 1337 | |
| 1338 | char buf[sizeof(msg)] = {}; |
| 1339 | |
| 1340 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1341 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1342 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1343 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1344 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1345 | EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf))); |
| 1346 | } |
| 1347 | |
| 1348 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src) |
| 1349 | { |
| 1350 | const char msg[] = "this is a message"; |
| 1351 | |
| 1352 | struct pldm_msgbuf _src; |
| 1353 | struct pldm_msgbuf* src = &_src; |
| 1354 | struct pldm_msgbuf _dst; |
| 1355 | struct pldm_msgbuf* dst = &_dst; |
| 1356 | |
| 1357 | char buf[sizeof(msg) + 1] = {}; |
| 1358 | |
| 1359 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1360 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1361 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1362 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1363 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1364 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1365 | } |
| 1366 | |
| 1367 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst) |
| 1368 | { |
| 1369 | const char msg[] = "this is a message"; |
| 1370 | |
| 1371 | struct pldm_msgbuf _src; |
| 1372 | struct pldm_msgbuf* src = &_src; |
| 1373 | struct pldm_msgbuf _dst; |
| 1374 | struct pldm_msgbuf* dst = &_dst; |
| 1375 | |
| 1376 | char buf[sizeof(msg) - 1] = {}; |
| 1377 | |
| 1378 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1379 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1380 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1381 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1382 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1383 | } |
| 1384 | |
| 1385 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src) |
| 1386 | { |
| 1387 | const char msg[] = {'a'}; |
| 1388 | |
| 1389 | struct pldm_msgbuf _src; |
| 1390 | struct pldm_msgbuf* src = &_src; |
| 1391 | struct pldm_msgbuf _dst; |
| 1392 | struct pldm_msgbuf* dst = &_dst; |
| 1393 | |
| 1394 | char buf[sizeof(msg)] = {}; |
| 1395 | |
| 1396 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1397 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1398 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1399 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1400 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1401 | } |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1402 | |
| 1403 | TEST(msgbuf, pldm_msgbuf_copy_utf16_exact) |
| 1404 | { |
| 1405 | const char16_t msg[] = u"this is a message"; |
| 1406 | |
| 1407 | struct pldm_msgbuf _src; |
| 1408 | struct pldm_msgbuf* src = &_src; |
| 1409 | struct pldm_msgbuf _dst; |
| 1410 | struct pldm_msgbuf* dst = &_dst; |
| 1411 | |
| 1412 | char buf[sizeof(msg)] = {}; |
| 1413 | |
| 1414 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1415 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1416 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1417 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1418 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1419 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1420 | } |
| 1421 | |
| 1422 | TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src) |
| 1423 | { |
| 1424 | const char16_t msg[] = u"this is a message"; |
| 1425 | |
| 1426 | struct pldm_msgbuf _src; |
| 1427 | struct pldm_msgbuf* src = &_src; |
| 1428 | struct pldm_msgbuf _dst; |
| 1429 | struct pldm_msgbuf* dst = &_dst; |
| 1430 | |
| 1431 | char buf[sizeof(msg) + 1] = {}; |
| 1432 | |
| 1433 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1434 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1435 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1436 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1437 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1438 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1439 | } |
| 1440 | |
| 1441 | TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst) |
| 1442 | { |
| 1443 | const char16_t msg[] = u"this is a message"; |
| 1444 | |
| 1445 | struct pldm_msgbuf _src; |
| 1446 | struct pldm_msgbuf* src = &_src; |
| 1447 | struct pldm_msgbuf _dst; |
| 1448 | struct pldm_msgbuf* dst = &_dst; |
| 1449 | |
| 1450 | char buf[sizeof(msg) - 1] = {}; |
| 1451 | |
| 1452 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1453 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1454 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1455 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1456 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1457 | } |
| 1458 | |
| 1459 | TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src) |
| 1460 | { |
| 1461 | const char16_t msg[] = {u'a'}; |
| 1462 | |
| 1463 | struct pldm_msgbuf _src; |
| 1464 | struct pldm_msgbuf* src = &_src; |
| 1465 | struct pldm_msgbuf _dst; |
| 1466 | struct pldm_msgbuf* dst = &_dst; |
| 1467 | |
| 1468 | char buf[sizeof(msg)] = {}; |
| 1469 | |
| 1470 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1471 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1472 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1473 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1474 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1475 | } |