summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_classify_track.html
blob: 38f1c8a04bddbbb77b6aa80727bdab85435cf107 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1262406 - Track element doesn't use the URL classifier.</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="text/javascript">
  const PREF = "browser.safebrowsing.malware.enabled";
  const track_path = "tests/toolkit/components/url-classifier/tests/mochitest/basic.vtt";
  const malware_url = "http://malware.example.com/" + track_path;
  const validtrack_url = "http://mochi.test:8888/" + track_path;

  var video = document.createElement("video");
  video.src = "seek.webm";
  video.crossOrigin = "anonymous";

  document.body.appendChild(video);

  function testValidTrack() {
    SpecialPowers.setBoolPref(PREF, true);

    return new Promise(function(resolve, reject) {
      var track = document.createElement("track");
      track.track.mode = "hidden";
      track.src = validtrack_url;
      video.appendChild(track);

      function onload() {
        ok(true, "Track should be loaded when url is not in blocklist");
        finish();
      }

      function onerror() {
        ok(false, "Error while loading track");
        finish();
      }

      function finish() {
        track.removeEventListener("load", onload);
        track.removeEventListener("error", onerror);
        resolve();
      }

      track.addEventListener("load", onload);
      track.addEventListener("error", onerror);
    });
  }

  function testBlocklistTrackSafebrowsingOff() {
    SpecialPowers.setBoolPref(PREF, false);

    return new Promise(function(resolve, reject) {
      var track = document.createElement("track");
      track.track.mode = "hidden";
      track.src = malware_url;
      video.appendChild(track);

      function onload() {
        ok(true, "Track should be loaded when url is in blocklist and safebrowsing is off");
        finish();
      }

      function onerror() {
        ok(false, "Error while loading track");
        finish();
      }

      function finish() {
        track.removeEventListener("load", onload);
        track.removeEventListener("error", onerror);
        resolve();
      }

      track.addEventListener("load", onload);
      track.addEventListener("error", onerror);
    });
  }

  function testBlocklistTrackSafebrowsingOn() {
    SpecialPowers.setBoolPref(PREF, true);

    return new Promise(function(resolve, reject) {
      var track = document.createElement("track");
      track.track.mode = "hidden";
      // Add a query string parameter here to avoid url classifier bypass classify
      // because of cache.
      track.src = malware_url + "?testsbon";
      video.appendChild(track);

      function onload() {
        ok(false, "Unexpected result while loading track in blocklist");
        finish();
      }

      function onerror() {
        ok(true, "Track should not be loaded when url is in blocklist and safebrowsing is on");
        finish();
      }

      function finish() {
        track.removeEventListener("load", onload);
        track.removeEventListener("error", onerror);
        resolve();
      }

      track.addEventListener("load", onload);
      track.addEventListener("error", onerror);
    });
  }

  function cleanup() {
    SpecialPowers.clearUserPref(PREF);
  }

  function setup() {
    var testData = [
      { url: "malware.example.com/",
        db: "test-malware-simple",
      },
    ];

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

  function runTest() {
    Promise.resolve()
      .then(classifierHelper.waitForInit)
      .then(setup)
      .then(testValidTrack)
      .then(testBlocklistTrackSafebrowsingOff)
      .then(testBlocklistTrackSafebrowsingOn)
      .then(function() {
        SimpleTest.finish();
      }).catch(function(e) {
        ok(false, "Some test failed with error " + e);
        SimpleTest.finish();
      });
  }

  SimpleTest.waitForExplicitFinish();
  SimpleTest.registerCleanupFunction(cleanup);
  SpecialPowers.pushPrefEnv({"set": [
    ["media.webvtt.regions.enabled", true],
    ["urlclassifier.malwareTable", "test-malware-simple"],
  ]}, runTest);

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