1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <ctype.h>
#include <stdio.h>
#include <mozilla/RefPtr.h>
#include "nsEscape.h"
#include "nsString.h"
#include "nsUrlClassifierUtils.h"
#include "stdlib.h"
#include "Common.h"
static char int_to_hex_digit(int32_t i) {
NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit");
return static_cast<char>(((i < 10) ? (i + '0') : ((i - 10) + 'A')));
}
static void CheckEquals(nsCString& expected, nsCString& actual) {
ASSERT_TRUE((expected).Equals((actual)))
<< "Expected:" << expected.get() << ", Actual:" << actual.get();
}
static void TestUnescapeHelper(const char* in, const char* expected) {
nsCString out, strIn(in), strExp(expected);
NS_UnescapeURL(strIn.get(), strIn.Length(), esc_AlwaysCopy, out);
CheckEquals(strExp, out);
}
static void TestEncodeHelper(const char* in, const char* expected) {
nsCString out, strIn(in), strExp(expected);
nsUrlClassifierUtils::GetInstance()->SpecialEncode(strIn, true, out);
CheckEquals(strExp, out);
}
static void TestCanonicalizeHelper(const char* in, const char* expected) {
nsCString out, strIn(in), strExp(expected);
nsUrlClassifierUtils::GetInstance()->CanonicalizePath(strIn, out);
CheckEquals(strExp, out);
}
static void TestCanonicalNumHelper(const char* in, uint32_t bytes,
bool allowOctal, const char* expected) {
nsCString out, strIn(in), strExp(expected);
nsUrlClassifierUtils::GetInstance()->CanonicalNum(strIn, bytes, allowOctal,
out);
CheckEquals(strExp, out);
}
void TestHostnameHelper(const char* in, const char* expected) {
nsCString out, strIn(in), strExp(expected);
nsUrlClassifierUtils::GetInstance()->CanonicalizeHostname(strIn, out);
CheckEquals(strExp, out);
}
// Make sure Unescape from nsEncode.h's unescape does what the server does.
TEST(UrlClassifierUtils, Unescape)
{
// test empty string
TestUnescapeHelper("\0", "\0");
// Test docoding of all characters.
nsCString allCharsEncoded, allCharsEncodedLowercase, allCharsAsString;
for (int32_t i = 1; i < 256; ++i) {
allCharsEncoded.Append('%');
allCharsEncoded.Append(int_to_hex_digit(i / 16));
allCharsEncoded.Append((int_to_hex_digit(i % 16)));
allCharsEncodedLowercase.Append('%');
allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i / 16)));
allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i % 16)));
allCharsAsString.Append(static_cast<char>(i));
}
nsCString out;
NS_UnescapeURL(allCharsEncoded.get(), allCharsEncoded.Length(),
esc_AlwaysCopy, out);
CheckEquals(allCharsAsString, out);
out.Truncate();
NS_UnescapeURL(allCharsEncodedLowercase.get(),
allCharsEncodedLowercase.Length(), esc_AlwaysCopy, out);
CheckEquals(allCharsAsString, out);
// Test %-related edge cases
TestUnescapeHelper("%", "%");
TestUnescapeHelper("%xx", "%xx");
TestUnescapeHelper("%%", "%%");
TestUnescapeHelper("%%%", "%%%");
TestUnescapeHelper("%%%%", "%%%%");
TestUnescapeHelper("%1", "%1");
TestUnescapeHelper("%1z", "%1z");
TestUnescapeHelper("a%1z", "a%1z");
TestUnescapeHelper("abc%d%e%fg%hij%klmno%", "abc%d%e%fg%hij%klmno%");
// A few more tests
TestUnescapeHelper("%25", "%");
TestUnescapeHelper("%25%32%35", "%25");
}
TEST(UrlClassifierUtils, Enc)
{
// Test empty string
TestEncodeHelper("", "");
// Test that all characters we shouldn't encode ([33-34],36,[38,126]) are not.
nsCString noenc;
for (int32_t i = 33; i < 127; i++) {
if (i != 35 && i != 37) { // skip %
noenc.Append(static_cast<char>(i));
}
}
nsCString out;
nsUrlClassifierUtils::GetInstance()->SpecialEncode(noenc, false, out);
CheckEquals(noenc, out);
// Test that all the chars that we should encode [0,32],35, 37,[127,255] are
nsCString yesAsString, yesExpectedString;
for (int32_t i = 1; i < 256; i++) {
if (i < 33 || i == 35 || i == 37 || i > 126) {
yesAsString.Append(static_cast<char>(i));
yesExpectedString.Append('%');
yesExpectedString.Append(int_to_hex_digit(i / 16));
yesExpectedString.Append(int_to_hex_digit(i % 16));
}
}
out.Truncate();
nsUrlClassifierUtils::GetInstance()->SpecialEncode(yesAsString, false, out);
CheckEquals(yesExpectedString, out);
TestEncodeHelper("blah//blah", "blah/blah");
}
TEST(UrlClassifierUtils, Canonicalize)
{
// Test repeated %-decoding. Note: %25 --> %, %32 --> 2, %35 --> 5
TestCanonicalizeHelper("%25", "%25");
TestCanonicalizeHelper("%25%32%35", "%25");
TestCanonicalizeHelper("asdf%25%32%35asd", "asdf%25asd");
TestCanonicalizeHelper("%%%25%32%35asd%%", "%25%25%25asd%25%25");
TestCanonicalizeHelper("%25%32%35%25%32%35%25%32%35", "%25%25%25");
TestCanonicalizeHelper("%25", "%25");
TestCanonicalizeHelper(
"%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_"
"55%252B",
"~a!b@c%23d$e%25f^00&11*22(33)44_55+");
TestCanonicalizeHelper("", "");
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/",
"168.188.99.26/.secure/www.ebay.com/");
TestCanonicalizeHelper(
"195.127.0.11/uploads/%20%20%20%20/.verify/"
".eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/",
"195.127.0.11/uploads/%20%20%20%20/.verify/"
".eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/");
// Added in bug 489455. %00 should no longer be changed to %01.
TestCanonicalizeHelper("%00", "%00");
}
void TestParseIPAddressHelper(const char* in, const char* expected) {
nsCString out, strIn(in), strExp(expected);
nsUrlClassifierUtils::GetInstance()->ParseIPAddress(strIn, out);
CheckEquals(strExp, out);
}
TEST(UrlClassifierUtils, ParseIPAddress)
{
TestParseIPAddressHelper("123.123.0.0.1", "");
TestParseIPAddressHelper("255.0.0.1", "255.0.0.1");
TestParseIPAddressHelper("12.0x12.01234", "12.18.2.156");
TestParseIPAddressHelper("276.2.3", "20.2.0.3");
TestParseIPAddressHelper("012.034.01.055", "10.28.1.45");
TestParseIPAddressHelper("0x12.0x43.0x44.0x01", "18.67.68.1");
TestParseIPAddressHelper("167838211", "10.1.2.3");
TestParseIPAddressHelper("3279880203", "195.127.0.11");
TestParseIPAddressHelper("0x12434401", "18.67.68.1");
TestParseIPAddressHelper("413960661", "24.172.137.213");
TestParseIPAddressHelper("03053104725", "24.172.137.213");
TestParseIPAddressHelper("030.0254.0x89d5", "24.172.137.213");
TestParseIPAddressHelper("1.234.4.0377", "1.234.4.255");
TestParseIPAddressHelper("1.2.3.00x0", "");
TestParseIPAddressHelper("10.192.95.89 xy", "10.192.95.89");
TestParseIPAddressHelper("10.192.95.89 xyz", "");
TestParseIPAddressHelper("1.2.3.0x0", "1.2.3.0");
TestParseIPAddressHelper("1.2.3.4", "1.2.3.4");
}
TEST(UrlClassifierUtils, CanonicalNum)
{
TestCanonicalNumHelper("", 1, true, "");
TestCanonicalNumHelper("10", 0, true, "");
TestCanonicalNumHelper("45", 1, true, "45");
TestCanonicalNumHelper("0x10", 1, true, "16");
TestCanonicalNumHelper("367", 2, true, "1.111");
TestCanonicalNumHelper("012345", 3, true, "0.20.229");
TestCanonicalNumHelper("0173", 1, true, "123");
TestCanonicalNumHelper("09", 1, false, "9");
TestCanonicalNumHelper("0x120x34", 2, true, "");
TestCanonicalNumHelper("0x12fc", 2, true, "18.252");
TestCanonicalNumHelper("3279880203", 4, true, "195.127.0.11");
TestCanonicalNumHelper("0x0000059", 1, true, "89");
TestCanonicalNumHelper("0x00000059", 1, true, "89");
TestCanonicalNumHelper("0x0000067", 1, true, "103");
}
TEST(UrlClassifierUtils, Hostname)
{
TestHostnameHelper("abcd123;[]", "abcd123;[]");
TestHostnameHelper("abc.123", "abc.123");
TestHostnameHelper("abc..123", "abc.123");
TestHostnameHelper("trailing.", "trailing");
TestHostnameHelper("i love trailing dots....", "i%20love%20trailing%20dots");
TestHostnameHelper(".leading", "leading");
TestHostnameHelper("..leading", "leading");
TestHostnameHelper(".dots.", "dots");
TestHostnameHelper(".both.", "both");
TestHostnameHelper(".both..", "both");
TestHostnameHelper("..both.", "both");
TestHostnameHelper("..both..", "both");
TestHostnameHelper("..a.b.c.d..", "a.b.c.d");
TestHostnameHelper("..127.0.0.1..", "127.0.0.1");
TestHostnameHelper("AB CD 12354", "ab%20cd%2012354");
TestHostnameHelper("\1\2\3\4\112\177", "%01%02%03%04j%7F");
TestHostnameHelper("<>.AS/-+", "<>.as/-+");
// Added in bug 489455. %00 should no longer be changed to %01.
TestHostnameHelper("%00", "%00");
}
TEST(UrlClassifierUtils, LongHostname)
{
static const int kTestSize = 1024 * 150;
char* str = static_cast<char*>(malloc(kTestSize + 1));
memset(str, 'x', kTestSize);
str[kTestSize] = '\0';
nsAutoCString out;
nsDependentCString in(str);
PRIntervalTime clockStart = PR_IntervalNow();
nsUrlClassifierUtils::GetInstance()->CanonicalizeHostname(in, out);
PRIntervalTime clockEnd = PR_IntervalNow();
CheckEquals(in, out);
printf("CanonicalizeHostname on long string (%dms)\n",
PR_IntervalToMilliseconds(clockEnd - clockStart));
}
|