| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/encoding.h> |
| 7 |
#include <aws/common/file.h> |
| 8 |
#include <aws/common/logging.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
|
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
static int s_run_hex_encoding_test_case( |
| 16 |
struct aws_allocator *allocator, |
| 17 |
const char *test_str, |
| 18 |
size_t test_str_len, |
| 19 |
const char *expected, |
| 20 |
size_t expected_len) { |
| 21 |
size_t output_len = 0; |
| 22 |
|
| 23 |
ASSERT_SUCCESS( |
| 24 |
aws_hex_compute_encoded_len(test_str_len, &output_len), |
| 25 |
"compute hex encoded len failed with error %d", |
| 26 |
aws_last_error()); |
| 27 |
ASSERT_INT_EQUALS(expected_len, output_len, "Output len on buffer should be %d", expected_len); |
| 28 |
|
| 29 |
struct aws_byte_cursor to_encode = aws_byte_cursor_from_array(test_str, test_str_len); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct aws_byte_buf allocation; |
| 34 |
ASSERT_SUCCESS(aws_byte_buf_init(&allocation, allocator, output_len + 2)); |
| 35 |
memset(allocation.buffer, 0xdd, allocation.capacity); |
| 36 |
|
| 37 |
struct aws_byte_buf output = aws_byte_buf_from_empty_array(allocation.buffer + 1, output_len); |
| 38 |
|
| 39 |
ASSERT_SUCCESS(aws_hex_encode(&to_encode, &output), "encode call should have succeeded"); |
| 40 |
|
| 41 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 42 |
expected, expected_len, output.buffer, output_len, "Encode output should have been {%s}", expected); |
| 43 |
ASSERT_INT_EQUALS(output_len, output.len); |
| 44 |
ASSERT_INT_EQUALS( |
| 45 |
(unsigned char)*(allocation.buffer), |
| 46 |
(unsigned char)0xdd, |
| 47 |
"Write should not have occurred before the start of the buffer."); |
| 48 |
ASSERT_INT_EQUALS( |
| 49 |
(unsigned char)*(allocation.buffer + output_len + 1), |
| 50 |
(unsigned char)0xdd, |
| 51 |
"Write should not have occurred after the end of the buffer."); |
| 52 |
|
| 53 |
ASSERT_SUCCESS( |
| 54 |
aws_hex_compute_decoded_len(expected_len, &output_len), |
| 55 |
"compute hex decoded len failed with error %d", |
| 56 |
aws_last_error()); |
| 57 |
memset(allocation.buffer, 0xdd, allocation.capacity); |
| 58 |
|
| 59 |
ASSERT_INT_EQUALS(test_str_len, output_len, "Output len on buffer should be %d", test_str_len); |
| 60 |
aws_byte_buf_reset(&output, false); |
| 61 |
|
| 62 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, expected_len); |
| 63 |
ASSERT_SUCCESS(aws_hex_decode(&expected_buf, &output), "decode call should have succeeded"); |
| 64 |
|
| 65 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 66 |
test_str, test_str_len, output.buffer, output_len, "Decode output should have been %s.", test_str); |
| 67 |
ASSERT_INT_EQUALS(output_len, output.len); |
| 68 |
ASSERT_INT_EQUALS( |
| 69 |
(unsigned char)*(allocation.buffer), |
| 70 |
(unsigned char)0xdd, |
| 71 |
"Write should not have occurred before the start of the buffer."); |
| 72 |
ASSERT_INT_EQUALS( |
| 73 |
(unsigned char)*(allocation.buffer + output_len + 1), |
| 74 |
(unsigned char)0xdd, |
| 75 |
"Write should not have occurred after the start of the buffer."); |
| 76 |
|
| 77 |
aws_byte_buf_clean_up(&allocation); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int s_hex_encoding_test_case_empty(struct aws_allocator *allocator, void *ctx) { |
| 82 |
(void)ctx; |
| 83 |
|
| 84 |
char test_data[] = ""; |
| 85 |
char expected[] = ""; |
| 86 |
|
| 87 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 88 |
} |
| 89 |
|
| 90 |
AWS_TEST_CASE(hex_encoding_test_case_empty_test, s_hex_encoding_test_case_empty) |
| 91 |
|
| 92 |
static int s_hex_encoding_test_case_f(struct aws_allocator *allocator, void *ctx) { |
| 93 |
(void)ctx; |
| 94 |
|
| 95 |
char test_data[] = "f"; |
| 96 |
char expected[] = "66"; |
| 97 |
|
| 98 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 99 |
} |
| 100 |
|
| 101 |
AWS_TEST_CASE(hex_encoding_test_case_f_test, s_hex_encoding_test_case_f) |
| 102 |
|
| 103 |
static int s_hex_encoding_test_case_fo(struct aws_allocator *allocator, void *ctx) { |
| 104 |
(void)ctx; |
| 105 |
|
| 106 |
char test_data[] = "fo"; |
| 107 |
char expected[] = "666f"; |
| 108 |
|
| 109 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 110 |
} |
| 111 |
|
| 112 |
AWS_TEST_CASE(hex_encoding_test_case_fo_test, s_hex_encoding_test_case_fo) |
| 113 |
|
| 114 |
static int s_hex_encoding_test_case_foo(struct aws_allocator *allocator, void *ctx) { |
| 115 |
(void)ctx; |
| 116 |
|
| 117 |
char test_data[] = "foo"; |
| 118 |
char expected[] = "666f6f"; |
| 119 |
|
| 120 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 121 |
} |
| 122 |
|
| 123 |
AWS_TEST_CASE(hex_encoding_test_case_foo_test, s_hex_encoding_test_case_foo) |
| 124 |
|
| 125 |
static int s_hex_encoding_test_case_foob(struct aws_allocator *allocator, void *ctx) { |
| 126 |
(void)ctx; |
| 127 |
|
| 128 |
char test_data[] = "foob"; |
| 129 |
char expected[] = "666f6f62"; |
| 130 |
|
| 131 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 132 |
} |
| 133 |
|
| 134 |
AWS_TEST_CASE(hex_encoding_test_case_foob_test, s_hex_encoding_test_case_foob) |
| 135 |
|
| 136 |
static int s_hex_encoding_test_case_fooba(struct aws_allocator *allocator, void *ctx) { |
| 137 |
(void)ctx; |
| 138 |
|
| 139 |
char test_data[] = "fooba"; |
| 140 |
char expected[] = "666f6f6261"; |
| 141 |
|
| 142 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 143 |
} |
| 144 |
|
| 145 |
AWS_TEST_CASE(hex_encoding_test_case_fooba_test, s_hex_encoding_test_case_fooba) |
| 146 |
|
| 147 |
static int s_hex_encoding_test_case_foobar(struct aws_allocator *allocator, void *ctx) { |
| 148 |
(void)ctx; |
| 149 |
|
| 150 |
char test_data[] = "foobar"; |
| 151 |
char expected[] = "666f6f626172"; |
| 152 |
|
| 153 |
return s_run_hex_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected)); |
| 154 |
} |
| 155 |
|
| 156 |
AWS_TEST_CASE(hex_encoding_test_case_foobar_test, s_hex_encoding_test_case_foobar) |
| 157 |
|
| 158 |
static int s_hex_encoding_test_case_missing_leading_zero_fn(struct aws_allocator *allocator, void *ctx) { |
| 159 |
(void)allocator; |
| 160 |
(void)ctx; |
| 161 |
|
| 162 |
uint8_t expected[] = {0x01, 0x02, 0x03, 0x04}; |
| 163 |
char test_data[] = "1020304"; |
| 164 |
|
| 165 |
uint8_t output[sizeof(expected)] = {0}; |
| 166 |
|
| 167 |
struct aws_byte_cursor test_buf = aws_byte_cursor_from_c_str(test_data); |
| 168 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(expected)); |
| 169 |
|
| 170 |
ASSERT_SUCCESS( |
| 171 |
aws_hex_decode(&test_buf, &output_buf), |
| 172 |
"Hex decoding failed with " |
| 173 |
"error code %d", |
| 174 |
aws_last_error()); |
| 175 |
|
| 176 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 177 |
expected, sizeof(expected), output, sizeof(output), "Hex decode expected output did not match actual output"); |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
AWS_TEST_CASE(hex_encoding_test_case_missing_leading_zero, s_hex_encoding_test_case_missing_leading_zero_fn) |
| 183 |
|
| 184 |
static int s_hex_encoding_invalid_buffer_size_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 185 |
(void)allocator; |
| 186 |
(void)ctx; |
| 187 |
|
| 188 |
char test_data[] = "foobar"; |
| 189 |
size_t size_too_small = 2; |
| 190 |
uint8_t output[] = {0, 0}; |
| 191 |
|
| 192 |
struct aws_byte_cursor test_buf = aws_byte_cursor_from_c_str(test_data); |
| 193 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, size_too_small); |
| 194 |
|
| 195 |
ASSERT_ERROR( |
| 196 |
AWS_ERROR_SHORT_BUFFER, |
| 197 |
aws_hex_encode(&test_buf, &output_buf), |
| 198 |
"Invalid buffer size should have failed with AWS_ERROR_SHORT_BUFFER"); |
| 199 |
|
| 200 |
ASSERT_ERROR( |
| 201 |
AWS_ERROR_SHORT_BUFFER, |
| 202 |
aws_hex_decode(&test_buf, &output_buf), |
| 203 |
"Invalid buffer size should have failed with AWS_ERROR_SHORT_BUFFER"); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
AWS_TEST_CASE(hex_encoding_invalid_buffer_size_test, s_hex_encoding_invalid_buffer_size_test_fn) |
| 208 |
|
| 209 |
static int s_hex_encoding_highbyte_string_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 210 |
(void)allocator; |
| 211 |
(void)ctx; |
| 212 |
|
| 213 |
char bad_input[] = "66\xb6\xb6" |
| 214 |
"6f6f6617"; |
| 215 |
uint8_t output[sizeof(bad_input)] = {0}; |
| 216 |
|
| 217 |
struct aws_byte_cursor bad_buf = aws_byte_cursor_from_c_str(bad_input); |
| 218 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 219 |
|
| 220 |
ASSERT_ERROR(AWS_ERROR_INVALID_HEX_STR, aws_hex_decode(&bad_buf, &output_buf)); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
AWS_TEST_CASE(hex_encoding_highbyte_string_test, s_hex_encoding_highbyte_string_test_fn) |
| 224 |
|
| 225 |
static int s_hex_encoding_overflow_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 226 |
(void)allocator; |
| 227 |
(void)ctx; |
| 228 |
|
| 229 |
char test_data[] = "foobar"; |
| 230 |
|
| 231 |
|
| 232 |
size_t overflow = (SIZE_MAX - 1); |
| 233 |
uint8_t output[] = {0, 0}; |
| 234 |
|
| 235 |
struct aws_byte_cursor test_buf = aws_byte_cursor_from_array(test_data, overflow); |
| 236 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 237 |
|
| 238 |
ASSERT_ERROR( |
| 239 |
AWS_ERROR_OVERFLOW_DETECTED, |
| 240 |
aws_hex_encode(&test_buf, &output_buf), |
| 241 |
"overflow buffer size should have failed with AWS_ERROR_OVERFLOW_DETECTED"); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
AWS_TEST_CASE(hex_encoding_overflow_test, s_hex_encoding_overflow_test_fn) |
| 246 |
|
| 247 |
static int s_hex_encoding_invalid_string_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 248 |
(void)allocator; |
| 249 |
(void)ctx; |
| 250 |
|
| 251 |
char bad_input[] = "666f6f6x6172"; |
| 252 |
uint8_t output[sizeof(bad_input)] = {0}; |
| 253 |
|
| 254 |
struct aws_byte_cursor bad_buf = aws_byte_cursor_from_c_str(bad_input); |
| 255 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 256 |
|
| 257 |
ASSERT_ERROR( |
| 258 |
AWS_ERROR_INVALID_HEX_STR, |
| 259 |
aws_hex_decode(&bad_buf, &output_buf), |
| 260 |
"An invalid string should have failed with AWS_ERROR_INVALID_HEX_STR"); |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
AWS_TEST_CASE(hex_encoding_invalid_string_test, s_hex_encoding_invalid_string_test_fn) |
| 265 |
|
| 266 |
AWS_STATIC_STRING_FROM_LITERAL(s_base64_encode_prefix, "Prefix"); |
| 267 |
|
| 268 |
|
| 269 |
static int s_run_base64_encoding_test_case( |
| 270 |
struct aws_allocator *allocator, |
| 271 |
const char *test_str, |
| 272 |
size_t test_str_len, |
| 273 |
const char *expected, |
| 274 |
size_t expected_len, |
| 275 |
bool url_safe) { |
| 276 |
|
| 277 |
size_t output_len = 0; |
| 278 |
|
| 279 |
|
| 280 |
ASSERT_SUCCESS( |
| 281 |
url_safe ? aws_base64_url_compute_encoded_len(test_str_len, &output_len) |
| 282 |
: aws_base64_compute_encoded_len(test_str_len, &output_len), |
| 283 |
"Compute base64 encoded length failed with %d", |
| 284 |
aws_last_error()); |
| 285 |
ASSERT_INT_EQUALS(expected_len, output_len, "Output len on string should be %d", expected_len); |
| 286 |
|
| 287 |
struct aws_byte_cursor to_encode = aws_byte_cursor_from_array(test_str, test_str_len); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
struct aws_byte_buf allocation; |
| 292 |
ASSERT_SUCCESS(aws_byte_buf_init(&allocation, allocator, output_len + 2)); |
| 293 |
memset(allocation.buffer, 0xdd, allocation.capacity); |
| 294 |
|
| 295 |
struct aws_byte_buf output = aws_byte_buf_from_empty_array(allocation.buffer + 1, output_len); |
| 296 |
|
| 297 |
ASSERT_SUCCESS( |
| 298 |
url_safe ? aws_base64_url_encode(&to_encode, &output) : aws_base64_encode(&to_encode, &output), |
| 299 |
"encode call should have succeeded"); |
| 300 |
|
| 301 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 302 |
expected, |
| 303 |
expected_len, |
| 304 |
output.buffer, |
| 305 |
output.len, |
| 306 |
"Encode output should have been {%s}, but got " PRInSTR, |
| 307 |
expected, |
| 308 |
AWS_BYTE_BUF_PRI(output)); |
| 309 |
ASSERT_INT_EQUALS( |
| 310 |
(unsigned char)*(allocation.buffer), |
| 311 |
(unsigned char)0xdd, |
| 312 |
"Write should not have occurred before the start of the buffer."); |
| 313 |
ASSERT_INT_EQUALS( |
| 314 |
(unsigned char)*(allocation.buffer + output_len + 1), |
| 315 |
(unsigned char)0xdd, |
| 316 |
"Write should not have occurred after the end of the buffer."); |
| 317 |
|
| 318 |
aws_byte_buf_clean_up(&allocation); |
| 319 |
|
| 320 |
|
| 321 |
ASSERT_SUCCESS(aws_byte_buf_init(&allocation, allocator, output_len + s_base64_encode_prefix->len)); |
| 322 |
struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_base64_encode_prefix); |
| 323 |
ASSERT_SUCCESS(aws_byte_buf_append(&allocation, &prefix_cursor)); |
| 324 |
|
| 325 |
ASSERT_SUCCESS( |
| 326 |
url_safe ? aws_base64_url_encode(&to_encode, &allocation) : aws_base64_encode(&to_encode, &allocation), |
| 327 |
"encode call should have succeeded"); |
| 328 |
|
| 329 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 330 |
expected, |
| 331 |
expected_len, |
| 332 |
allocation.buffer + s_base64_encode_prefix->len, |
| 333 |
expected_len, |
| 334 |
"Encode output should have been {%s}", |
| 335 |
expected); |
| 336 |
|
| 337 |
struct aws_byte_cursor prefix_output = {.ptr = allocation.buffer, .len = s_base64_encode_prefix->len}; |
| 338 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 339 |
s_base64_encode_prefix->bytes, |
| 340 |
s_base64_encode_prefix->len, |
| 341 |
allocation.buffer, |
| 342 |
s_base64_encode_prefix->len, |
| 343 |
"Encode prefix should have been {%s}, was {" PRInSTR "}.", |
| 344 |
s_base64_encode_prefix->bytes, |
| 345 |
AWS_BYTE_CURSOR_PRI(prefix_output)); |
| 346 |
|
| 347 |
aws_byte_buf_clean_up(&allocation); |
| 348 |
|
| 349 |
|
| 350 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, expected_len); |
| 351 |
ASSERT_SUCCESS( |
| 352 |
aws_base64_compute_decoded_len(&expected_cur, &output_len), |
| 353 |
"Compute base64 decoded length failed with %d", |
| 354 |
aws_last_error()); |
| 355 |
ASSERT_INT_EQUALS(test_str_len, output_len, "Output len on string should be %d", test_str_len); |
| 356 |
|
| 357 |
ASSERT_SUCCESS(aws_byte_buf_init(&allocation, allocator, output_len + 2)); |
| 358 |
memset(allocation.buffer, 0xdd, allocation.capacity); |
| 359 |
|
| 360 |
output = aws_byte_buf_from_empty_array(allocation.buffer + 1, output_len); |
| 361 |
|
| 362 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, expected_len); |
| 363 |
ASSERT_SUCCESS(aws_base64_decode(&expected_buf, &output), "decode call should have succeeded"); |
| 364 |
|
| 365 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 366 |
test_str, |
| 367 |
test_str_len, |
| 368 |
output.buffer, |
| 369 |
output_len, |
| 370 |
"Decode output should have been {%s} (len=%zu).", |
| 371 |
test_str, |
| 372 |
test_str_len); |
| 373 |
ASSERT_INT_EQUALS( |
| 374 |
(unsigned char)*(allocation.buffer), |
| 375 |
(unsigned char)0xdd, |
| 376 |
"Write should not have occurred before the start of the buffer."); |
| 377 |
ASSERT_INT_EQUALS( |
| 378 |
(unsigned char)*(allocation.buffer + output_len + 1), |
| 379 |
(unsigned char)0xdd, |
| 380 |
"Write should not have occurred after the start of the buffer."); |
| 381 |
|
| 382 |
aws_byte_buf_clean_up(&allocation); |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
static int s_base64_encoding_test_case_empty(struct aws_allocator *allocator, void *ctx) { |
| 388 |
(void)ctx; |
| 389 |
|
| 390 |
char test_data[] = ""; |
| 391 |
char expected[] = ""; |
| 392 |
|
| 393 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 394 |
} |
| 395 |
AWS_TEST_CASE(base64_encoding_test_case_empty_test, s_base64_encoding_test_case_empty) |
| 396 |
|
| 397 |
static int s_base64_url_encoding_test_case_empty(struct aws_allocator *allocator, void *ctx) { |
| 398 |
(void)ctx; |
| 399 |
|
| 400 |
char test_data[] = ""; |
| 401 |
char expected[] = ""; |
| 402 |
|
| 403 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 404 |
} |
| 405 |
AWS_TEST_CASE(base64_url_encoding_test_case_empty_test, s_base64_url_encoding_test_case_empty) |
| 406 |
|
| 407 |
static int s_base64_encoding_test_case_f(struct aws_allocator *allocator, void *ctx) { |
| 408 |
(void)ctx; |
| 409 |
|
| 410 |
char test_data[] = "f"; |
| 411 |
char expected[] = "Zg=="; |
| 412 |
|
| 413 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 414 |
} |
| 415 |
AWS_TEST_CASE(base64_encoding_test_case_f_test, s_base64_encoding_test_case_f) |
| 416 |
|
| 417 |
static int s_base64_url_encoding_test_case_f(struct aws_allocator *allocator, void *ctx) { |
| 418 |
(void)ctx; |
| 419 |
|
| 420 |
char test_data[] = "f"; |
| 421 |
char expected[] = "Zg"; |
| 422 |
|
| 423 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 424 |
} |
| 425 |
AWS_TEST_CASE(base64_url_encoding_test_case_f_test, s_base64_url_encoding_test_case_f) |
| 426 |
|
| 427 |
static int s_base64_encoding_test_case_fo(struct aws_allocator *allocator, void *ctx) { |
| 428 |
(void)ctx; |
| 429 |
|
| 430 |
char test_data[] = "fo"; |
| 431 |
char expected[] = "Zm8="; |
| 432 |
|
| 433 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 434 |
} |
| 435 |
AWS_TEST_CASE(base64_encoding_test_case_fo_test, s_base64_encoding_test_case_fo) |
| 436 |
|
| 437 |
static int s_base64_url_encoding_test_case_fo(struct aws_allocator *allocator, void *ctx) { |
| 438 |
(void)ctx; |
| 439 |
|
| 440 |
char test_data[] = "fo"; |
| 441 |
char expected[] = "Zm8"; |
| 442 |
|
| 443 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 444 |
} |
| 445 |
AWS_TEST_CASE(base64_url_encoding_test_case_fo_test, s_base64_url_encoding_test_case_fo) |
| 446 |
|
| 447 |
static int s_base64_encoding_test_case_foo(struct aws_allocator *allocator, void *ctx) { |
| 448 |
(void)ctx; |
| 449 |
|
| 450 |
char test_data[] = "foo"; |
| 451 |
char expected[] = "Zm9v"; |
| 452 |
|
| 453 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 454 |
} |
| 455 |
AWS_TEST_CASE(base64_encoding_test_case_foo_test, s_base64_encoding_test_case_foo) |
| 456 |
|
| 457 |
static int s_base64_url_encoding_test_case_foo(struct aws_allocator *allocator, void *ctx) { |
| 458 |
(void)ctx; |
| 459 |
|
| 460 |
char test_data[] = "foo"; |
| 461 |
char expected[] = "Zm9v"; |
| 462 |
|
| 463 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 464 |
} |
| 465 |
AWS_TEST_CASE(base64_url_encoding_test_case_foo_test, s_base64_url_encoding_test_case_foo) |
| 466 |
|
| 467 |
static int s_base64_encoding_test_case_foob(struct aws_allocator *allocator, void *ctx) { |
| 468 |
(void)ctx; |
| 469 |
|
| 470 |
char test_data[] = "foob"; |
| 471 |
char expected[] = "Zm9vYg=="; |
| 472 |
|
| 473 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 474 |
} |
| 475 |
AWS_TEST_CASE(base64_encoding_test_case_foob_test, s_base64_encoding_test_case_foob) |
| 476 |
|
| 477 |
static int s_base64_url_encoding_test_case_foob(struct aws_allocator *allocator, void *ctx) { |
| 478 |
(void)ctx; |
| 479 |
|
| 480 |
char test_data[] = "foob"; |
| 481 |
char expected[] = "Zm9vYg"; |
| 482 |
|
| 483 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 484 |
} |
| 485 |
AWS_TEST_CASE(base64_url_encoding_test_case_foob_test, s_base64_url_encoding_test_case_foob) |
| 486 |
|
| 487 |
static int s_base64_encoding_test_case_fooba(struct aws_allocator *allocator, void *ctx) { |
| 488 |
(void)ctx; |
| 489 |
|
| 490 |
char test_data[] = "fooba"; |
| 491 |
char expected[] = "Zm9vYmE="; |
| 492 |
|
| 493 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 494 |
} |
| 495 |
AWS_TEST_CASE(base64_encoding_test_case_fooba_test, s_base64_encoding_test_case_fooba) |
| 496 |
|
| 497 |
static int s_base64_url_encoding_test_case_fooba(struct aws_allocator *allocator, void *ctx) { |
| 498 |
(void)ctx; |
| 499 |
|
| 500 |
char test_data[] = "fooba"; |
| 501 |
char expected[] = "Zm9vYmE"; |
| 502 |
|
| 503 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 504 |
} |
| 505 |
AWS_TEST_CASE(base64_url_encoding_test_case_fooba_test, s_base64_url_encoding_test_case_fooba) |
| 506 |
|
| 507 |
static int s_base64_encoding_test_case_foobar(struct aws_allocator *allocator, void *ctx) { |
| 508 |
(void)ctx; |
| 509 |
|
| 510 |
char test_data[] = "foobar"; |
| 511 |
char expected[] = "Zm9vYmFy"; |
| 512 |
|
| 513 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 514 |
} |
| 515 |
AWS_TEST_CASE(base64_encoding_test_case_foobar_test, s_base64_encoding_test_case_foobar) |
| 516 |
|
| 517 |
static int s_base64_url_encoding_test_case_foobar(struct aws_allocator *allocator, void *ctx) { |
| 518 |
(void)ctx; |
| 519 |
|
| 520 |
char test_data[] = "foobar"; |
| 521 |
char expected[] = "Zm9vYmFy"; |
| 522 |
|
| 523 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 524 |
} |
| 525 |
AWS_TEST_CASE(base64_url_encoding_test_case_foobar_test, s_base64_url_encoding_test_case_foobar) |
| 526 |
|
| 527 |
static int s_base64_encoding_test_case_32bytes(struct aws_allocator *allocator, void *ctx) { |
| 528 |
(void)ctx; |
| 529 |
|
| 530 |
|
| 531 |
char test_data[] = "this is a 32 byte long string!!!"; |
| 532 |
char expected[] = "dGhpcyBpcyBhIDMyIGJ5dGUgbG9uZyBzdHJpbmchISE="; |
| 533 |
|
| 534 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), false); |
| 535 |
} |
| 536 |
AWS_TEST_CASE(base64_encoding_test_case_32bytes_test, s_base64_encoding_test_case_32bytes) |
| 537 |
|
| 538 |
static int s_base64_url_encoding_test_case_32bytes(struct aws_allocator *allocator, void *ctx) { |
| 539 |
(void)ctx; |
| 540 |
|
| 541 |
|
| 542 |
char test_data[] = "this is a 32 byte long string!!!"; |
| 543 |
char expected[] = "dGhpcyBpcyBhIDMyIGJ5dGUgbG9uZyBzdHJpbmchISE"; |
| 544 |
|
| 545 |
return s_run_base64_encoding_test_case(allocator, test_data, strlen(test_data), expected, strlen(expected), true); |
| 546 |
} |
| 547 |
AWS_TEST_CASE(base64_url_encoding_test_case_32bytes_test, s_base64_url_encoding_test_case_32bytes) |
| 548 |
|
| 549 |
static int s_base64_encoding_test_zeros_fn(struct aws_allocator *allocator, void *ctx) { |
| 550 |
(void)ctx; |
| 551 |
|
| 552 |
uint8_t test_data[6] = {0}; |
| 553 |
char expected[] = "AAAAAAAA"; |
| 554 |
|
| 555 |
return s_run_base64_encoding_test_case( |
| 556 |
allocator, (char *)test_data, sizeof(test_data), expected, strlen(expected), false); |
| 557 |
} |
| 558 |
AWS_TEST_CASE(base64_encoding_test_zeros, s_base64_encoding_test_zeros_fn) |
| 559 |
|
| 560 |
static int s_base64_url_encoding_test_zeros_fn(struct aws_allocator *allocator, void *ctx) { |
| 561 |
(void)ctx; |
| 562 |
|
| 563 |
uint8_t test_data[6] = {0}; |
| 564 |
char expected[] = "AAAAAAAA"; |
| 565 |
|
| 566 |
return s_run_base64_encoding_test_case( |
| 567 |
allocator, (char *)test_data, sizeof(test_data), expected, strlen(expected), true); |
| 568 |
} |
| 569 |
AWS_TEST_CASE(base64_url_encoding_test_zeros, s_base64_url_encoding_test_zeros_fn) |
| 570 |
|
| 571 |
static int s_base64_encoding_test_roundtrip(struct aws_allocator *allocator, void *ctx) { |
| 572 |
(void)ctx; |
| 573 |
(void)allocator; |
| 574 |
|
| 575 |
fprintf(stderr, "--test\n"); |
| 576 |
uint8_t test_data[32]; |
| 577 |
for (size_t i = 0; i < sizeof(test_data); i++) { |
| 578 |
|
| 579 |
#if 0 |
| 580 |
test_data[i] = 0x; |
| 581 |
test_data[i + 1] = 0x20; |
| 582 |
test_data[i + 2] = 0xc4; |
| 583 |
#endif |
| 584 |
test_data[i] = (uint8_t)i; |
| 585 |
|
| 586 |
} |
| 587 |
struct aws_byte_cursor original_data = aws_byte_cursor_from_array(test_data, sizeof(test_data)); |
| 588 |
|
| 589 |
struct aws_byte_buf hex; |
| 590 |
aws_byte_buf_init(&hex, allocator, 65); |
| 591 |
|
| 592 |
uint8_t test_b64[128] = {0}; |
| 593 |
struct aws_byte_buf b64_data = aws_byte_buf_from_empty_array(test_b64, sizeof(test_b64)); |
| 594 |
|
| 595 |
aws_base64_encode(&original_data, &b64_data); |
| 596 |
b64_data.len--; |
| 597 |
|
| 598 |
struct aws_byte_buf decoded_buf; |
| 599 |
aws_byte_buf_init(&decoded_buf, allocator, 32); |
| 600 |
|
| 601 |
struct aws_byte_cursor b64_cur = aws_byte_cursor_from_buf(&b64_data); |
| 602 |
aws_base64_decode(&b64_cur, &decoded_buf); |
| 603 |
|
| 604 |
if (memcmp(decoded_buf.buffer, original_data.ptr, decoded_buf.len) != 0) { |
| 605 |
aws_hex_encode(&original_data, &hex); |
| 606 |
fprintf(stderr, "Base64 round-trip failed\n"); |
| 607 |
fprintf(stderr, "Original: " PRInSTR "\n", AWS_BYTE_BUF_PRI(hex)); |
| 608 |
fprintf(stderr, "Base64 : "); |
| 609 |
for (size_t i = 0; i < sizeof(test_b64); i++) { |
| 610 |
if (!test_b64[i]) { |
| 611 |
break; |
| 612 |
} |
| 613 |
fprintf(stderr, " %c", test_b64[i]); |
| 614 |
} |
| 615 |
fprintf(stderr, "\n"); |
| 616 |
aws_byte_buf_reset(&hex, true ); |
| 617 |
struct aws_byte_cursor decoded_cur = aws_byte_cursor_from_buf(&decoded_buf); |
| 618 |
aws_hex_encode(&decoded_cur, &hex); |
| 619 |
fprintf(stderr, "Decoded : " PRInSTR "\n", AWS_BYTE_BUF_PRI(hex)); |
| 620 |
return 1; |
| 621 |
} |
| 622 |
|
| 623 |
aws_byte_buf_clean_up(&hex); |
| 624 |
aws_byte_buf_clean_up(&decoded_buf); |
| 625 |
return 0; |
| 626 |
} |
| 627 |
AWS_TEST_CASE(base64_encoding_test_roundtrip, s_base64_encoding_test_roundtrip) |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static int s_base64_encoding_test_all_values_fn(struct aws_allocator *allocator, void *ctx) { |
| 636 |
(void)ctx; |
| 637 |
|
| 638 |
uint8_t test_data[255] = {0}; |
| 639 |
|
| 640 |
for (uint8_t i = 0; i < (uint8_t)sizeof(test_data); ++i) { |
| 641 |
test_data[i] = i; |
| 642 |
} |
| 643 |
|
| 644 |
char expected[] = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERU" |
| 645 |
"ZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouM" |
| 646 |
"jY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0t" |
| 647 |
"PU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"; |
| 648 |
|
| 649 |
return s_run_base64_encoding_test_case( |
| 650 |
allocator, (char *)test_data, sizeof(test_data), expected, strlen(expected), false); |
| 651 |
} |
| 652 |
AWS_TEST_CASE(base64_encoding_test_all_values, s_base64_encoding_test_all_values_fn) |
| 653 |
|
| 654 |
static int s_base64_url_encoding_test_all_values_fn(struct aws_allocator *allocator, void *ctx) { |
| 655 |
(void)ctx; |
| 656 |
|
| 657 |
uint8_t test_data[255] = {0}; |
| 658 |
|
| 659 |
for (uint8_t i = 0; i < (uint8_t)sizeof(test_data); ++i) { |
| 660 |
test_data[i] = i; |
| 661 |
} |
| 662 |
|
| 663 |
char expected[] = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERU" |
| 664 |
"ZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouM" |
| 665 |
"jY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0t" |
| 666 |
"PU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-"; |
| 667 |
|
| 668 |
return s_run_base64_encoding_test_case( |
| 669 |
allocator, (char *)test_data, sizeof(test_data), expected, strlen(expected), true); |
| 670 |
} |
| 671 |
AWS_TEST_CASE(base64_url_encoding_test_all_values, s_base64_url_encoding_test_all_values_fn) |
| 672 |
|
| 673 |
static int s_base64_encoding_buffer_size_too_small_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 674 |
(void)allocator; |
| 675 |
(void)ctx; |
| 676 |
|
| 677 |
char test_data[] = "foobar"; |
| 678 |
char encoded_data[] = "Zm9vYmFy"; |
| 679 |
size_t size_too_small = 4; |
| 680 |
uint8_t output[] = {0, 0}; |
| 681 |
|
| 682 |
struct aws_byte_cursor test_buf = aws_byte_cursor_from_c_str(test_data); |
| 683 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, size_too_small); |
| 684 |
|
| 685 |
ASSERT_ERROR( |
| 686 |
AWS_ERROR_SHORT_BUFFER, |
| 687 |
aws_base64_encode(&test_buf, &output_buf), |
| 688 |
"Invalid buffer size should have failed with AWS_ERROR_SHORT_BUFFER"); |
| 689 |
|
| 690 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str(encoded_data); |
| 691 |
|
| 692 |
ASSERT_ERROR( |
| 693 |
AWS_ERROR_SHORT_BUFFER, |
| 694 |
aws_base64_decode(&encoded_buf, &output_buf), |
| 695 |
"Invalid buffer size should have failed with AWS_ERROR_SHORT_BUFFER"); |
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
AWS_TEST_CASE(base64_encoding_buffer_size_too_small_test, s_base64_encoding_buffer_size_too_small_test_fn) |
| 700 |
|
| 701 |
static int s_base64_encoding_buffer_size_overflow_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 702 |
(void)allocator; |
| 703 |
(void)ctx; |
| 704 |
|
| 705 |
char test_data[] = "foobar"; |
| 706 |
|
| 707 |
|
| 708 |
size_t overflow = (SIZE_MAX - 1) & ~0x03; |
| 709 |
uint8_t output[] = {0, 0}; |
| 710 |
|
| 711 |
struct aws_byte_cursor test_buf = aws_byte_cursor_from_array(test_data, overflow + 2); |
| 712 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 713 |
|
| 714 |
ASSERT_ERROR( |
| 715 |
AWS_ERROR_OVERFLOW_DETECTED, |
| 716 |
aws_base64_encode(&test_buf, &output_buf), |
| 717 |
"overflow buffer size should have failed with AWS_ERROR_OVERFLOW_DETECTED"); |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
AWS_TEST_CASE(base64_encoding_buffer_size_overflow_test, s_base64_encoding_buffer_size_overflow_test_fn) |
| 725 |
|
| 726 |
static int s_base64_encoding_buffer_size_invalid_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 727 |
(void)allocator; |
| 728 |
(void)ctx; |
| 729 |
|
| 730 |
char encoded_data[] = "Zm9vYmFy"; |
| 731 |
|
| 732 |
|
| 733 |
uint8_t output[] = {0, 0}; |
| 734 |
|
| 735 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_array(encoded_data, sizeof(encoded_data)); |
| 736 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 737 |
|
| 738 |
ASSERT_ERROR( |
| 739 |
AWS_ERROR_INVALID_BASE64_STR, |
| 740 |
aws_base64_decode(&encoded_buf, &output_buf), |
| 741 |
"Non multiple of 4 buffer size should have failed with AWS_ERROR_INVALID_BASE64_STR"); |
| 742 |
return 0; |
| 743 |
} |
| 744 |
|
| 745 |
AWS_TEST_CASE(base64_encoding_buffer_size_invalid_test, s_base64_encoding_buffer_size_invalid_test_fn) |
| 746 |
|
| 747 |
static int s_base64_encoding_invalid_buffer_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 748 |
(void)allocator; |
| 749 |
(void)ctx; |
| 750 |
|
| 751 |
char encoded_data[] = "Z\n9vYmFy"; |
| 752 |
uint8_t output[sizeof(encoded_data)] = {0}; |
| 753 |
|
| 754 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str(encoded_data); |
| 755 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 756 |
|
| 757 |
ASSERT_ERROR( |
| 758 |
AWS_ERROR_INVALID_BASE64_STR, |
| 759 |
aws_base64_decode(&encoded_buf, &output_buf), |
| 760 |
"buffer with invalid character should have failed with AWS_ERROR_INVALID_BASE64_STR"); |
| 761 |
return 0; |
| 762 |
} |
| 763 |
|
| 764 |
AWS_TEST_CASE(base64_encoding_invalid_buffer_test, s_base64_encoding_invalid_buffer_test_fn) |
| 765 |
|
| 766 |
static int s_base64_encoding_highbyte_string_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 767 |
(void)allocator; |
| 768 |
(void)ctx; |
| 769 |
|
| 770 |
char bad_input[] = "AAAA\xC1" |
| 771 |
"AAA"; |
| 772 |
uint8_t output[sizeof(bad_input)] = {0}; |
| 773 |
|
| 774 |
struct aws_byte_cursor bad_buf = aws_byte_cursor_from_c_str(bad_input); |
| 775 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 776 |
|
| 777 |
ASSERT_ERROR(AWS_ERROR_INVALID_BASE64_STR, aws_base64_decode(&bad_buf, &output_buf)); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
AWS_TEST_CASE(base64_encoding_highbyte_string_test, s_base64_encoding_highbyte_string_test_fn) |
| 781 |
|
| 782 |
static int s_base64_encoding_invalid_padding_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 783 |
(void)allocator; |
| 784 |
(void)ctx; |
| 785 |
|
| 786 |
char encoded_data[] = "Zm9vY==="; |
| 787 |
uint8_t output[sizeof(encoded_data)] = {0}; |
| 788 |
|
| 789 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str(encoded_data); |
| 790 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 791 |
|
| 792 |
ASSERT_ERROR( |
| 793 |
AWS_ERROR_INVALID_BASE64_STR, |
| 794 |
aws_base64_decode(&encoded_buf, &output_buf), |
| 795 |
"buffer with invalid padding should have failed with AWS_ERROR_INVALID_BASE64_STR"); |
| 796 |
return 0; |
| 797 |
} |
| 798 |
|
| 799 |
AWS_TEST_CASE(base64_encoding_invalid_padding_test, s_base64_encoding_invalid_padding_test_fn) |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
static int s_base64_encoding_interior_padding_rejected_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 804 |
(void)allocator; |
| 805 |
(void)ctx; |
| 806 |
|
| 807 |
*/ |
| 808 |
const char *invalid_inputs[] = {"AB=D", "AB=d", "XX=Y", "ab=+"}; |
| 809 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_inputs); ++i) { |
| 810 |
uint8_t output[16] = {0}; |
| 811 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str(invalid_inputs[i]); |
| 812 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 813 |
|
| 814 |
ASSERT_ERROR( |
| 815 |
AWS_ERROR_INVALID_BASE64_STR, |
| 816 |
aws_base64_decode(&encoded_buf, &output_buf), |
| 817 |
"input \"%s\" with interior '=' should be rejected", |
| 818 |
invalid_inputs[i]); |
| 819 |
ASSERT_UINT_EQUALS(0, output_buf.len, "output.len must be 0 on rejection"); |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
{ |
| 824 |
|
| 825 |
uint8_t output[16] = {0}; |
| 826 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str("Zg=="); |
| 827 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 828 |
ASSERT_SUCCESS(aws_base64_decode(&encoded_buf, &output_buf)); |
| 829 |
ASSERT_UINT_EQUALS(1, output_buf.len); |
| 830 |
ASSERT_UINT_EQUALS('f', output_buf.buffer[0]); |
| 831 |
} |
| 832 |
{ |
| 833 |
|
| 834 |
uint8_t output[16] = {0}; |
| 835 |
struct aws_byte_cursor encoded_buf = aws_byte_cursor_from_c_str("Zm8="); |
| 836 |
struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output)); |
| 837 |
ASSERT_SUCCESS(aws_base64_decode(&encoded_buf, &output_buf)); |
| 838 |
ASSERT_UINT_EQUALS(2, output_buf.len); |
| 839 |
ASSERT_UINT_EQUALS('f', output_buf.buffer[0]); |
| 840 |
ASSERT_UINT_EQUALS('o', output_buf.buffer[1]); |
| 841 |
} |
| 842 |
|
| 843 |
return 0; |
| 844 |
} |
| 845 |
|
| 846 |
AWS_TEST_CASE(base64_encoding_interior_padding_rejected_test, s_base64_encoding_interior_padding_rejected_test_fn) |
| 847 |
|
| 848 |
|
| 849 |
static int s_uint64_buffer_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 850 |
(void)allocator; |
| 851 |
(void)ctx; |
| 852 |
|
| 853 |
uint64_t test_value = 0x1020304050607080; |
| 854 |
uint8_t buffer[8] = {0}; |
| 855 |
aws_write_u64(test_value, buffer); |
| 856 |
|
| 857 |
uint8_t expected[] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}; |
| 858 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "Uint64_t to buffer failed"); |
| 859 |
|
| 860 |
uint64_t unmarshalled_value = aws_read_u64(buffer); |
| 861 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 862 |
return 0; |
| 863 |
} |
| 864 |
|
| 865 |
AWS_TEST_CASE(uint64_buffer_test, s_uint64_buffer_test_fn) |
| 866 |
|
| 867 |
static int s_uint64_buffer_non_aligned_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 868 |
(void)allocator; |
| 869 |
(void)ctx; |
| 870 |
|
| 871 |
uint64_t test_value = 0x1020304050607080; |
| 872 |
uint8_t *buffer = (uint8_t *)aws_mem_acquire(allocator, 9); |
| 873 |
|
| 874 |
ASSERT_FALSE((size_t)buffer & 0x07, "Heap allocated buffer should have been 8-byte aligned."); |
| 875 |
|
| 876 |
aws_write_u64(test_value, buffer + 1); |
| 877 |
|
| 878 |
uint8_t expected[] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}; |
| 879 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), (buffer + 1), sizeof(expected), "Uint64_t to buffer failed"); |
| 880 |
|
| 881 |
uint64_t unmarshalled_value = aws_read_u64(buffer + 1); |
| 882 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 883 |
|
| 884 |
aws_mem_release(allocator, (void *)buffer); |
| 885 |
|
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
AWS_TEST_CASE(uint64_buffer_non_aligned_test, s_uint64_buffer_non_aligned_test_fn) |
| 890 |
|
| 891 |
static int s_uint32_buffer_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 892 |
(void)allocator; |
| 893 |
(void)ctx; |
| 894 |
|
| 895 |
uint32_t test_value = 0x10203040; |
| 896 |
uint8_t buffer[4] = {0}; |
| 897 |
aws_write_u32(test_value, buffer); |
| 898 |
|
| 899 |
uint8_t expected[] = {0x10, 0x20, 0x30, 0x40}; |
| 900 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "Uint32_t to buffer failed"); |
| 901 |
|
| 902 |
uint32_t unmarshalled_value = aws_read_u32(buffer); |
| 903 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 904 |
|
| 905 |
return 0; |
| 906 |
} |
| 907 |
|
| 908 |
AWS_TEST_CASE(uint32_buffer_test, s_uint32_buffer_test_fn) |
| 909 |
|
| 910 |
static int s_uint32_buffer_non_aligned_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 911 |
(void)ctx; |
| 912 |
|
| 913 |
uint32_t test_value = 0x10203040; |
| 914 |
uint8_t *buffer = (uint8_t *)aws_mem_acquire(allocator, 9); |
| 915 |
|
| 916 |
ASSERT_FALSE((size_t)buffer & 0x07, "Heap allocated buffer should have been 8-byte aligned."); |
| 917 |
|
| 918 |
aws_write_u32(test_value, buffer + 5); |
| 919 |
|
| 920 |
uint8_t expected[] = {0x10, 0x20, 0x30, 0x40}; |
| 921 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), (buffer + 5), sizeof(expected), "Uint32_t to buffer failed"); |
| 922 |
|
| 923 |
uint64_t unmarshalled_value = aws_read_u32(buffer + 5); |
| 924 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 925 |
|
| 926 |
aws_mem_release(allocator, (void *)buffer); |
| 927 |
|
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
AWS_TEST_CASE(uint32_buffer_non_aligned_test, s_uint32_buffer_non_aligned_test_fn) |
| 932 |
|
| 933 |
static int s_uint24_buffer_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 934 |
(void)allocator; |
| 935 |
(void)ctx; |
| 936 |
|
| 937 |
uint32_t test_value = 0x102030; |
| 938 |
uint8_t buffer[3] = {0}; |
| 939 |
aws_write_u24(test_value, buffer); |
| 940 |
|
| 941 |
uint8_t expected[] = {0x10, 0x20, 0x30}; |
| 942 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "24 bit int to buffer failed"); |
| 943 |
|
| 944 |
uint32_t unmarshalled_value = aws_read_u24(buffer); |
| 945 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 946 |
|
| 947 |
return 0; |
| 948 |
} |
| 949 |
|
| 950 |
AWS_TEST_CASE(uint24_buffer_test, s_uint24_buffer_test_fn) |
| 951 |
|
| 952 |
static int s_uint24_buffer_non_aligned_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 953 |
(void)ctx; |
| 954 |
|
| 955 |
uint32_t test_value = 0x102030; |
| 956 |
uint8_t *buffer = (uint8_t *)aws_mem_acquire(allocator, 9); |
| 957 |
|
| 958 |
ASSERT_FALSE((size_t)buffer & 0x07, "Heap allocated buffer should have been 8-byte aligned."); |
| 959 |
aws_write_u24(test_value, buffer + 6); |
| 960 |
|
| 961 |
uint8_t expected[] = {0x10, 0x20, 0x30}; |
| 962 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), (buffer + 6), sizeof(expected), "24 bit int to buffer failed"); |
| 963 |
|
| 964 |
uint32_t unmarshalled_value = aws_read_u24(buffer + 6); |
| 965 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 966 |
aws_mem_release(allocator, (void *)buffer); |
| 967 |
|
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
AWS_TEST_CASE(uint24_buffer_non_aligned_test, s_uint24_buffer_non_aligned_test_fn) |
| 972 |
|
| 973 |
static int s_uint16_buffer_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 974 |
(void)allocator; |
| 975 |
(void)ctx; |
| 976 |
|
| 977 |
uint16_t test_value = 0x1020; |
| 978 |
uint8_t buffer[2] = {0}; |
| 979 |
aws_write_u16(test_value, buffer); |
| 980 |
|
| 981 |
uint8_t expected[] = {0x10, 0x20}; |
| 982 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "Uint16_t to buffer failed"); |
| 983 |
|
| 984 |
uint16_t unmarshalled_value = aws_read_u16(buffer); |
| 985 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 986 |
|
| 987 |
return 0; |
| 988 |
} |
| 989 |
|
| 990 |
AWS_TEST_CASE(uint16_buffer_test, s_uint16_buffer_test_fn) |
| 991 |
|
| 992 |
static int s_uint16_buffer_non_aligned_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 993 |
(void)ctx; |
| 994 |
|
| 995 |
uint16_t test_value = 0x1020; |
| 996 |
uint8_t *buffer = (uint8_t *)aws_mem_acquire(allocator, 9); |
| 997 |
|
| 998 |
ASSERT_FALSE((size_t)buffer & 0x07, "Heap allocated buffer should have been 8-byte aligned."); |
| 999 |
aws_write_u16(test_value, buffer + 7); |
| 1000 |
|
| 1001 |
uint8_t expected[] = {0x10, 0x20}; |
| 1002 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), (buffer + 7), sizeof(expected), "16 bit int to buffer failed"); |
| 1003 |
|
| 1004 |
uint16_t unmarshalled_value = aws_read_u16(buffer + 7); |
| 1005 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 1006 |
aws_mem_release(allocator, (void *)buffer); |
| 1007 |
|
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
|
| 1011 |
AWS_TEST_CASE(uint16_buffer_non_aligned_test, s_uint16_buffer_non_aligned_test_fn) |
| 1012 |
|
| 1013 |
|
| 1014 |
static int s_uint16_buffer_signed_positive_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 1015 |
(void)allocator; |
| 1016 |
(void)ctx; |
| 1017 |
|
| 1018 |
int16_t test_value = 0x4030; |
| 1019 |
uint8_t buffer[2] = {0}; |
| 1020 |
aws_write_u16((uint16_t)test_value, buffer); |
| 1021 |
|
| 1022 |
uint8_t expected[] = {0x40, 0x30}; |
| 1023 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "Uint16_t to buffer failed"); |
| 1024 |
|
| 1025 |
int16_t unmarshalled_value = (int16_t)aws_read_u16(buffer); |
| 1026 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 1027 |
|
| 1028 |
return 0; |
| 1029 |
} |
| 1030 |
|
| 1031 |
AWS_TEST_CASE(uint16_buffer_signed_positive_test, s_uint16_buffer_signed_positive_test_fn) |
| 1032 |
|
| 1033 |
static int s_uint16_buffer_signed_negative_test_fn(struct aws_allocator *allocator, void *ctx) { |
| 1034 |
(void)allocator; |
| 1035 |
(void)ctx; |
| 1036 |
|
| 1037 |
int16_t test_value = -2; |
| 1038 |
uint8_t buffer[2] = {0}; |
| 1039 |
aws_write_u16((uint16_t)test_value, buffer); |
| 1040 |
|
| 1041 |
uint8_t expected[] = {0xFF, 0xFE}; |
| 1042 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), buffer, sizeof(buffer), "Uint16_t to buffer failed"); |
| 1043 |
|
| 1044 |
int16_t unmarshalled_value = (int16_t)aws_read_u16(buffer); |
| 1045 |
ASSERT_INT_EQUALS(test_value, unmarshalled_value, "After unmarshalling the encoded data, it didn't match"); |
| 1046 |
|
| 1047 |
return 0; |
| 1048 |
} |
| 1049 |
|
| 1050 |
AWS_TEST_CASE(uint16_buffer_signed_negative_test, s_uint16_buffer_signed_negative_test_fn) |
| 1051 |
|
| 1052 |
static int s_run_hex_encoding_append_dynamic_test_case( |
| 1053 |
struct aws_allocator *allocator, |
| 1054 |
const char *test_str, |
| 1055 |
const char *expected, |
| 1056 |
size_t initial_capacity, |
| 1057 |
size_t starting_offset) { |
| 1058 |
|
| 1059 |
size_t output_size = 2 * strlen(test_str); |
| 1060 |
|
| 1061 |
struct aws_byte_cursor to_encode = aws_byte_cursor_from_c_str(test_str); |
| 1062 |
|
| 1063 |
struct aws_byte_buf dest; |
| 1064 |
ASSERT_SUCCESS(aws_byte_buf_init(&dest, allocator, initial_capacity)); |
| 1065 |
memset(dest.buffer, 0xdd, dest.capacity); |
| 1066 |
|
| 1067 |
dest.len = starting_offset; |
| 1068 |
|
| 1069 |
ASSERT_SUCCESS(aws_hex_encode_append_dynamic(&to_encode, &dest), "encode call should have succeeded"); |
| 1070 |
|
| 1071 |
size_t expected_size = strlen(expected); |
| 1072 |
|
| 1073 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1074 |
expected, |
| 1075 |
expected_size, |
| 1076 |
dest.buffer + starting_offset, |
| 1077 |
output_size, |
| 1078 |
"Encode output should have been {%s}, was {%s}.", |
| 1079 |
expected, |
| 1080 |
dest.buffer + starting_offset); |
| 1081 |
ASSERT_INT_EQUALS(output_size, dest.len - starting_offset); |
| 1082 |
|
| 1083 |
for (size_t i = 0; i < starting_offset; ++i) { |
| 1084 |
ASSERT_INT_EQUALS( |
| 1085 |
(unsigned char)*(dest.buffer + i), |
| 1086 |
(unsigned char)0xdd, |
| 1087 |
"Write should not have occurred before the the encoding's starting position."); |
| 1088 |
} |
| 1089 |
|
| 1090 |
for (size_t i = starting_offset + output_size; i < dest.capacity; ++i) { |
| 1091 |
ASSERT_INT_EQUALS( |
| 1092 |
(unsigned char)*(dest.buffer + i), |
| 1093 |
(unsigned char)0xdd, |
| 1094 |
"Write should not have occurred after the encoding's final position."); |
| 1095 |
} |
| 1096 |
|
| 1097 |
aws_byte_buf_clean_up(&dest); |
| 1098 |
return 0; |
| 1099 |
} |
| 1100 |
|
| 1101 |
static int s_hex_encoding_append_dynamic_test_case_fooba(struct aws_allocator *allocator, void *ctx) { |
| 1102 |
(void)ctx; |
| 1103 |
|
| 1104 |
char test_data[] = "fooba"; |
| 1105 |
char expected[] = "666f6f6261"; |
| 1106 |
|
| 1107 |
ASSERT_TRUE(s_run_hex_encoding_append_dynamic_test_case(allocator, test_data, expected, 5, 3) == AWS_OP_SUCCESS); |
| 1108 |
ASSERT_TRUE(s_run_hex_encoding_append_dynamic_test_case(allocator, test_data, expected, 50, 3) == AWS_OP_SUCCESS); |
| 1109 |
|
| 1110 |
return 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
AWS_TEST_CASE(hex_encoding_append_dynamic_test_case_fooba, s_hex_encoding_append_dynamic_test_case_fooba) |
| 1114 |
|
| 1115 |
static int s_hex_encoding_append_dynamic_test_case_empty(struct aws_allocator *allocator, void *ctx) { |
| 1116 |
(void)ctx; |
| 1117 |
|
| 1118 |
char test_data[] = ""; |
| 1119 |
char expected[] = ""; |
| 1120 |
|
| 1121 |
ASSERT_TRUE(s_run_hex_encoding_append_dynamic_test_case(allocator, test_data, expected, 5, 3) == AWS_OP_SUCCESS); |
| 1122 |
ASSERT_TRUE(s_run_hex_encoding_append_dynamic_test_case(allocator, test_data, expected, 50, 3) == AWS_OP_SUCCESS); |
| 1123 |
|
| 1124 |
return 0; |
| 1125 |
} |
| 1126 |
|
| 1127 |
AWS_TEST_CASE(hex_encoding_append_dynamic_test_case_empty, s_hex_encoding_append_dynamic_test_case_empty) |
| 1128 |
|
| 1129 |
static int read_file_contents(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename) { |
| 1130 |
AWS_ZERO_STRUCT(*out_buf); |
| 1131 |
FILE *fp = aws_fopen(filename, "r"); |
| 1132 |
ASSERT_NOT_NULL(fp); |
| 1133 |
|
| 1134 |
ASSERT_INT_EQUALS(fseek(fp, 0L, SEEK_END), 0); |
| 1135 |
size_t allocation_size = (size_t)ftell(fp); |
| 1136 |
ASSERT_SUCCESS(aws_byte_buf_init(out_buf, alloc, allocation_size)); |
| 1137 |
ASSERT_INT_EQUALS(fseek(fp, 0L, SEEK_SET), 0); |
| 1138 |
|
| 1139 |
size_t read = fread(out_buf->buffer, 1, allocation_size, fp); |
| 1140 |
|
| 1141 |
if (read < (allocation_size - 1)) { |
| 1142 |
ASSERT_INT_EQUALS(read, allocation_size); |
| 1143 |
} |
| 1144 |
out_buf->len = read; |
| 1145 |
|
| 1146 |
ASSERT_INT_EQUALS(fclose(fp), 0); |
| 1147 |
return AWS_OP_SUCCESS; |
| 1148 |
} |
| 1149 |
|
| 1150 |
static int s_text_encoding_utf8(struct aws_allocator *allocator, void *ctx) { |
| 1151 |
(void)ctx; |
| 1152 |
struct aws_byte_buf contents; |
| 1153 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf8.txt")); |
| 1154 |
ASSERT_INT_EQUALS(AWS_TEXT_UTF8, aws_text_detect_encoding(contents.buffer, contents.len)); |
| 1155 |
aws_byte_buf_clean_up(&contents); |
| 1156 |
return 0; |
| 1157 |
} |
| 1158 |
|
| 1159 |
AWS_TEST_CASE(text_encoding_utf8, s_text_encoding_utf8) |
| 1160 |
|
| 1161 |
static int s_text_encoding_utf16(struct aws_allocator *allocator, void *ctx) { |
| 1162 |
(void)ctx; |
| 1163 |
struct aws_byte_buf contents; |
| 1164 |
|
| 1165 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf16le.txt")); |
| 1166 |
ASSERT_INT_EQUALS(AWS_TEXT_UTF16, aws_text_detect_encoding(contents.buffer, contents.len)); |
| 1167 |
aws_byte_buf_clean_up(&contents); |
| 1168 |
|
| 1169 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf16be.txt")); |
| 1170 |
ASSERT_INT_EQUALS(AWS_TEXT_UTF16, aws_text_detect_encoding(contents.buffer, contents.len)); |
| 1171 |
aws_byte_buf_clean_up(&contents); |
| 1172 |
|
| 1173 |
return 0; |
| 1174 |
} |
| 1175 |
|
| 1176 |
AWS_TEST_CASE(text_encoding_utf16, s_text_encoding_utf16) |
| 1177 |
|
| 1178 |
static int s_text_encoding_ascii(struct aws_allocator *allocator, void *ctx) { |
| 1179 |
(void)ctx; |
| 1180 |
char all_ascii_chars[128]; |
| 1181 |
for (char c = 0; c < AWS_ARRAY_SIZE(all_ascii_chars); ++c) { |
| 1182 |
all_ascii_chars[(int)c] = (c + 1) % 128; |
| 1183 |
} |
| 1184 |
|
| 1185 |
ASSERT_INT_EQUALS( |
| 1186 |
AWS_TEXT_ASCII, aws_text_detect_encoding((const uint8_t *)all_ascii_chars, AWS_ARRAY_SIZE(all_ascii_chars))); |
| 1187 |
|
| 1188 |
struct aws_byte_buf contents; |
| 1189 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "ascii.txt")); |
| 1190 |
ASSERT_INT_EQUALS(AWS_TEXT_ASCII, aws_text_detect_encoding(contents.buffer, contents.len)); |
| 1191 |
aws_byte_buf_clean_up(&contents); |
| 1192 |
|
| 1193 |
return 0; |
| 1194 |
} |
| 1195 |
|
| 1196 |
AWS_TEST_CASE(text_encoding_ascii, s_text_encoding_ascii) |
| 1197 |
|
| 1198 |
static int s_text_encoding_is_utf8(struct aws_allocator *allocator, void *ctx) { |
| 1199 |
(void)ctx; |
| 1200 |
{ |
| 1201 |
struct aws_byte_buf contents; |
| 1202 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf8.txt")); |
| 1203 |
ASSERT_TRUE(aws_text_is_utf8(contents.buffer, contents.len)); |
| 1204 |
aws_byte_buf_clean_up(&contents); |
| 1205 |
} |
| 1206 |
{ |
| 1207 |
struct aws_byte_buf contents; |
| 1208 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "ascii.txt")); |
| 1209 |
ASSERT_TRUE(aws_text_is_utf8(contents.buffer, contents.len)); |
| 1210 |
aws_byte_buf_clean_up(&contents); |
| 1211 |
} |
| 1212 |
{ |
| 1213 |
struct aws_byte_buf contents; |
| 1214 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf16be.txt")); |
| 1215 |
ASSERT_FALSE(aws_text_is_utf8(contents.buffer, contents.len)); |
| 1216 |
aws_byte_buf_clean_up(&contents); |
| 1217 |
} |
| 1218 |
{ |
| 1219 |
struct aws_byte_buf contents; |
| 1220 |
ASSERT_SUCCESS(read_file_contents(&contents, allocator, "utf16le.txt")); |
| 1221 |
ASSERT_FALSE(aws_text_is_utf8(contents.buffer, contents.len)); |
| 1222 |
aws_byte_buf_clean_up(&contents); |
| 1223 |
} |
| 1224 |
|
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
AWS_TEST_CASE(text_encoding_is_utf8, s_text_encoding_is_utf8) |
| 1229 |
|
| 1230 |
struct utf8_example { |
| 1231 |
const char *name; |
| 1232 |
struct aws_byte_cursor text; |
| 1233 |
}; |
| 1234 |
|
| 1235 |
static struct utf8_example s_valid_utf8_examples[] = { |
| 1236 |
{ |
| 1237 |
.name = "1 letter", |
| 1238 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("a"), |
| 1239 |
}, |
| 1240 |
{ |
| 1241 |
.name = "Several ascii letters", |
| 1242 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ascii word"), |
| 1243 |
}, |
| 1244 |
{ |
| 1245 |
.name = "empty string", |
| 1246 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""), |
| 1247 |
}, |
| 1248 |
{ |
| 1249 |
.name = "Embedded null byte", |
| 1250 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("a\x00b"), |
| 1251 |
}, |
| 1252 |
{ |
| 1253 |
.name = "2 byte codepoint", |
| 1254 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\xA3"), |
| 1255 |
}, |
| 1256 |
{ |
| 1257 |
.name = "3 byte codepoint", |
| 1258 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE2\x82\xAC"), |
| 1259 |
}, |
| 1260 |
{ |
| 1261 |
.name = "4 byte codepoint", |
| 1262 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x90\x8D\x88"), |
| 1263 |
}, |
| 1264 |
{ |
| 1265 |
.name = "A variety of different length codepoints", |
| 1266 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 1267 |
"\xF0\x90\x8D\x88\xE2\x82\xAC\xC2\xA3\x24\xC2\xA3\xE2\x82\xAC\xF0\x90\x8D\x88"), |
| 1268 |
}, |
| 1269 |
{ |
| 1270 |
.name = "UTF8 BOM", |
| 1271 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBB\xBF"), |
| 1272 |
}, |
| 1273 |
{ |
| 1274 |
.name = "UTF8 BOM plus extra", |
| 1275 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBB\xBF\x24\xC2\xA3"), |
| 1276 |
}, |
| 1277 |
{ |
| 1278 |
.name = "First possible 1 byte codepoint", |
| 1279 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x00"), |
| 1280 |
}, |
| 1281 |
{ |
| 1282 |
.name = "First possible 2 byte codepoint", |
| 1283 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\x80"), |
| 1284 |
}, |
| 1285 |
{ |
| 1286 |
.name = "First possible 3 byte codepoint", |
| 1287 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE0\xA0\x80"), |
| 1288 |
}, |
| 1289 |
{ |
| 1290 |
.name = "First possible 4 byte codepoint", |
| 1291 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x90\x80\x80"), |
| 1292 |
}, |
| 1293 |
{ |
| 1294 |
.name = "Last possible 1 byte codepoint", |
| 1295 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x7F"), |
| 1296 |
}, |
| 1297 |
{ |
| 1298 |
.name = "Last possible 2 byte codepoint", |
| 1299 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xDF\xBF"), |
| 1300 |
}, |
| 1301 |
{ |
| 1302 |
.name = "Last possible 3 byte codepoint", |
| 1303 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBF\xBF"), |
| 1304 |
}, |
| 1305 |
{ |
| 1306 |
.name = "Last possible 4 byte codepoint", |
| 1307 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF7\xBF\xBF\xBF"), |
| 1308 |
}, |
| 1309 |
{ |
| 1310 |
.name = "Last valid codepoint before prohibited range U+D800 - U+DFFF", |
| 1311 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xED\x9F\xBF"), |
| 1312 |
}, |
| 1313 |
{ |
| 1314 |
.name = "Next valid codepoint after prohibited range U+D800 - U+DFFF", |
| 1315 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEE\x80\x80"), |
| 1316 |
}, |
| 1317 |
{ |
| 1318 |
.name = "Boundary condition", |
| 1319 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xEF\xBF\xBD"), |
| 1320 |
}, |
| 1321 |
{ |
| 1322 |
.name = "Boundary condition", |
| 1323 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF4\x8F\xBF\xBF"), |
| 1324 |
}, |
| 1325 |
{ |
| 1326 |
.name = "Boundary condition", |
| 1327 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF4\x90\x80\x80"), |
| 1328 |
}, |
| 1329 |
}; |
| 1330 |
|
| 1331 |
static struct utf8_example s_illegal_utf8_examples[] = { |
| 1332 |
{ |
| 1333 |
.name = "Missing last byte of 2 byte codepoint", |
| 1334 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2"), |
| 1335 |
}, |
| 1336 |
{ |
| 1337 |
.name = "Missing last byte of 3 byte codepoint", |
| 1338 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE2\x82"), |
| 1339 |
}, |
| 1340 |
{ |
| 1341 |
.name = "Missing last byte of 4 byte codepoint", |
| 1342 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x90\x8D"), |
| 1343 |
}, |
| 1344 |
{ |
| 1345 |
.name = "5 byte codepoints not allowed by RFC-3629", |
| 1346 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF8\x88\x80\x80\x80"), |
| 1347 |
}, |
| 1348 |
{ |
| 1349 |
.name = "6 byte codepoints not allowed by RFC-3629", |
| 1350 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xFC\x84\x80\x80\x80\x80"), |
| 1351 |
}, |
| 1352 |
{ |
| 1353 |
.name = "Illegal first byte", |
| 1354 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xFF"), |
| 1355 |
}, |
| 1356 |
{ |
| 1357 |
.name = "Overlong 2 byte encoding of U+00", |
| 1358 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC0\x80"), |
| 1359 |
}, |
| 1360 |
{ |
| 1361 |
.name = "Overlong 3 byte encoding of U+00", |
| 1362 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xE0\x80\x80"), |
| 1363 |
}, |
| 1364 |
{ |
| 1365 |
.name = "Overlong 4 byte encoding of U+00", |
| 1366 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xF0\x80\x80\x80"), |
| 1367 |
}, |
| 1368 |
{ |
| 1369 |
.name = "Continuation byte as first byte (lowest possible value)", |
| 1370 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x80"), |
| 1371 |
}, |
| 1372 |
{ |
| 1373 |
.name = "Continuation byte as first byte (highest possible value)", |
| 1374 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xBF"), |
| 1375 |
}, |
| 1376 |
{ |
| 1377 |
.name = "Unexpected continuation byte after valid codepoint", |
| 1378 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x61\x80"), |
| 1379 |
}, |
| 1380 |
{ |
| 1381 |
.name = "Illegal value for continuation byte (starts 11xxxxxx)", |
| 1382 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\xC0"), |
| 1383 |
}, |
| 1384 |
{ |
| 1385 |
.name = "Illegal value for continuation byte (starts 00xxxxxx)", |
| 1386 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xC2\x3F"), |
| 1387 |
}, |
| 1388 |
{ |
| 1389 |
.name = "Codepoint in prohibited range U+D800 - U+DFFF (lowest possible value)", |
| 1390 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xED\xA0\x80"), |
| 1391 |
}, |
| 1392 |
{ |
| 1393 |
.name = "Codepoint in prohibited range U+D800 - U+DFFF (highest possible value)", |
| 1394 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xED\xBF\xBF"), |
| 1395 |
}, |
| 1396 |
{ |
| 1397 |
.name = "Codepoint in prohibited range U+D800 - U+DFFF (in the middle)", |
| 1398 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\xED\xA3\xBF"), |
| 1399 |
}, |
| 1400 |
}; |
| 1401 |
|
| 1402 |
static int s_utf8_validation_callback_always_fails(const uint32_t codepoint, void *user_data) { |
| 1403 |
(void)codepoint; |
| 1404 |
(void)user_data; |
| 1405 |
return aws_raise_error(AWS_ERROR_INVALID_UTF8); |
| 1406 |
} |
| 1407 |
|
| 1408 |
static int s_utf8_validation_callback_always_passes(const uint32_t codepoint, void *user_data) { |
| 1409 |
(void)codepoint; |
| 1410 |
(void)user_data; |
| 1411 |
return AWS_OP_SUCCESS; |
| 1412 |
} |
| 1413 |
|
| 1414 |
static struct utf8_example s_valid_utf8_examples_for_callback[] = { |
| 1415 |
{ |
| 1416 |
.name = "one byte", |
| 1417 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x01"), |
| 1418 |
}, |
| 1419 |
{ |
| 1420 |
.name = "empty string", |
| 1421 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""), |
| 1422 |
}, |
| 1423 |
{ |
| 1424 |
.name = "Several valid bytes", |
| 1425 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\x01\x02\x02\x01\x01"), |
| 1426 |
}}; |
| 1427 |
|
| 1428 |
static int s_utf8_validation_callback(const uint32_t codepoint, void *user_data) { |
| 1429 |
(void)user_data; |
| 1430 |
return (codepoint >= 0x01 && codepoint <= 0x02) ? AWS_ERROR_SUCCESS : aws_raise_error(AWS_ERROR_INVALID_UTF8); |
| 1431 |
} |
| 1432 |
|
| 1433 |
static int s_text_is_valid_utf8(struct aws_allocator *allocator, void *ctx) { |
| 1434 |
(void)ctx; |
| 1435 |
|
| 1436 |
|
| 1437 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1438 |
struct utf8_example example = s_valid_utf8_examples[i]; |
| 1439 |
printf("valid example [%zu]: %s\n", i, example.name); |
| 1440 |
ASSERT_SUCCESS(aws_decode_utf8(example.text, NULL )); |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
struct aws_byte_buf all_good_text; |
| 1445 |
aws_byte_buf_init(&all_good_text, allocator, 1024); |
| 1446 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1447 |
aws_byte_buf_append_dynamic(&all_good_text, &s_valid_utf8_examples[i].text); |
| 1448 |
} |
| 1449 |
ASSERT_SUCCESS(aws_decode_utf8(aws_byte_cursor_from_buf(&all_good_text), NULL )); |
| 1450 |
aws_byte_buf_clean_up(&all_good_text); |
| 1451 |
|
| 1452 |
|
| 1453 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_illegal_utf8_examples); ++i) { |
| 1454 |
struct utf8_example example = s_illegal_utf8_examples[i]; |
| 1455 |
printf("illegal example [%zu]: %s\n", i, example.name); |
| 1456 |
ASSERT_FAILS(aws_decode_utf8(example.text, NULL )); |
| 1457 |
} |
| 1458 |
|
| 1459 |
return 0; |
| 1460 |
} |
| 1461 |
|
| 1462 |
AWS_TEST_CASE(text_is_valid_utf8, s_text_is_valid_utf8); |
| 1463 |
|
| 1464 |
static int s_text_is_valid_utf8_callback(struct aws_allocator *allocator, void *ctx) { |
| 1465 |
(void)ctx; |
| 1466 |
|
| 1467 |
struct aws_utf8_decoder_options with_validation_callback = { |
| 1468 |
.on_codepoint = s_utf8_validation_callback, |
| 1469 |
}; |
| 1470 |
|
| 1471 |
struct aws_utf8_decoder_options with_validation_callback_always_fails = { |
| 1472 |
.on_codepoint = s_utf8_validation_callback_always_fails, |
| 1473 |
}; |
| 1474 |
|
| 1475 |
struct aws_utf8_decoder_options with_validation_callback_always_passes = { |
| 1476 |
.on_codepoint = s_utf8_validation_callback_always_passes, |
| 1477 |
}; |
| 1478 |
|
| 1479 |
|
| 1480 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples_for_callback); ++i) { |
| 1481 |
struct utf8_example example = s_valid_utf8_examples_for_callback[i]; |
| 1482 |
printf("valid example [%zu]: %s\n", i, example.name); |
| 1483 |
ASSERT_SUCCESS(aws_decode_utf8(example.text, &with_validation_callback)); |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1488 |
struct utf8_example example = s_valid_utf8_examples[i]; |
| 1489 |
if (example.text.len == 0) { |
| 1490 |
|
| 1491 |
printf("empty string would be always valid[%zu]: %s\n", i, example.name); |
| 1492 |
ASSERT_SUCCESS(aws_decode_utf8(example.text, &with_validation_callback)); |
| 1493 |
} else { |
| 1494 |
printf("valid example should still failed by the callback[%zu]: %s\n", i, example.name); |
| 1495 |
ASSERT_FAILS(aws_decode_utf8(example.text, &with_validation_callback)); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1501 |
struct utf8_example example = s_valid_utf8_examples[i]; |
| 1502 |
if (example.text.len == 0) { |
| 1503 |
|
| 1504 |
printf("empty string would be always valid[%zu]: %s\n", i, example.name); |
| 1505 |
ASSERT_SUCCESS(aws_decode_utf8(example.text, &with_validation_callback)); |
| 1506 |
} else { |
| 1507 |
printf("The callback should fail the valid example [%zu]: %s\n", i, example.name); |
| 1508 |
ASSERT_FAILS(aws_decode_utf8(example.text, &with_validation_callback_always_fails)); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
struct aws_byte_buf all_good_text; |
| 1514 |
aws_byte_buf_init(&all_good_text, allocator, 1024); |
| 1515 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1516 |
aws_byte_buf_append_dynamic(&all_good_text, &s_valid_utf8_examples[i].text); |
| 1517 |
} |
| 1518 |
ASSERT_FAILS(aws_decode_utf8(aws_byte_cursor_from_buf(&all_good_text), &with_validation_callback_always_fails)); |
| 1519 |
aws_byte_buf_clean_up(&all_good_text); |
| 1520 |
|
| 1521 |
|
| 1522 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_illegal_utf8_examples); ++i) { |
| 1523 |
struct utf8_example example = s_illegal_utf8_examples[i]; |
| 1524 |
printf("illegal example [%zu]: %s\n", i, example.name); |
| 1525 |
ASSERT_FAILS(aws_decode_utf8(example.text, &with_validation_callback_always_passes)); |
| 1526 |
} |
| 1527 |
|
| 1528 |
return 0; |
| 1529 |
} |
| 1530 |
|
| 1531 |
AWS_TEST_CASE(text_is_valid_utf8_callback, s_text_is_valid_utf8_callback); |
| 1532 |
|
| 1533 |
static int s_utf8_decoder_update_in_chunks( |
| 1534 |
struct aws_utf8_decoder *decoder, |
| 1535 |
struct aws_byte_cursor text, |
| 1536 |
size_t chunk_size) { |
| 1537 |
|
| 1538 |
while (text.len > 0) { |
| 1539 |
struct aws_byte_cursor chunk = aws_byte_cursor_advance(&text, aws_min_size(chunk_size, text.len)); |
| 1540 |
if (aws_utf8_decoder_update(decoder, chunk)) { |
| 1541 |
return AWS_OP_ERR; |
| 1542 |
} |
| 1543 |
} |
| 1544 |
return AWS_OP_SUCCESS; |
| 1545 |
} |
| 1546 |
|
| 1547 |
static int s_utf8_decoder(struct aws_allocator *allocator, void *ctx) { |
| 1548 |
(void)ctx; |
| 1549 |
struct aws_utf8_decoder *decoder = aws_utf8_decoder_new(allocator, NULL ); |
| 1550 |
ASSERT_NOT_NULL(decoder); |
| 1551 |
|
| 1552 |
|
| 1553 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_utf8_examples); ++i) { |
| 1554 |
struct utf8_example example = s_valid_utf8_examples[i]; |
| 1555 |
printf("valid example [%zu]: %s\n", i, example.name); |
| 1556 |
aws_utf8_decoder_reset(decoder); |
| 1557 |
|
| 1558 |
for (size_t chunk_size = 1; chunk_size <= example.text.len; ++chunk_size) { |
| 1559 |
printf(" processing %zu byte chunks\n", chunk_size); |
| 1560 |
ASSERT_SUCCESS(s_utf8_decoder_update_in_chunks(decoder, example.text, chunk_size)); |
| 1561 |
} |
| 1562 |
ASSERT_SUCCESS(aws_utf8_decoder_finalize(decoder)); |
| 1563 |
} |
| 1564 |
|
| 1565 |
|
| 1566 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_illegal_utf8_examples); ++i) { |
| 1567 |
struct utf8_example example = s_illegal_utf8_examples[i]; |
| 1568 |
printf("illegal example [%zu]: %s\n", i, example.name); |
| 1569 |
aws_utf8_decoder_reset(decoder); |
| 1570 |
|
| 1571 |
bool decoder_error = false; |
| 1572 |
for (size_t chunk_size = 1; chunk_size <= example.text.len; ++chunk_size) { |
| 1573 |
printf(" processing %zu byte chunks\n", chunk_size); |
| 1574 |
if (s_utf8_decoder_update_in_chunks(decoder, example.text, chunk_size)) { |
| 1575 |
ASSERT_INT_EQUALS(AWS_ERROR_INVALID_UTF8, aws_last_error()); |
| 1576 |
decoder_error = true; |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (!decoder_error) { |
| 1582 |
ASSERT_ERROR(AWS_ERROR_INVALID_UTF8, aws_utf8_decoder_finalize(decoder)); |
| 1583 |
} |
| 1584 |
} |
| 1585 |
|
| 1586 |
aws_utf8_decoder_destroy(decoder); |
| 1587 |
return 0; |
| 1588 |
} |
| 1589 |
|
| 1590 |
AWS_TEST_CASE(utf8_decoder, s_utf8_decoder); |