summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_bug1254766.html
blob: 67dff80255cbe9f112d3eb624c5ea0f1e3e5be20 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1272239 - Test gethash.</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="classifierHelper.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>

<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">

<script src="head.js"></script>
<script class="testbody" type="text/javascript">
const MALWARE_LIST = "mochi-malware-simple";
const MALWARE_HOST1 = "malware.example.com/";
const MALWARE_HOST2 = "test1.example.com/";

const UNWANTED_LIST = "mochi-unwanted-simple";
const UNWANTED_HOST1 = "unwanted.example.com/";
const UNWANTED_HOST2 = "test2.example.com/";


const UNUSED_MALWARE_HOST = "unused.malware.com/";
const UNUSED_UNWANTED_HOST = "unused.unwanted.com/";

const GETHASH_URL =
  "http://mochi.test:8888/tests/toolkit/components/url-classifier/tests/mochitest/gethash.sjs";

var gPreGethashCounter = 0;
var gCurGethashCounter = 0;

var expectLoad = false;

function loadTestFrame() {
  return new Promise(function(resolve) {
    var iframe = document.createElement("iframe");
    iframe.setAttribute("src", "gethashFrame.html");
    document.body.appendChild(iframe);

    iframe.onload = function() {
      document.body.removeChild(iframe);
      resolve();
    };
  }).then(getGethashCounter);
}

function getGethashCounter() {
  return new Promise(function(resolve) {
    var xhr = new XMLHttpRequest;
    xhr.open("PUT", GETHASH_URL + "?gethashcount");
    xhr.setRequestHeader("Content-Type", "text/plain");
    xhr.onreadystatechange = function() {
      if (this.readyState == this.DONE) {
        gPreGethashCounter = gCurGethashCounter;
        gCurGethashCounter = parseInt(xhr.response);
        resolve();
      }
    };
    xhr.send();
  });
}

// setup function allows classifier send gethash request for test database
// also it calculate to fullhash for url and store those hashes in gethash sjs.
async function setup() {
  await classifierHelper.allowCompletion([MALWARE_LIST, UNWANTED_LIST], GETHASH_URL);

  return Promise.all([
    addCompletionToServer(MALWARE_LIST, MALWARE_HOST1, GETHASH_URL),
    addCompletionToServer(MALWARE_LIST, MALWARE_HOST2, GETHASH_URL),
    addCompletionToServer(UNWANTED_LIST, UNWANTED_HOST1, GETHASH_URL),
    addCompletionToServer(UNWANTED_LIST, UNWANTED_HOST2, GETHASH_URL),
  ]);
}

// Reset function in helper try to simulate the behavior we restart firefox
function reset() {
  return classifierHelper.resetDatabase()
    .catch(err => {
      ok(false, "Couldn't update classifier. Error code: " + err);
      // Abort test.
      SimpleTest.finish();
    });
}

function updateUnusedUrl() {
  var testData  = [
    { url: UNUSED_MALWARE_HOST,  db: MALWARE_LIST },
    { url: UNUSED_UNWANTED_HOST, db: UNWANTED_LIST },
  ];

  return classifierHelper.addUrlToDB(testData)
    .catch(err => {
      ok(false, "Couldn't update classifier. Error code: " + err);
      // Abort test.
      SimpleTest.finish();
    });
}

function addPrefixToDB() {
  return update(true);
}

function addCompletionToDB() {
  return update(false);
}

function update(prefix = false) {
  var length = prefix ? 4 : 32;
  var testData  = [
    { url: MALWARE_HOST1,  db: MALWARE_LIST,  len: length },
    { url: MALWARE_HOST2,  db: MALWARE_LIST,  len: length },
    { url: UNWANTED_HOST1, db: UNWANTED_LIST, len: length },
    { url: UNWANTED_HOST2, db: UNWANTED_LIST, len: length },
  ];

  return classifierHelper.addUrlToDB(testData)
    .catch(err => {
      ok(false, "Couldn't update classifier. Error code: " + err);
      // Abort test.
      SimpleTest.finish();
    });
}

