summaryrefslogtreecommitdiff
path: root/src/tests/emile
diff options
context:
space:
mode:
authorVincent Torri <vincent dot torri at gmail dot com>2016-02-04 14:59:36 +0100
committerTom Hacohen <tom@stosb.com>2016-02-16 12:41:06 +0000
commit41e626c190f0ad6da94c43d243cf4ab7d93898c1 (patch)
tree20faa6308f1365bffa44372dc1e69e51b145b11c /src/tests/emile
parent9dcad9fd9ea0636ecad32ef44b03cd5c10c1d587 (diff)
Test rework #16: Emile
Diffstat (limited to 'src/tests/emile')
-rw-r--r--src/tests/emile/emile_suite.c227
-rw-r--r--src/tests/emile/emile_suite.h9
-rw-r--r--src/tests/emile/emile_test_base.c20
-rw-r--r--src/tests/emile/emile_test_base64.c124
4 files changed, 165 insertions, 215 deletions
diff --git a/src/tests/emile/emile_suite.c b/src/tests/emile/emile_suite.c
index 1e421fba39..2c968cfc34 100644
--- a/src/tests/emile/emile_suite.c
+++ b/src/tests/emile/emile_suite.c
@@ -1,231 +1,28 @@
1#ifdef HAVE_CONFIG_H 1#ifdef HAVE_CONFIG_H
2#include <config.h> 2#include <config.h>
3#endif /* ifdef HAVE_CONFIG_H */ 3#endif
4
5#include <stdlib.h>
6#include <stdio.h>
7
8#include <check.h>
9
10#include <Eina.h>
11#include <Emile.h>
12
13START_TEST(emile_test_init)
14{
15 fail_if(emile_init() <= 0);
16 fail_if(emile_shutdown() != 0);
17}
18END_TEST
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
129static void
130emile_base_test(TCase *tc)
131{
132 tcase_add_test(tc, emile_test_init);
133}
134 4
135static void 5#include "emile_suite.h"
136emile_base64_test(TCase *tc) 6#include "../efl_check.h"
137{
138 tcase_add_test(tc, emile_test_base64);
139 tcase_add_test(tc, emile_test_base64url);
140}
141 7
142static const struct 8static const Efl_Test_Case etc[] = {
143{ 9 { "Emile_Base", emile_test_base },
144 const char *name; 10 { "Emile_Base64", emile_test_base64 },
145 void (*build)(TCase *tc); 11 { NULL, NULL }
146} tests[] = {
147 { "Emile_Base", emile_base_test },
148 { "Emile_Base64", emile_base64_test }
149}; 12};
150 13
151static void
152_list_tests(void)
153{
154 unsigned int i;
155
156 fputs("Available tests cases :\n", stderr);
157 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
158 fprintf(stderr, "\t%s\n", tests[i].name);
159}
160
161static Eina_Bool
162_use_test(const char *name, int argc, const char *argv[])
163{
164 argc--;
165 argv--;
166
167 if (argc < 1)
168 return EINA_TRUE;
169
170 for (; argc > 1; argc--, argv++)
171 if (strcmp(name, *argv) == 0)
172 return EINA_TRUE;
173 return EINA_FALSE;
174}
175
176static Suite *
177emile_suite_build(int argc, const char **argv)
178{
179 TCase *tc;
180 Suite *s;
181 unsigned int i;
182
183 s = suite_create("Emile");
184
185 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
186 {
187 if (!_use_test(tests[i].name, argc, argv))
188 continue;
189
190 tc = tcase_create(tests[i].name);
191 tests[i].build(tc);
192 suite_add_tcase(s, tc);
193#ifndef _WIN32
194 tcase_set_timeout(tc, 0);
195#endif
196 }
197
198 return s;
199}
200
201int 14int
202main(int argc, char *argv[]) 15main(int argc, char *argv[])
203{ 16{
204 SRunner *sr;
205 Suite *s;
206 int failed_count; 17 int failed_count;
207 int j;
208 18
209 for (j = 1; j < argc; j++) 19 if (!_efl_test_option_disp(argc, argv, etc))
210 if ((strcmp(argv[j], "-h") == 0) || (strcmp(argv[j], "--help") == 0)) 20 return 0;
211 {
212 fprintf(stderr, "Usage:\n\t%s [test_case1 .. [test_caseN]]\n", argv[0]);
213 _list_tests();
214 return 0;
215 }
216 else if ((strcmp(argv[j], "-l") == 0) || (strcmp(argv[j], "--list") == 0))
217 {
218 _list_tests();
219 return 0;
220 }
221 21
222 s = emile_suite_build(argc, (const char **)argv); 22 putenv("EFL_RUN_IN_TREE=1");
223 sr = srunner_create(s);
224 23
225 srunner_set_xml(sr, TESTS_BUILD_DIR "/check-results.xml"); 24 failed_count = _efl_suite_build_and_run(argc - 1, (const char **)argv + 1,
226 srunner_run_all(sr, CK_ENV); 25 "Emile", etc);
227 failed_count = srunner_ntests_failed(sr);
228 srunner_free(sr);
229 26
230 return (failed_count == 0) ? 0 : 255; 27 return (failed_count == 0) ? 0 : 255;
231} 28}
diff --git a/src/tests/emile/emile_suite.h b/src/tests/emile/emile_suite.h
new file mode 100644
index 0000000000..9e940d107a
--- /dev/null
+++ b/src/tests/emile/emile_suite.h
@@ -0,0 +1,9 @@
1#ifndef _EMILE_SUITE_H
2#define _EMILE_SUITE_H
3
4#include <check.h>
5
6void emile_test_base(TCase *tc);
7void emile_test_base64(TCase *tc);
8
9#endif /* _EMILE_SUITE_H */
diff --git a/src/tests/emile/emile_test_base.c b/src/tests/emile/emile_test_base.c
new file mode 100644
index 0000000000..d4514d1ea8
--- /dev/null
+++ b/src/tests/emile/emile_test_base.c
@@ -0,0 +1,20 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Eina.h>
6#include <Emile.h>
7
8#include "emile_suite.h"
9
10START_TEST(emile_test_init)
11{
12 fail_if(emile_init() <= 0);
13 fail_if(emile_shutdown() != 0);
14}
15END_TEST
16
17void emile_test_base(TCase *tc)
18{
19 tcase_add_test(tc, emile_test_init);
20}
diff --git a/src/tests/emile/emile_test_base64.c b/src/tests/emile/emile_test_base64.c
new file mode 100644
index 0000000000..ca035766c8
--- /dev/null
+++ b/src/tests/emile/emile_test_base64.c
@@ -0,0 +1,124 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Eina.h>
6#include <Emile.h>
7
8#include "emile_suite.h"
9
10/* All cases are taken from https://en.wikipedia.org/wiki/Base64 */
11static const struct {
12 char *decoded_str;
13 char *encoded_normal;
14 char *encoded_url;
15 unsigned int len;
16 Eina_Bool not;
17} base64_tests[] = {
18 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZS4=", "YW55IGNhcm5hbCBwbGVhc3VyZS4", 20 },
19 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3VyZQ==", "YW55IGNhcm5hbCBwbGVhc3VyZQ", 19 },
20 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3Vy", "YW55IGNhcm5hbCBwbGVhc3Vy", 18 },
21 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhc3U=", "YW55IGNhcm5hbCBwbGVhc3U", 17 },
22 { "any carnal pleasure.", "YW55IGNhcm5hbCBwbGVhcw==", "YW55IGNhcm5hbCBwbGVhcw", 16 },
23 { "pleasure.", "cGxlYXN1cmUu", "cGxlYXN1cmUu", 9 },
24 { "leasure.", "bGVhc3VyZS4=", "bGVhc3VyZS4", 8 },
25 { "easure.", "ZWFzdXJlLg==", "ZWFzdXJlLg", 7 },
26 { "asure.", "YXN1cmUu", "YXN1cmUu", 6 },
27 { "sure.", "c3VyZS4=", "c3VyZS4", 5 },
28 /* The following 2 cases are manually generated for -/ testing*/
29 { "aabc123!?", "YWFiYzEyMyE/", "YWFiYzEyMyE_", 9 },
30 { "abc123!?$*&()'-=@~", "YWJjMTIzIT8kKiYoKSctPUB+", "YWJjMTIzIT8kKiYoKSctPUB-", 18 }
31};
32
33START_TEST(emile_test_base64_normal)
34{
35 Eina_Binbuf *buffer, *decoded;
36 Eina_Strbuf *str, *encoded;
37 unsigned int i;
38
39 buffer = eina_binbuf_new();
40 str = eina_strbuf_new();
41
42 for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
43 {
44 eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
45 eina_strbuf_append(str, base64_tests[i].encoded_normal);
46
47 encoded = emile_base64_encode(buffer);
48 fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_normal));
49
50 decoded = emile_base64_decode(str);
51 fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
52
53 fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
54
55 eina_strbuf_free(encoded);
56 eina_binbuf_free(decoded);
57
58 eina_binbuf_reset(buffer);
59 eina_strbuf_reset(str);
60 }
61
62 //Failure scenarios.
63 decoded = emile_base64_decode(NULL);
64 fail_if(decoded);
65
66 eina_strbuf_append(str, "TWFu");
67 decoded = emile_base64_decode(str);
68 eina_strbuf_reset(str);
69
70 fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
71 eina_binbuf_free(decoded);
72
73 eina_strbuf_append(str, "abc");
74 decoded = emile_base64_decode(str);
75 eina_strbuf_reset(str);
76 fail_if(decoded);
77}
78END_TEST
79
80START_TEST(emile_test_base64_url)
81{
82 Eina_Binbuf *buffer, *decoded;
83 Eina_Strbuf *str, *encoded;
84 unsigned int i;
85
86 buffer = eina_binbuf_new();
87 str = eina_strbuf_new();
88
89 for (i = 0; i < sizeof (base64_tests) / sizeof (base64_tests[0]); i++)
90 {
91 eina_binbuf_append_length(buffer, (const unsigned char *) base64_tests[i].decoded_str, base64_tests[i].len);
92 eina_strbuf_append(str, base64_tests[i].encoded_url);
93
94 encoded = emile_base64url_encode(buffer);
95 fail_if(strcmp(eina_strbuf_string_get(encoded), base64_tests[i].encoded_url));
96
97 decoded = emile_base64url_decode(str);
98 fail_if(memcmp(eina_binbuf_string_get(decoded), base64_tests[i].decoded_str, base64_tests[i].len));
99
100 fail_if(eina_binbuf_length_get(decoded) != base64_tests[i].len);
101
102 eina_strbuf_free(encoded);
103 eina_binbuf_free(decoded);
104
105 eina_binbuf_reset(buffer);
106 eina_strbuf_reset(str);
107 }
108
109 //Failure scenarios.
110 decoded = emile_base64url_decode(NULL);
111 fail_if(decoded);
112
113 eina_strbuf_append(str, "TWFu");
114 decoded = emile_base64url_decode(str);
115 fail_if(memcmp(eina_binbuf_string_get(decoded), "Man", 3));
116}
117END_TEST
118
119void
120emile_test_base64(TCase *tc)
121{
122 tcase_add_test(tc, emile_test_base64_normal);
123 tcase_add_test(tc, emile_test_base64_url);
124}