@@ -168,6 +168,17 @@ static int s_hex_encoding_test_case_foobar(struct aws_allocator *allocator, void
168
168
169
169
AWS_TEST_CASE (hex_encoding_test_case_foobar_test , s_hex_encoding_test_case_foobar )
170
170
171
+ static int s_hex_encoding_append_test_case (struct aws_allocator * allocator , void * ctx ) {
172
+ (void )ctx ;
173
+
174
+ char test_data [] = "foobar" ;
175
+ char expected [] = "666f6f626172" ;
176
+
177
+ return s_run_hex_encoding_test_case (allocator , test_data , sizeof (test_data ), expected , sizeof (expected ) - 1 );
178
+ }
179
+
180
+ AWS_TEST_CASE (hex_encoding_append_test_case , s_hex_encoding_append_test_case )
181
+
171
182
static int s_hex_encoding_test_case_missing_leading_zero_fn (struct aws_allocator * allocator , void * ctx ) {
172
183
(void )allocator ;
173
184
(void )ctx ;
@@ -878,3 +889,80 @@ static int s_uint16_buffer_signed_negative_test_fn(struct aws_allocator *allocat
878
889
}
879
890
880
891
AWS_TEST_CASE (uint16_buffer_signed_negative_test , s_uint16_buffer_signed_negative_test_fn )
892
+
893
+ static int s_run_hex_encoding_append_dynamic_test_case (
894
+ struct aws_allocator * allocator ,
895
+ const char * test_str ,
896
+ const char * expected ,
897
+ size_t initial_capacity ,
898
+ size_t starting_offset ) {
899
+
900
+ size_t output_size = 2 * strlen (test_str );
901
+
902
+ struct aws_byte_cursor to_encode = aws_byte_cursor_from_c_str (test_str );
903
+
904
+ struct aws_byte_buf dest ;
905
+ ASSERT_SUCCESS (aws_byte_buf_init (& dest , allocator , initial_capacity ));
906
+ memset (dest .buffer , 0xdd , dest .capacity );
907
+
908
+ dest .len = starting_offset ;
909
+
910
+ ASSERT_SUCCESS (aws_hex_encode_append_dynamic (& to_encode , & dest ), "encode call should have succeeded" );
911
+
912
+ size_t expected_size = strlen (expected );
913
+
914
+ ASSERT_BIN_ARRAYS_EQUALS (
915
+ expected ,
916
+ expected_size ,
917
+ dest .buffer + starting_offset ,
918
+ output_size ,
919
+ "Encode output should have been {%s}, was {%s}." ,
920
+ expected ,
921
+ dest .buffer + starting_offset );
922
+ ASSERT_INT_EQUALS (output_size , dest .len - starting_offset );
923
+
924
+ for (size_t i = 0 ; i < starting_offset ; ++ i ) {
925
+ ASSERT_INT_EQUALS (
926
+ (unsigned char )* (dest .buffer + i ),
927
+ (unsigned char )0xdd ,
928
+ "Write should not have occurred before the the encoding's starting position." );
929
+ }
930
+
931
+ for (size_t i = starting_offset + output_size ; i < dest .capacity ; ++ i ) {
932
+ ASSERT_INT_EQUALS (
933
+ (unsigned char )* (dest .buffer + i ),
934
+ (unsigned char )0xdd ,
935
+ "Write should not have occurred after the encoding's final position." );
936
+ }
937
+
938
+ aws_byte_buf_clean_up (& dest );
939
+ return 0 ;
940
+ }
941
+
942
+ static int s_hex_encoding_append_dynamic_test_case_fooba (struct aws_allocator * allocator , void * ctx ) {
943
+ (void )ctx ;
944
+
945
+ char test_data [] = "fooba" ;
946
+ char expected [] = "666f6f6261" ;
947
+
948
+ ASSERT_TRUE (s_run_hex_encoding_append_dynamic_test_case (allocator , test_data , expected , 5 , 3 ) == AWS_OP_SUCCESS );
949
+ ASSERT_TRUE (s_run_hex_encoding_append_dynamic_test_case (allocator , test_data , expected , 50 , 3 ) == AWS_OP_SUCCESS );
950
+
951
+ return 0 ;
952
+ }
953
+
954
+ AWS_TEST_CASE (hex_encoding_append_dynamic_test_case_fooba , s_hex_encoding_append_dynamic_test_case_fooba )
955
+
956
+ static int s_hex_encoding_append_dynamic_test_case_empty (struct aws_allocator * allocator , void * ctx ) {
957
+ (void )ctx ;
958
+
959
+ char test_data [] = "" ;
960
+ char expected [] = "" ;
961
+
962
+ ASSERT_TRUE (s_run_hex_encoding_append_dynamic_test_case (allocator , test_data , expected , 5 , 3 ) == AWS_OP_SUCCESS );
963
+ ASSERT_TRUE (s_run_hex_encoding_append_dynamic_test_case (allocator , test_data , expected , 50 , 3 ) == AWS_OP_SUCCESS );
964
+
965
+ return 0 ;
966
+ }
967
+
968
+ AWS_TEST_CASE (hex_encoding_append_dynamic_test_case_empty , s_hex_encoding_append_dynamic_test_case_empty )
0 commit comments