1 #include "CppUTest/TestHarness.h" 2 #include "CppUTest/CommandLineTestRunner.h" 3 #include "btstack_util.h" 4 #include "btstack_base64_decoder.h" 5 6 void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){ 7 int i; 8 for (i=0; i<size; i++){ 9 if (expected[i] != actual[i]) { 10 printf("offset %u wrong\n", i); 11 printf("expected: "); printf_hexdump(expected, size); 12 printf("actual: "); printf_hexdump(actual, size); 13 } 14 BYTES_EQUAL(expected[i], actual[i]); 15 } 16 } 17 18 TEST_GROUP(Base64Decoder){ 19 btstack_base64_decoder_t context; 20 void setup(void){ 21 btstack_base64_decoder_init(&context); 22 } 23 }; 24 25 TEST(Base64Decoder, InvalidChar){ 26 int result = btstack_base64_decoder_process_byte(&context, (uint8_t) '@'); 27 CHECK_EQUAL(BTSTACK_BASE64_DECODER_INVALID, result); 28 } 29 30 TEST(Base64Decoder, abc){ 31 const uint8_t input[] = "YWJj"; 32 uint8_t output[3]; 33 int result = btstack_base64_decoder_process_block(input, strlen((const char*) input), output, sizeof(output)); 34 CHECK_EQUAL(sizeof(output), result); 35 CHECK_EQUAL_ARRAY((uint8_t *) "abc", output, 3); 36 } 37 38 TEST(Base64Decoder, ab){ 39 const uint8_t input[] = "YWI="; 40 uint8_t output[3]; 41 int result = btstack_base64_decoder_process_block(input, strlen((const char*) input), output, sizeof(output)); 42 CHECK_EQUAL(2, result); 43 CHECK_EQUAL_ARRAY((uint8_t *) "ab", output, 2); 44 } 45 46 TEST(Base64Decoder, a){ 47 const uint8_t input[] = "YQ=="; 48 uint8_t output[3]; 49 int result = btstack_base64_decoder_process_block(input, strlen((const char*) input), output, sizeof(output)); 50 CHECK_EQUAL(1, result); 51 CHECK_EQUAL_ARRAY((uint8_t *) "a", output, 1); 52 } 53 54 int main (int argc, const char * argv[]){ 55 return CommandLineTestRunner::RunAllTests(argc, argv); 56 } 57