Bug 489455 - Update URLClassifier to follow Safebrowsing spec
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Tue, 21 Apr 2009 19:34:06 -0400
changeset 27588 83b59076910c431c1a8393a5b9d2caa5aa314ed1
parent 27587 639af68a74e39c61716b618e50b6dd065adc78b8
child 27589 b4dd3013fea637d93f359c11e294519d545c6f9e
push id6632
push usersdwilsh@shawnwilsher.com
push dateTue, 21 Apr 2009 23:34:35 +0000
treeherdermozilla-central@83b59076910c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs489455
milestone1.9.2a1pre
Bug 489455 - Update URLClassifier to follow Safebrowsing spec This makes us not change %00 to %01. r=tony
toolkit/components/url-classifier/src/nsUrlClassifierUtils.cpp
toolkit/components/url-classifier/tests/Makefile.in
toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
--- a/toolkit/components/url-classifier/src/nsUrlClassifierUtils.cpp
+++ b/toolkit/components/url-classifier/src/nsUrlClassifierUtils.cpp
@@ -369,21 +369,16 @@ nsUrlClassifierUtils::SpecialEncode(cons
   PRBool changed = PR_FALSE;
   const char* curChar = url.BeginReading();
   const char* end = url.EndReading();
 
   unsigned char lastChar = '\0';
   while (curChar != end) {
     unsigned char c = static_cast<unsigned char>(*curChar);
     if (ShouldURLEscape(c)) {
-      // We don't want to deal with 0, as it can break certain strings, just
-      // encode as one.
-      if (c == 0)
-        c = 1;
-
       _retval.Append('%');
       _retval.Append(int_to_hex_digit(c / 16));
       _retval.Append(int_to_hex_digit(c % 16));
 
       changed = PR_TRUE;
     } else if (foldSlashes && (c == '/' && lastChar == '/')) {
       // skip
     } else {
--- a/toolkit/components/url-classifier/tests/Makefile.in
+++ b/toolkit/components/url-classifier/tests/Makefile.in
@@ -58,34 +58,25 @@ REQUIRES = \
 # mochitest tests
 DIRS += mochitest \
 	$(NULL)
 
 # xpcshell tests
 XPCSHELL_TESTS=unit
 
 # simple c++ tests (no xpcom)
-CPPSRCS = \
+CPP_UNIT_TESTS = \
 	TestUrlClassifierUtils.cpp \
 	$(NULL)
 
-SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX))
-
 LOCAL_INCLUDES = \
 	-I$(srcdir)/../src \
 	-I$(srcdir)/../public \
 	$(NULL)
 
 LIBS = \
 	../src/$(LIB_PREFIX)urlclassifier_s.$(LIB_SUFFIX) \
 	$(MOZ_COMPONENT_LIBS) \
 	$(XPCOM_LIBS) \
 	$(NSPR_LIBS) \
 	$(NULL)
 
 include $(topsrcdir)/config/rules.mk
-
-check::
-	@echo Running tests...
-	@$(EXIT_ON_ERROR) \
-	for f in $(SIMPLE_PROGRAMS); do \
-	  $(RUN_TEST_PROGRAM) $(DIST)/bin/$$f; \
-  done
--- a/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
+++ b/toolkit/components/url-classifier/tests/TestUrlClassifierUtils.cpp
@@ -36,30 +36,30 @@
 
 #include <stdio.h>
 #include <ctype.h>
 #include "nsEscape.h"
 #include "nsString.h"
 #include "nsUrlClassifierUtils.h"
 #include "nsNetUtil.h"
 #include "stdlib.h"
+#include "TestHarness.h"
 
 static int gTotalTests = 0;
 static int gPassedTests = 0;
 
 static char int_to_hex_digit(PRInt32 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)
 {
   if (!(expected).Equals((actual))) {
-    fprintf(stderr, "FAILED: expected |%s| but was |%s|\n", (expected).get(),
-            (actual).get());
+    fail("expected |%s| but got |%s|", (expected).get(), (actual).get());
   } else {
     gPassedTests++;
   }
   gTotalTests++;
 }
 
 void TestUnescapeHelper(const char* in, const char* expected)
 {
@@ -180,16 +180,18 @@ void TestCanonicalize()
   TestCanonicalizeHelper("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B",
       "~a!b@c#d$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 utils;
   utils.Init();
 
@@ -273,17 +275,18 @@ void TestHostname()
   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("asdf!@#$a", "asdf!@#$a");
   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");
 }
 
 void TestLongHostname()
 {
   static const int kTestSize = 1024 * 150;
   char *str = static_cast<char*>(malloc(kTestSize + 1));
   memset(str, 'x', kTestSize);
   str[kTestSize] = '\0';
@@ -319,35 +322,37 @@ void TestFragmentSet()
   // Now add a new item.  This should now push "b" off the list,
   // but leave "a"
   set.Put(NS_LITERAL_CSTRING("d"));
 
   gTotalTests++;
   if (set.Has(NS_LITERAL_CSTRING("a")))
     gPassedTests++;
   else
-    fprintf(stderr, "FAILED: set.Has(\"a\") failed.\n");
+    fail("set.Has(\"a\") failed.");
 
   gTotalTests++;
   if (!set.Has(NS_LITERAL_CSTRING("b")))
     gPassedTests++;
   else
-    fprintf(stderr, "FAILED: !set.Has(\"b\") failed.\n");
+    fail("!set.Has(\"b\") failed.");
 }
 
 int main(int argc, char **argv)
 {
-  NS_LogInit();
+  ScopedXPCOM xpcom("URLClassiferUtils");
 
   TestUnescape();
   TestEnc();
   TestCanonicalize();
   TestCanonicalNum();
   TestParseIPAddress();
   TestHostname();
   TestLongHostname();
   TestFragmentSet();
 
+  if (gPassedTests == gTotalTests)
+    passed(__FILE__);
   printf("%d of %d tests passed\n", gPassedTests, gTotalTests);
   // Non-zero return status signals test failure to build system.
 
   return (gPassedTests != gTotalTests);
 }