1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Url Classifier code
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
41 #include "nsUrlClassifierUtils.h"
42 #include "nsNetUtil.h"
45 static int gTotalTests
= 0;
46 static int gPassedTests
= 0;
48 static char int_to_hex_digit(PRInt32 i
) {
49 NS_ASSERTION((i
>= 0) && (i
<= 15), "int too big in int_to_hex_digit");
50 return static_cast<char>(((i
< 10) ? (i
+ '0') : ((i
- 10) + 'A')));
53 static void CheckEquals(nsCString
& expected
, nsCString
& actual
)
55 if (!(expected
).Equals((actual
))) {
56 fprintf(stderr
, "FAILED: expected |%s| but was |%s|\n", (expected
).get(),
64 void TestUnescapeHelper(const char* in
, const char* expected
)
66 nsCString out
, strIn(in
), strExp(expected
);
67 nsUrlClassifierUtils utils
;
69 NS_UnescapeURL(strIn
.get(), strIn
.Length(), esc_AlwaysCopy
, out
);
70 CheckEquals(strExp
, out
);
73 // Make sure Unescape from nsEncode.h's unescape does what the server does.
77 TestUnescapeHelper("\0", "\0");
79 // Test docoding of all characters.
80 nsCString allCharsEncoded
, allCharsEncodedLowercase
, allCharsAsString
;
81 for (PRInt32 i
= 1; i
< 256; ++i
) {
82 allCharsEncoded
.Append('%');
83 allCharsEncoded
.Append(int_to_hex_digit(i
/ 16));
84 allCharsEncoded
.Append((int_to_hex_digit(i
% 16)));
86 allCharsEncodedLowercase
.Append('%');
87 allCharsEncodedLowercase
.Append(tolower(int_to_hex_digit(i
/ 16)));
88 allCharsEncodedLowercase
.Append(tolower(int_to_hex_digit(i
% 16)));
90 allCharsAsString
.Append(static_cast<char>(i
));
93 nsUrlClassifierUtils utils
;
95 NS_UnescapeURL(allCharsEncoded
.get(), allCharsEncoded
.Length(), esc_AlwaysCopy
, out
);
96 CheckEquals(allCharsAsString
, out
);
99 NS_UnescapeURL(allCharsEncodedLowercase
.get(), allCharsEncodedLowercase
.Length(), esc_AlwaysCopy
, out
);
100 CheckEquals(allCharsAsString
, out
);
102 // Test %-related edge cases
103 TestUnescapeHelper("%", "%");
104 TestUnescapeHelper("%xx", "%xx");
105 TestUnescapeHelper("%%", "%%");
106 TestUnescapeHelper("%%%", "%%%");
107 TestUnescapeHelper("%%%%", "%%%%");
108 TestUnescapeHelper("%1", "%1");
109 TestUnescapeHelper("%1z", "%1z");
110 TestUnescapeHelper("a%1z", "a%1z");
111 TestUnescapeHelper("abc%d%e%fg%hij%klmno%", "abc%d%e%fg%hij%klmno%");
114 TestUnescapeHelper("%25", "%");
115 TestUnescapeHelper("%25%32%35", "%25");
118 void TestEncodeHelper(const char* in
, const char* expected
)
120 nsCString out
, strIn(in
), strExp(expected
);
121 nsUrlClassifierUtils utils
;
123 utils
.SpecialEncode(strIn
, PR_TRUE
, out
);
124 CheckEquals(strExp
, out
);
130 TestEncodeHelper("", "");
132 // Test that all characters we shouldn't encode ([33-36],[38,126]) are not.
134 for (PRInt32 i
= 33; i
< 127; i
++) {
135 if (i
!= 37) { // skip %
136 noenc
.Append(static_cast<char>(i
));
139 nsUrlClassifierUtils utils
;
141 utils
.SpecialEncode(noenc
, PR_FALSE
, out
);
142 CheckEquals(noenc
, out
);
144 // Test that all the chars that we should encode [0,32],37,[127,255] are
145 nsCString yesAsString
, yesExpectedString
;
146 for (PRInt32 i
= 1; i
< 256; i
++) {
147 if (i
< 33 || i
== 37 || i
> 126) {
148 yesAsString
.Append(static_cast<char>(i
));
149 yesExpectedString
.Append('%');
150 yesExpectedString
.Append(int_to_hex_digit(i
/ 16));
151 yesExpectedString
.Append(int_to_hex_digit(i
% 16));
156 utils
.SpecialEncode(yesAsString
, PR_FALSE
, out
);
157 CheckEquals(yesExpectedString
, out
);
159 TestEncodeHelper("blah//blah", "blah/blah");
162 void TestCanonicalizeHelper(const char* in
, const char* expected
)
164 nsCString out
, strIn(in
), strExp(expected
);
165 nsUrlClassifierUtils utils
;
167 utils
.CanonicalizePath(strIn
, out
);
168 CheckEquals(strExp
, out
);
171 void TestCanonicalize()
173 // Test repeated %-decoding. Note: %25 --> %, %32 --> 2, %35 --> 5
174 TestCanonicalizeHelper("%25", "%25");
175 TestCanonicalizeHelper("%25%32%35", "%25");
176 TestCanonicalizeHelper("asdf%25%32%35asd", "asdf%25asd");
177 TestCanonicalizeHelper("%%%25%32%35asd%%", "%25%25%25asd%25%25");
178 TestCanonicalizeHelper("%25%32%35%25%32%35%25%32%35", "%25%25%25");
179 TestCanonicalizeHelper("%25", "%25");
180 TestCanonicalizeHelper("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B",
181 "~a!b@c#d$e%25f^00&11*22(33)44_55+");
183 TestCanonicalizeHelper("", "");
184 TestCanonicalizeHelper("%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/%77%77%77%2E%65%62%61%79%2E%63%6F%6D/",
185 "168.188.99.26/.secure/www.ebay.com/");
186 TestCanonicalizeHelper("195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/",
187 "195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/");
190 void TestParseIPAddressHelper(const char *in
, const char *expected
)
192 nsCString out
, strIn(in
), strExp(expected
);
193 nsUrlClassifierUtils utils
;
196 utils
.ParseIPAddress(strIn
, out
);
197 CheckEquals(strExp
, out
);
200 void TestParseIPAddress()
202 TestParseIPAddressHelper("123.123.0.0.1", "");
203 TestParseIPAddressHelper("255.0.0.1", "255.0.0.1");
204 TestParseIPAddressHelper("12.0x12.01234", "12.18.2.156");
205 TestParseIPAddressHelper("276.2.3", "20.2.0.3");
206 TestParseIPAddressHelper("012.034.01.055", "10.28.1.45");
207 TestParseIPAddressHelper("0x12.0x43.0x44.0x01", "18.67.68.1");
208 TestParseIPAddressHelper("167838211", "10.1.2.3");
209 TestParseIPAddressHelper("3279880203", "195.127.0.11");
210 TestParseIPAddressHelper("0x12434401", "18.67.68.1");
211 TestParseIPAddressHelper("413960661", "24.172.137.213");
212 TestParseIPAddressHelper("03053104725", "24.172.137.213");
213 TestParseIPAddressHelper("030.0254.0x89d5", "24.172.137.213");
214 TestParseIPAddressHelper("1.234.4.0377", "1.234.4.255");
215 TestParseIPAddressHelper("1.2.3.00x0", "");
216 TestParseIPAddressHelper("10.192.95.89 xy", "10.192.95.89");
217 TestParseIPAddressHelper("10.192.95.89 xyz", "");
218 TestParseIPAddressHelper("1.2.3.0x0", "1.2.3.0");
219 TestParseIPAddressHelper("1.2.3.4", "1.2.3.4");
222 void TestCanonicalNumHelper(const char *in
, PRUint32 bytes
,
223 bool allowOctal
, const char *expected
)
225 nsCString out
, strIn(in
), strExp(expected
);
226 nsUrlClassifierUtils utils
;
229 utils
.CanonicalNum(strIn
, bytes
, allowOctal
, out
);
230 CheckEquals(strExp
, out
);
233 void TestCanonicalNum()
235 TestCanonicalNumHelper("", 1, true, "");
236 TestCanonicalNumHelper("10", 0, true, "");
237 TestCanonicalNumHelper("45", 1, true, "45");
238 TestCanonicalNumHelper("0x10", 1, true, "16");
239 TestCanonicalNumHelper("367", 2, true, "1.111");
240 TestCanonicalNumHelper("012345", 3, true, "0.20.229");
241 TestCanonicalNumHelper("0173", 1, true, "123");
242 TestCanonicalNumHelper("09", 1, false, "9");
243 TestCanonicalNumHelper("0x120x34", 2, true, "");
244 TestCanonicalNumHelper("0x12fc", 2, true, "18.252");
245 TestCanonicalNumHelper("3279880203", 4, true, "195.127.0.11");
246 TestCanonicalNumHelper("0x0000059", 1, true, "89");
247 TestCanonicalNumHelper("0x00000059", 1, true, "89");
248 TestCanonicalNumHelper("0x0000067", 1, true, "103");
251 void TestHostnameHelper(const char *in
, const char *expected
)
253 nsCString out
, strIn(in
), strExp(expected
);
254 nsUrlClassifierUtils utils
;
257 utils
.CanonicalizeHostname(strIn
, out
);
258 CheckEquals(strExp
, out
);
263 TestHostnameHelper("abcd123;[]", "abcd123;[]");
264 TestHostnameHelper("abc.123", "abc.123");
265 TestHostnameHelper("abc..123", "abc.123");
266 TestHostnameHelper("trailing.", "trailing");
267 TestHostnameHelper("i love trailing dots....", "i%20love%20trailing%20dots");
268 TestHostnameHelper(".leading", "leading");
269 TestHostnameHelper("..leading", "leading");
270 TestHostnameHelper(".dots.", "dots");
271 TestHostnameHelper(".both.", "both");
272 TestHostnameHelper(".both..", "both");
273 TestHostnameHelper("..both.", "both");
274 TestHostnameHelper("..both..", "both");
275 TestHostnameHelper("..a.b.c.d..", "a.b.c.d");
276 TestHostnameHelper("..127.0.0.1..", "127.0.0.1");
277 TestHostnameHelper("asdf!@#$a", "asdf!@#$a");
278 TestHostnameHelper("AB CD 12354", "ab%20cd%2012354");
279 TestHostnameHelper("\1\2\3\4\112\177", "%01%02%03%04j%7F");
280 TestHostnameHelper("<>.AS/-+", "<>.as/-+");
284 void TestLongHostname()
286 static const int kTestSize
= 1024 * 150;
287 char *str
= static_cast<char*>(malloc(kTestSize
+ 1));
288 memset(str
, 'x', kTestSize
);
289 str
[kTestSize
] = '\0';
291 nsUrlClassifierUtils utils
;
295 nsDependentCString
in(str
);
296 PRIntervalTime clockStart
= PR_IntervalNow();
297 utils
.CanonicalizeHostname(in
, out
);
298 PRIntervalTime clockEnd
= PR_IntervalNow();
300 CheckEquals(in
, out
);
302 printf("CanonicalizeHostname on long string (%dms)\n",
303 PR_IntervalToMilliseconds(clockEnd
- clockStart
));
306 int main(int argc
, char **argv
)
314 TestParseIPAddress();
318 printf("%d of %d tests passed\n", gPassedTests
, gTotalTests
);
319 // Non-zero return status signals test failure to build system.
321 return (gPassedTests
!= gTotalTests
);