summaryrefslogtreecommitdiff
path: root/src/tests/emile
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2016-01-07 15:53:12 -0800
committerCedric BAIL <cedric@osg.samsung.com>2016-01-07 16:27:57 -0800
commitbb921aff04b5497d9339e73625c07a26511cdf3b (patch)
tree99ff80fab28d6073a4ef2c4c987ec801c65da851 /src/tests/emile
parentc451d5885bbf5d936c52c26731e044da7cd43075 (diff)
emile: move all eina_str_base64 to emile_base64.
Diffstat (limited to 'src/tests/emile')
-rw-r--r--src/tests/emile/emile_suite.c121
1 files changed, 118 insertions, 3 deletions
diff --git a/src/tests/emile/emile_suite.c b/src/tests/emile/emile_suite.c
index 7d37367bb1..1e421fba39 100644
--- a/src/tests/emile/emile_suite.c
+++ b/src/tests/emile/emile_suite.c
@@ -17,20 +17,135 @@ START_TEST(emile_test_init)
17} 17}
18END_TEST 18END_TEST
19 19
20/* All cases are taken from https://en.wikipedia.org/wiki/Base64 */
21static const struct {
22 char *decoded_str;
23 char *encoded_normal;
24 char *encoded_url;
25 unsigned int len;
26 Eina_Bool not;
27} base64_tests[] = {
28 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", "YW55IGNhcm5hbCBwbGVhc3VyZS4", 20 },
29 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZQ==", "YW55IGNhcm5hbCBwbGVhc3VyZQ", 19 },
30 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3Vy", "YW55IGNhcm5hbCBwbGVhc3Vy", 18 },
31 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3U=", "YW55IGNhcm5hbCBwbGVhc3U", 17 },
32 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhcw==", "YW55IGNhcm5hbCBwbGVhcw", 16 },
33 { "pleasure.", "cGxlYXN1cmUu", "cGxlYXN1cmUu", 9 },
34 { "leasure.", "bGVhc3VyZS4=", "bGVhc3VyZS4", 8 },
35 { "easure.", "ZWFzdXJlLg==", "ZWFzdXJlLg", 7 },
36 { "asure.", "YXN1cmUu", "YXN1cmUu", 6 },
37 { "sure.", "c3VyZS4=", "c3VyZS4", 5 },
38 /* The following 2 cases are manually generated for -/ testing*/
39 { "aabc123!?", "YWFiYzEyMyE/", "YWFiYzEyMyE_", 9 },
40 { "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", "YWJjMTIzIT8kKiYoKSctPUB-", 18 }
41};
42
43START_TEST(emile_test_base64)
44{
45 Eina_Binbuf *buffer, *decoded;
46 Eina_Strbuf *str, *encoded;
47 unsigned int i;
48
49 buffer = eina_binbuf_new();
50 str = eina_strbuf_new();
51
52 for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
53 {
54 eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
55 eina_strbuf_append(str, base64_tests[i].encoded_normal);
56
57 encoded = emile_base64_encode(buffer);
58 fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_normal));
59
60 decoded = emile_base64_decode(str);
61 fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
62
63 fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
64
65 eina_strbuf_free(encoded);
66 eina_binbuf_free(decoded);
67
68 eina_binbuf_reset(buffer);
69 eina_strbuf_reset(str);
70 }
71
72 //Failure scenarios.
73 decoded = emile_base64_decode(NULL);
74 fail_if(decoded);
75
76 eina_strbuf_append(str, "TWFu");
77 decoded = emile_base64_decode(str);
78 eina_strbuf_reset(str);
79
80 fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
81 eina_binbuf_free(decoded);
82
83 eina_strbuf_append(str, "abc");
84 decoded = emile_base64_decode(str);
85 eina_strbuf_reset(str);
86 fail_if(decoded);
87}
88END_TEST
89
90START_TEST(emile_test_base64url)
91{
92 Eina_Binbuf *buffer, *decoded;
93 Eina_Strbuf *str, *encoded;
94 unsigned int i;
95
96 buffer = eina_binbuf_new();
97 str = eina_strbuf_new();
98
99 for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
100 {
101 eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
102 eina_strbuf_append(str, base64_tests[i].encoded_url);
103
104 encoded = emile_base64url_encode(buffer);
105 fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_url));
106
107 decoded = emile_base64url_decode(str);
108 fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
109
110 fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
111
112 eina_strbuf_free(encoded);
113 eina_binbuf_free(decoded);
114
115 eina_binbuf_reset(buffer);
116 eina_strbuf_reset(str);
117 }
118
119 //Failure scenarios.
120 decoded = emile_base64url_decode(NULL);
121 fail_if(decoded);
122
123 eina_strbuf_append(str, "TWFu");
124 decoded = emile_base64url_decode(str);
125 fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
126}
127END_TEST
128
20static void 129static void
21emile_base_test(TCase *tc) 130emile_base_test(TCase *tc)
22{ 131{
23 tcase_add_test(tc, emile_test_init); 132 tcase_add_test(tc, emile_test_init);
24} 133}
25 134
135static void
136emile_base64_test(TCase *tc)
137{
138 tcase_add_test(tc, emile_test_base64);
139 tcase_add_test(tc, emile_test_base64url);
140}
141
26static const struct 142static const struct
27{ 143{
28 const char *name; 144 const char *name;
29 void (*build)(TCase *tc); 145 void (*build)(TCase *tc);
30} tests[] = { 146} tests[] = {
31 { 147 { "Emile_Base", emile_base_test },
32 "Emile_Base", emile_base_test 148 { "Emile_Base64", emile_base64_test }
33 }
34}; 149};
35 150
36static void 151static void