Fix error creation and warning
[claws.git] / src / common / tests / md5_test.c
blob679c14e2f5d89e1442ee035fd11d696f023cf5b1
1 #include "config.h"
3 #include <glib.h>
5 #include <common/md5.h>
7 struct td_digest {
8 gchar *input;
9 gchar *expected_output;
12 struct td_digest td_rfc1321_1 = { "", "d41d8cd98f00b204e9800998ecf8427e" };
13 struct td_digest td_rfc1321_2 = { "a", "0cc175b9c0f1b6a831c399e269772661" };
14 struct td_digest td_rfc1321_3 = { "abc", "900150983cd24fb0d6963f7d28e17f72" };
15 struct td_digest td_rfc1321_4 = { "message digest", "f96b697d7cb7938d525a2f31aaf161d0" };
16 struct td_digest td_rfc1321_5 = { "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" };
17 struct td_digest td_rfc1321_6 = { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f" };
18 struct td_digest td_rfc1321_7 = { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a" };
20 struct td_hmac {
21 gchar *key;
22 int key_len;
23 gchar *data;
24 int data_len;
25 gchar *expected_output;
28 struct td_hmac td_hmac_null_key = {
29 NULL, 50,
30 "", 0,
33 struct td_hmac td_hmac_negative_key_length = {
34 "", -1,
35 "", 0,
38 struct td_hmac td_hmac_null_data = {
39 "", 0,
40 NULL, 50,
43 struct td_hmac td_hmac_negative_data_length = {
44 "abc", 3,
45 "", -1,
46 "",
49 struct td_hmac td_rfc2202_1 = {
50 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16,
51 "Hi There", 8,
52 "9294727a3638bb1c13f48ef8158bfc9d"
54 struct td_hmac td_rfc2202_2 = {
55 "Jefe", 4,
56 "what do ya want for nothing?", 28,
57 "750c783e6ab0b503eaa86e310a5db738"
59 struct td_hmac td_rfc2202_3 = {
60 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 16,
61 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd", 50,
62 "56be34521d144c88dbb8c733f0e8b3f6"
64 struct td_hmac td_rfc2202_4 = {
65 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", 25,
66 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd", 50,
67 "697eaf0aca3a3aea3a75164746ffaa79"
69 struct td_hmac td_rfc2202_5 = {
70 "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c", 16,
71 "Test With Truncation", 20,
72 "56461ef2342edc00f9bab995690efd4c"
74 struct td_hmac td_rfc2202_6 = {
75 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 80,
76 "Test Using Larger Than Block-Size Key - Hash Key First", 54,
77 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
79 struct td_hmac td_rfc2202_7 = {
80 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 80,
81 "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data", 73,
82 "6f630fad67cda0ee1fb1f562db3aa53e"
85 static void
86 test_md5_hex_digest_null_input(void)
88 char outbuf[33];
90 outbuf[0] = '\0';
92 if (!g_test_undefined())
93 return;
95 if (g_test_subprocess()) {
96 md5_hex_digest(outbuf, NULL);
97 return;
100 g_test_trap_subprocess(NULL, 0, 0);
101 g_test_trap_assert_failed();
102 g_test_trap_assert_stderr("*CRITICAL*md5_hex_digest: assertion*failed*");
103 g_assert_cmpint(outbuf[0], ==, '\0');
106 static void
107 test_md5_hex_digest_null_output(void)
109 if (!g_test_undefined())
110 return;
112 if (g_test_subprocess()) {
113 md5_hex_digest(NULL, "");
114 return;
117 g_test_trap_subprocess(NULL, 0, 0);
118 g_test_trap_assert_failed();
119 g_test_trap_assert_stderr("*CRITICAL*md5_hex_digest: assertion*failed*");
122 static void
123 test_md5_hex_digest(gconstpointer user_data)
125 char outbuf[33];
126 struct td_digest *data = (struct td_digest *)user_data;
128 md5_hex_digest(outbuf, data->input);
130 g_assert_cmpint(outbuf[32], ==, '\0');
131 g_assert_cmpstr(outbuf, ==, data->expected_output);
134 static void
135 test_md5_hex_hmac_null_output(void)
137 if (!g_test_undefined())
138 return;
140 if (g_test_subprocess()) {
141 md5_hex_hmac(NULL, "", 0, "", 0);
142 return;
145 g_test_trap_subprocess(NULL, 0, 0);
146 g_test_trap_assert_failed();
147 g_test_trap_assert_stderr("*CRITICAL*md5_hex_hmac: assertion*failed*");
150 /* We expect all test cases using this function to fail with
151 * failed assertion */
152 static void
153 test_md5_hex_hmac_fails(gconstpointer user_data)
155 char outbuf[33];
156 struct td_hmac *data = (struct td_hmac *)user_data;
158 if (!g_test_undefined())
159 return;
161 if (g_test_subprocess()) {
162 md5_hex_hmac(outbuf,
163 data->data, data->data_len,
164 data->key, data->key_len);
165 return;
168 g_test_trap_subprocess(NULL, 0, 0);
169 g_test_trap_assert_failed();
170 g_test_trap_assert_stderr("*CRITICAL*md5_hex_hmac: assertion*failed*");
173 static void
174 test_md5_hex_hmac(gconstpointer user_data)
176 char outbuf[33];
177 struct td_hmac *data = (struct td_hmac *)user_data;
179 md5_hex_hmac(outbuf,
180 data->data, data->data_len,
181 data->key, data->key_len);
183 g_assert_cmpint(outbuf[32], ==, '\0');
184 g_assert_cmpstr(outbuf, ==, data->expected_output);
189 main(int argc, char *argv[])
191 g_test_init(&argc, &argv, NULL);
193 g_test_add_func("/common/md5/hex_digest/null_input",
194 test_md5_hex_digest_null_input);
195 g_test_add_func("/common/md5/hex_digest/null_output",
196 test_md5_hex_digest_null_output);
198 g_test_add_data_func("/common/md5/hex_digest/rfc1321_1",
199 &td_rfc1321_1,
200 test_md5_hex_digest);
201 g_test_add_data_func("/common/md5/hex_digest/rfc1321_2",
202 &td_rfc1321_2,
203 test_md5_hex_digest);
204 g_test_add_data_func("/common/md5/hex_digest/rfc1321_3",
205 &td_rfc1321_3,
206 test_md5_hex_digest);
207 g_test_add_data_func("/common/md5/hex_digest/rfc1321_4",
208 &td_rfc1321_4,
209 test_md5_hex_digest);
210 g_test_add_data_func("/common/md5/hex_digest/rfc1321_5",
211 &td_rfc1321_5,
212 test_md5_hex_digest);
213 g_test_add_data_func("/common/md5/hex_digest/rfc1321_6",
214 &td_rfc1321_6,
215 test_md5_hex_digest);
216 g_test_add_data_func("/common/md5/hex_digest/rfc1321_7",
217 &td_rfc1321_7,
218 test_md5_hex_digest);
220 g_test_add_data_func("/common/md5/hex_hmac/null_key",
221 &td_hmac_null_key,
222 test_md5_hex_hmac_fails);
223 g_test_add_data_func("/common/md5/hex_hmac/negative_key_length",
224 &td_hmac_negative_key_length,
225 test_md5_hex_hmac_fails);
226 g_test_add_data_func("/common/md5/hex_hmac/null_data",
227 &td_hmac_null_data,
228 test_md5_hex_hmac_fails);
229 g_test_add_data_func("/common/md5/hex_hmac/negative_data_length",
230 &td_hmac_negative_data_length,
231 test_md5_hex_hmac_fails);
232 g_test_add_func("/common/md5/hex_hmac/null_output",
233 test_md5_hex_hmac_null_output);
235 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_1",
236 &td_rfc2202_1,
237 test_md5_hex_hmac);
238 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_2",
239 &td_rfc2202_2,
240 test_md5_hex_hmac);
241 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_3",
242 &td_rfc2202_3,
243 test_md5_hex_hmac);
244 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_4",
245 &td_rfc2202_4,
246 test_md5_hex_hmac);
247 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_5",
248 &td_rfc2202_5,
249 test_md5_hex_hmac);
250 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_6",
251 &td_rfc2202_6,
252 test_md5_hex_hmac);
253 g_test_add_data_func("/common/md5/hex_hmac/rfc2202_7",
254 &td_rfc2202_7,
255 test_md5_hex_hmac);
257 return g_test_run();