]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
69a9128d9c29fe512174801aa56f1820918e9509
[wxWidgets.git] / tests / uris / uris.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxURI unit test
4 // Author: Ryan Norton
5 // Created: 2004-08-14
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Ryan Norton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/uri.h"
25
26 #include "wx/cppunit.h"
27
28 // ----------------------------------------------------------------------------
29 // test class
30 // ----------------------------------------------------------------------------
31
32 class URITestCase : public CppUnit::TestCase
33 {
34 public:
35 URITestCase();
36
37 private:
38 CPPUNIT_TEST_SUITE( URITestCase );
39 CPPUNIT_TEST( IPv4 );
40 CPPUNIT_TEST( IPv6 );
41 CPPUNIT_TEST( Paths );
42 CPPUNIT_TEST( NormalResolving );
43 CPPUNIT_TEST( ComplexResolving );
44 CPPUNIT_TEST( ReallyComplexResolving );
45 CPPUNIT_TEST( QueryFragmentResolving );
46 CPPUNIT_TEST( BackwardsResolving );
47 CPPUNIT_TEST( Assignment );
48 CPPUNIT_TEST( Comparison );
49 CPPUNIT_TEST_SUITE_END();
50
51 void IPv4();
52 void IPv6();
53 void Paths();
54 void NormalResolving();
55 void ComplexResolving();
56 void ReallyComplexResolving();
57 void QueryFragmentResolving();
58 void BackwardsResolving();
59 void Assignment();
60 void Comparison();
61
62 DECLARE_NO_COPY_CLASS(URITestCase)
63 };
64
65 // register in the unnamed registry so that these tests are run by default
66 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
67
68 // also include in it's own registry so that these tests can be run alone
69 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
70
71 URITestCase::URITestCase()
72 {
73 }
74
75
76 #define URI_TEST(uristring, cond) \
77 uri = new wxURI(wxT(uristring));\
78 CPPUNIT_ASSERT(cond);\
79 delete uri;
80
81 #define URI_PRINT(uri)\
82 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
83 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
84 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
85 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
86 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
87 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
88 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
89
90 void URITestCase::IPv4()
91 {
92 wxURI* uri;
93
94
95 URI_TEST("http://user:password@192.168.1.100:5050/path",
96 uri->GetHostType() == wxURI_IPV4ADDRESS);
97
98 URI_TEST("http://user:password@192.255.1.100:5050/path",
99 uri->GetHostType() == wxURI_IPV4ADDRESS);
100
101 //bogus ipv4
102 URI_TEST("http://user:password@192.256.1.100:5050/path",
103 uri->GetHostType() != wxURI_IPV4ADDRESS);
104 }
105
106 void URITestCase::IPv6()
107 {
108 wxURI* uri;
109
110 // IPv6address = 6( h16 ":" ) ls32
111 // / "::" 5( h16 ":" ) ls32
112 // / [ h16 ] "::" 4( h16 ":" ) ls32
113 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
114 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
115 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
116 // / [ *4( h16 ":" ) h16 ] "::" ls32
117 // / [ *5( h16 ":" ) h16 ] "::" h16
118 // / [ *6( h16 ":" ) h16 ] "::"
119 // ls32 = ( h16 ":" h16 ) / IPv4address
120
121 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
122 uri->GetHostType() == wxURI_IPV6ADDRESS);
123
124 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
125 uri->GetHostType() == wxURI_IPV6ADDRESS);
126
127 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
128 uri->GetHostType() == wxURI_IPV6ADDRESS);
129
130 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
131 uri->GetHostType() == wxURI_IPV6ADDRESS);
132 }
133
134 void URITestCase::Paths()
135 {
136 wxURI* uri;
137
138 //path tests
139 URI_TEST("http://user:password@192.256.1.100:5050/../path",
140 uri->GetPath() == wxT("/path"));
141
142 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
143 uri->GetPath() == wxT("/"));
144
145 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
146 uri->GetPath() == wxT("/path/"));
147
148 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
149 uri->GetPath() == wxT("/path/"));
150
151 URI_TEST("path/john/../../../joe",
152 uri->Get() == wxT("../joe"));
153 }
154 #undef URI_TEST
155
156 #define URI_TEST_RESOLVE(string, eq, strict) \
157 uri = new wxURI(wxT(string));\
158 uri->Resolve(masteruri, strict);\
159 CPPUNIT_ASSERT(uri->Get() == wxT(eq));\
160 delete uri;
161
162 #define URI_TEST(string, eq) \
163 URI_TEST_RESOLVE(string, eq, true);
164
165
166 //examples taken from RFC 2396.bis
167
168 void URITestCase::NormalResolving()
169 {
170 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
171 wxURI* uri;
172
173 URI_TEST("g:h" ,"g:h")
174 URI_TEST("g" ,"http://a/b/c/g")
175 URI_TEST("./g" ,"http://a/b/c/g")
176 URI_TEST("g/" ,"http://a/b/c/g/")
177 URI_TEST("/g" ,"http://a/g")
178 URI_TEST("//g" ,"http://g")
179 URI_TEST("?y" ,"http://a/b/c/d;p?y")
180 URI_TEST("g?y" ,"http://a/b/c/g?y")
181 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
182 URI_TEST("g#s" ,"http://a/b/c/g#s")
183 URI_TEST("g?y#s","http://a/b/c/g?y#s")
184 URI_TEST(";x" ,"http://a/b/c/;x")
185 URI_TEST("g;x" ,"http://a/b/c/g;x")
186 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
187
188 URI_TEST("" ,"http://a/b/c/d;p?q")
189 URI_TEST("." ,"http://a/b/c/")
190 URI_TEST("./" ,"http://a/b/c/")
191 URI_TEST(".." ,"http://a/b/")
192 URI_TEST("../" ,"http://a/b/")
193 URI_TEST("../g" ,"http://a/b/g")
194 URI_TEST("../..","http://a/")
195 URI_TEST("../../" , "http://a/")
196 URI_TEST("../../g" , "http://a/g")
197 }
198
199 void URITestCase::ComplexResolving()
200 {
201 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
202 wxURI* uri;
203
204 //odd path examples
205 URI_TEST("/./g" ,"http://a/g")
206 URI_TEST("/../g" ,"http://a/g")
207 URI_TEST("g." ,"http://a/b/c/g.")
208 URI_TEST(".g" ,"http://a/b/c/.g")
209 URI_TEST("g.." ,"http://a/b/c/g..")
210 URI_TEST("..g" ,"http://a/b/c/..g")
211 }
212 //Should Fail
213 //"../../../g" = "http://a/g"
214 //"../../../../g" = "http://a/g"
215
216 void URITestCase::ReallyComplexResolving()
217 {
218 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
219 wxURI* uri;
220
221 //even more odder path examples
222 URI_TEST("./../g" ,"http://a/b/g")
223 URI_TEST("./g/." ,"http://a/b/c/g/")
224 URI_TEST("g/./h" ,"http://a/b/c/g/h")
225 URI_TEST("g/../h" ,"http://a/b/c/h")
226 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
227 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
228 }
229
230 void URITestCase::QueryFragmentResolving()
231 {
232 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
233 wxURI* uri;
234
235 //query/fragment ambigiousness
236 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
237 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
238 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
239 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
240 }
241
242 void URITestCase::BackwardsResolving()
243 {
244 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
245 wxURI* uri;
246
247 //"NEW"
248 URI_TEST("http:g" , "http:g") //strict
249 //bw compat
250 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
251 }
252
253 void URITestCase::Assignment()
254 {
255 wxURI uri1(wxT("http://mysite.com")),
256 uri2(wxT("http://mysite2.com"));
257
258 uri2 = uri1;
259
260 CPPUNIT_ASSERT(uri1.Get() == uri2.Get());
261 }
262
263 void URITestCase::Comparison()
264 {
265 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
266 }