Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / tests / misc / url-scan.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright 2004 Ximian, Inc. (www.ximian.com)
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <camel/camel-mime-filter-tohtml.h>
33
34 #include "camel-test.h"
35
36 struct {
37         char *text, *url;
38 } url_tests[] = {
39         { "bob@foo.com", "mailto:bob@foo.com" },
40         { "Ends with bob@foo.com", "mailto:bob@foo.com" },
41         { "bob@foo.com at start", "mailto:bob@foo.com" },
42         { "bob@foo.com.", "mailto:bob@foo.com" },
43         { "\"bob@foo.com\"", "mailto:bob@foo.com" },
44         { "<bob@foo.com>", "mailto:bob@foo.com" },
45         { "(bob@foo.com)", "mailto:bob@foo.com" },
46         { "bob@foo.com, 555-9999", "mailto:bob@foo.com" },
47         { "|bob@foo.com|555-9999|", "mailto:bob@foo.com" },
48         { "bob@ no match bob@", NULL },
49         { "@foo.com no match @foo.com", NULL },
50         { "\"bob\"@foo.com", NULL },
51         { "M@ke money fast!", NULL },
52         { "ASCII art @_@ @>->-", NULL },
53
54         { "http://www.foo.com", "http://www.foo.com" },
55         { "Ends with http://www.foo.com", "http://www.foo.com" },
56         { "http://www.foo.com at start", "http://www.foo.com" },
57         { "http://www.foo.com.", "http://www.foo.com" },
58         { "http://www.foo.com/.", "http://www.foo.com/" },
59         { "<http://www.foo.com>", "http://www.foo.com" },
60         { "(http://www.foo.com)", "http://www.foo.com" },
61         { "http://www.foo.com, 555-9999", "http://www.foo.com" },
62         { "|http://www.foo.com|555-9999|", "http://www.foo.com" },
63         { "foo http://www.foo.com/ bar", "http://www.foo.com/" },
64         { "foo http://www.foo.com/index.html bar", "http://www.foo.com/index.html" },
65         { "foo http://www.foo.com/q?99 bar", "http://www.foo.com/q?99" },
66         { "foo http://www.foo.com/;foo=bar&baz=quux bar", "http://www.foo.com/;foo=bar&baz=quux" },
67         { "foo http://www.foo.com/index.html#anchor bar", "http://www.foo.com/index.html#anchor" },
68         { "http://www.foo.com/index.html; foo", "http://www.foo.com/index.html" },
69         { "http://www.foo.com/index.html: foo", "http://www.foo.com/index.html" },
70         { "http://www.foo.com/index.html-- foo", "http://www.foo.com/index.html" },
71         { "http://www.foo.com/index.html?", "http://www.foo.com/index.html" },
72         { "http://www.foo.com/index.html!", "http://www.foo.com/index.html" },
73         { "\"http://www.foo.com/index.html\"", "http://www.foo.com/index.html" },
74         { "'http://www.foo.com/index.html'", "http://www.foo.com/index.html" },
75         { "http://bob@www.foo.com/bar/baz/", "http://bob@www.foo.com/bar/baz/" },
76         { "http no match http", NULL },
77         { "http: no match http:", NULL },
78         { "http:// no match http://", NULL },
79         { "unrecognized://bob@foo.com/path", "mailto:bob@foo.com" },
80
81         { "src/www.c", NULL },
82         { "Ewwwwww.Gross.", NULL },
83
84 };
85
86 static int num_url_tests = G_N_ELEMENTS (url_tests);
87
88 int main (int argc, char **argv)
89 {
90         char *html, *url, *p;
91         int i, errors = 0;
92         guint32 flags;
93         
94         camel_test_init (argc, argv);
95         
96         camel_test_start ("URL scanning");
97         
98         flags = CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS | CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES;
99         for (i = 0; i < num_url_tests; i++) {
100                 camel_test_push ("'%s' => '%s'", url_tests[i].text, url_tests[i].url ? url_tests[i].url : "None");
101                 
102                 html = camel_text_to_html (url_tests[i].text, flags, 0);
103                 
104                 url = strstr (html, "href=\"");
105                 if (url) {
106                         url += 6;
107                         p = strchr (url, '"');
108                         if (p)
109                                 *p = '\0';
110                         
111                         while ((p = strstr (url, "&amp;")))
112                                 memmove (p + 1, p + 5, strlen (p + 5) + 1);
113                 }
114                 
115                 if ((url && (!url_tests[i].url || strcmp (url, url_tests[i].url) != 0)) ||
116                     (!url && url_tests[i].url)) {
117                         printf ("FAILED on \"%s\" -> %s\n  (got %s)\n\n",
118                                 url_tests[i].text,
119                                 url_tests[i].url ? url_tests[i].url : "(nothing)",
120                                 url ? url : "(nothing)");
121                         errors++;
122                 }
123                 
124                 g_free (html);
125         }
126         
127         printf ("\n%d errors\n", errors);
128         
129         camel_test_end ();
130         
131         return errors;
132 }