// This testcase is to make sure gethash works:
// 1. Add prefixes to DB.
// 2. Load test frame contains malware & unwanted url, those urls should be blocked.
// 3. The second step should also trigger a gethash request since completions is not in
//    either cache or DB.
// 4. Load test frame again, since completions is stored in cache now, no gethash
//    request should be triggered.
function testGethash() {
  return Promise.resolve()
    .then(addPrefixToDB)
    .then(loadTestFrame)
    .then(() => {
       ok(gCurGethashCounter > gPreGethashCounter, "Gethash request is triggered.");
})
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(reset);
}

// This testcae is to make sure completions in update works:
// 1. Add completions to DB.
// 2. Load test frame, since completions is stored in DB, gethash request should
//    not be triggered.
function testUpdate() {
  return Promise.resolve()
    .then(addCompletionToDB)
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(reset);
}

// This testcase is to make sure an update request will not clear completions in DB:
// 1. Add completions to DB.
// 2. Load test frame to make sure completions is stored in database, in this case, gethash
//    should not be triggered.
// 3. Trigger an update, cache is cleared, but completions in DB should still remain.
// 4. Load test frame again, since completions is in DB, gethash request should not be triggered.
function testUpdateNotClearCompletions() {
  return Promise.resolve()
    .then(addCompletionToDB)
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(updateUnusedUrl)
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(reset);
}

// This testcase is to make sure completion store in DB will properly load after restarting.
// 1. Add completions to DB.
// 2. Simulate firefox restart by calling reloadDatabase.
// 3. Load test frame, since completions should be loaded from DB, no gethash request should
//    be triggered.
function testUpdateCompletionsAfterReload() {
  return Promise.resolve()
    .then(addCompletionToDB)
    .then(classifierHelper.reloadDatabase)
    // Call getTables to ensure the DB is fully reloaded before we load the test
    // frame.
    .then(classifierHelper.getTables)
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(reset);
}

// This testcase is to make sure cache will be cleared after restarting
// 1. Add prefixes to DB.
// 2. Load test frame, this should trigger a gethash request and completions will be stored in
//    cache.
// 3. Load test frame again, no gethash should be triggered because of cache.
// 4. Simulate firefox restart by calling reloadDatabase.
// 5. Load test frame again, since cache is cleared, gethash request should be triggered.
function testGethashCompletionsAfterReload() {
  return Promise.resolve()
    .then(addPrefixToDB)
    .then(loadTestFrame)
    .then(() => {
       ok(gCurGethashCounter > gPreGethashCounter, "Gethash request is triggered.");
})
    .then(loadTestFrame)
    .then(() => {
      ok(gCurGethashCounter == gPreGethashCounter, "Gethash request is not triggered.");
})
    .then(classifierHelper.reloadDatabase)
    // Call getTables to ensure the DB is fully reloaded before we load the test
    // frame.
    .then(classifierHelper.getTables)
    .then(loadTestFrame)
    .then(() => {
       ok(gCurGethashCounter > gPreGethashCounter, "Gethash request is triggered.");
})
    .then(reset);
}

function runTest() {
  Promise.resolve()
    .then(classifierHelper.waitForInit)
    .then(setup)
    .then(testGethash)
    .then(testUpdate)
    .then(testUpdateNotClearCompletions)
    .then(testUpdateCompletionsAfterReload)
    .then(testGethashCompletionsAfterReload)
    .then(function() {
      SimpleTest.finish();
    }).catch(function(e) {
      ok(false, "Some test failed with error " + e);
      SimpleTest.finish();
    });
}

SimpleTest.waitForExplicitFinish();

// 'network.predictor.enabled' is disabled because if other testcase load
// evil.js, evil.css ...etc resources, it may cause we load them from cache
// directly and bypass classifier check
SpecialPowers.pushPrefEnv({"set": [
  ["browser.safebrowsing.malware.enabled", true],
  ["urlclassifier.malwareTable", "mochi-malware-simple,mochi-unwanted-simple"],
  ["network.predictor.enabled", false],
  ["urlclassifier.gethash.timeout_ms", 30000],
]}, runTest);

</script>
</pre>
</body>
</html>