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