summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_classifier_match.html
blob: f6ea460be181e3b5f2afc34f0ba0c3178ceb1466 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test the URI Classifier Matched Info (bug 1288633) </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 class="testbody" type="application/javascript">
var Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;
var Cr = SpecialPowers.Cr;

var inputDatas = [
  { url: "malware.mochi.test/",
    db: "mochi-block-simple",
  },
  { url: "malware1.mochi.test/",
    db: "mochi1-block-simple",
  },
  { url: "malware1.mochi.test/",
    db: "mochi1-malware-simple",
    provider: "mozilla",
  },
  { url: "malware2.mochi.test/",
    db: "mochi2-unwanted-simple",
    provider: "mozilla",
  },
  { url: "malware2.mochi.test/",
    db: "mochi2-malware-simple",
    provider: "mozilla",
  },
  { url: "malware3.mochi.test/",
    db: "mochig3-malware-simple",
    provider: "google",
  },
  { url: "malware3.mochi.test/",
    db: "mochim3-malware-simple",
    provider: "mozilla",
  },
];

function hash(str) {
  let hasher = Cc["@mozilla.org/security/hash;1"]
                               .createInstance(Ci.nsICryptoHash);

  let data = new TextEncoder().encode(str);
  hasher.init(hasher.SHA256);
  hasher.update(data, data.length);

  return hasher.finish(false);
}

var testDatas = [
  // Match empty provider
  { url: "http://malware.mochi.test",
    expect: { error: Cr.NS_ERROR_BLOCKED_URI,
              table: "mochi-block-simple",
              provider: "",
              fullhash: (function() {
                return hash("malware.mochi.test/");
              })(),
            },
  },
  // Match multiple tables, only one has valid provider
  { url: "http://malware1.mochi.test",
    expect: { error: Cr.NS_ERROR_MALWARE_URI,
              table: "mochi1-malware-simple",
              provider: "mozilla",
              fullhash: (function() {
                return hash("malware1.mochi.test/");
              })(),
            },
  },
  // Match multiple tables, handle order
  { url: "http://malware2.mochi.test",
    expect: { error: Cr.NS_ERROR_MALWARE_URI,
              table: "mochi2-malware-simple",
              provider: "mozilla",
              fullhash: (function() {
                return hash("malware2.mochi.test/");
              })(),
            },
  },
  // Match multiple tables, handle order
  { url: "http://malware3.mochi.test",
    expect: { error: Cr.NS_ERROR_MALWARE_URI,
              table: "mochig3-malware-simple",
              provider: "google",
              fullhash: (function() {
                return hash("malware3.mochi.test/");
              })(),
            },
  },

];

SimpleTest.waitForExplicitFinish();

function setupTestData(datas) {
  let prefValues = {};
  for (let data of datas) {
    if (!data.provider) {
      continue;
    }
    let providerPref = "browser.safebrowsing.provider." + data.provider + ".lists";
    let prefValue;
    if (!prefValues[providerPref]) {
      prefValue = data.db;
    } else {
      prefValue = prefValues[providerPref] + "," + data.db;
    }
    prefValues[providerPref] = prefValue;
  }

  // Convert map to array
  let prefArray = [];
  for (var pref in prefValues) {
    prefArray.push([pref, prefValues[pref]]);
  }

  let activeTablePref = "urlclassifier.malwareTable";
  let activeTable = SpecialPowers.getCharPref(activeTablePref);
  for (let data of datas) {
      activeTable += "," + data.db;
  }
  prefArray.push([activeTablePref, activeTable]);

  return SpecialPowers.pushPrefEnv({set: prefArray});
}

function runTest() {
  return new Promise(resolve => {
    function runNextTest() {
      if (!testDatas.length) {
        resolve();
        return;
      }
      let test = testDatas.shift();
      let uri = SpecialPowers.Services.io.newURI(test.url);
      let prin = SpecialPowers.Services.scriptSecurityManager.createContentPrincipal(uri, {});
      SpecialPowers.doUrlClassify(prin, function(errorCode, table, provider, fullhash) {
        is(errorCode, test.expect.error, `Test url ${test.url} correct error`);
        is(table, test.expect.table, `Test url ${test.url} correct table`);
        is(provider, test.expect.provider, `Test url ${test.url} correct provider`);
        is(fullhash, btoa(test.expect.fullhash), `Test url ${test.url} correct full hash`);
        runNextTest();
      });
    }
    runNextTest();
  });
}

SpecialPowers.pushPrefEnv(
  {"set": [["browser.safebrowsing.malware.enabled", true]]},
  function() {
    classifierHelper.waitForInit()
      .then(() => setupTestData(inputDatas))
      .then(() => classifierHelper.addUrlToDB(inputDatas))
      .then(runTest)
      .then(function() {
        SimpleTest.finish();
      }).catch(function(e) {
        ok(false, "Some tests failed with error " + e);
        SimpleTest.finish();
      });
  });

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