| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "aws/s3/private/s3_checksums.h" |
| 6 |
#include <aws/common/byte_buf.h> |
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
#include <s3_checksums_test_case_helper.h> |
| 10 |
|
| 11 |
#define AWS_CRC32_LEN 4 |
| 12 |
|
| 13 |
static int s_crc32_nist_test_case_1_fn(struct aws_allocator *allocator, void *ctx) { |
| 14 |
(void)ctx; |
| 15 |
|
| 16 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abc"); |
| 17 |
uint8_t expected[] = {0x35, 0x24, 0x41, 0xc2}; |
| 18 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 19 |
|
| 20 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_CRC32); |
| 21 |
} |
| 22 |
|
| 23 |
AWS_TEST_CASE(crc32_nist_test_case_1, s_crc32_nist_test_case_1_fn) |
| 24 |
|
| 25 |
static int s_crc32_nist_test_case_2_fn(struct aws_allocator *allocator, void *ctx) { |
| 26 |
(void)ctx; |
| 27 |
|
| 28 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(""); |
| 29 |
uint8_t expected[] = {0x00, 0x00, 0x00, 0x00}; |
| 30 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 31 |
|
| 32 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_CRC32); |
| 33 |
} |
| 34 |
|
| 35 |
AWS_TEST_CASE(crc32_nist_test_case_2, s_crc32_nist_test_case_2_fn) |
| 36 |
|
| 37 |
static int s_crc32_nist_test_case_3_fn(struct aws_allocator *allocator, void *ctx) { |
| 38 |
(void)ctx; |
| 39 |
|
| 40 |
struct aws_byte_cursor input = |
| 41 |
aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 42 |
uint8_t expected[] = {0x17, 0x1a, 0x3f, 0x5f}; |
| 43 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 44 |
|
| 45 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_CRC32); |
| 46 |
} |
| 47 |
|
| 48 |
AWS_TEST_CASE(crc32_nist_test_case_3, s_crc32_nist_test_case_3_fn) |
| 49 |
|
| 50 |
static int s_crc32_nist_test_case_4_fn(struct aws_allocator *allocator, void *ctx) { |
| 51 |
(void)ctx; |
| 52 |
|
| 53 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 54 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 55 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 56 |
uint8_t expected[] = {0x19, 0x1f, 0x33, 0x49}; |
| 57 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 58 |
|
| 59 |
return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_CRC32); |
| 60 |
} |
| 61 |
|
| 62 |
AWS_TEST_CASE(crc32_nist_test_case_4, s_crc32_nist_test_case_4_fn) |
| 63 |
|
| 64 |
static int s_crc32_nist_test_case_5_fn(struct aws_allocator *allocator, void *ctx) { |
| 65 |
(void)ctx; |
| 66 |
|
| 67 |
aws_s3_library_init(allocator); |
| 68 |
|
| 69 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_CRC32); |
| 70 |
ASSERT_NOT_NULL(checksum); |
| 71 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a"); |
| 72 |
|
| 73 |
for (size_t i = 0; i < 1000000; ++i) { |
| 74 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 75 |
} |
| 76 |
|
| 77 |
uint8_t output[AWS_CRC32_LEN] = {0}; |
| 78 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 79 |
output_buf.len = 0; |
| 80 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 81 |
|
| 82 |
uint8_t expected[] = {0xdc, 0x25, 0xbf, 0xbc}; |
| 83 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 84 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 85 |
|
| 86 |
aws_checksum_destroy(checksum); |
| 87 |
|
| 88 |
aws_s3_library_clean_up(); |
| 89 |
|
| 90 |
return AWS_OP_SUCCESS; |
| 91 |
} |
| 92 |
|
| 93 |
AWS_TEST_CASE(crc32_nist_test_case_5, s_crc32_nist_test_case_5_fn) |
| 94 |
|
| 95 |
static int s_crc32_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) { |
| 96 |
(void)ctx; |
| 97 |
|
| 98 |
aws_s3_library_init(allocator); |
| 99 |
|
| 100 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_CRC32); |
| 101 |
ASSERT_NOT_NULL(checksum); |
| 102 |
struct aws_byte_cursor input = |
| 103 |
aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"); |
| 104 |
|
| 105 |
for (size_t i = 0; i < 16777216; ++i) { |
| 106 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 107 |
} |
| 108 |
|
| 109 |
uint8_t output[AWS_CRC32_LEN] = {0}; |
| 110 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 111 |
output_buf.len = 0; |
| 112 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 113 |
|
| 114 |
uint8_t expected[] = {0x55, 0x1c, 0xbc, 0x00}; |
| 115 |
|
| 116 |
struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 117 |
ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len); |
| 118 |
|
| 119 |
aws_checksum_destroy(checksum); |
| 120 |
|
| 121 |
aws_s3_library_clean_up(); |
| 122 |
|
| 123 |
return AWS_OP_SUCCESS; |
| 124 |
} |
| 125 |
|
| 126 |
AWS_TEST_CASE(crc32_nist_test_case_6, s_crc32_nist_test_case_6_fn) |
| 127 |
|
| 128 |
static int s_crc32_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) { |
| 129 |
(void)ctx; |
| 130 |
|
| 131 |
aws_s3_library_init(allocator); |
| 132 |
|
| 133 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 134 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 135 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 136 |
uint8_t output[AWS_CRC32_LEN] = {0}; |
| 137 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 138 |
output_buf.len = 1; |
| 139 |
|
| 140 |
ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_checksum_compute(allocator, AWS_SCA_CRC32, &input, &output_buf)); |
| 141 |
|
| 142 |
aws_s3_library_clean_up(); |
| 143 |
|
| 144 |
return AWS_OP_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
AWS_TEST_CASE(crc32_test_invalid_buffer, s_crc32_test_invalid_buffer_fn) |
| 148 |
|
| 149 |
static int s_crc32_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) { |
| 150 |
(void)ctx; |
| 151 |
|
| 152 |
aws_s3_library_init(allocator); |
| 153 |
|
| 154 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 155 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 156 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 157 |
uint8_t expected[] = {0x19, 0x1f, 0x33, 0x49}; |
| 158 |
|
| 159 |
uint8_t output[AWS_CRC32_LEN] = {0}; |
| 160 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 161 |
output_buf.len = 0; |
| 162 |
|
| 163 |
ASSERT_SUCCESS(aws_checksum_compute(allocator, AWS_SCA_CRC32, &input, &output_buf)); |
| 164 |
ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len); |
| 165 |
|
| 166 |
aws_s3_library_clean_up(); |
| 167 |
|
| 168 |
return AWS_OP_SUCCESS; |
| 169 |
} |
| 170 |
|
| 171 |
AWS_TEST_CASE(crc32_test_oneshot, s_crc32_test_oneshot_fn) |
| 172 |
|
| 173 |
static int s_crc32_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) { |
| 174 |
(void)ctx; |
| 175 |
|
| 176 |
aws_s3_library_init(allocator); |
| 177 |
|
| 178 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij" |
| 179 |
"klmghijklmnhijklmnoijklmnopjklmnopqklm" |
| 180 |
"nopqrlmnopqrsmnopqrstnopqrstu"); |
| 181 |
|
| 182 |
struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_CRC32); |
| 183 |
ASSERT_NOT_NULL(checksum); |
| 184 |
|
| 185 |
uint8_t output[AWS_CRC32_LEN] = {0}; |
| 186 |
struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output)); |
| 187 |
output_buf.len = 0; |
| 188 |
|
| 189 |
ASSERT_SUCCESS(aws_checksum_update(checksum, &input)); |
| 190 |
ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf)); |
| 191 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_update(checksum, &input)); |
| 192 |
ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_finalize(checksum, &output_buf)); |
| 193 |
|
| 194 |
aws_checksum_destroy(checksum); |
| 195 |
|
| 196 |
aws_s3_library_clean_up(); |
| 197 |
|
| 198 |
return AWS_OP_SUCCESS; |
| 199 |
} |
| 200 |
|
| 201 |
AWS_TEST_CASE(crc32_test_invalid_state, s_crc32_test_invalid_state_fn) |