summaryrefslogtreecommitdiff
path: root/toolkit/components/url-classifier/tests/unittests.xul
blob: 0c9ce898bcf3f4b3b2afc483a8dcc682b9fcdd54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?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>