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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <?xml version="1.0"?>
michael@0 2 <window id="PROT_unittest"
michael@0 3 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 4 onload="onProtUnittestLoad();"
michael@0 5 title="prot unittests">
michael@0 6
michael@0 7 <script><![CDATA[
michael@0 8 const Cc = Components.classes;
michael@0 9 const Ci = Components.interfaces;
michael@0 10
michael@0 11 function G_Debug(zone, s) {
michael@0 12 var label = document.createElement('label');
michael@0 13 var txt = "[" + zone + "] " + s;
michael@0 14 label.appendChild(document.createTextNode(txt));
michael@0 15
michael@0 16 document.documentElement.appendChild(label);
michael@0 17 }
michael@0 18
michael@0 19 function G_Assert(zone, cond, msg) {
michael@0 20 if (!cond) {
michael@0 21 G_Debug(zone, msg);
michael@0 22 throw msg;
michael@0 23 }
michael@0 24 }
michael@0 25
michael@0 26 function ProtectionTableTests() {
michael@0 27 var z = "trtable UNITTEST";
michael@0 28
michael@0 29 G_Debug(z, "Starting");
michael@0 30
michael@0 31 var url = "http://www.yahoo.com?foo=bar";
michael@0 32 var url2 = "http://168.188.99.26/.secure/www.ebay.com/";
michael@0 33 var urlTable = Cc['@mozilla.org/url-classifier/table;1?type=url']
michael@0 34 .createInstance(Ci.nsIUrlClassifierTable);
michael@0 35 urlTable.insert(url, "1");
michael@0 36 urlTable.insert(url2, "1");
michael@0 37 G_Assert(z, urlTable.exists(url), "URL lookups broken");
michael@0 38 G_Assert(z, !urlTable.exists("about:config"), "about:config breaks domlook");
michael@0 39 G_Assert(z, urlTable.exists(url2), "URL lookups broken");
michael@0 40 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,
michael@0 41 "URL Canonicalization broken");
michael@0 42 G_Assert(z, urlTable.count == 2, 'urlTable: wrong size');
michael@0 43
michael@0 44 var dom1 = "bar.com";
michael@0 45 var dom2 = "amazon.co.uk";
michael@0 46 var dom3 = "127.0.0.1";
michael@0 47 var domainTable = Cc['@mozilla.org/url-classifier/table;1?type=domain']
michael@0 48 .createInstance(Ci.nsIUrlClassifierTable);
michael@0 49 domainTable.insert(dom1, "1");
michael@0 50 domainTable.insert(dom2, "1");
michael@0 51 domainTable.insert(dom3, "1");
michael@0 52 G_Assert(z, domainTable.exists("http://www.bar.com/?zaz=asdf#url"),
michael@0 53 "Domain lookups broken (single dot)");
michael@0 54 G_Assert(z, domainTable.exists("http://www.amazon.co.uk/?z=af#url"),
michael@0 55 "Domain lookups broken (two dots)");
michael@0 56 G_Assert(z, domainTable.exists("http://127.0.0.1/?z=af#url"),
michael@0 57 "Domain lookups broken (IP)");
michael@0 58 G_Assert(z, domainTable.count == 3, 'domainTable: wrong size');
michael@0 59
michael@0 60 var site1 = "google.com/safebrowsing/";
michael@0 61 var site2 = "www.foo.bar/";
michael@0 62 var site3 = "127.0.0.1/";
michael@0 63 var siteTable = Cc['@mozilla.org/url-classifier/table;1?type=site']
michael@0 64 .createInstance(Ci.nsIUrlClassifierTable);
michael@0 65 siteTable.insert(site1, "1");
michael@0 66 siteTable.insert(site2, "1");
michael@0 67 siteTable.insert(site3, "1");
michael@0 68 G_Assert(z, siteTable.exists("http://www.google.com/safebrowsing/1.php"),
michael@0 69 "Site lookups broken - reducing");
michael@0 70 G_Assert(z, siteTable.exists("http://www.foo.bar/some/random/path"),
michael@0 71 "Site lookups broken - fqdn");
michael@0 72 G_Assert(z, siteTable.exists("http://127.0.0.1/something?hello=1"),
michael@0 73 "Site lookups broken - IP");
michael@0 74 G_Assert(z, !siteTable.exists("http://www.google.com/search/"),
michael@0 75 "Site lookups broken - overreaching");
michael@0 76 G_Assert(z, siteTable.count == 3, 'siteTable: wrong size');
michael@0 77
michael@0 78 var url1 = "http://poseidon.marinet.gr/~eleni/eBay/index.php";
michael@0 79 var domainHash = "01844755C8143C4579BB28DD59C23747";
michael@0 80 var enchashTable = Cc['@mozilla.org/url-classifier/table;1?type=enchash']
michael@0 81 .createInstance(Ci.nsIUrlClassifierTable);
michael@0 82 enchashTable.insert(domainHash, "bGtEQWJuMl9FA3Kl5RiXMpgFU8nDJl9J0hXjUck9+"
michael@0 83 + "mMUQwAN6llf0gJeY5DIPPc2f+a8MSBFJN17ANGJ"
michael@0 84 + "Zl5oZVsQfSW4i12rlScsx4tweZAE");
michael@0 85 G_Assert(z, enchashTable.exists(url1), 'enchash lookup failed');
michael@0 86 G_Assert(z, !enchashTable.exists(url1 + '/foo'),
michael@0 87 "enchash lookup broken - overreaching");
michael@0 88 G_Assert(z, enchashTable.count == 1, 'enchashTable: wrong size');
michael@0 89
michael@0 90 // TODO: test replace
michael@0 91 G_Debug(z, "PASSED");
michael@0 92 }
michael@0 93
michael@0 94 function ProtectionListManagerTests() {
michael@0 95 var z = "listmanager UNITTEST";
michael@0 96 G_Debug(z, "Starting");
michael@0 97
michael@0 98 // test lookup and register
michael@0 99 var listManagerInst = Cc["@mozilla.org/url-classifier/listmanager;1"]
michael@0 100 .createInstance(Ci.nsIUrlListManager);
michael@0 101 var listName = 'foo-bar-url';
michael@0 102 listManagerInst.registerTable(listName, false);
michael@0 103 listManagerInst.safeInsert(listName, 'test', '1');
michael@0 104 G_Assert(z, listManagerInst.safeExists(listName, 'test'),
michael@0 105 'insert/exist failed');
michael@0 106
michael@0 107 // test serialization
michael@0 108 var baseName = (new Date().getTime()) + ".tmp";
michael@0 109 var tempDir = Cc["@mozilla.org/file/directory_service;1"]
michael@0 110 .getService(Ci.nsIProperties)
michael@0 111 .get("TmpD", Ci.nsILocalFile);
michael@0 112 tempDir.append(baseName);
michael@0 113 tempDir.createUnique(tempDir.DIRECTORY_TYPE, 0744);
michael@0 114
michael@0 115 var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
michael@0 116 .getService(Ci.nsIUrlListManager);
michael@0 117 listManager.setAppDir(tempDir);
michael@0 118
michael@0 119 var data = "";
michael@0 120
michael@0 121 var set1Name = "test1-foo-domain";
michael@0 122 data += "[" + set1Name + " 1.2]\n";
michael@0 123 var set1 = {};
michael@0 124 for (var i = 0; i < 10; i++) {
michael@0 125 set1["http://" + i + ".com"] = 1;
michael@0 126 data += "+" + i + ".com\t1\n";
michael@0 127 }
michael@0 128
michael@0 129 data += "\n";
michael@0 130 var set2Name = "test2-foo-domain";
michael@0 131 // TODO must have blank line
michael@0 132 data += "\n[" + set2Name + " 1.7]\n";
michael@0 133 var set2 = {};
michael@0 134 for (var i = 0; i < 5; i++) {
michael@0 135 set2["http://" + i + ".com"] = 1;
michael@0 136 data += "+" + i + ".com\t1\n";
michael@0 137 }
michael@0 138
michael@0 139 function deserialized(tablesKnown, tablesData) {
michael@0 140 listManager.wrappedJSObject.dataReady(tablesKnown, tablesData);
michael@0 141
michael@0 142 var file = tempDir.clone();
michael@0 143 file.append(set1Name + ".sst");
michael@0 144 G_Assert(z, file.exists() && file.isFile() && file.isReadable(),
michael@0 145 "Failed to write out: " + file.path);
michael@0 146
michael@0 147 file = tempDir.clone();
michael@0 148 file.append(set2Name + ".sst");
michael@0 149 G_Assert(z, file.exists() && file.isFile() && file.isReadable(),
michael@0 150 "Failed to write out: " + file.path);
michael@0 151
michael@0 152 // now try to read them back from disk
michael@0 153 listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
michael@0 154 .createInstance(Ci.nsIUrlListManager);
michael@0 155 listManager.setAppDir(tempDir);
michael@0 156 var tables = [ set1Name, set2Name ];
michael@0 157 listManager.enableUpdate(set1Name);
michael@0 158 listManager.enableUpdate(set2Name);
michael@0 159 listManager.wrappedJSObject.readDataFiles();
michael@0 160
michael@0 161 // assert that the values match
michael@0 162 for (var prop in set1) {
michael@0 163 G_Assert(z,
michael@0 164 listManager.wrappedJSObject.tablesData[set1Name].exists(prop),
michael@0 165 "Couldn't find member " + prop + "of set1 from disk.");
michael@0 166 }
michael@0 167
michael@0 168 for (var prop in set2) {
michael@0 169 G_Assert(z,
michael@0 170 listManager.wrappedJSObject.tablesData[set2Name].exists(prop),
michael@0 171 "Couldn't find member " + prop + "of set2 from disk.");
michael@0 172 }
michael@0 173
michael@0 174 tempDir.remove(true);
michael@0 175
michael@0 176 G_Debug(z, "PASSED");
michael@0 177 };
michael@0 178
michael@0 179 // Use the unwrapped object for the unittest
michael@0 180 listManager.wrappedJSObject.deserialize_(data, deserialized);
michael@0 181 }
michael@0 182
michael@0 183 function onProtUnittestLoad() {
michael@0 184 ProtectionTableTests();
michael@0 185 ProtectionListManagerTests();
michael@0 186 }
michael@0 187 ]]></script>
michael@0 188 </window>

mercurial