1 | #include <config.h> |
---|
2 | |
---|
3 | #include <ctype.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <string.h> |
---|
7 | #include "libsoup/soup-uri.h" |
---|
8 | |
---|
9 | struct { |
---|
10 | char *uri_string, *result; |
---|
11 | } abs_tests[] = { |
---|
12 | { "foo:", "foo:" }, |
---|
13 | { "file:/dev/null", "file:/dev/null" }, |
---|
14 | { "file:///dev/null", "file:///dev/null" }, |
---|
15 | { "ftp://user@host/path", "ftp://user@host/path" }, |
---|
16 | { "ftp://user@host:9999/path", "ftp://user@host:9999/path" }, |
---|
17 | { "ftp://user:password@host/path", "ftp://user@host/path" }, |
---|
18 | { "ftp://user:password@host:9999/path", "ftp://user@host:9999/path" }, |
---|
19 | { "http://us%65r@host", "http://user@host" }, |
---|
20 | { "http://us%40r@host", "http://us%40r@host" }, |
---|
21 | { "http://us%3ar@host", "http://us%3ar@host" }, |
---|
22 | { "http://us%2fr@host", "http://us%2fr@host" } |
---|
23 | }; |
---|
24 | int num_abs_tests = G_N_ELEMENTS(abs_tests); |
---|
25 | |
---|
26 | /* From RFC 2396. */ |
---|
27 | char *base = "http://a/b/c/d;p?q"; |
---|
28 | struct { |
---|
29 | char *uri_string, *result; |
---|
30 | } rel_tests[] = { |
---|
31 | { "g:h", "g:h" }, |
---|
32 | { "g", "http://a/b/c/g" }, |
---|
33 | { "./g", "http://a/b/c/g" }, |
---|
34 | { "g/", "http://a/b/c/g/" }, |
---|
35 | { "/g", "http://a/g" }, |
---|
36 | { "//g", "http://g" }, |
---|
37 | { "?y", "http://a/b/c/?y" }, |
---|
38 | { "g?y", "http://a/b/c/g?y" }, |
---|
39 | { "#s", "http://a/b/c/d;p?q#s" }, |
---|
40 | { "g#s", "http://a/b/c/g#s" }, |
---|
41 | { "g?y#s", "http://a/b/c/g?y#s" }, |
---|
42 | { ";x", "http://a/b/c/;x" }, |
---|
43 | { "g;x", "http://a/b/c/g;x" }, |
---|
44 | { "g;x?y#s", "http://a/b/c/g;x?y#s" }, |
---|
45 | { ".", "http://a/b/c/" }, |
---|
46 | { "./", "http://a/b/c/" }, |
---|
47 | { "..", "http://a/b/" }, |
---|
48 | { "../", "http://a/b/" }, |
---|
49 | { "../g", "http://a/b/g" }, |
---|
50 | { "../..", "http://a/" }, |
---|
51 | { "../../", "http://a/" }, |
---|
52 | { "../../g", "http://a/g" }, |
---|
53 | { "", "http://a/b/c/d;p?q" }, |
---|
54 | { "../../../g", "http://a/../g" }, |
---|
55 | { "../../../../g", "http://a/../../g" }, |
---|
56 | { "/./g", "http://a/./g" }, |
---|
57 | { "/../g", "http://a/../g" }, |
---|
58 | { "g.", "http://a/b/c/g." }, |
---|
59 | { ".g", "http://a/b/c/.g" }, |
---|
60 | { "g..", "http://a/b/c/g.." }, |
---|
61 | { "..g", "http://a/b/c/..g" }, |
---|
62 | { "./../g", "http://a/b/g" }, |
---|
63 | { "./g/.", "http://a/b/c/g/" }, |
---|
64 | { "g/./h", "http://a/b/c/g/h" }, |
---|
65 | { "g/../h", "http://a/b/c/h" }, |
---|
66 | { "g;x=1/./y", "http://a/b/c/g;x=1/y" }, |
---|
67 | { "g;x=1/../y", "http://a/b/c/y" }, |
---|
68 | { "g?y/./x", "http://a/b/c/g?y/./x" }, |
---|
69 | { "g?y/../x", "http://a/b/c/g?y/../x" }, |
---|
70 | { "g#s/./x", "http://a/b/c/g#s/./x" }, |
---|
71 | { "g#s/../x", "http://a/b/c/g#s/../x" }, |
---|
72 | |
---|
73 | /* RFC 2396 notes that some old parsers will parse this as |
---|
74 | * a relative URL ("http://a/b/c/g"), but it should be |
---|
75 | * interpreted as absolute. libsoup should parse it |
---|
76 | * correctly as being absolute, but then reject it since it's |
---|
77 | * an http URL with no host. |
---|
78 | */ |
---|
79 | { "http:g", NULL } |
---|
80 | }; |
---|
81 | int num_rel_tests = G_N_ELEMENTS(rel_tests); |
---|
82 | |
---|
83 | static gboolean |
---|
84 | do_uri (SoupUri *base_uri, const char *base_str, |
---|
85 | const char *in_uri, const char *out_uri) |
---|
86 | { |
---|
87 | SoupUri *uri; |
---|
88 | char *uri_string; |
---|
89 | |
---|
90 | if (base_uri) { |
---|
91 | printf ("<%s> + <%s> = <%s>? ", base_str, in_uri, |
---|
92 | out_uri ? out_uri : "ERR"); |
---|
93 | uri = soup_uri_new_with_base (base_uri, in_uri); |
---|
94 | } else { |
---|
95 | printf ("<%s> => <%s>? ", in_uri, |
---|
96 | out_uri ? out_uri : "ERR"); |
---|
97 | uri = soup_uri_new (in_uri); |
---|
98 | } |
---|
99 | |
---|
100 | if (!uri) { |
---|
101 | if (out_uri) { |
---|
102 | printf ("ERR\n Could not parse %s\n", in_uri); |
---|
103 | return FALSE; |
---|
104 | } else { |
---|
105 | printf ("OK\n"); |
---|
106 | return TRUE; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | uri_string = soup_uri_to_string (uri, FALSE); |
---|
111 | soup_uri_free (uri); |
---|
112 | |
---|
113 | if (!out_uri) { |
---|
114 | printf ("ERR\n Got %s\n", uri_string); |
---|
115 | return FALSE; |
---|
116 | } |
---|
117 | |
---|
118 | if (strcmp (uri_string, out_uri) != 0) { |
---|
119 | printf ("NO\n Unparses to <%s>\n", uri_string); |
---|
120 | g_free (uri_string); |
---|
121 | return FALSE; |
---|
122 | } |
---|
123 | g_free (uri_string); |
---|
124 | |
---|
125 | printf ("OK\n"); |
---|
126 | return TRUE; |
---|
127 | } |
---|
128 | |
---|
129 | int |
---|
130 | main (int argc, char **argv) |
---|
131 | { |
---|
132 | SoupUri *base_uri; |
---|
133 | char *uri_string; |
---|
134 | int i, errs = 0; |
---|
135 | |
---|
136 | printf ("Absolute URI parsing\n"); |
---|
137 | for (i = 0; i < num_abs_tests; i++) { |
---|
138 | if (!do_uri (NULL, NULL, abs_tests[i].uri_string, |
---|
139 | abs_tests[i].result)) |
---|
140 | errs++; |
---|
141 | } |
---|
142 | |
---|
143 | printf ("\nRelative URI parsing\n"); |
---|
144 | base_uri = soup_uri_new (base); |
---|
145 | if (!base_uri) { |
---|
146 | fprintf (stderr, "Could not parse %s!\n", base); |
---|
147 | exit (1); |
---|
148 | } |
---|
149 | |
---|
150 | uri_string = soup_uri_to_string (base_uri, FALSE); |
---|
151 | if (strcmp (uri_string, base) != 0) { |
---|
152 | fprintf (stderr, "URI <%s> unparses to <%s>\n", |
---|
153 | base, uri_string); |
---|
154 | errs++; |
---|
155 | } |
---|
156 | g_free (uri_string); |
---|
157 | |
---|
158 | for (i = 0; i < num_rel_tests; i++) { |
---|
159 | if (!do_uri (base_uri, base, rel_tests[i].uri_string, |
---|
160 | rel_tests[i].result)) |
---|
161 | errs++; |
---|
162 | } |
---|
163 | |
---|
164 | printf ("\n%d errors\n", errs); |
---|
165 | return errs; |
---|
166 | } |
---|