michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include "nsEscape.h" michael@0: #include "nsString.h" michael@0: #include "nsUrlClassifierUtils.h" michael@0: #include "nsNetUtil.h" michael@0: #include "stdlib.h" michael@0: #include "TestHarness.h" michael@0: michael@0: static int gTotalTests = 0; michael@0: static int gPassedTests = 0; michael@0: michael@0: static char int_to_hex_digit(int32_t i) { michael@0: NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit"); michael@0: return static_cast(((i < 10) ? (i + '0') : ((i - 10) + 'A'))); michael@0: } michael@0: michael@0: static void CheckEquals(nsCString & expected, nsCString & actual) michael@0: { michael@0: if (!(expected).Equals((actual))) { michael@0: fail("expected |%s| but got |%s|", (expected).get(), (actual).get()); michael@0: } else { michael@0: gPassedTests++; michael@0: } michael@0: gTotalTests++; michael@0: } michael@0: michael@0: void TestUnescapeHelper(const char* in, const char* expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: michael@0: NS_UnescapeURL(strIn.get(), strIn.Length(), esc_AlwaysCopy, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: // Make sure Unescape from nsEncode.h's unescape does what the server does. michael@0: void TestUnescape() michael@0: { michael@0: // test empty string michael@0: TestUnescapeHelper("\0", "\0"); michael@0: michael@0: // Test docoding of all characters. michael@0: nsCString allCharsEncoded, allCharsEncodedLowercase, allCharsAsString; michael@0: for (int32_t i = 1; i < 256; ++i) { michael@0: allCharsEncoded.Append('%'); michael@0: allCharsEncoded.Append(int_to_hex_digit(i / 16)); michael@0: allCharsEncoded.Append((int_to_hex_digit(i % 16))); michael@0: michael@0: allCharsEncodedLowercase.Append('%'); michael@0: allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i / 16))); michael@0: allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i % 16))); michael@0: michael@0: allCharsAsString.Append(static_cast(i)); michael@0: } michael@0: michael@0: nsUrlClassifierUtils utils; michael@0: nsCString out; michael@0: NS_UnescapeURL(allCharsEncoded.get(), allCharsEncoded.Length(), esc_AlwaysCopy, out); michael@0: CheckEquals(allCharsAsString, out); michael@0: michael@0: out.Truncate(); michael@0: NS_UnescapeURL(allCharsEncodedLowercase.get(), allCharsEncodedLowercase.Length(), esc_AlwaysCopy, out); michael@0: CheckEquals(allCharsAsString, out); michael@0: michael@0: // Test %-related edge cases michael@0: TestUnescapeHelper("%", "%"); michael@0: TestUnescapeHelper("%xx", "%xx"); michael@0: TestUnescapeHelper("%%", "%%"); michael@0: TestUnescapeHelper("%%%", "%%%"); michael@0: TestUnescapeHelper("%%%%", "%%%%"); michael@0: TestUnescapeHelper("%1", "%1"); michael@0: TestUnescapeHelper("%1z", "%1z"); michael@0: TestUnescapeHelper("a%1z", "a%1z"); michael@0: TestUnescapeHelper("abc%d%e%fg%hij%klmno%", "abc%d%e%fg%hij%klmno%"); michael@0: michael@0: // A few more tests michael@0: TestUnescapeHelper("%25", "%"); michael@0: TestUnescapeHelper("%25%32%35", "%25"); michael@0: } michael@0: michael@0: void TestEncodeHelper(const char* in, const char* expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: michael@0: utils.SpecialEncode(strIn, true, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: void TestEnc() michael@0: { michael@0: // Test empty string michael@0: TestEncodeHelper("", ""); michael@0: michael@0: // Test that all characters we shouldn't encode ([33-36],[38,126]) are not. michael@0: nsCString noenc; michael@0: for (int32_t i = 33; i < 127; i++) { michael@0: if (i != 37) { // skip % michael@0: noenc.Append(static_cast(i)); michael@0: } michael@0: } michael@0: nsUrlClassifierUtils utils; michael@0: nsCString out; michael@0: utils.SpecialEncode(noenc, false, out); michael@0: CheckEquals(noenc, out); michael@0: michael@0: // Test that all the chars that we should encode [0,32],37,[127,255] are michael@0: nsCString yesAsString, yesExpectedString; michael@0: for (int32_t i = 1; i < 256; i++) { michael@0: if (i < 33 || i == 37 || i > 126) { michael@0: yesAsString.Append(static_cast(i)); michael@0: yesExpectedString.Append('%'); michael@0: yesExpectedString.Append(int_to_hex_digit(i / 16)); michael@0: yesExpectedString.Append(int_to_hex_digit(i % 16)); michael@0: } michael@0: } michael@0: michael@0: out.Truncate(); michael@0: utils.SpecialEncode(yesAsString, false, out); michael@0: CheckEquals(yesExpectedString, out); michael@0: michael@0: TestEncodeHelper("blah//blah", "blah/blah"); michael@0: } michael@0: michael@0: void TestCanonicalizeHelper(const char* in, const char* expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: michael@0: utils.CanonicalizePath(strIn, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: void TestCanonicalize() michael@0: { michael@0: // Test repeated %-decoding. Note: %25 --> %, %32 --> 2, %35 --> 5 michael@0: TestCanonicalizeHelper("%25", "%25"); michael@0: TestCanonicalizeHelper("%25%32%35", "%25"); michael@0: TestCanonicalizeHelper("asdf%25%32%35asd", "asdf%25asd"); michael@0: TestCanonicalizeHelper("%%%25%32%35asd%%", "%25%25%25asd%25%25"); michael@0: TestCanonicalizeHelper("%25%32%35%25%32%35%25%32%35", "%25%25%25"); michael@0: TestCanonicalizeHelper("%25", "%25"); michael@0: TestCanonicalizeHelper("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B", michael@0: "~a!b@c#d$e%25f^00&11*22(33)44_55+"); michael@0: michael@0: TestCanonicalizeHelper("", ""); michael@0: 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/", michael@0: "168.188.99.26/.secure/www.ebay.com/"); michael@0: TestCanonicalizeHelper("195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/", michael@0: "195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/"); michael@0: // Added in bug 489455. %00 should no longer be changed to %01. michael@0: TestCanonicalizeHelper("%00", "%00"); michael@0: } michael@0: michael@0: void TestParseIPAddressHelper(const char *in, const char *expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: utils.Init(); michael@0: michael@0: utils.ParseIPAddress(strIn, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: void TestParseIPAddress() michael@0: { michael@0: TestParseIPAddressHelper("123.123.0.0.1", ""); michael@0: TestParseIPAddressHelper("255.0.0.1", "255.0.0.1"); michael@0: TestParseIPAddressHelper("12.0x12.01234", "12.18.2.156"); michael@0: TestParseIPAddressHelper("276.2.3", "20.2.0.3"); michael@0: TestParseIPAddressHelper("012.034.01.055", "10.28.1.45"); michael@0: TestParseIPAddressHelper("0x12.0x43.0x44.0x01", "18.67.68.1"); michael@0: TestParseIPAddressHelper("167838211", "10.1.2.3"); michael@0: TestParseIPAddressHelper("3279880203", "195.127.0.11"); michael@0: TestParseIPAddressHelper("0x12434401", "18.67.68.1"); michael@0: TestParseIPAddressHelper("413960661", "24.172.137.213"); michael@0: TestParseIPAddressHelper("03053104725", "24.172.137.213"); michael@0: TestParseIPAddressHelper("030.0254.0x89d5", "24.172.137.213"); michael@0: TestParseIPAddressHelper("1.234.4.0377", "1.234.4.255"); michael@0: TestParseIPAddressHelper("1.2.3.00x0", ""); michael@0: TestParseIPAddressHelper("10.192.95.89 xy", "10.192.95.89"); michael@0: TestParseIPAddressHelper("10.192.95.89 xyz", ""); michael@0: TestParseIPAddressHelper("1.2.3.0x0", "1.2.3.0"); michael@0: TestParseIPAddressHelper("1.2.3.4", "1.2.3.4"); michael@0: } michael@0: michael@0: void TestCanonicalNumHelper(const char *in, uint32_t bytes, michael@0: bool allowOctal, const char *expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: utils.Init(); michael@0: michael@0: utils.CanonicalNum(strIn, bytes, allowOctal, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: void TestCanonicalNum() michael@0: { michael@0: TestCanonicalNumHelper("", 1, true, ""); michael@0: TestCanonicalNumHelper("10", 0, true, ""); michael@0: TestCanonicalNumHelper("45", 1, true, "45"); michael@0: TestCanonicalNumHelper("0x10", 1, true, "16"); michael@0: TestCanonicalNumHelper("367", 2, true, "1.111"); michael@0: TestCanonicalNumHelper("012345", 3, true, "0.20.229"); michael@0: TestCanonicalNumHelper("0173", 1, true, "123"); michael@0: TestCanonicalNumHelper("09", 1, false, "9"); michael@0: TestCanonicalNumHelper("0x120x34", 2, true, ""); michael@0: TestCanonicalNumHelper("0x12fc", 2, true, "18.252"); michael@0: TestCanonicalNumHelper("3279880203", 4, true, "195.127.0.11"); michael@0: TestCanonicalNumHelper("0x0000059", 1, true, "89"); michael@0: TestCanonicalNumHelper("0x00000059", 1, true, "89"); michael@0: TestCanonicalNumHelper("0x0000067", 1, true, "103"); michael@0: } michael@0: michael@0: void TestHostnameHelper(const char *in, const char *expected) michael@0: { michael@0: nsCString out, strIn(in), strExp(expected); michael@0: nsUrlClassifierUtils utils; michael@0: utils.Init(); michael@0: michael@0: utils.CanonicalizeHostname(strIn, out); michael@0: CheckEquals(strExp, out); michael@0: } michael@0: michael@0: void TestHostname() michael@0: { michael@0: TestHostnameHelper("abcd123;[]", "abcd123;[]"); michael@0: TestHostnameHelper("abc.123", "abc.123"); michael@0: TestHostnameHelper("abc..123", "abc.123"); michael@0: TestHostnameHelper("trailing.", "trailing"); michael@0: TestHostnameHelper("i love trailing dots....", "i%20love%20trailing%20dots"); michael@0: TestHostnameHelper(".leading", "leading"); michael@0: TestHostnameHelper("..leading", "leading"); michael@0: TestHostnameHelper(".dots.", "dots"); michael@0: TestHostnameHelper(".both.", "both"); michael@0: TestHostnameHelper(".both..", "both"); michael@0: TestHostnameHelper("..both.", "both"); michael@0: TestHostnameHelper("..both..", "both"); michael@0: TestHostnameHelper("..a.b.c.d..", "a.b.c.d"); michael@0: TestHostnameHelper("..127.0.0.1..", "127.0.0.1"); michael@0: TestHostnameHelper("asdf!@#$a", "asdf!@#$a"); michael@0: TestHostnameHelper("AB CD 12354", "ab%20cd%2012354"); michael@0: TestHostnameHelper("\1\2\3\4\112\177", "%01%02%03%04j%7F"); michael@0: TestHostnameHelper("<>.AS/-+", "<>.as/-+"); michael@0: // Added in bug 489455. %00 should no longer be changed to %01. michael@0: TestHostnameHelper("%00", "%00"); michael@0: } michael@0: michael@0: void TestLongHostname() michael@0: { michael@0: static const int kTestSize = 1024 * 150; michael@0: char *str = static_cast(malloc(kTestSize + 1)); michael@0: memset(str, 'x', kTestSize); michael@0: str[kTestSize] = '\0'; michael@0: michael@0: nsUrlClassifierUtils utils; michael@0: utils.Init(); michael@0: michael@0: nsAutoCString out; michael@0: nsDependentCString in(str); michael@0: PRIntervalTime clockStart = PR_IntervalNow(); michael@0: utils.CanonicalizeHostname(in, out); michael@0: PRIntervalTime clockEnd = PR_IntervalNow(); michael@0: michael@0: CheckEquals(in, out); michael@0: michael@0: printf("CanonicalizeHostname on long string (%dms)\n", michael@0: PR_IntervalToMilliseconds(clockEnd - clockStart)); michael@0: } michael@0: michael@0: void TestFragmentSet() michael@0: { michael@0: nsUrlClassifierFragmentSet set; michael@0: set.Init(3); michael@0: michael@0: set.Put(NS_LITERAL_CSTRING("a")); michael@0: set.Put(NS_LITERAL_CSTRING("b")); michael@0: set.Put(NS_LITERAL_CSTRING("c")); michael@0: michael@0: // At this point, adding a fourth element would push "a" off. michael@0: // Make sure that set.Has("a") moves it to the front of the list michael@0: set.Has(NS_LITERAL_CSTRING("a")); michael@0: michael@0: // Now add a new item. This should now push "b" off the list, michael@0: // but leave "a" michael@0: set.Put(NS_LITERAL_CSTRING("d")); michael@0: michael@0: gTotalTests++; michael@0: if (set.Has(NS_LITERAL_CSTRING("a"))) michael@0: gPassedTests++; michael@0: else michael@0: fail("set.Has(\"a\") failed."); michael@0: michael@0: gTotalTests++; michael@0: if (!set.Has(NS_LITERAL_CSTRING("b"))) michael@0: gPassedTests++; michael@0: else michael@0: fail("!set.Has(\"b\") failed."); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: ScopedXPCOM xpcom("URLClassiferUtils"); michael@0: michael@0: TestUnescape(); michael@0: TestEnc(); michael@0: TestCanonicalize(); michael@0: TestCanonicalNum(); michael@0: TestParseIPAddress(); michael@0: TestHostname(); michael@0: TestLongHostname(); michael@0: TestFragmentSet(); michael@0: michael@0: if (gPassedTests == gTotalTests) michael@0: passed(__FILE__); michael@0: printf("%d of %d tests passed\n", gPassedTests, gTotalTests); michael@0: // Non-zero return status signals test failure to build system. michael@0: michael@0: return (gPassedTests != gTotalTests); michael@0: }