toolkit/components/url-classifier/tests/unittests.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/url-classifier/tests/unittests.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<window id="PROT_unittest"
     1.6 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 
     1.7 +        onload="onProtUnittestLoad();"
     1.8 +        title="prot unittests">
     1.9 +
    1.10 +<script><![CDATA[
    1.11 +  const Cc = Components.classes;
    1.12 +  const Ci = Components.interfaces;
    1.13 +
    1.14 +  function G_Debug(zone, s) {
    1.15 +    var label = document.createElement('label');
    1.16 +    var txt = "[" + zone + "] " + s;
    1.17 +    label.appendChild(document.createTextNode(txt));
    1.18 +
    1.19 +    document.documentElement.appendChild(label);
    1.20 +  }
    1.21 +  
    1.22 +  function G_Assert(zone, cond, msg) {
    1.23 +    if (!cond) {
    1.24 +      G_Debug(zone, msg);
    1.25 +      throw msg;
    1.26 +    }
    1.27 +  }
    1.28 +
    1.29 +  function ProtectionTableTests() {
    1.30 +    var z = "trtable UNITTEST";
    1.31 +
    1.32 +    G_Debug(z, "Starting");  
    1.33 +
    1.34 +    var url = "http://www.yahoo.com?foo=bar";
    1.35 +    var url2 = "http://168.188.99.26/.secure/www.ebay.com/";
    1.36 +    var urlTable = Cc['@mozilla.org/url-classifier/table;1?type=url']
    1.37 +                     .createInstance(Ci.nsIUrlClassifierTable);
    1.38 +    urlTable.insert(url, "1");
    1.39 +    urlTable.insert(url2, "1");
    1.40 +    G_Assert(z, urlTable.exists(url), "URL lookups broken");
    1.41 +    G_Assert(z, !urlTable.exists("about:config"), "about:config breaks domlook");
    1.42 +    G_Assert(z, urlTable.exists(url2), "URL lookups broken");
    1.43 +    G_Assert(z, urlTable.exists("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/") == true,
    1.44 +             "URL Canonicalization broken");
    1.45 +    G_Assert(z, urlTable.count == 2, 'urlTable: wrong size');
    1.46 +
    1.47 +    var dom1 = "bar.com";
    1.48 +    var dom2 = "amazon.co.uk";
    1.49 +    var dom3 = "127.0.0.1";
    1.50 +    var domainTable = Cc['@mozilla.org/url-classifier/table;1?type=domain']
    1.51 +                        .createInstance(Ci.nsIUrlClassifierTable);
    1.52 +    domainTable.insert(dom1, "1");
    1.53 +    domainTable.insert(dom2, "1");
    1.54 +    domainTable.insert(dom3, "1");
    1.55 +    G_Assert(z, domainTable.exists("http://www.bar.com/?zaz=asdf#url"),
    1.56 +             "Domain lookups broken (single dot)");
    1.57 +    G_Assert(z, domainTable.exists("http://www.amazon.co.uk/?z=af#url"),
    1.58 +             "Domain lookups broken (two dots)");
    1.59 +    G_Assert(z, domainTable.exists("http://127.0.0.1/?z=af#url"),
    1.60 +             "Domain lookups broken (IP)");
    1.61 +    G_Assert(z, domainTable.count == 3, 'domainTable: wrong size');
    1.62 +
    1.63 +    var site1 = "google.com/safebrowsing/";
    1.64 +    var site2 = "www.foo.bar/";
    1.65 +    var site3 = "127.0.0.1/";
    1.66 +    var siteTable = Cc['@mozilla.org/url-classifier/table;1?type=site']
    1.67 +                      .createInstance(Ci.nsIUrlClassifierTable);
    1.68 +    siteTable.insert(site1, "1");
    1.69 +    siteTable.insert(site2, "1");
    1.70 +    siteTable.insert(site3, "1");
    1.71 +    G_Assert(z, siteTable.exists("http://www.google.com/safebrowsing/1.php"),
    1.72 +             "Site lookups broken - reducing");
    1.73 +    G_Assert(z, siteTable.exists("http://www.foo.bar/some/random/path"),
    1.74 +             "Site lookups broken - fqdn");
    1.75 +    G_Assert(z, siteTable.exists("http://127.0.0.1/something?hello=1"),
    1.76 +             "Site lookups broken - IP");
    1.77 +    G_Assert(z, !siteTable.exists("http://www.google.com/search/"),
    1.78 +             "Site lookups broken - overreaching");
    1.79 +    G_Assert(z, siteTable.count == 3, 'siteTable: wrong size');
    1.80 +
    1.81 +    var url1 = "http://poseidon.marinet.gr/~eleni/eBay/index.php";
    1.82 +    var domainHash = "01844755C8143C4579BB28DD59C23747";
    1.83 +    var enchashTable = Cc['@mozilla.org/url-classifier/table;1?type=enchash']
    1.84 +                         .createInstance(Ci.nsIUrlClassifierTable);
    1.85 +    enchashTable.insert(domainHash, "bGtEQWJuMl9FA3Kl5RiXMpgFU8nDJl9J0hXjUck9+"
    1.86 +                                    + "mMUQwAN6llf0gJeY5DIPPc2f+a8MSBFJN17ANGJ"
    1.87 +                                    + "Zl5oZVsQfSW4i12rlScsx4tweZAE");
    1.88 +    G_Assert(z, enchashTable.exists(url1), 'enchash lookup failed');
    1.89 +    G_Assert(z, !enchashTable.exists(url1 + '/foo'),
    1.90 +             "enchash lookup broken - overreaching");
    1.91 +    G_Assert(z, enchashTable.count == 1, 'enchashTable: wrong size');
    1.92 +
    1.93 +    // TODO: test replace
    1.94 +    G_Debug(z, "PASSED");
    1.95 +  }
    1.96 +  
    1.97 +  function ProtectionListManagerTests() {
    1.98 +    var z = "listmanager UNITTEST";
    1.99 +    G_Debug(z, "Starting");
   1.100 +
   1.101 +    // test lookup and register
   1.102 +    var listManagerInst = Cc["@mozilla.org/url-classifier/listmanager;1"]
   1.103 +                            .createInstance(Ci.nsIUrlListManager);
   1.104 +    var listName = 'foo-bar-url';
   1.105 +    listManagerInst.registerTable(listName, false);
   1.106 +    listManagerInst.safeInsert(listName, 'test', '1');
   1.107 +    G_Assert(z, listManagerInst.safeExists(listName, 'test'),
   1.108 +                'insert/exist failed');
   1.109 +
   1.110 +    // test serialization
   1.111 +    var baseName = (new Date().getTime()) + ".tmp";
   1.112 +    var tempDir = Cc["@mozilla.org/file/directory_service;1"]
   1.113 +                  .getService(Ci.nsIProperties)
   1.114 +                  .get("TmpD", Ci.nsILocalFile);
   1.115 +    tempDir.append(baseName);
   1.116 +    tempDir.createUnique(tempDir.DIRECTORY_TYPE, 0744);
   1.117 +
   1.118 +    var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
   1.119 +                        .getService(Ci.nsIUrlListManager);
   1.120 +    listManager.setAppDir(tempDir);
   1.121 +    
   1.122 +    var data = "";
   1.123 +
   1.124 +    var set1Name = "test1-foo-domain";
   1.125 +    data += "[" + set1Name + " 1.2]\n";
   1.126 +    var set1 = {};
   1.127 +    for (var i = 0; i < 10; i++) {
   1.128 +      set1["http://" + i + ".com"] = 1;
   1.129 +      data += "+" + i + ".com\t1\n";
   1.130 +    }
   1.131 +
   1.132 +    data += "\n";
   1.133 +    var set2Name = "test2-foo-domain";
   1.134 +    // TODO must have blank line
   1.135 +    data += "\n[" + set2Name + " 1.7]\n";
   1.136 +    var set2 = {};
   1.137 +    for (var i = 0; i < 5; i++) {
   1.138 +      set2["http://" + i + ".com"] = 1;
   1.139 +      data += "+" + i + ".com\t1\n";
   1.140 +    }
   1.141 +
   1.142 +    function deserialized(tablesKnown, tablesData) {
   1.143 +      listManager.wrappedJSObject.dataReady(tablesKnown, tablesData);
   1.144 +
   1.145 +      var file = tempDir.clone();
   1.146 +      file.append(set1Name + ".sst");
   1.147 +      G_Assert(z, file.exists() && file.isFile() && file.isReadable(), 
   1.148 +               "Failed to write out: " + file.path);
   1.149 +      
   1.150 +      file = tempDir.clone();
   1.151 +      file.append(set2Name + ".sst");
   1.152 +      G_Assert(z, file.exists() && file.isFile() && file.isReadable(), 
   1.153 +               "Failed to write out: " + file.path);
   1.154 +      
   1.155 +      // now try to read them back from disk
   1.156 +      listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
   1.157 +                       .createInstance(Ci.nsIUrlListManager);
   1.158 +      listManager.setAppDir(tempDir);
   1.159 +      var tables = [ set1Name, set2Name ];
   1.160 +      listManager.enableUpdate(set1Name);
   1.161 +      listManager.enableUpdate(set2Name);
   1.162 +      listManager.wrappedJSObject.readDataFiles();
   1.163 +      
   1.164 +      // assert that the values match
   1.165 +      for (var prop in set1) {
   1.166 +        G_Assert(z, 
   1.167 +                 listManager.wrappedJSObject.tablesData[set1Name].exists(prop), 
   1.168 +                 "Couldn't find member " + prop + "of set1 from disk.");
   1.169 +      }
   1.170 +      
   1.171 +      for (var prop in set2) {
   1.172 +        G_Assert(z,
   1.173 +                 listManager.wrappedJSObject.tablesData[set2Name].exists(prop), 
   1.174 +                 "Couldn't find member " + prop + "of set2 from disk.");
   1.175 +      }
   1.176 +
   1.177 +      tempDir.remove(true);
   1.178 +      
   1.179 +      G_Debug(z, "PASSED");
   1.180 +    };
   1.181 +
   1.182 +    // Use the unwrapped object for the unittest
   1.183 +    listManager.wrappedJSObject.deserialize_(data, deserialized);
   1.184 +  }
   1.185 +
   1.186 +  function onProtUnittestLoad() {
   1.187 +    ProtectionTableTests();
   1.188 +    ProtectionListManagerTests();
   1.189 +  }
   1.190 +]]></script>
   1.191 +</window>

mercurial