eina: Adding test case for base64 decode function.

Summary:
Depends on D3381

Signed-off-by: Srivardhan Hebbar <sri.hebbar@samsung.com>

Reviewers: cedric, jpeg

Subscribers: jpeg

Differential Revision: https://phab.enlightenment.org/D3382
This commit is contained in:
Srivardhan Hebbar 2015-12-10 16:44:27 +09:00 committed by Jean-Philippe Andre
parent fa422dd3d1
commit 4103f38eed
1 changed files with 27 additions and 7 deletions

View File

@ -365,13 +365,13 @@ START_TEST(str_strftime)
} }
END_TEST END_TEST
START_TEST(str_base64_encode) START_TEST(str_base64_encode_decode)
{ {
/* All cases are taken from https://en.wikipedia.org/wiki/Base64 */ /* All cases are taken from https://en.wikipedia.org/wiki/Base64 */
static const struct { static const struct {
char *str; char *decoded_str;
char *expected; char *encoded_str;
unsigned int len; int len;
Eina_Bool not; Eina_Bool not;
} tests[] = { } tests[] = {
{ "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", 20 }, { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", 20 },
@ -389,15 +389,35 @@ START_TEST(str_base64_encode)
{ "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", 18 } { "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", 18 }
}; };
unsigned int i; unsigned int i;
int len;
unsigned char *decoded;
for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++) for (i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
{ {
char *encoded; char *encoded;
encoded = eina_str_base64_encode((unsigned char*) tests[i].str, tests[i].len); encoded = eina_str_base64_encode((unsigned char*) tests[i].decoded_str, tests[i].len);
fail_if(strcmp(encoded, tests[i].expected)); fail_if(strcmp(encoded, tests[i].encoded_str));
decoded = eina_str_base64_decode(tests[i].encoded_str, &len);
fail_if(memcmp(decoded, tests[i].decoded_str, tests[i].len));
fprintf(stderr, "len = %d, tests[%d].len = %d\n", len, i, tests[i].len);
fail_if(len != tests[i].len);
free(encoded); free(encoded);
free(decoded);
} }
//Failure scenarios.
decoded = eina_str_base64_decode(NULL, &len);
fail_if(decoded);
decoded = eina_str_base64_decode("TWFu", NULL);
fail_if(memcmp(decoded, "Man", 3));
decoded = eina_str_base64_decode("abc", &len);
fail_if(decoded);
} }
END_TEST END_TEST
@ -440,7 +460,7 @@ eina_test_str(TCase *tc)
tcase_add_test(tc, str_join_len); tcase_add_test(tc, str_join_len);
tcase_add_test(tc, str_memdup); tcase_add_test(tc, str_memdup);
tcase_add_test(tc, str_strftime); tcase_add_test(tc, str_strftime);
tcase_add_test(tc, str_base64_encode); tcase_add_test(tc, str_base64_encode_decode);
#ifdef HAVE_ICONV #ifdef HAVE_ICONV
tcase_add_test(tc, str_convert); tcase_add_test(tc, str_convert);
#endif #endif