Line data Source code
1 : /* TLD library -- test the TLD interface for emails
2 : * Copyright (C) 2013-2015 Made to Order Software Corp.
3 : *
4 : * Permission is hereby granted, free of charge, to any person obtaining a
5 : * copy of this software and associated documentation files (the
6 : * "Software"), to deal in the Software without restriction, including
7 : * without limitation the rights to use, copy, modify, merge, publish,
8 : * distribute, sublicense, and/or sell copies of the Software, and to
9 : * permit persons to whom the Software is furnished to do so, subject to
10 : * the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included
13 : * in all copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 : */
23 :
24 : /** \file
25 : * \brief Test the tld_email_list class.
26 : *
27 : * This file implements various tests to verify that the
28 : * tld_email_list functions as expected.
29 : */
30 :
31 : #include "libtld/tld.h"
32 : #include <stdlib.h>
33 : #include <stdio.h>
34 : #include <string.h>
35 : #include <sstream>
36 :
37 : /// The number of errors encountered before exiting.
38 : int err_count = 0;
39 :
40 : /// Whether to be verbose, turned off by default.
41 : int verbose = 0;
42 :
43 :
44 : /** \brief Print an error.
45 : *
46 : * This function prints the specified \p msg in stderr and increases
47 : * the error counter by one.
48 : *
49 : * \param[in] msg The message to be printed.
50 : */
51 0 : void error(const std::string& msg)
52 : {
53 0 : fprintf(stderr, "%s\n", msg.c_str());
54 0 : ++err_count;
55 0 : }
56 :
57 :
58 : /// Macro to check that exceptions are raised without having to write the try/catch each time.
59 : #define EXPECTED_THROW(s, e) \
60 : try \
61 : { \
62 : static_cast<void>(s); \
63 : error("error: bad." #s "() of \"\" did not throw an error."); \
64 : } \
65 : catch(const e&) \
66 : { \
67 : }
68 :
69 :
70 : /** \brief Define a valid email string.
71 : *
72 : * This structure is used to define a valid email string. The string may
73 : * include any number of emails as defined by the \p f_count field. Note
74 : * that the count is increased by 1 for each group definition in the list
75 : * defined in the \p f_input_email string.
76 : *
77 : * This structure is used to validate many different types of email
78 : * addresses to make sure that our parser works properly.
79 : */
80 : struct valid_email
81 : {
82 : /// The valid emails to be parsed.
83 : const char * f_input_email;
84 : /// The number of emails returned on f_input_email was parsed, plus one per group.
85 : int f_count;
86 : };
87 :
88 : //const char * f_group;
89 : //const char * f_original_email;
90 : //const char * f_fullname;
91 : //const char * f_username;
92 : //const char * f_domain;
93 : //const char * f_email_only;
94 : //const char * f_canonicalized_email;
95 :
96 : /// List of results to verify all the fields of the parser output. There is one entry per group and email.
97 : const tld_email list_of_results[] =
98 : {
99 : { "", "alexis@m2osw.com",
100 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
101 : { "", "a@m2osw.com",
102 : "", "a", "m2osw.com", "a@m2osw.com", "a@m2osw.com" },
103 : { "", "b@c.com",
104 : "", "b", "c.com", "b@c.com", "b@c.com" },
105 : { "", "alexis@m2osw.com",
106 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
107 : { "", "\"Wilke, Alexis\" <alexis@m2osw.com>",
108 : "Wilke, Alexis", "alexis", "m2osw.com", "alexis@m2osw.com", "\"Wilke, Alexis\" <alexis@m2osw.com>" },
109 : { "", "(* Pascal Comments *) \t alexis@m2osw.com\n (Just (kidding) he! he!)",
110 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
111 : { "", "(Start-Comment)alexis@ \t [ \t m2osw.com \t ] \n (More (comment) here)",
112 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
113 : { "", "(Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there)",
114 : "", "al.ex.is", "m2osw.com", "al.ex.is@m2osw.com", "al.ex.is@m2osw.com" },
115 : { "", "< (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) >",
116 : "", "al.ex.is", "m2osw.com", "al.ex.is@m2osw.com", "al.ex.is@m2osw.com" },
117 : { "", "(With full name) Alexis Wilke < (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) >",
118 : "Alexis Wilke", "al.ex.is", "m2osw.com", "al.ex.is@m2osw.com", "Alexis Wilke <al.ex.is@m2osw.com>" },
119 : { "This Group", "",
120 : "", "", "", "", "" },
121 : { "This Group", "(With full name) Alexis Wilke < \n alexis \t @ \t [ \t m2osw.com \t ] \n (Less) >",
122 : "Alexis Wilke", "alexis", "m2osw.com", "alexis@m2osw.com", "Alexis Wilke <alexis@m2osw.com>" },
123 : { "People", "",
124 : "", "", "", "", "" },
125 : { "People", "Alexis Wilke <alexis@m2osw.com>",
126 : "Alexis Wilke", "alexis", "m2osw.com", "alexis@m2osw.com", "Alexis Wilke <alexis@m2osw.com>" },
127 : { "People", "John Smith <john@m2osw.com>",
128 : "John Smith", "john", "m2osw.com", "john@m2osw.com", "John Smith <john@m2osw.com>" },
129 : { "Lists", "",
130 : "", "", "", "", "" },
131 : { "Lists", "Contact <contact@m2osw.com>",
132 : "Contact", "contact", "m2osw.com", "contact@m2osw.com", "Contact <contact@m2osw.com>" },
133 : { "Lists", "Resume <resume@m2osw.com>",
134 : "Resume", "resume", "m2osw.com", "resume@m2osw.com", "Resume <resume@m2osw.com>" },
135 : { "", "normal@m2osw.com",
136 : "", "normal", "m2osw.com", "normal@m2osw.com", "normal@m2osw.com" },
137 : { "No-Reply", "",
138 : "", "", "", "", "" },
139 : { "No-Reply", "no-reply@m2osw.com",
140 : "", "no-reply", "m2osw.com", "no-reply@m2osw.com", "no-reply@m2osw.com" },
141 : { "", "\"Complex <name> for !a! \\\"USER\\\"\" <user@example.co.uk>",
142 : "Complex <name> for !a! \"USER\"", "user", "example.co.uk", "user@example.co.uk", "\"Complex <name> for !a! \\\"USER\\\"\" <user@example.co.uk>" },
143 : { "", "(Comment \n New-Line) alexis@m2osw.com",
144 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
145 : { "", "(Comment (Sub-Comment (Sub-Sub-Comment (Sub-Sub-Sub-Comment \\) This is still the Sub-Sub-Sub-Comment!!!)))) alexis@m2osw.com",
146 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
147 : { "Group with some sub-comments", "",
148 : "", "", "", "", "" },
149 : { "Group with some sub-comments", "alexis@m2osw.com",
150 : "", "alexis", "m2osw.com", "alexis@m2osw.com", "alexis@m2osw.com" },
151 : { "", "\"Wilke, Alexis\" <\"alexis,wilke\"@[:special:.m2osw.com]>",
152 : "Wilke, Alexis", "alexis,wilke", ":special:.m2osw.com", "\"alexis,wilke\"@[:special:.m2osw.com]", "\"Wilke, Alexis\" <\"alexis,wilke\"@[:special:.m2osw.com]>" },
153 :
154 : { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
155 : };
156 :
157 : /// The list of valid emails used to check the parser out.
158 : const valid_email list_of_valid_emails[] =
159 : {
160 : { "alexis@m2osw.com", 1 },
161 : { "a@m2osw.com", 1 },
162 : { "b@c.com", 1 },
163 : { " \t alexis@m2osw.com\n \t", 1 },
164 : { "\"Wilke, Alexis\" <alexis@m2osw.com>", 1 },
165 : { " (* Pascal Comments *) \t alexis@m2osw.com\n (Just (kidding) he! he!) \t", 1 },
166 : { "(Start-Comment)alexis@ \t [ \t m2osw.com \t ] \n (More (comment) here) \r\n\t", 1 },
167 : { "(Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) \r\n\t", 1 },
168 : { "< (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) > \r\n\t", 1 },
169 : { "(With full name) Alexis Wilke < (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) > \r\n\t", 1 },
170 : { " (Now a group:) This Group: (With full name) Alexis Wilke < \n alexis \t @ \t [ \t m2osw.com \t ] \n (Less) >; \r\n\t", 2 },
171 : { "People: Alexis Wilke <alexis@m2osw.com>, John Smith <john@m2osw.com>; Lists: Contact <contact@m2osw.com>, Resume <resume@m2osw.com>; normal@m2osw.com, No-Reply: no-reply@m2osw.com;", 9 },
172 : { "\"Complex <name> for !a! \\\"USER\\\"\" <user@example.co.uk>", 1 },
173 : { "(Comment \n New-Line) alexis@m2osw.com", 1 },
174 : { "(Comment (Sub-Comment (Sub-Sub-Comment (Sub-Sub-Sub-Comment \\) This is still the Sub-Sub-Sub-Comment!!!)))) alexis@m2osw.com", 1 },
175 : { "Group with (Comment (Sub-Comment (Sub-Sub-Comment (Sub-Sub-Sub-Comment \\) This is still the Sub-Sub-Sub-Comment!!!)))) some sub-comments \t : alexis@m2osw.com;", 2 },
176 : { "\"Wilke, Alexis\" <\"alexis,wilke\"@[:special:.m2osw.com]>", 1 },
177 :
178 : // end of list
179 : { NULL, 0 }
180 : };
181 :
182 :
183 : /** \brief Transform an email string in a C-like string.
184 : *
185 : * This function transforms the characters in \p e into a set of C-like
186 : * escape characters so it can safely be printed in the console.
187 : *
188 : * For example, the character 0x09 is transformed to the character \\t.
189 : *
190 : * \param[in] e The email to be transformed.
191 : *
192 : * \return The transformed email.
193 : */
194 0 : std::string email_to_vstring(const std::string& e)
195 : {
196 0 : std::string result;
197 : char buf[3];
198 :
199 0 : for(const char *s(e.c_str()); *s != '\0'; ++s)
200 : {
201 0 : if(static_cast<unsigned char>(*s) < ' ')
202 : {
203 0 : switch(*s)
204 : {
205 0 : case '\a': result += "\\a"; break;
206 0 : case '\b': result += "\\b"; break;
207 0 : case '\f': result += "\\f"; break;
208 0 : case '\n': result += "\\n"; break;
209 0 : case '\r': result += "\\r"; break;
210 0 : case '\t': result += "\\t"; break;
211 0 : case '\v': result += "\\v"; break;
212 : default:
213 0 : buf[0] = '^';
214 0 : buf[1] = *s + '@';
215 0 : buf[2] = '\0';
216 0 : result += buf;
217 0 : break;
218 :
219 : }
220 : }
221 0 : else if(*s == 0x7F)
222 : {
223 0 : result += "<DEL>";
224 : }
225 0 : else if(static_cast<unsigned char>(*s) > 0x80)
226 : {
227 : static const char *hc = "0123456789ABCDEF";
228 0 : result += "\\x";
229 0 : buf[0] = hc[*s >> 4];
230 0 : buf[1] = hc[*s & 15];
231 0 : buf[2] = '\0';
232 0 : result += buf;
233 : }
234 : else
235 : {
236 0 : result += *s;
237 : }
238 : }
239 :
240 0 : return result;
241 : }
242 :
243 :
244 1 : void test_valid_emails()
245 : {
246 1 : const tld_email *results(list_of_results);
247 18 : for(const valid_email *v(list_of_valid_emails); v->f_input_email != NULL; ++v)
248 : {
249 17 : if(verbose)
250 : {
251 0 : printf("*** testing email \"%s\", start with C++ test\n", email_to_vstring(v->f_input_email).c_str());
252 0 : fflush(stdout);
253 : }
254 :
255 17 : const tld_email * const cresults(results);
256 :
257 : // C++ test
258 : {
259 17 : tld_email_list list;
260 17 : tld_result r(list.parse(v->f_input_email, 0));
261 17 : int max(v->f_count);
262 17 : if(r != TLD_RESULT_SUCCESS)
263 : {
264 0 : error("error: unexpected return value.");
265 : }
266 17 : else if(list.count() != max)
267 : {
268 0 : fprintf(stderr, "parse() returned %d as count, expected %d\n", list.count(), max);
269 0 : error("error: unexpected count");
270 : }
271 : else
272 : {
273 : // test the C++ function first
274 : {
275 17 : tld_email_list::tld_email_t e;
276 44 : for(int i(0); i < max; ++i, ++results)
277 : {
278 27 : if(results->f_group == NULL)
279 : {
280 0 : error("error: end of results array reached before completion of the test.\n");
281 0 : return;
282 : }
283 :
284 27 : if(!list.next(e))
285 : {
286 0 : error("error: next() returned false too soon.");
287 : }
288 27 : if(e.f_group != results->f_group)
289 : {
290 0 : error("error: next() returned the wrong group. Got \"" + e.f_group + "\" instead of \"" + results->f_group + "\".");
291 : }
292 27 : if(e.f_original_email != results->f_original_email)
293 : {
294 0 : error("error: next() returned the wrong original email. Got \"" + e.f_original_email + "\" instead of \"" + results->f_original_email + "\".");
295 : }
296 27 : if(e.f_fullname != results->f_fullname)
297 : {
298 0 : error("error: next() returned the wrong fullname. Got \"" + e.f_fullname + "\" instead of \"" + results->f_fullname + "\".");
299 : }
300 27 : if(e.f_username != results->f_username)
301 : {
302 0 : error("error: next() returned the wrong username. Got \"" + e.f_username + "\" instead of \"" + results->f_username + "\".");
303 : }
304 27 : if(e.f_domain != results->f_domain)
305 : {
306 0 : error("error: next() returned the wrong username. Got \"" + e.f_domain + "\" instead of \"" + results->f_domain + "\".");
307 : }
308 27 : if(e.f_email_only != results->f_email_only)
309 : {
310 0 : error("error: next() returned the wrong email only. Got \"" + e.f_email_only + "\" instead of \"" + results->f_email_only + "\".");
311 : }
312 27 : if(e.f_canonicalized_email != results->f_canonicalized_email)
313 : {
314 0 : error("error: next() returned the wrong canonicalized email. Got \"" + e.f_canonicalized_email + "\" instead of \"" + results->f_canonicalized_email + "\".");
315 : }
316 : }
317 17 : if(list.next(e))
318 : {
319 0 : error("error: next(e) returned the wrong result, it should be false after the whole set of emails were read.");
320 17 : }
321 : }
322 : // try the C function which also allows us to test the rewind()
323 17 : list.rewind();
324 : {
325 17 : results = cresults;
326 : tld_email e;
327 44 : for(int i(0); i < max; ++i, ++results)
328 : {
329 27 : if(!list.next(&e))
330 : {
331 0 : error("error: next() returned false too soon.");
332 : }
333 27 : if(strcmp(e.f_group, results->f_group) != 0)
334 : {
335 0 : error("error: next() returned the wrong group. Got \"" + std::string(e.f_group) + "\" from \"" + results->f_group + "\".");
336 : }
337 27 : if(strcmp(e.f_original_email, results->f_original_email) != 0)
338 : {
339 0 : error("error: next() returned the wrong original email. Got \"" + std::string(e.f_original_email) + "\" instead of \"" + results->f_original_email + "\".");
340 : }
341 27 : if(strcmp(e.f_fullname, results->f_fullname) != 0)
342 : {
343 0 : error("error: next() returned the wrong fullname.");
344 : }
345 27 : if(strcmp(e.f_username, results->f_username) != 0)
346 : {
347 0 : error("error: next() returned the wrong username.");
348 : }
349 27 : if(strcmp(e.f_domain, results->f_domain) != 0)
350 : {
351 0 : error("error: next() returned the wrong username.");
352 : }
353 27 : if(strcmp(e.f_email_only, results->f_email_only) != 0)
354 : {
355 0 : error("error: next() returned the wrong email only.");
356 : }
357 27 : if(strcmp(e.f_canonicalized_email, results->f_canonicalized_email) != 0)
358 : {
359 0 : error("error: next() returned the wrong canonicalized email.");
360 : }
361 : }
362 17 : if(list.next(&e))
363 : {
364 0 : error("error: next(&e) returned the wrong result, it should be false after the whole set of emails were read.");
365 : }
366 : }
367 17 : }
368 : }
369 :
370 17 : if(verbose)
371 : {
372 0 : printf("*** C test now\n");
373 0 : fflush(stdout);
374 : }
375 : // C test
376 : {
377 : tld_email_list *list;
378 17 : list = tld_email_alloc();
379 17 : tld_result r = tld_email_parse(list, v->f_input_email, 0);
380 17 : int max(v->f_count);
381 17 : if(r != TLD_RESULT_SUCCESS)
382 : {
383 0 : error("error: unexpected return value.");
384 : }
385 17 : else if(tld_email_count(list) != max)
386 : {
387 0 : fprintf(stderr, "parse() returned %d as count, expected %d\n", tld_email_count(list), max);
388 0 : error("error: unexpected count");
389 : }
390 : else
391 : {
392 : // test the C++ function first
393 102 : for(int repeat(0); repeat < 2; ++repeat)
394 : {
395 34 : results = cresults;
396 : struct tld_email e;
397 88 : for(int i(0); i < max; ++i, ++results)
398 : {
399 54 : if(results->f_group == NULL)
400 : {
401 0 : error("error: end of results array reached before completion of the test.\n");
402 0 : return;
403 : }
404 :
405 54 : if(tld_email_next(list, &e) != 1)
406 : {
407 0 : error("error: next() returned false too soon.");
408 : }
409 54 : if(strcmp(e.f_group, results->f_group) != 0)
410 : {
411 0 : error("error: next() returned the wrong group. Got \"" + std::string(e.f_group) + "\" from \"" + results->f_group + "\".");
412 : }
413 54 : if(strcmp(e.f_original_email, results->f_original_email) != 0)
414 : {
415 0 : error("error: next() returned the wrong original email. Got \"" + std::string(e.f_original_email) + "\" instead of \"" + results->f_original_email + "\".");
416 : }
417 54 : if(strcmp(e.f_fullname, results->f_fullname) != 0)
418 : {
419 0 : error("error: next() returned the wrong fullname.");
420 : }
421 54 : if(strcmp(e.f_username, results->f_username) != 0)
422 : {
423 0 : error("error: next() returned the wrong username.");
424 : }
425 54 : if(strcmp(e.f_domain, results->f_domain) != 0)
426 : {
427 0 : error("error: next() returned the wrong username.");
428 : }
429 54 : if(strcmp(e.f_email_only, results->f_email_only) != 0)
430 : {
431 0 : error("error: next() returned the wrong email only.");
432 : }
433 54 : if(strcmp(e.f_canonicalized_email, results->f_canonicalized_email) != 0)
434 : {
435 0 : error("error: next() returned the wrong canonicalized email.");
436 : }
437 : }
438 34 : if(tld_email_next(list, &e) != 0)
439 : {
440 0 : error("error: next(&e) returned the wrong result, it should be false after the whole set of emails were read.");
441 : }
442 : // try again
443 34 : tld_email_rewind(list);
444 : }
445 : }
446 17 : tld_email_free(list);
447 : }
448 : }
449 :
450 : {
451 : // all valid atom characters
452 : const char valid_chars[] =
453 : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
454 : "abcdefghijklmnopqrstuvwxyz"
455 : "0123456789"
456 1 : "!#$%&'*+-/=?^_`{|}~" // here there is a NUL
457 : ;
458 82 : for(size_t i(0); i < sizeof(valid_chars) / sizeof(valid_chars[0]) - 1; ++i)
459 : {
460 81 : tld_email_list list;
461 162 : std::string e("abc");
462 81 : e += valid_chars[i];
463 81 : e += "def@m2osw.com";
464 81 : if(verbose)
465 : {
466 0 : printf("*** testing all atom characters with email \"%s\"\n", email_to_vstring(e).c_str());
467 0 : fflush(stdout);
468 : }
469 81 : tld_result r(list.parse(e, 0));
470 81 : if(r != TLD_RESULT_SUCCESS)
471 : {
472 0 : error("error: unexpected return value.");
473 : }
474 81 : }
475 : }
476 :
477 : {
478 : // all valid quoted characters: " " to "\x7E" except the " and \ characters
479 : if(sizeof(int) < 4)
480 : {
481 : error("error: the ctrl variable needs to be at least 32 bits");
482 : return;
483 : }
484 1 : const int ctrl(1 << '\t');
485 127 : for(size_t i(1); i <= 126; ++i)
486 : {
487 126 : switch(i)
488 : {
489 : case '"':
490 : case '\\':
491 : case 0x7F: // not included in the loop anyway
492 2 : break;
493 :
494 : default:
495 124 : if(i >= ' ' || (ctrl & (1 << i)) != 0)
496 : {
497 94 : tld_email_list list;
498 188 : std::string e("\"abc");
499 94 : e += static_cast<char>(i);
500 94 : e += "def\"@m2osw.com";
501 94 : if(verbose)
502 : {
503 0 : printf("*** testing all atom characters with email \"%s\"\n", email_to_vstring(e).c_str());
504 0 : fflush(stdout);
505 : }
506 94 : tld_result r(list.parse(e, 0));
507 94 : if(r != TLD_RESULT_SUCCESS)
508 : {
509 0 : error("error: unexpected return value.");
510 94 : }
511 : }
512 124 : break;
513 :
514 : }
515 : }
516 : }
517 :
518 : {
519 : // all valid quoted pair: '\t' and " " to "\x7E"
520 97 : for(size_t i(31); i <= 126; ++i)
521 : {
522 96 : tld_email_list list;
523 192 : std::string e("\"abc\\");
524 96 : if(i == 31)
525 : {
526 1 : e += static_cast<char>('\t');
527 : }
528 : else
529 : {
530 95 : e += static_cast<char>(i);
531 : }
532 96 : e += "def\"@m2osw.com";
533 96 : if(verbose)
534 : {
535 0 : printf("*** testing all atom characters with email \"%s\"\n", email_to_vstring(e).c_str());
536 0 : fflush(stdout);
537 : }
538 96 : tld_result r(list.parse(e, 0));
539 96 : if(r != TLD_RESULT_SUCCESS)
540 : {
541 0 : error("error: unexpected return value.");
542 : }
543 96 : }
544 : }
545 :
546 : {
547 : // all valid comment characters: " " to "\x7E" except the " and \ characters
548 : if(sizeof(int) < 4)
549 : {
550 : error("error: the ctrl variable needs to be at least 32 bits");
551 : return;
552 : }
553 1 : const int ctrl((1 << '\t') | (1 << '\r') | (1 << '\n'));
554 127 : for(size_t i(1); i <= 126; ++i)
555 : {
556 : // we skip all the special characters in a comment since
557 : // those are already tested somewhere else
558 126 : switch(i)
559 : {
560 : case '(': // avoid a sub-comment
561 : case ')': // avoid closing the comment mid-way
562 : case '\\': // tested somewhere else
563 : case 0x7F: // not included in the loop anyway
564 3 : break;
565 :
566 : default:
567 123 : if(i >= ' ' || (ctrl & (1 << i)) != 0)
568 : {
569 95 : tld_email_list list;
570 190 : std::string e("(Comment \"");
571 95 : e += static_cast<char>(i);
572 95 : e += "\" char.) alexis@m2osw.com";
573 95 : if(verbose)
574 : {
575 0 : printf("*** testing all atom characters with email \"%s\"\n", email_to_vstring(e).c_str());
576 0 : fflush(stdout);
577 : }
578 95 : tld_result r(list.parse(e, 0));
579 95 : if(r != TLD_RESULT_SUCCESS)
580 : {
581 0 : error("error: unexpected return value.");
582 95 : }
583 : }
584 123 : break;
585 :
586 : }
587 : }
588 : }
589 :
590 : {
591 : // all valid domain characters: "!" to "\x7E" except the [, ], and \ characters
592 95 : for(size_t i('!'); i <= 126; ++i)
593 : {
594 : // a dot is valid but we cannot test it between two other dots
595 94 : if(i == '[' || i == ']' || i == '\\' || i == '.')
596 : {
597 4 : continue;
598 : }
599 90 : tld_email_list list;
600 180 : std::string e("alexis@[ m2osw.");
601 90 : e += static_cast<char>(i);
602 90 : e += ".com\t]";
603 90 : if(verbose)
604 : {
605 0 : printf("*** testing all atom characters with email \"%s\"\n", email_to_vstring(e).c_str());
606 0 : fflush(stdout);
607 : }
608 90 : tld_result r(list.parse(e, 0));
609 90 : if(r != TLD_RESULT_SUCCESS)
610 : {
611 0 : error("error: unexpected return value while testing a domain with a special character");
612 : }
613 90 : }
614 : }
615 :
616 : {
617 1 : if(tld_email_list::quote_string("Test quoting a simple comment", '(') != "(Test quoting a simple comment)")
618 : {
619 0 : error("error: unexpected return value when testing a simple comment quotation");
620 : }
621 1 : if(tld_email_list::quote_string("Test (quoting) a complex )comment(", '(') != "(Test \\(quoting\\) a complex \\)comment\\()")
622 : {
623 0 : error("error: unexpected return value when testing a complex comment quotation");
624 : }
625 : }
626 : }
627 :
628 :
629 :
630 :
631 : /** \brief Define an invalid email.
632 : *
633 : * This structure is used to list invalid emails in order to test that such
634 : * emails are not accepted by the parser. The structure includes the expected
635 : * result as well as a string pointer to the invalid email.
636 : */
637 : struct invalid_email
638 : {
639 : /// The expected reslut, if the call does not return this exact value the test fails
640 : tld_result f_result;
641 : /// The pointer to the invalid email to be tested
642 : const char * f_input_email;
643 : };
644 :
645 : const invalid_email list_of_invalid_emails[] =
646 : {
647 : { TLD_RESULT_INVALID, "alexism2osw.com (missing @)" },
648 : { TLD_RESULT_INVALID, " \v alexis@m2osw.com\n \t (bad control)" },
649 : { TLD_RESULT_INVALID, " (* Pascal Comments *) \t alexis@m2osw.com\n (missing closing parenthesis\\)" },
650 : { TLD_RESULT_INVALID, "(Start-Comment)alexis@ \t [ \t m2osw.com \t ] \n (extra after domain done) \"more\tdata\" \r\n\t" },
651 : { TLD_RESULT_INVALID, "(Test with dots in user name) al.ex.is@ \t(missing closing bracket ]) [ \t m2osw.com \t " },
652 : { TLD_RESULT_NULL, "< (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (Missing >) \r\n\t" },
653 : { TLD_RESULT_INVALID, "(Full name with period) Alexis.Wilke < (Test with dots in user name) al.ex.is@ \t [ \t m2osw.com \t ] \n (More (comments) there) > \r\n\t" },
654 : { TLD_RESULT_INVALID, " (Now a group:) This Group: (With full name) Alexis Wilke < \n alexis \t @ \t [ \t m2osw.com \t ] \n (missing ;) > \r\n\t" },
655 : { TLD_RESULT_INVALID, "Good Group: alexis@m2osw.com, bad-group: test@example.com;" },
656 : { TLD_RESULT_INVALID, "(No Group Name): alexis@m2osw.com;" },
657 : { TLD_RESULT_INVALID, " (No Group Name) : alexis@m2osw.com;" },
658 : { TLD_RESULT_INVALID, ": alexis@m2osw.com;" },
659 : { TLD_RESULT_INVALID, "(Group with CTRL) Group \v Unexpected: alexis@m2osw.com;" },
660 : { TLD_RESULT_INVALID, "\"alexis@m2osw.com;" },
661 : { TLD_RESULT_INVALID, "\"alexis@m2osw.com;\v\"" },
662 : { TLD_RESULT_INVALID, "\"Alexis Wilke\\" }, // \ followed by NUL
663 : { TLD_RESULT_INVALID, "(Comment with \\\\ followed by NUL: \\" },
664 : { TLD_RESULT_INVALID, "(Test Errors Once Done) \"Wilke, Alexis\" <alexis@m2osw.com> \"Bad\"" },
665 : { TLD_RESULT_INVALID, "(Comment with CTRL \b) \"Wilke, Alexis\" <alexis@m2osw.com>" },
666 : { TLD_RESULT_INVALID, "[m2osw.com]" },
667 : { TLD_RESULT_INVALID, "(Test Errors Once Done) \"Wilke, Alexis\" <alexis@m2osw.com> [Bad]" },
668 : { TLD_RESULT_INVALID, "(Test Errors Once Done) alexis@start[Bad]" },
669 : { TLD_RESULT_INVALID, "(Test Errors Once Done) alexis@[first][Bad]" },
670 : { TLD_RESULT_INVALID, "(Test Errors Once Done) alexis@[control:\v]" },
671 : { TLD_RESULT_NULL, "(Test Errors Once Done) alexis@[ spaces BAD]" },
672 : { TLD_RESULT_INVALID, "(Spurious Angle) alexis>@m2osw.com" },
673 : { TLD_RESULT_INVALID, "(Spurious Angle) alexis@m2osw.com>" },
674 : { TLD_RESULT_INVALID, "(Double Angle) <alexis@m2osw.com>>" },
675 : { TLD_RESULT_NULL, "(Missing domain) <alexis@>" },
676 : { TLD_RESULT_NULL, "(Missing domain) alexis@" },
677 : { TLD_RESULT_INVALID, "(2 domains) <alexis@[m2osw.com]bad>" },
678 : { TLD_RESULT_INVALID, "(Double @) <alexis@m2osw.com> @" },
679 : { TLD_RESULT_INVALID, "(Double @) alexis@m2osw.com@" },
680 : { TLD_RESULT_INVALID, "(Extra Chars) <alexis@m2osw.com> bad" },
681 : { TLD_RESULT_NULL, "(Empty username within brackets) <@m2osw.com>" },
682 : { TLD_RESULT_NULL, "(Empty User Name) @m2osw.com" },
683 : { TLD_RESULT_INVALID, "(Cannot start with a dot) .alexis@m2osw.com" },
684 : { TLD_RESULT_INVALID, "(Cannot start with a dot) <.alexis@m2osw.com>" },
685 : { TLD_RESULT_INVALID, "(Cannot end with a dot) alexis.@m2osw.com" },
686 : { TLD_RESULT_INVALID, "(Cannot end with a dot) <alexis.@m2osw.com>" },
687 : { TLD_RESULT_INVALID, "(Cannot include double dots) ale..xis@m2osw.com" },
688 : //{ TLD_RESULT_INVALID, "(End domain with dot not considered valid!) alexis@m2osw.com." }, viewed as valid! (that bad?)
689 : { TLD_RESULT_INVALID, "(End domain with dot not considered valid!) <alexis@m2osw.com.>" },
690 : { TLD_RESULT_NULL, "(Bad Emails) alexis,m2osw.com" },
691 : { TLD_RESULT_INVALID, "(Bad Char) alexis@m2osw\001com" },
692 : { TLD_RESULT_NOT_FOUND, "(Bad Extension) alexis@m2osw.comm" },
693 : { TLD_RESULT_INVALID, "(Bad Extension) alexis@m2osw.ar" },
694 : { TLD_RESULT_INVALID, "(Bad Extension) alexis@m2osw.nom.ar" },
695 : { TLD_RESULT_NO_TLD, "(Bad Extension) alexis@m2osw" },
696 : { TLD_RESULT_BAD_URI, "(Bad Extension) alexis@[m2osw..com]" },
697 :
698 : // end of list
699 : { TLD_RESULT_SUCCESS, NULL }
700 : };
701 :
702 1 : void test_invalid_emails()
703 : {
704 50 : for(const invalid_email *v(list_of_invalid_emails); v->f_input_email != NULL; ++v)
705 : {
706 49 : if(verbose)
707 : {
708 0 : printf("+++ testing email \"%s\"\n", email_to_vstring(v->f_input_email).c_str());
709 : }
710 :
711 : // C++ test
712 : {
713 49 : tld_email_list list;
714 49 : tld_result r(list.parse(v->f_input_email, 0));
715 49 : if(r != v->f_result)
716 : {
717 0 : std::stringstream ss;
718 0 : ss << "error: unexpected return value. Got " << static_cast<int>(r) << ", expected " << static_cast<int>(v->f_result) << " for \"" << v->f_input_email << "\" (C++)";
719 0 : error(ss.str());
720 49 : }
721 : }
722 :
723 : // C test
724 : {
725 : tld_email_list *list;
726 49 : list = tld_email_alloc();
727 49 : tld_result r = tld_email_parse(list, v->f_input_email, 0);
728 49 : if(r != v->f_result)
729 : {
730 0 : std::stringstream ss;
731 0 : ss << "error: unexpected return value. Got " << static_cast<int>(r) << ", expected " << static_cast<int>(v->f_result) << " for \"" << v->f_input_email << "\" (C)";
732 0 : error(ss.str());
733 : }
734 49 : tld_email_free(list);
735 49 : list = NULL;
736 : }
737 : }
738 1 : }
739 :
740 :
741 6 : void contract_furfilled(tld_email_list::tld_email_t& e)
742 : {
743 12 : if(!e.f_group.empty()
744 6 : || !e.f_original_email.empty()
745 6 : || !e.f_fullname.empty()
746 6 : || !e.f_username.empty()
747 6 : || !e.f_domain.empty()
748 6 : || !e.f_email_only.empty()
749 12 : || !e.f_canonicalized_email.empty())
750 : {
751 0 : error("error: one of the structure parameters was modified on error!");
752 : }
753 6 : }
754 :
755 :
756 1 : void test_direct_email()
757 : {
758 1 : tld_email_list::tld_email_t email;
759 :
760 : ////////////// EMAILS
761 : // missing closing \"
762 2 : EXPECTED_THROW(email.parse("\"blah alexis@m2osw.com"), std::logic_error);
763 1 : contract_furfilled(email);
764 :
765 : // missing closing )
766 2 : EXPECTED_THROW(email.parse("(comment alexis@m2osw.com"), std::logic_error);
767 1 : contract_furfilled(email);
768 :
769 : // use of \ at the end of the comment
770 2 : EXPECTED_THROW(email.parse("(comment\\"), std::logic_error);
771 1 : contract_furfilled(email);
772 :
773 : // missing closing ]
774 2 : EXPECTED_THROW(email.parse("alexis@[m2osw.com"), std::logic_error);
775 1 : contract_furfilled(email);
776 :
777 : ////////////// GROUP
778 : // missing closing )
779 2 : EXPECTED_THROW(email.parse_group("Group (comment"), std::logic_error);
780 1 : contract_furfilled(email);
781 :
782 : // use of \ at the end of the comment
783 2 : EXPECTED_THROW(email.parse_group("Group (comment \\"), std::logic_error);
784 1 : contract_furfilled(email);
785 1 : }
786 :
787 :
788 :
789 : /** \brief Structure used to define a set of fields to test.
790 : *
791 : * This structure is used in this test to define a list of fields
792 : * to test against the library.
793 : */
794 : struct email_field_types
795 : {
796 : const char * f_field;
797 : tld_email_field_type f_type;
798 : };
799 :
800 : /** \var email_field_types::f_field
801 : * \brief The name of the field to be tested.
802 : */
803 : /** \var email_field_types::f_type
804 : * \brief The type we expect the library to return for that field.
805 : */
806 :
807 : const email_field_types list_of_email_field_types[] =
808 : {
809 : // make sure case does not have side effects
810 : { "to", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
811 : { "To", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
812 : { "tO", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
813 : { "TO", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
814 :
815 : // check all fields that are expected to include emails
816 : { "from", TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST },
817 : { "resent-from", TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST },
818 : { "sender", TLD_EMAIL_FIELD_TYPE_MAILBOX },
819 : { "resent-sender", TLD_EMAIL_FIELD_TYPE_MAILBOX },
820 : { "to", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
821 : { "cc", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
822 : { "reply-to", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
823 : { "resent-to", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
824 : { "resent-cc", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
825 : { "bcc", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT },
826 : { "resent-bcc", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT },
827 :
828 : // check all fields with a colon
829 : { "from: someone", TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST },
830 : { "resent-from: someone", TLD_EMAIL_FIELD_TYPE_MAILBOX_LIST },
831 : { "sender: someone", TLD_EMAIL_FIELD_TYPE_MAILBOX },
832 : { "resent-sender: someone", TLD_EMAIL_FIELD_TYPE_MAILBOX },
833 : { "to: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
834 : { "cc: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
835 : { "reply-to: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
836 : { "resent-to: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
837 : { "resent-cc: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST },
838 : { "bcc: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT },
839 : { "resent-bcc: someone", TLD_EMAIL_FIELD_TYPE_ADDRESS_LIST_OPT },
840 :
841 : // check other fields
842 : { "message-id", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
843 : { "date", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
844 : { "subject", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
845 : { "x-extension", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
846 :
847 : // check other fields with a colon
848 : { "message-id: something", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
849 : { "date: something", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
850 : { "subject: something", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
851 : { "x-extension: something", TLD_EMAIL_FIELD_TYPE_UNKNOWN },
852 :
853 : // check for invalid field names
854 : { "s\xfc\x62ject", TLD_EMAIL_FIELD_TYPE_INVALID },
855 : { "subj\xe9\x63t", TLD_EMAIL_FIELD_TYPE_INVALID },
856 : { "-bad-dash", TLD_EMAIL_FIELD_TYPE_INVALID },
857 : { "0bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
858 : { "1bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
859 : { "2bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
860 : { "3bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
861 : { "4bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
862 : { "5bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
863 : { "6bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
864 : { "7bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
865 : { "8bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
866 : { "9bad-digit", TLD_EMAIL_FIELD_TYPE_INVALID },
867 : { "" /*empty*/, TLD_EMAIL_FIELD_TYPE_INVALID },
868 : };
869 :
870 1 : void test_email_field_types()
871 : {
872 49 : for(size_t i(0); i < sizeof(list_of_email_field_types) / sizeof(list_of_email_field_types[0]); ++i)
873 : {
874 48 : tld_email_field_type type(tld_email_list::email_field_type(list_of_email_field_types[i].f_field));
875 48 : if(type != list_of_email_field_types[i].f_type)
876 : {
877 0 : std::stringstream ss;
878 0 : ss << "error: email type mismatch for \"" << list_of_email_field_types[i].f_field
879 0 : << "\", expected " << static_cast<int>(list_of_email_field_types[i].f_type)
880 0 : << ", got " << static_cast<int>(type) << " instead.";
881 0 : error(ss.str());
882 : }
883 : }
884 1 : }
885 :
886 :
887 :
888 1 : int main(int argc, char *argv[])
889 : {
890 1 : printf("testing tld emails version %s\n", tld_version());
891 :
892 1 : if(argc > 1)
893 : {
894 0 : if(strcmp(argv[1], "-v") == 0)
895 : {
896 0 : verbose = 1;
897 : }
898 : }
899 :
900 : /* Call all the tests, one by one.
901 : *
902 : * Failures are "recorded" in the err_count global variable
903 : * and the process stops with an error message and exit(1)
904 : * if err_count is not zero.
905 : *
906 : * Exceptions that should not occur are expected to also
907 : * be caught and reported as errors.
908 : */
909 : try
910 : {
911 1 : test_valid_emails();
912 1 : test_invalid_emails();
913 1 : test_direct_email();
914 1 : test_email_field_types();
915 : }
916 : catch(const invalid_domain&)
917 : {
918 : error("error: caught an exception when all emails are expected to be valid.");
919 : }
920 :
921 1 : if(err_count)
922 : {
923 : fprintf(stderr, "%d error%s occured.\n",
924 0 : err_count, err_count != 1 ? "s" : "");
925 : }
926 1 : exit(err_count ? 1 : 0);
927 : }
928 :
929 : /* vim: ts=4 sw=4 et
930 : */
|