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}; |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 876 | const size_t required = 4; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 877 | uint16_t testVal; |
| 878 | [[maybe_unused]] 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); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 881 | EXPECT_EQ( |
| 882 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 883 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 884 | |
| 885 | struct pldm_msgbuf _ctxExtract; |
| 886 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 887 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 888 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 889 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 890 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
Varsha Kaverappa | 7939382 | 2024-08-07 00:40:13 -0500 | [diff] [blame] | 891 | EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL), |
| 892 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 893 | |
| 894 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 895 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 896 | } |
| 897 | |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 898 | TEST(msgbuf, span_required_under) |
| 899 | { |
| 900 | struct pldm_msgbuf _ctx; |
| 901 | struct pldm_msgbuf* ctx = &_ctx; |
| 902 | |
| 903 | uint8_t buf[1] = {}; |
| 904 | void* cursor = nullptr; |
| 905 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 906 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, buf, 0), PLDM_SUCCESS); |
Andrew Jeffery | 2ff8cf8 | 2024-05-17 15:20:46 +0930 | [diff] [blame] | 907 | ctx->remaining = INTMAX_MIN; |
| 908 | EXPECT_NE(pldm_msgbuf_span_required(ctx, 1, &cursor), PLDM_SUCCESS); |
| 909 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH); |
| 910 | } |
| 911 | |
Thu Nguyen | 9c83d68 | 2024-07-02 08:43:09 +0000 | [diff] [blame] | 912 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good) |
| 913 | { |
| 914 | struct pldm_msgbuf _ctxExtract; |
| 915 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 916 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 917 | constexpr size_t required = 6; |
| 918 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 919 | uint16_t testVal; |
| 920 | uint8_t testVal1; |
| 921 | char* retBuff = NULL; |
| 922 | |
| 923 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 924 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 925 | EXPECT_EQ(0x2211, testVal); |
| 926 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 927 | 0); |
| 928 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 929 | EXPECT_EQ(0x77, testVal1); |
| 930 | |
| 931 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 932 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 933 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 934 | } |
| 935 | |
| 936 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_good_with_length) |
| 937 | { |
| 938 | struct pldm_msgbuf _ctxExtract; |
| 939 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 940 | uint8_t src[9] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77}; |
| 941 | constexpr size_t required = 6; |
| 942 | const char expectData[required] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 943 | uint16_t testVal; |
| 944 | uint8_t testVal1; |
| 945 | char* retBuff = NULL; |
| 946 | size_t length; |
| 947 | |
| 948 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 949 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 950 | EXPECT_EQ(0x2211, testVal); |
| 951 | EXPECT_EQ( |
| 952 | pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, &length), |
| 953 | 0); |
| 954 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &testVal1), 0); |
| 955 | EXPECT_EQ(0x77, testVal1); |
| 956 | |
| 957 | EXPECT_EQ(required, strlen(retBuff) + 1); |
| 958 | EXPECT_EQ(length, strlen(retBuff) + 1); |
| 959 | EXPECT_EQ(required, length); |
| 960 | EXPECT_EQ(strncmp(expectData, retBuff, strlen(retBuff) + 1), 0); |
| 961 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 962 | } |
| 963 | |
| 964 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_allow_null_args) |
| 965 | { |
| 966 | struct pldm_msgbuf _ctxExtract; |
| 967 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 968 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00}; |
| 969 | uint16_t testVal; |
| 970 | |
| 971 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 972 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 973 | EXPECT_EQ(0x2211, testVal); |
| 974 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, NULL, NULL), 0); |
| 975 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 976 | } |
| 977 | |
| 978 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_bad_no_terminator) |
| 979 | { |
| 980 | struct pldm_msgbuf _ctxExtract; |
| 981 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 982 | uint8_t src[8] = {0x11, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77}; |
| 983 | uint16_t testVal; |
| 984 | char* retBuff = NULL; |
| 985 | |
| 986 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 987 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 988 | EXPECT_EQ(0x2211, testVal); |
| 989 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 990 | -EOVERFLOW); |
| 991 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 992 | } |
| 993 | |
| 994 | TEST(msgbuf, pldm_msgbuf_span_string_ascii_under) |
| 995 | { |
| 996 | struct pldm_msgbuf _ctxExtract; |
| 997 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 998 | |
| 999 | uint8_t src[1] = {}; |
| 1000 | char* retBuff = NULL; |
| 1001 | |
| 1002 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1003 | ctxExtract->remaining = INTMAX_MIN; |
| 1004 | EXPECT_NE(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&retBuff, NULL), |
| 1005 | 0); |
| 1006 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1007 | } |
| 1008 | |
Thu Nguyen | 1523778 | 2024-07-02 09:30:41 +0000 | [diff] [blame] | 1009 | static size_t str16len(char16_t* startptr) |
| 1010 | { |
| 1011 | char16_t* endptr = startptr; |
| 1012 | while (*endptr) |
| 1013 | { |
| 1014 | endptr++; |
| 1015 | } |
| 1016 | return endptr - startptr; |
| 1017 | } |
| 1018 | |
| 1019 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good) |
| 1020 | { |
| 1021 | struct pldm_msgbuf _ctxExtract; |
| 1022 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1023 | uint8_t src[] __attribute__((aligned(alignof(char16_t)))) = { |
| 1024 | 0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1025 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 1026 | const char expectData[] = {0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1027 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1028 | uint16_t testVal; |
| 1029 | uint16_t testVal1; |
| 1030 | void* retBuff = NULL; |
| 1031 | |
| 1032 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1033 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1034 | EXPECT_EQ(0x2211, testVal); |
| 1035 | |
| 1036 | ASSERT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1037 | 0); |
| 1038 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1039 | EXPECT_EQ(0x1234, testVal1); |
| 1040 | |
| 1041 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1042 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1043 | EXPECT_EQ(0, memcmp(expectData, retBuff, sizeof(expectData))); |
| 1044 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1045 | } |
| 1046 | |
| 1047 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_good2) |
| 1048 | { |
| 1049 | struct pldm_msgbuf _ctxExtract; |
| 1050 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1051 | uint8_t src[24] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, 0x6c, |
| 1052 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12, |
| 1053 | 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00, 0x34, 0x12}; |
| 1054 | constexpr size_t required = 6; |
| 1055 | const char16_t expectData[required] = {0x6811, 0x6522, 0x6c33, |
| 1056 | 0x6c44, 0x6f55, 0x0000}; |
| 1057 | const char16_t expectData1[3] = {0x6c44, 0x6f55, 0x0000}; |
| 1058 | uint16_t testVal; |
| 1059 | uint16_t testVal1; |
| 1060 | char* retBuff = NULL; |
| 1061 | char* retBuff1 = NULL; |
| 1062 | size_t length = 0; |
| 1063 | |
| 1064 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1065 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1066 | EXPECT_EQ(0x2211, testVal); |
| 1067 | |
| 1068 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1069 | 0); |
| 1070 | |
| 1071 | ASSERT_EQ(0, (uintptr_t)retBuff & (alignof(char16_t) - 1)); |
| 1072 | EXPECT_EQ(6, str16len((char16_t*)retBuff) + 1); |
| 1073 | EXPECT_EQ(memcmp(expectData, retBuff, |
| 1074 | sizeof(char16_t) * (str16len((char16_t*)retBuff) + 1)), |
| 1075 | 0); |
| 1076 | |
| 1077 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1078 | EXPECT_EQ(0x1234, testVal1); |
| 1079 | |
| 1080 | EXPECT_EQ( |
| 1081 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff1, &length), |
| 1082 | 0); |
| 1083 | |
| 1084 | EXPECT_EQ(0, length % 2); |
| 1085 | EXPECT_EQ(memcmp(expectData1, retBuff1, length), 0); |
| 1086 | |
| 1087 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal1), 0); |
| 1088 | EXPECT_EQ(0x1234, testVal1); |
| 1089 | |
| 1090 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1091 | } |
| 1092 | |
| 1093 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_allow_null_args) |
| 1094 | { |
| 1095 | struct pldm_msgbuf _ctxExtract; |
| 1096 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1097 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1098 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x00, 0x00}; |
| 1099 | uint16_t testVal; |
| 1100 | |
| 1101 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1102 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1103 | EXPECT_EQ(0x2211, testVal); |
| 1104 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, NULL, NULL), 0); |
| 1105 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1106 | } |
| 1107 | |
| 1108 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_no_terminator) |
| 1109 | { |
| 1110 | struct pldm_msgbuf _ctxExtract; |
| 1111 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1112 | uint8_t src[14] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1113 | 0x6c, 0x44, 0x6c, 0x55, 0x6f, 0x66, 0x77}; |
| 1114 | uint16_t testVal; |
| 1115 | char16_t* retBuff = NULL; |
| 1116 | |
| 1117 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1118 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1119 | EXPECT_EQ(0x2211, testVal); |
| 1120 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1121 | -EOVERFLOW); |
| 1122 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1123 | } |
| 1124 | |
| 1125 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_bad_odd_size) |
| 1126 | { |
| 1127 | struct pldm_msgbuf _ctxExtract; |
| 1128 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1129 | uint8_t src[] = {0x11, 0x22, 0x11, 0x68, 0x22, 0x65, 0x33, |
| 1130 | 0x6c, 0x44, 0x6c, 0x55, 0x00, 0x00}; |
| 1131 | uint16_t testVal; |
| 1132 | char16_t* retBuff = NULL; |
| 1133 | |
| 1134 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1135 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), 0); |
| 1136 | EXPECT_EQ(0x2211, testVal); |
| 1137 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1138 | -EOVERFLOW); |
| 1139 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1140 | } |
| 1141 | |
| 1142 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_mix) |
| 1143 | { |
| 1144 | struct pldm_msgbuf _ctxExtract; |
| 1145 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1146 | uint8_t src[36] = {0x2, 0x65, 0x6e, 0x00, // Language Tag "en" |
| 1147 | 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, |
| 1148 | 0x58, 0x00, 0x00, // Entity Name "S0S" |
| 1149 | 0x66, 0x6e, 0x00, // Language Tag "en" |
| 1150 | 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1" |
| 1151 | 0x67, 0x6e, 0x00, // Language Tag "en" |
| 1152 | 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, |
| 1153 | 0x00, // Entity Name "RR3" |
| 1154 | 0x77, 0x88}; |
| 1155 | uint8_t name_count; |
| 1156 | uint16_t test_val; |
| 1157 | char* tag = NULL; |
| 1158 | char* name = NULL; |
| 1159 | char* tag1 = NULL; |
| 1160 | char* name1 = NULL; |
| 1161 | char* tag2 = NULL; |
| 1162 | char* name2 = NULL; |
| 1163 | const char expectTag0[3] = {0x65, 0x6e, 0x00}; |
| 1164 | const char expectTag1[3] = {0x66, 0x6e, 0x00}; |
| 1165 | const char expectTag2[3] = {0x67, 0x6e, 0x00}; |
| 1166 | |
| 1167 | const char16_t expectName0[5] = {0x5300, 0x3000, 0x5300, 0x5800, 0x0000}; |
| 1168 | const char16_t expectName1[3] = {0x5300, 0x3100, 0x0000}; |
| 1169 | const char16_t expectName2[4] = {0x5200, 0x5200, 0x3300, 0x0000}; |
| 1170 | size_t length = 0; |
| 1171 | |
| 1172 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, sizeof(src)), 0); |
| 1173 | EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &name_count), 0); |
| 1174 | EXPECT_EQ(0x2, name_count); |
| 1175 | |
| 1176 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag, NULL), 0); |
| 1177 | EXPECT_EQ(strncmp(expectTag0, tag, strlen(tag) + 1), 0); |
| 1178 | |
| 1179 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name, NULL), |
| 1180 | 0); |
| 1181 | ASSERT_EQ(0, (uintptr_t)name & (alignof(char16_t) - 1)); |
| 1182 | EXPECT_EQ(5, str16len((char16_t*)name) + 1); |
| 1183 | EXPECT_EQ(memcmp(expectName0, name, |
| 1184 | sizeof(char16_t) * (str16len((char16_t*)name) + 1)), |
| 1185 | 0); |
| 1186 | |
| 1187 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag1, &length), |
| 1188 | 0); |
| 1189 | EXPECT_EQ(strncmp(expectTag1, tag1, length), 0); |
| 1190 | EXPECT_EQ( |
| 1191 | pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name1, &length), 0); |
| 1192 | EXPECT_EQ(0, length % 2); |
| 1193 | EXPECT_EQ(memcmp(expectName1, name1, length), 0); |
| 1194 | |
| 1195 | EXPECT_EQ(pldm_msgbuf_span_string_ascii(ctxExtract, (void**)&tag2, NULL), |
| 1196 | 0); |
| 1197 | EXPECT_EQ(strncmp(expectTag2, tag2, strlen(tag2) + 1), 0); |
| 1198 | EXPECT_EQ(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&name2, NULL), |
| 1199 | 0); |
| 1200 | ASSERT_EQ(0, (uintptr_t)name2 & (alignof(char16_t) - 1)); |
| 1201 | EXPECT_EQ(4, str16len((char16_t*)name2) + 1); |
| 1202 | EXPECT_EQ(memcmp(expectName2, name2, |
| 1203 | sizeof(char16_t) * (str16len((char16_t*)name2) + 1)), |
| 1204 | 0); |
| 1205 | |
| 1206 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &test_val), 0); |
| 1207 | EXPECT_EQ(0x8877, test_val); |
| 1208 | |
| 1209 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), 0); |
| 1210 | } |
| 1211 | |
| 1212 | TEST(msgbuf, pldm_msgbuf_span_string_utf16_under) |
| 1213 | { |
| 1214 | struct pldm_msgbuf _ctxExtract; |
| 1215 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1216 | |
| 1217 | uint8_t src[1] = {}; |
| 1218 | char* retBuff = NULL; |
| 1219 | |
| 1220 | ASSERT_EQ(pldm_msgbuf_init_errno(ctxExtract, 0, src, 0), 0); |
| 1221 | ctxExtract->remaining = INTMAX_MIN; |
| 1222 | EXPECT_NE(pldm_msgbuf_span_string_utf16(ctxExtract, (void**)&retBuff, NULL), |
| 1223 | 0); |
| 1224 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), -EOVERFLOW); |
| 1225 | } |
| 1226 | |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1227 | TEST(msgbuf, pldm_msgbuf_span_remaining_good) |
| 1228 | { |
| 1229 | struct pldm_msgbuf _ctx; |
| 1230 | struct pldm_msgbuf* ctx = &_ctx; |
| 1231 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1232 | uint8_t buf[8] = {0}; |
| 1233 | uint16_t testVal; |
| 1234 | uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1235 | size_t remaining; |
| 1236 | uint8_t* retBuff = NULL; |
| 1237 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1238 | 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] | 1239 | EXPECT_EQ( |
| 1240 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 1241 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1242 | |
| 1243 | struct pldm_msgbuf _ctxExtract; |
| 1244 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1245 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1246 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1247 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1248 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
| 1249 | EXPECT_EQ( |
| 1250 | pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining), |
| 1251 | PLDM_SUCCESS); |
| 1252 | |
| 1253 | EXPECT_EQ(remaining, sizeof(expectData)); |
| 1254 | EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0); |
| 1255 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1256 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
| 1257 | } |
| 1258 | |
| 1259 | TEST(msgbuf, pldm_msgbuf_span_remaining_bad) |
| 1260 | { |
| 1261 | struct pldm_msgbuf _ctx; |
| 1262 | struct pldm_msgbuf* ctx = &_ctx; |
| 1263 | uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; |
| 1264 | uint8_t buf[8] = {0}; |
| 1265 | uint16_t testVal; |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1266 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1267 | 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] | 1268 | EXPECT_EQ( |
| 1269 | pldm_msgbuf_insert_array_uint8(ctx, sizeof(src), src, sizeof(src)), |
| 1270 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1271 | |
| 1272 | struct pldm_msgbuf _ctxExtract; |
| 1273 | struct pldm_msgbuf* ctxExtract = &_ctxExtract; |
| 1274 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1275 | ASSERT_EQ(pldm_msgbuf_init_cc(ctxExtract, 0, buf, sizeof(buf)), |
| 1276 | PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1277 | EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS); |
Thu Nguyen | 062c876 | 2023-04-22 20:45:04 +0700 | [diff] [blame] | 1278 | |
| 1279 | EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS); |
| 1280 | EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS); |
Andrew Jeffery | 07febdb | 2024-05-17 14:17:14 +0930 | [diff] [blame] | 1281 | } |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1282 | |
| 1283 | TEST(msgbuf, pldm_msgbuf_copy_good) |
| 1284 | { |
| 1285 | struct pldm_msgbuf _src; |
| 1286 | struct pldm_msgbuf* src = &_src; |
| 1287 | uint16_t buf[1] = {htole16(0x5aa5)}; |
| 1288 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1289 | ASSERT_EQ(pldm_msgbuf_init_cc(src, sizeof(buf), buf, sizeof(buf)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1290 | PLDM_SUCCESS); |
| 1291 | |
| 1292 | struct pldm_msgbuf _dst; |
| 1293 | struct pldm_msgbuf* dst = &_dst; |
| 1294 | uint16_t checkVal = 0; |
| 1295 | uint8_t buf1[sizeof(buf)] = {}; |
| 1296 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1297 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1298 | PLDM_SUCCESS); |
| 1299 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, buf[0], name), PLDM_SUCCESS); |
| 1300 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1301 | ASSERT_EQ(pldm_msgbuf_init_cc(dst, sizeof(buf1), buf1, sizeof(buf1)), |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1302 | PLDM_SUCCESS); |
| 1303 | EXPECT_EQ(pldm_msgbuf_extract_uint16(dst, &checkVal), PLDM_SUCCESS); |
| 1304 | |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1305 | EXPECT_EQ(pldm_msgbuf_destroy(src), PLDM_SUCCESS); |
| 1306 | EXPECT_EQ(pldm_msgbuf_destroy(dst), PLDM_SUCCESS); |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1307 | |
| 1308 | EXPECT_EQ(buf[0], checkVal); |
Varsha Kaverappa | 909bf7c | 2024-05-03 06:18:42 -0500 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | TEST(msgbuf, pldm_msgbuf_copy_bad) |
| 1312 | { |
| 1313 | struct pldm_msgbuf _src; |
| 1314 | struct pldm_msgbuf* src = &_src; |
| 1315 | struct pldm_msgbuf _dst; |
| 1316 | struct pldm_msgbuf* dst = &_dst; |
| 1317 | uint8_t buf[1] = {sizeof(uint8_t)}; |
| 1318 | uint8_t buf1[1] = {sizeof(uint16_t)}; |
| 1319 | uint16_t value = 8; |
| 1320 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1321 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf, sizeof(buf)), PLDM_SUCCESS); |
| 1322 | 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] | 1323 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1324 | PLDM_ERROR_INVALID_LENGTH); |
| 1325 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 1326 | ASSERT_EQ(pldm_msgbuf_init_cc(src, 0, buf1, sizeof(buf1)), PLDM_SUCCESS); |
| 1327 | 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] | 1328 | EXPECT_EQ(pldm_msgbuf_copy(dst, src, value, name), |
| 1329 | PLDM_ERROR_INVALID_LENGTH); |
| 1330 | } |
Andrew Jeffery | 8b87960 | 2024-07-08 12:50:19 +0930 | [diff] [blame] | 1331 | |
| 1332 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_exact) |
| 1333 | { |
| 1334 | const char msg[] = "this is a message"; |
| 1335 | |
| 1336 | struct pldm_msgbuf _src; |
| 1337 | struct pldm_msgbuf* src = &_src; |
| 1338 | struct pldm_msgbuf _dst; |
| 1339 | struct pldm_msgbuf* dst = &_dst; |
| 1340 | |
| 1341 | char buf[sizeof(msg)] = {}; |
| 1342 | |
| 1343 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1344 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1345 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1346 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1347 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1348 | EXPECT_EQ(0, memcmp(msg, buf, sizeof(buf))); |
| 1349 | } |
| 1350 | |
| 1351 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_dst_exceeds_src) |
| 1352 | { |
| 1353 | const char msg[] = "this is a message"; |
| 1354 | |
| 1355 | struct pldm_msgbuf _src; |
| 1356 | struct pldm_msgbuf* src = &_src; |
| 1357 | struct pldm_msgbuf _dst; |
| 1358 | struct pldm_msgbuf* dst = &_dst; |
| 1359 | |
| 1360 | char buf[sizeof(msg) + 1] = {}; |
| 1361 | |
| 1362 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1363 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1364 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), 0); |
| 1365 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1366 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1367 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1368 | } |
| 1369 | |
| 1370 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_src_exceeds_dst) |
| 1371 | { |
| 1372 | const char msg[] = "this is a message"; |
| 1373 | |
| 1374 | struct pldm_msgbuf _src; |
| 1375 | struct pldm_msgbuf* src = &_src; |
| 1376 | struct pldm_msgbuf _dst; |
| 1377 | struct pldm_msgbuf* dst = &_dst; |
| 1378 | |
| 1379 | char buf[sizeof(msg) - 1] = {}; |
| 1380 | |
| 1381 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1382 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1383 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1384 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1385 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1386 | } |
| 1387 | |
| 1388 | TEST(msgbuf, pldm_msgbuf_copy_string_ascii_unterminated_src) |
| 1389 | { |
| 1390 | const char msg[] = {'a'}; |
| 1391 | |
| 1392 | struct pldm_msgbuf _src; |
| 1393 | struct pldm_msgbuf* src = &_src; |
| 1394 | struct pldm_msgbuf _dst; |
| 1395 | struct pldm_msgbuf* dst = &_dst; |
| 1396 | |
| 1397 | char buf[sizeof(msg)] = {}; |
| 1398 | |
| 1399 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1400 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1401 | EXPECT_EQ(pldm_msgbuf_copy_string_ascii(dst, src), -EOVERFLOW); |
| 1402 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1403 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1404 | } |
Andrew Jeffery | 56f73f9 | 2024-07-08 12:50:28 +0930 | [diff] [blame] | 1405 | |
| 1406 | TEST(msgbuf, pldm_msgbuf_copy_utf16_exact) |
| 1407 | { |
| 1408 | const char16_t msg[] = u"this is a message"; |
| 1409 | |
| 1410 | struct pldm_msgbuf _src; |
| 1411 | struct pldm_msgbuf* src = &_src; |
| 1412 | struct pldm_msgbuf _dst; |
| 1413 | struct pldm_msgbuf* dst = &_dst; |
| 1414 | |
| 1415 | char buf[sizeof(msg)] = {}; |
| 1416 | |
| 1417 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1418 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1419 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1420 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1421 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1422 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1423 | } |
| 1424 | |
| 1425 | TEST(msgbuf, pldm_msgbuf_copy_utf16_dst_exceeds_src) |
| 1426 | { |
| 1427 | const char16_t msg[] = u"this is a message"; |
| 1428 | |
| 1429 | struct pldm_msgbuf _src; |
| 1430 | struct pldm_msgbuf* src = &_src; |
| 1431 | struct pldm_msgbuf _dst; |
| 1432 | struct pldm_msgbuf* dst = &_dst; |
| 1433 | |
| 1434 | char buf[sizeof(msg) + 1] = {}; |
| 1435 | |
| 1436 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1437 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1438 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), 0); |
| 1439 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1440 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1441 | EXPECT_EQ(0, memcmp(buf, msg, sizeof(msg))); |
| 1442 | } |
| 1443 | |
| 1444 | TEST(msgbuf, pldm_msgbuf_copy_utf16_src_exceeds_dst) |
| 1445 | { |
| 1446 | const char16_t msg[] = u"this is a message"; |
| 1447 | |
| 1448 | struct pldm_msgbuf _src; |
| 1449 | struct pldm_msgbuf* src = &_src; |
| 1450 | struct pldm_msgbuf _dst; |
| 1451 | struct pldm_msgbuf* dst = &_dst; |
| 1452 | |
| 1453 | char buf[sizeof(msg) - 1] = {}; |
| 1454 | |
| 1455 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1456 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1457 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1458 | ASSERT_EQ(pldm_msgbuf_destroy(dst), -EOVERFLOW); |
| 1459 | ASSERT_EQ(pldm_msgbuf_destroy(src), 0); |
| 1460 | } |
| 1461 | |
| 1462 | TEST(msgbuf, pldm_msgbuf_copy_utf16_unterminated_src) |
| 1463 | { |
| 1464 | const char16_t msg[] = {u'a'}; |
| 1465 | |
| 1466 | struct pldm_msgbuf _src; |
| 1467 | struct pldm_msgbuf* src = &_src; |
| 1468 | struct pldm_msgbuf _dst; |
| 1469 | struct pldm_msgbuf* dst = &_dst; |
| 1470 | |
| 1471 | char buf[sizeof(msg)] = {}; |
| 1472 | |
| 1473 | ASSERT_EQ(pldm_msgbuf_init_errno(src, 0, msg, sizeof(msg)), 0); |
| 1474 | ASSERT_EQ(pldm_msgbuf_init_errno(dst, 0, buf, sizeof(buf)), 0); |
| 1475 | EXPECT_EQ(pldm_msgbuf_copy_string_utf16(dst, src), -EOVERFLOW); |
| 1476 | ASSERT_EQ(pldm_msgbuf_destroy(dst), 0); |
| 1477 | ASSERT_EQ(pldm_msgbuf_destroy(src), -EOVERFLOW); |
| 1478 | } |