RetroZilla/toolkit/components/url-classifier/tests/unittests.xul
2015-10-20 23:03:22 -04:00

189 lines
7.0 KiB
XML

<?xml version="1.0"?>
<window id="PROT_unittest"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="onProtUnittestLoad();"
title="prot unittests">
<script><![CDATA[
const Cc = Components.classes;
const Ci = Components.interfaces;
function G_Debug(zone, s) {
var label = document.createElement('label');
var txt = "[" + zone + "] " + s;
label.appendChild(document.createTextNode(txt));
document.documentElement.appendChild(label);
}
function G_Assert(zone, cond, msg) {
if (!cond) {
G_Debug(zone, msg);
throw msg;
}
}
function ProtectionTableTests() {
var z = "trtable UNITTEST";
G_Debug(z, "Starting");
var url = "http://www.yahoo.com?foo=bar";
var url2 = "http://168.188.99.26/.secure/www.ebay.com/";
var urlTable = Cc['@mozilla.org/url-classifier/table;1?type=url']
.createInstance(Ci.nsIUrlClassifierTable);
urlTable.insert(url, "1");
urlTable.insert(url2, "1");
G_Assert(z, urlTable.exists(url), "URL lookups broken");
G_Assert(z, !urlTable.exists("about:config"), "about:config breaks domlook");
G_Assert(z, urlTable.exists(url2), "URL lookups broken");
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,
"URL Canonicalization broken");
G_Assert(z, urlTable.count == 2, 'urlTable: wrong size');
var dom1 = "bar.com";
var dom2 = "amazon.co.uk";
var dom3 = "127.0.0.1";
var domainTable = Cc['@mozilla.org/url-classifier/table;1?type=domain']
.createInstance(Ci.nsIUrlClassifierTable);
domainTable.insert(dom1, "1");
domainTable.insert(dom2, "1");
domainTable.insert(dom3, "1");
G_Assert(z, domainTable.exists("http://www.bar.com/?zaz=asdf#url"),
"Domain lookups broken (single dot)");
G_Assert(z, domainTable.exists("http://www.amazon.co.uk/?z=af#url"),
"Domain lookups broken (two dots)");
G_Assert(z, domainTable.exists("http://127.0.0.1/?z=af#url"),
"Domain lookups broken (IP)");
G_Assert(z, domainTable.count == 3, 'domainTable: wrong size');
var site1 = "google.com/safebrowsing/";
var site2 = "www.foo.bar/";
var site3 = "127.0.0.1/";
var siteTable = Cc['@mozilla.org/url-classifier/table;1?type=site']
.createInstance(Ci.nsIUrlClassifierTable);
siteTable.insert(site1, "1");
siteTable.insert(site2, "1");
siteTable.insert(site3, "1");
G_Assert(z, siteTable.exists("http://www.google.com/safebrowsing/1.php"),
"Site lookups broken - reducing");
G_Assert(z, siteTable.exists("http://www.foo.bar/some/random/path"),
"Site lookups broken - fqdn");
G_Assert(z, siteTable.exists("http://127.0.0.1/something?hello=1"),
"Site lookups broken - IP");
G_Assert(z, !siteTable.exists("http://www.google.com/search/"),
"Site lookups broken - overreaching");
G_Assert(z, siteTable.count == 3, 'siteTable: wrong size');
var url1 = "http://poseidon.marinet.gr/~eleni/eBay/index.php";
var domainHash = "01844755C8143C4579BB28DD59C23747";
var enchashTable = Cc['@mozilla.org/url-classifier/table;1?type=enchash']
.createInstance(Ci.nsIUrlClassifierTable);
enchashTable.insert(domainHash, "bGtEQWJuMl9FA3Kl5RiXMpgFU8nDJl9J0hXjUck9+"
+ "mMUQwAN6llf0gJeY5DIPPc2f+a8MSBFJN17ANGJ"
+ "Zl5oZVsQfSW4i12rlScsx4tweZAE");
G_Assert(z, enchashTable.exists(url1), 'enchash lookup failed');
G_Assert(z, !enchashTable.exists(url1 + '/foo'),
"enchash lookup broken - overreaching");
G_Assert(z, enchashTable.count == 1, 'enchashTable: wrong size');
// TODO: test replace
G_Debug(z, "PASSED");
}
function ProtectionListManagerTests() {
var z = "listmanager UNITTEST";
G_Debug(z, "Starting");
// test lookup and register
var listManagerInst = Cc["@mozilla.org/url-classifier/listmanager;1"]
.createInstance(Ci.nsIUrlListManager);
var listName = 'foo-bar-url';
listManagerInst.registerTable(listName, false);
listManagerInst.safeInsert(listName, 'test', '1');
G_Assert(z, listManagerInst.safeExists(listName, 'test'),
'insert/exist failed');
// test serialization
var baseName = (new Date().getTime()) + ".tmp";
var tempDir = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("TmpD", Ci.nsILocalFile);
tempDir.append(baseName);
tempDir.createUnique(tempDir.DIRECTORY_TYPE, 0744);
var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
.getService(Ci.nsIUrlListManager);
listManager.setAppDir(tempDir);
var data = "";
var set1Name = "test1-foo-domain";
data += "[" + set1Name + " 1.2]\n";
var set1 = {};
for (var i = 0; i < 10; i++) {
set1["http://" + i + ".com"] = 1;
data += "+" + i + ".com\t1\n";
}
data += "\n";
var set2Name = "test2-foo-domain";
// TODO must have blank line
data += "\n[" + set2Name + " 1.7]\n";
var set2 = {};
for (var i = 0; i < 5; i++) {
set2["http://" + i + ".com"] = 1;
data += "+" + i + ".com\t1\n";
}
function deserialized(tablesKnown, tablesData) {
listManager.wrappedJSObject.dataReady(tablesKnown, tablesData);
var file = tempDir.clone();
file.append(set1Name + ".sst");
G_Assert(z, file.exists() && file.isFile() && file.isReadable(),
"Failed to write out: " + file.path);
file = tempDir.clone();
file.append(set2Name + ".sst");
G_Assert(z, file.exists() && file.isFile() && file.isReadable(),
"Failed to write out: " + file.path);
// now try to read them back from disk
listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
.createInstance(Ci.nsIUrlListManager);
listManager.setAppDir(tempDir);
var tables = [ set1Name, set2Name ];
listManager.enableUpdate(set1Name);
listManager.enableUpdate(set2Name);
listManager.wrappedJSObject.readDataFiles();
// assert that the values match
for (var prop in set1) {
G_Assert(z,
listManager.wrappedJSObject.tablesData[set1Name].exists(prop),
"Couldn't find member " + prop + "of set1 from disk.");
}
for (var prop in set2) {
G_Assert(z,
listManager.wrappedJSObject.tablesData[set2Name].exists(prop),
"Couldn't find member " + prop + "of set2 from disk.");
}
tempDir.remove(true);
G_Debug(z, "PASSED");
};
// Use the unwrapped object for the unittest
listManager.wrappedJSObject.deserialize_(data, deserialized);
}
function onProtUnittestLoad() {
ProtectionTableTests();
ProtectionListManagerTests();
}
]]></script>
</window>