Back to index

lightning-sunbird  0.9+nobinonly
TestUrlClassifierUtils.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Url Classifier code
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Google Inc.
00018  * Portions created by the Initial Developer are Copyright (C) 2007
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include <stdio.h>
00038 #include <ctype.h>
00039 #include "nsEscape.h"
00040 #include "nsString.h"
00041 #include "nsUrlClassifierUtils.h"
00042 
00043 static int gTotalTests = 0;
00044 static int gPassedTests = 0;
00045 
00046 static char int_to_hex_digit(PRInt32 i) {
00047   NS_ASSERTION((i >= 0) && (i <= 15), "int too big in int_to_hex_digit");
00048   return NS_STATIC_CAST(char, ((i < 10) ? (i + '0') : ((i - 10) + 'A')));
00049 }
00050 
00051 static void CheckEquals(nsCString & expected, nsCString & actual)
00052 {
00053   if (!(expected).Equals((actual))) {
00054     fprintf(stderr, "FAILED: expected |%s| but was |%s|\n", (expected).get(),
00055             (actual).get());
00056   } else {
00057     gPassedTests++;
00058   }
00059   gTotalTests++;
00060 }
00061 
00062 void TestUnescapeHelper(const char* in, const char* expected)
00063 {
00064   nsCString out, strIn(in), strExp(expected);
00065   nsUrlClassifierUtils utils;
00066   
00067   NS_UnescapeURL(strIn.get(), strIn.Length(), esc_AlwaysCopy, out);
00068   CheckEquals(strExp, out);
00069 }
00070 
00071 // Make sure Unescape from nsEncode.h's unescape does what the server does.
00072 void TestUnescape()
00073 {
00074   // test empty string
00075   TestUnescapeHelper("\0", "\0");
00076 
00077   // Test docoding of all characters.
00078   nsCString allCharsEncoded, allCharsEncodedLowercase, allCharsAsString;
00079   for (PRInt32 i = 1; i < 256; ++i) {
00080     allCharsEncoded.Append('%');
00081     allCharsEncoded.Append(int_to_hex_digit(i / 16));
00082     allCharsEncoded.Append((int_to_hex_digit(i % 16)));
00083     
00084     allCharsEncodedLowercase.Append('%');
00085     allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i / 16)));
00086     allCharsEncodedLowercase.Append(tolower(int_to_hex_digit(i % 16)));
00087     
00088     allCharsAsString.Append(NS_STATIC_CAST(char, i));
00089   }
00090   
00091   nsUrlClassifierUtils utils;
00092   nsCString out;
00093   NS_UnescapeURL(allCharsEncoded.get(), allCharsEncoded.Length(), esc_AlwaysCopy, out);
00094   CheckEquals(allCharsAsString, out);
00095   
00096   out.Truncate();
00097   NS_UnescapeURL(allCharsEncodedLowercase.get(), allCharsEncodedLowercase.Length(), esc_AlwaysCopy, out);
00098   CheckEquals(allCharsAsString, out);
00099 
00100   // Test %-related edge cases
00101   TestUnescapeHelper("%", "%");
00102   TestUnescapeHelper("%xx", "%xx");
00103   TestUnescapeHelper("%%", "%%");
00104   TestUnescapeHelper("%%%", "%%%");
00105   TestUnescapeHelper("%%%%", "%%%%");
00106   TestUnescapeHelper("%1", "%1");
00107   TestUnescapeHelper("%1z", "%1z");
00108   TestUnescapeHelper("a%1z", "a%1z");
00109   TestUnescapeHelper("abc%d%e%fg%hij%klmno%", "abc%d%e%fg%hij%klmno%");
00110 
00111   // A few more tests
00112   TestUnescapeHelper("%25", "%");
00113   TestUnescapeHelper("%25%32%35", "%25");
00114 }
00115 
00116 void TestEncodeHelper(const char* in, const char* expected)
00117 {
00118   nsCString out, strIn(in), strExp(expected);
00119   nsUrlClassifierUtils utils;
00120   
00121   utils.SpecialEncode(strIn, out);
00122   CheckEquals(strExp, out);
00123 }
00124 
00125 void TestEnc()
00126 {
00127   // Test empty string
00128   TestEncodeHelper("", "");
00129 
00130   // Test that all characters we shouldn't encode ([33-36],[38,126]) are not.
00131   nsCString noenc;
00132   for (PRInt32 i = 33; i < 127; i++) {
00133     if (i != 37) {                      // skip %
00134       noenc.Append(NS_STATIC_CAST(char, i));
00135     }
00136   }
00137   nsUrlClassifierUtils utils;
00138   nsCString out;
00139   utils.SpecialEncode(noenc, out);
00140   CheckEquals(noenc, out);
00141 
00142   // Test that all the chars that we should encode [0,32],37,[127,255] are
00143   nsCString yesAsString, yesExpectedString;
00144   for (PRInt32 i = 1; i < 256; i++) {
00145     if (i < 33 || i == 37 || i > 126) {
00146       yesAsString.Append(NS_STATIC_CAST(char, i));
00147       yesExpectedString.Append('%');
00148       yesExpectedString.Append(int_to_hex_digit(i / 16));
00149       yesExpectedString.Append(int_to_hex_digit(i % 16));
00150     }
00151   }
00152   
00153   out.Truncate();
00154   utils.SpecialEncode(yesAsString, out);
00155   CheckEquals(yesExpectedString, out);
00156 }
00157 
00158 void TestCanonicalizeHelper(const char* in, const char* expected)
00159 {
00160   nsCString out, strIn(in), strExp(expected);
00161   nsUrlClassifierUtils utils;
00162   
00163   nsresult rv = utils.CanonicalizeURL(strIn, out);
00164   NS_ASSERTION(rv == NS_OK, "com didn't return NS_OK");
00165   CheckEquals(strExp, out);
00166 }
00167 
00168 void TestCanonicalize()
00169 {
00170   // Test repeated %-decoding. Note: %25 --> %, %32 --> 2, %35 --> 5
00171   TestCanonicalizeHelper("%25", "%25");
00172   TestCanonicalizeHelper("%25%32%35", "%25");
00173   TestCanonicalizeHelper("asdf%25%32%35asd", "asdf%25asd");
00174   TestCanonicalizeHelper("%%%25%32%35asd%%", "%25%25%25asd%25%25");
00175   TestCanonicalizeHelper("%25%32%35%25%32%35%25%32%35", "%25%25%25");
00176   TestCanonicalizeHelper("%25", "%25");
00177   TestCanonicalizeHelper("%257Ea%2521b%2540c%2523d%2524e%25f%255E00%252611%252A22%252833%252944_55%252B",
00178       "~a!b@c#d$e%25f^00&11*22(33)44_55+");
00179 
00180   TestCanonicalizeHelper("", "");
00181   TestCanonicalizeHelper("http://www.google.com", "http://www.google.com");
00182   TestCanonicalizeHelper("http://%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/",
00183                          "http://168.188.99.26/.secure/www.ebay.com/");
00184   TestCanonicalizeHelper("http://195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/",
00185                          "http://195.127.0.11/uploads/%20%20%20%20/.verify/.eBaysecure=updateuserdataxplimnbqmn-xplmvalidateinfoswqpcmlx=hgplmcx/");
00186 }
00187 
00188 int main(int argc, char **argv)
00189 {
00190   TestUnescape();
00191   TestEnc();
00192   TestCanonicalize();
00193   printf("%d of %d tests passed\n", gPassedTests, gTotalTests);
00194   return (gPassedTests != gTotalTests);
00195 }