| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/byte_buf.h> |
| 7 |
#include <aws/common/private/byte_buf.h> |
| 8 |
|
| 9 |
#include <aws/testing/aws_test_harness.h> |
| 10 |
|
| 11 |
#include <ctype.h> |
| 12 |
|
| 13 |
#ifndef SSIZE_MAX |
| 14 |
# define SSIZE_MAX (SIZE_MAX >> 1) |
| 15 |
#endif |
| 16 |
|
| 17 |
AWS_TEST_CASE(nospec_index_test, s_nospec_index_test_fn) |
| 18 |
static int s_nospec_index_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 19 |
(void)allocator; |
| 20 |
(void)ctx; |
| 21 |
|
| 22 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(0, 1)); |
| 23 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(0, 0)); |
| 24 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(0, SSIZE_MAX)); |
| 25 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(0, SIZE_MAX)); |
| 26 |
|
| 27 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(1, 1)); |
| 28 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(1, 2)); |
| 29 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(1, 4)); |
| 30 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(1, SSIZE_MAX)); |
| 31 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(1, SIZE_MAX)); |
| 32 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(1, 0)); |
| 33 |
|
| 34 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(4, 3)); |
| 35 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask(4, 4)); |
| 36 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask(4, 5)); |
| 37 |
|
| 38 |
ASSERT_UINT_EQUALS(UINTPTR_MAX, aws_nospec_mask((SIZE_MAX >> 1) - 1, (SIZE_MAX >> 1))); |
| 39 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask((SIZE_MAX >> 1) + 1, (SIZE_MAX >> 1))); |
| 40 |
ASSERT_UINT_EQUALS(0, aws_nospec_mask((SIZE_MAX >> 1), (SIZE_MAX >> 1) + 1)); |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
#define ASSERT_NOADVANCE(advlen, cursorlen) \ |
| 46 |
do { \ |
| 47 |
struct aws_byte_cursor cursor; \ |
| 48 |
cursor.ptr = (uint8_t *)&cursor; \ |
| 49 |
cursor.len = (cursorlen); \ |
| 50 |
struct aws_byte_cursor rv = advance(&cursor, (advlen)); \ |
| 51 |
ASSERT_NULL(rv.ptr, "advance(cursorlen=%s, advlen=%s) should fail", #cursorlen, #advlen); \ |
| 52 |
ASSERT_UINT_EQUALS(0, rv.len, "advance(cursorlen=%s, advlen=%s) should fail", #cursorlen, #advlen); \ |
| 53 |
} while (0) |
| 54 |
|
| 55 |
#define ASSERT_ADVANCE(advlen, cursorlen) \ |
| 56 |
do { \ |
| 57 |
uint8_t *orig_cursor; \ |
| 58 |
struct aws_byte_cursor cursor; \ |
| 59 |
cursor.len = (cursorlen); \ |
| 60 |
cursor.ptr = orig_cursor = malloc(cursor.len); \ |
| 61 |
if (!cursor.ptr) { \ |
| 62 |
abort(); \ |
| 63 |
} \ |
| 64 |
struct aws_byte_cursor rv = advance(&cursor, (advlen)); \ |
| 65 |
ASSERT_PTR_EQUALS(orig_cursor, rv.ptr, "Wrong ptr in advance(cursorlen=%s, advlen=%s)", #cursorlen, #advlen); \ |
| 66 |
ASSERT_PTR_EQUALS(orig_cursor + (advlen), cursor.ptr, "Wrong new cursorptr in advance"); \ |
| 67 |
ASSERT_UINT_EQUALS((advlen), rv.len, "Wrong returned length"); \ |
| 68 |
ASSERT_UINT_EQUALS((cursorlen) - (advlen), cursor.len, "Wrong residual length"); \ |
| 69 |
free(orig_cursor); \ |
| 70 |
} while (0) |
| 71 |
|
| 72 |
static int s_test_byte_cursor_advance_internal( |
| 73 |
struct aws_byte_cursor (*advance)(struct aws_byte_cursor *cursor, size_t len)) { |
| 74 |
ASSERT_ADVANCE(0, 1); |
| 75 |
ASSERT_ADVANCE(1, 1); |
| 76 |
ASSERT_NOADVANCE(2, 1); |
| 77 |
|
| 78 |
ASSERT_ADVANCE(4, 5); |
| 79 |
ASSERT_ADVANCE(5, 5); |
| 80 |
ASSERT_NOADVANCE(6, 5); |
| 81 |
|
| 82 |
ASSERT_NOADVANCE((SIZE_MAX >> 1) + 1, (SIZE_MAX >> 1)); |
| 83 |
ASSERT_NOADVANCE((SIZE_MAX >> 1), (SIZE_MAX >> 1) + 1); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
AWS_TEST_CASE(test_byte_cursor_advance, s_test_byte_cursor_advance_fn) |
| 89 |
static int s_test_byte_cursor_advance_fn(struct aws_allocator *allocator, void *ctx) { |
| 90 |
(void)allocator; |
| 91 |
(void)ctx; |
| 92 |
|
| 93 |
return s_test_byte_cursor_advance_internal(aws_byte_cursor_advance); |
| 94 |
} |
| 95 |
|
| 96 |
AWS_TEST_CASE(test_byte_cursor_advance_nospec, s_test_byte_cursor_advance_nospec_fn) |
| 97 |
static int s_test_byte_cursor_advance_nospec_fn(struct aws_allocator *allocator, void *ctx) { |
| 98 |
(void)allocator; |
| 99 |
(void)ctx; |
| 100 |
|
| 101 |
return s_test_byte_cursor_advance_internal(aws_byte_cursor_advance_nospec); |
| 102 |
} |
| 103 |
|
| 104 |
static const uint8_t TEST_VECTOR[] = { |
| 105 |
0xaa, 0xbb, 0xaa, |
| 106 |
0xbb, 0xcc, 0xbb, |
| 107 |
0x42, |
| 108 |
0x12, 0x34, |
| 109 |
0x12, 0x34, |
| 110 |
0xab, 0xcd, 0xef, |
| 111 |
0x45, 0x67, 0x89, 0xab, |
| 112 |
0x45, 0x67, 0x89, 0xab, |
| 113 |
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, |
| 114 |
0x42, 0x42, 0x42, |
| 115 |
}; |
| 116 |
|
| 117 |
AWS_TEST_CASE(byte_cursor_write_tests, s_byte_cursor_write_tests_fn); |
| 118 |
static int s_byte_cursor_write_tests_fn(struct aws_allocator *allocator, void *ctx) { |
| 119 |
(void)allocator; |
| 120 |
(void)ctx; |
| 121 |
|
| 122 |
uint8_t buf[sizeof(TEST_VECTOR) + 1]; |
| 123 |
memset(buf, 0, sizeof(buf)); |
| 124 |
buf[sizeof(buf) - 1] = 0x99; |
| 125 |
|
| 126 |
struct aws_byte_buf cur = aws_byte_buf_from_empty_array(buf, sizeof(buf) - 1); |
| 127 |
|
| 128 |
uint8_t aba[] = {0xaa, 0xbb, 0xaa}; |
| 129 |
uint8_t bcb[] = {0xbb, 0xcc, 0xbb}; |
| 130 |
|
| 131 |
ASSERT_TRUE(aws_byte_buf_write(&cur, aba, sizeof(aba))); |
| 132 |
struct aws_byte_buf bcb_buf = aws_byte_buf_from_array(bcb, sizeof(bcb)); |
| 133 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_buffer(&cur, bcb_buf)); |
| 134 |
ASSERT_TRUE(aws_byte_buf_write_u8(&cur, 0x42)); |
| 135 |
ASSERT_TRUE(aws_byte_buf_write_be16(&cur, 0x1234)); |
| 136 |
ASSERT_TRUE(aws_byte_buf_write_be16(&cur, 0x1234)); |
| 137 |
ASSERT_TRUE(aws_byte_buf_write_be24(&cur, 0xabcdef)); |
| 138 |
ASSERT_TRUE(aws_byte_buf_write_be32(&cur, 0x456789ab)); |
| 139 |
ASSERT_TRUE(aws_byte_buf_write_be32(&cur, 0x456789ab)); |
| 140 |
ASSERT_TRUE(aws_byte_buf_write_be64(&cur, (uint64_t)0x1122334455667788ULL)); |
| 141 |
ASSERT_TRUE(aws_byte_buf_write_u8_n(&cur, 0x42, 3)); |
| 142 |
|
| 143 |
ASSERT_FALSE(aws_byte_buf_write_u8(&cur, 0xFF)); |
| 144 |
ASSERT_UINT_EQUALS(0x99, buf[sizeof(buf) - 1]); |
| 145 |
|
| 146 |
ASSERT_BIN_ARRAYS_EQUALS(TEST_VECTOR, sizeof(TEST_VECTOR), buf, sizeof(TEST_VECTOR)); |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
AWS_TEST_CASE(byte_cursor_read_tests, s_byte_cursor_read_tests_fn); |
| 152 |
static int s_byte_cursor_read_tests_fn(struct aws_allocator *allocator, void *ctx) { |
| 153 |
(void)allocator; |
| 154 |
(void)ctx; |
| 155 |
|
| 156 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(TEST_VECTOR, sizeof(TEST_VECTOR)); |
| 157 |
|
| 158 |
uint8_t aba[3] = {0}; |
| 159 |
uint8_t bcb[3] = {0}; |
| 160 |
ASSERT_TRUE(aws_byte_cursor_read(&cur, aba, sizeof(aba))); |
| 161 |
struct aws_byte_buf buf = aws_byte_buf_from_empty_array(bcb, sizeof(bcb)); |
| 162 |
ASSERT_TRUE(aws_byte_cursor_read_and_fill_buffer(&cur, &buf)); |
| 163 |
uint8_t aba_expect[] = {0xaa, 0xbb, 0xaa}, bcb_expect[] = {0xbb, 0xcc, 0xbb}; |
| 164 |
ASSERT_BIN_ARRAYS_EQUALS(aba_expect, 3, aba, 3); |
| 165 |
ASSERT_BIN_ARRAYS_EQUALS(bcb_expect, 3, bcb, 3); |
| 166 |
|
| 167 |
uint8_t u8; |
| 168 |
ASSERT_TRUE(aws_byte_cursor_read_u8(&cur, &u8)); |
| 169 |
ASSERT_UINT_EQUALS(u8, 0x42); |
| 170 |
uint16_t u16; |
| 171 |
ASSERT_TRUE(aws_byte_cursor_read_be16(&cur, &u16)); |
| 172 |
ASSERT_UINT_EQUALS(u16, 0x1234); |
| 173 |
ASSERT_TRUE(aws_byte_cursor_read_le16(&cur, &u16)); |
| 174 |
ASSERT_UINT_EQUALS(u16, 0x3412); |
| 175 |
uint32_t u24; |
| 176 |
ASSERT_TRUE(aws_byte_cursor_read_be24(&cur, &u24)); |
| 177 |
ASSERT_UINT_EQUALS(u24, 0xabcdef); |
| 178 |
uint32_t u32; |
| 179 |
ASSERT_TRUE(aws_byte_cursor_read_be32(&cur, &u32)); |
| 180 |
ASSERT_UINT_EQUALS(u32, 0x456789ab); |
| 181 |
ASSERT_TRUE(aws_byte_cursor_read_le32(&cur, &u32)); |
| 182 |
ASSERT_UINT_EQUALS(u32, 0xab896745); |
| 183 |
uint64_t u64; |
| 184 |
ASSERT_TRUE(aws_byte_cursor_read_be64(&cur, &u64)); |
| 185 |
ASSERT_UINT_EQUALS(u64, (uint64_t)0x1122334455667788ULL); |
| 186 |
|
| 187 |
|
| 188 |
ASSERT_UINT_EQUALS(3, aws_byte_cursor_advance(&cur, 3).len); |
| 189 |
|
| 190 |
ASSERT_FALSE(aws_byte_cursor_read_u8(&cur, &u8)); |
| 191 |
ASSERT_UINT_EQUALS(u8, 0x42); |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
AWS_TEST_CASE(byte_cursor_limit_tests, s_byte_cursor_limit_tests_fn); |
| 197 |
static int s_byte_cursor_limit_tests_fn(struct aws_allocator *allocator, void *ctx) { |
| 198 |
(void)allocator; |
| 199 |
(void)ctx; |
| 200 |
|
| 201 |
uint8_t buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 202 |
uint8_t starting_buf[sizeof(buf)]; |
| 203 |
memcpy(starting_buf, buf, sizeof(buf)); |
| 204 |
|
| 205 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(buf, sizeof(buf)); |
| 206 |
struct aws_byte_buf buffer = aws_byte_buf_from_empty_array(buf, sizeof(buf)); |
| 207 |
|
| 208 |
uint64_t u64 = 0; |
| 209 |
uint32_t u32 = 0; |
| 210 |
uint16_t u16 = 0; |
| 211 |
uint8_t u8 = 0; |
| 212 |
uint8_t arr[2] = {0}; |
| 213 |
struct aws_byte_buf arrbuf = aws_byte_buf_from_array(arr, sizeof(arr)); |
| 214 |
|
| 215 |
cur.len = 7; |
| 216 |
buffer.capacity = 7; |
| 217 |
ASSERT_FALSE(aws_byte_cursor_read_be64(&cur, &u64)); |
| 218 |
ASSERT_UINT_EQUALS(0, u64); |
| 219 |
ASSERT_FALSE(aws_byte_buf_write_be64(&buffer, 0)); |
| 220 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 221 |
|
| 222 |
cur.len = 3; |
| 223 |
buffer.capacity = 3; |
| 224 |
ASSERT_FALSE(aws_byte_cursor_read_be32(&cur, &u32)); |
| 225 |
ASSERT_UINT_EQUALS(0, u32); |
| 226 |
ASSERT_FALSE(aws_byte_buf_write_be32(&buffer, 0)); |
| 227 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 228 |
|
| 229 |
cur.len = 2; |
| 230 |
buffer.capacity = 2; |
| 231 |
ASSERT_FALSE(aws_byte_cursor_read_be32(&cur, &u32)); |
| 232 |
ASSERT_UINT_EQUALS(0, u32); |
| 233 |
ASSERT_FALSE(aws_byte_buf_write_be24(&buffer, 0)); |
| 234 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 235 |
|
| 236 |
cur.len = 1; |
| 237 |
buffer.capacity = 1; |
| 238 |
ASSERT_FALSE(aws_byte_cursor_read_be16(&cur, &u16)); |
| 239 |
ASSERT_UINT_EQUALS(0, u16); |
| 240 |
ASSERT_FALSE(aws_byte_buf_write_be16(&buffer, 0)); |
| 241 |
ASSERT_FALSE(aws_byte_cursor_read(&cur, arr, sizeof(arr))); |
| 242 |
ASSERT_FALSE(aws_byte_buf_write_from_whole_buffer(&buffer, arrbuf)); |
| 243 |
ASSERT_FALSE(aws_byte_cursor_read_and_fill_buffer(&cur, &arrbuf)); |
| 244 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 245 |
ASSERT_UINT_EQUALS(0, arr[0]); |
| 246 |
ASSERT_UINT_EQUALS(0, arr[1]); |
| 247 |
|
| 248 |
cur.len = 0; |
| 249 |
aws_byte_buf_clean_up(&buffer); |
| 250 |
ASSERT_FALSE(aws_byte_cursor_read_u8(&cur, &u8)); |
| 251 |
ASSERT_UINT_EQUALS(0, u8); |
| 252 |
ASSERT_FALSE(aws_byte_buf_write_u8(&buffer, 0)); |
| 253 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 254 |
|
| 255 |
ASSERT_FALSE(aws_byte_buf_write_u8_n(&buffer, 0x0, 8)); |
| 256 |
ASSERT_BIN_ARRAYS_EQUALS(buf, sizeof(buf), starting_buf, sizeof(starting_buf)); |
| 257 |
|
| 258 |
ASSERT_TRUE(aws_byte_cursor_read(&cur, arr, 0)); |
| 259 |
ASSERT_TRUE(aws_byte_buf_write(&buffer, arr, 0)); |
| 260 |
aws_byte_buf_clean_up(&arrbuf); |
| 261 |
ASSERT_TRUE(aws_byte_cursor_read_and_fill_buffer(&cur, &arrbuf)); |
| 262 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_buffer(&buffer, arrbuf)); |
| 263 |
ASSERT_UINT_EQUALS(0, arr[0]); |
| 264 |
ASSERT_UINT_EQUALS(0, arr[1]); |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
AWS_TEST_CASE(test_byte_cursor_read_hex_u8, s_test_byte_cursor_read_hex_u8) |
| 270 |
static int s_test_byte_cursor_read_hex_u8(struct aws_allocator *allocator, void *ctx) { |
| 271 |
(void)allocator; |
| 272 |
(void)ctx; |
| 273 |
|
| 274 |
struct aws_byte_cursor cur; |
| 275 |
uint8_t val = 0; |
| 276 |
|
| 277 |
cur = aws_byte_cursor_from_c_str("90"); |
| 278 |
ASSERT_TRUE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 279 |
ASSERT_UINT_EQUALS(0x90, val); |
| 280 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 281 |
|
| 282 |
cur = aws_byte_cursor_from_c_str("001"); |
| 283 |
ASSERT_TRUE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 284 |
ASSERT_UINT_EQUALS(0x00, val); |
| 285 |
ASSERT_UINT_EQUALS(1, cur.len); |
| 286 |
ASSERT_UINT_EQUALS('1', cur.ptr[0]); |
| 287 |
|
| 288 |
cur = aws_byte_cursor_from_c_str("Fa"); |
| 289 |
ASSERT_TRUE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 290 |
ASSERT_UINT_EQUALS(0xFA, val); |
| 291 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 292 |
|
| 293 |
|
| 294 |
cur = aws_byte_cursor_from_c_str("0"); |
| 295 |
ASSERT_FALSE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 296 |
ASSERT_UINT_EQUALS(1, cur.len); |
| 297 |
|
| 298 |
cur.len = 0; |
| 299 |
ASSERT_FALSE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 300 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 301 |
|
| 302 |
|
| 303 |
uint8_t bad_chars[][2] = { |
| 304 |
{'0', 0}, |
| 305 |
{'-', '0'}, |
| 306 |
{'/', '0'}, |
| 307 |
{'g', '0'}, |
| 308 |
{'x', '0'}, |
| 309 |
}; |
| 310 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(bad_chars); ++i) { |
| 311 |
cur = aws_byte_cursor_from_array(bad_chars[i], 2); |
| 312 |
ASSERT_FALSE(aws_byte_cursor_read_hex_u8(&cur, &val)); |
| 313 |
ASSERT_UINT_EQUALS(2, cur.len); |
| 314 |
} |
| 315 |
|
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
#define TEST_STRING "hello" |
| 320 |
|
| 321 |
static const char *s_empty = ""; |
| 322 |
static const char *s_all_whitespace = " \t\r\n "; |
| 323 |
static const char *s_left_whitespace = "\t \r" TEST_STRING; |
| 324 |
static const char *s_right_whitespace = TEST_STRING " \r \t \n"; |
| 325 |
static const char *s_both_whitespace = " \t \r\n " TEST_STRING " \r \t \n"; |
| 326 |
static const char *expected_non_empty_result = TEST_STRING; |
| 327 |
|
| 328 |
static int s_test_byte_cursor_right_trim_empty(struct aws_allocator *allocator, void *ctx) { |
| 329 |
(void)ctx; |
| 330 |
(void)allocator; |
| 331 |
|
| 332 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_empty); |
| 333 |
|
| 334 |
struct aws_byte_cursor result = aws_byte_cursor_right_trim_pred(&test_cursor, aws_isspace); |
| 335 |
|
| 336 |
ASSERT_TRUE(result.len == 0); |
| 337 |
|
| 338 |
return 0; |
| 339 |
} |
| 340 |
AWS_TEST_CASE(test_byte_cursor_right_trim_empty, s_test_byte_cursor_right_trim_empty) |
| 341 |
|
| 342 |
static int s_test_byte_cursor_right_trim_all_whitespace(struct aws_allocator *allocator, void *ctx) { |
| 343 |
(void)ctx; |
| 344 |
(void)allocator; |
| 345 |
|
| 346 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_all_whitespace); |
| 347 |
|
| 348 |
struct aws_byte_cursor result = aws_byte_cursor_right_trim_pred(&test_cursor, aws_isspace); |
| 349 |
|
| 350 |
ASSERT_TRUE(result.len == 0); |
| 351 |
|
| 352 |
return 0; |
| 353 |
} |
| 354 |
AWS_TEST_CASE(test_byte_cursor_right_trim_all_whitespace, s_test_byte_cursor_right_trim_all_whitespace) |
| 355 |
|
| 356 |
static int s_test_byte_cursor_right_trim_basic(struct aws_allocator *allocator, void *ctx) { |
| 357 |
(void)ctx; |
| 358 |
(void)allocator; |
| 359 |
|
| 360 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_right_whitespace); |
| 361 |
|
| 362 |
struct aws_byte_cursor result = aws_byte_cursor_right_trim_pred(&test_cursor, aws_isspace); |
| 363 |
|
| 364 |
size_t expected_length = strlen(expected_non_empty_result); |
| 365 |
ASSERT_TRUE(strncmp((const char *)result.ptr, expected_non_empty_result, expected_length) == 0); |
| 366 |
ASSERT_TRUE(result.len == expected_length); |
| 367 |
|
| 368 |
return 0; |
| 369 |
} |
| 370 |
AWS_TEST_CASE(test_byte_cursor_right_trim_basic, s_test_byte_cursor_right_trim_basic) |
| 371 |
|
| 372 |
static int s_test_byte_cursor_left_trim_empty(struct aws_allocator *allocator, void *ctx) { |
| 373 |
(void)ctx; |
| 374 |
(void)allocator; |
| 375 |
|
| 376 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_empty); |
| 377 |
|
| 378 |
struct aws_byte_cursor result = aws_byte_cursor_right_trim_pred(&test_cursor, aws_isspace); |
| 379 |
|
| 380 |
ASSERT_TRUE(result.len == 0); |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
AWS_TEST_CASE(test_byte_cursor_left_trim_empty, s_test_byte_cursor_left_trim_empty) |
| 385 |
|
| 386 |
static int s_test_byte_cursor_left_trim_all_whitespace(struct aws_allocator *allocator, void *ctx) { |
| 387 |
(void)ctx; |
| 388 |
(void)allocator; |
| 389 |
|
| 390 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_all_whitespace); |
| 391 |
|
| 392 |
struct aws_byte_cursor result = aws_byte_cursor_right_trim_pred(&test_cursor, aws_isspace); |
| 393 |
|
| 394 |
ASSERT_TRUE(result.len == 0); |
| 395 |
|
| 396 |
return 0; |
| 397 |
} |
| 398 |
AWS_TEST_CASE(test_byte_cursor_left_trim_all_whitespace, s_test_byte_cursor_left_trim_all_whitespace) |
| 399 |
|
| 400 |
static int s_test_byte_cursor_left_trim_basic(struct aws_allocator *allocator, void *ctx) { |
| 401 |
(void)ctx; |
| 402 |
(void)allocator; |
| 403 |
|
| 404 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_left_whitespace); |
| 405 |
|
| 406 |
struct aws_byte_cursor result = aws_byte_cursor_left_trim_pred(&test_cursor, aws_isspace); |
| 407 |
|
| 408 |
ASSERT_TRUE(strcmp((const char *)result.ptr, expected_non_empty_result) == 0); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
AWS_TEST_CASE(test_byte_cursor_left_trim_basic, s_test_byte_cursor_left_trim_basic) |
| 413 |
|
| 414 |
static int s_test_byte_cursor_trim_basic(struct aws_allocator *allocator, void *ctx) { |
| 415 |
(void)ctx; |
| 416 |
(void)allocator; |
| 417 |
|
| 418 |
struct aws_byte_cursor test_cursor = aws_byte_cursor_from_c_str(s_both_whitespace); |
| 419 |
|
| 420 |
struct aws_byte_cursor result = aws_byte_cursor_trim_pred(&test_cursor, aws_isspace); |
| 421 |
|
| 422 |
size_t expected_length = strlen(expected_non_empty_result); |
| 423 |
ASSERT_TRUE(strncmp((const char *)result.ptr, expected_non_empty_result, expected_length) == 0); |
| 424 |
ASSERT_TRUE(result.len == expected_length); |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
AWS_TEST_CASE(test_byte_cursor_trim_basic, s_test_byte_cursor_trim_basic) |
| 429 |
|
| 430 |
AWS_TEST_CASE(test_byte_cursor_read_be_i32, s_test_byte_cursor_read_be_i32) |
| 431 |
static int s_test_byte_cursor_read_be_i32(struct aws_allocator *allocator, void *ctx) { |
| 432 |
(void)allocator; |
| 433 |
(void)ctx; |
| 434 |
|
| 435 |
int32_t test_values[] = {0, 1, -1, 42, -42, INT32_MAX, INT32_MIN, 0x7F7F7F7F}; |
| 436 |
|
| 437 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(test_values); ++i) { |
| 438 |
uint8_t buf[4] = {0}; |
| 439 |
struct aws_byte_buf write_buf = aws_byte_buf_from_empty_array(buf, sizeof(buf)); |
| 440 |
ASSERT_TRUE(aws_byte_buf_write_be32(&write_buf, (uint32_t)test_values[i])); |
| 441 |
|
| 442 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(buf, sizeof(buf)); |
| 443 |
int32_t result = 0; |
| 444 |
ASSERT_TRUE(aws_byte_cursor_read_be_i32(&cur, &result)); |
| 445 |
ASSERT_INT_EQUALS(test_values[i], result); |
| 446 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 447 |
} |
| 448 |
|
| 449 |
uint32_t edge_case[] = {0, UINT32_MAX, 0x80000000, 0x7FFFFFFF, 0xABCDABCD }; |
| 450 |
int32_t edge_case_values[] = {0, -1, INT32_MIN, INT32_MAX, -1412584499}; |
| 451 |
|
| 452 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(edge_case_values); ++i) { |
| 453 |
uint8_t buf[4] = {0}; |
| 454 |
struct aws_byte_buf write_buf = aws_byte_buf_from_empty_array(buf, sizeof(buf)); |
| 455 |
ASSERT_TRUE(aws_byte_buf_write_be32(&write_buf, edge_case[i])); |
| 456 |
|
| 457 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(buf, sizeof(buf)); |
| 458 |
int32_t result = 0; |
| 459 |
ASSERT_TRUE(aws_byte_cursor_read_be_i32(&cur, &result)); |
| 460 |
ASSERT_INT_EQUALS(edge_case_values[i], result); |
| 461 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
uint8_t short_buf[3] = {0xFF, 0xFF, 0xFF}; |
| 466 |
struct aws_byte_cursor short_cur = aws_byte_cursor_from_array(short_buf, sizeof(short_buf)); |
| 467 |
int32_t unchanged = 0; |
| 468 |
ASSERT_FALSE(aws_byte_cursor_read_be_i32(&short_cur, &unchanged)); |
| 469 |
ASSERT_INT_EQUALS(0, unchanged); |
| 470 |
ASSERT_UINT_EQUALS(3, short_cur.len); |
| 471 |
|
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
AWS_TEST_CASE(test_byte_cursor_read_le_i32, s_test_byte_cursor_read_le_i32) |
| 476 |
static int s_test_byte_cursor_read_le_i32(struct aws_allocator *allocator, void *ctx) { |
| 477 |
(void)allocator; |
| 478 |
(void)ctx; |
| 479 |
|
| 480 |
if (aws_is_big_endian()) { |
| 481 |
return AWS_OP_SKIP; |
| 482 |
} |
| 483 |
|
| 484 |
int32_t test_values[] = {0, 1, -1, 42, -42, INT32_MAX, INT32_MIN, 0x7F7F7F7F}; |
| 485 |
|
| 486 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(test_values); ++i) { |
| 487 |
uint8_t buf[4] = {0}; |
| 488 |
struct aws_byte_buf write_buf = aws_byte_buf_from_empty_array(buf, sizeof(buf)); |
| 489 |
|
| 490 |
ASSERT_TRUE(aws_byte_buf_write(&write_buf, (uint8_t *)&test_values[i], 4)); |
| 491 |
|
| 492 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(buf, sizeof(buf)); |
| 493 |
int32_t result = 0; |
| 494 |
ASSERT_TRUE(aws_byte_cursor_read_le_i32(&cur, &result)); |
| 495 |
ASSERT_INT_EQUALS(test_values[i], result); |
| 496 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 497 |
} |
| 498 |
|
| 499 |
uint32_t edge_case[] = {0, UINT32_MAX, 0x80000000, 0x7FFFFFFF, 0xABCDABCD }; |
| 500 |
int32_t edge_case_values[] = {0, -1, INT32_MIN, INT32_MAX, -1412584499}; |
| 501 |
|
| 502 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(edge_case_values); ++i) { |
| 503 |
uint8_t buf[4] = {0}; |
| 504 |
struct aws_byte_buf write_buf = aws_byte_buf_from_empty_array(buf, sizeof(buf)); |
| 505 |
ASSERT_TRUE(aws_byte_buf_write(&write_buf, (uint8_t *)&edge_case[i], 4)); |
| 506 |
|
| 507 |
struct aws_byte_cursor cur = aws_byte_cursor_from_array(buf, sizeof(buf)); |
| 508 |
int32_t result = 0; |
| 509 |
ASSERT_TRUE(aws_byte_cursor_read_le_i32(&cur, &result)); |
| 510 |
ASSERT_INT_EQUALS(edge_case_values[i], result); |
| 511 |
ASSERT_UINT_EQUALS(0, cur.len); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
uint8_t short_buf[3] = {0xFF, 0xFF, 0xFF}; |
| 516 |
struct aws_byte_cursor short_cur = aws_byte_cursor_from_array(short_buf, sizeof(short_buf)); |
| 517 |
int32_t unchanged = 0; |
| 518 |
ASSERT_FALSE(aws_byte_cursor_read_le_i32(&short_cur, &unchanged)); |
| 519 |
ASSERT_INT_EQUALS(0, unchanged); |
| 520 |
ASSERT_UINT_EQUALS(3, short_cur.len); |
| 521 |
|
| 522 |
return 0; |
| 523 |
} |