summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/test_safebrowsing_bug1272239.html
blob: 475145591dca014cebea35bbd6f7e6698bb8d4a2 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1272239 - Only tables with provider could register gethash url in listmanager.</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/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">

var Cc = SpecialPowers.Cc;
var Ci = SpecialPowers.Ci;

// List all the tables
const prefs = [
  "urlclassifier.phishTable",
  "urlclassifier.malwareTable",
  "urlclassifier.downloadBlockTable",
  "urlclassifier.downloadAllowTable",
  "urlclassifier.trackingTable",
  "urlclassifier.trackingWhitelistTable",
  "urlclassifier.blockedTable",
];

// Get providers
var providers = {};

var branch = SpecialPowers.Services.prefs.getBranch("browser.safebrowsing.provider.");
var children = branch.getChildList("");

for (var child of children) {
  var prefComponents =  child.split(".");
  var providerName = prefComponents[0];
  providers[providerName] = {};
}

// Get lists from |browser.safebrowsing.provider.PROVIDER_NAME.lists| preference.
var listsWithProvider = [];
var listsToProvider = [];
for (let provider in providers) {
  let pref = "browser.safebrowsing.provider." + provider + ".lists";
  let list = SpecialPowers.getCharPref(pref).split(",");

  listsToProvider = listsToProvider.concat(list.map( () => { return provider; }));
  listsWithProvider = listsWithProvider.concat(list);
}

// Get all the lists
var lists = [];
for (let pref of prefs) {
  lists = lists.concat(SpecialPowers.getCharPref(pref).split(","));
}

var listmanager = Cc["@mozilla.org/url-classifier/listmanager;1"].
                  getService(Ci.nsIUrlListManager);

let googleKey = SpecialPowers.Services.urlFormatter.formatURL("%GOOGLE_SAFEBROWSING_API_KEY%").trim();

for (let list of lists) {
  if (!list)
    continue;

  // For lists having a provider, it should have a correct gethash url
  // For lists without a provider, for example, moztest-malware-simple, it should not
  // have a gethash url.
  var url = listmanager.getGethashUrl(list);
  var index = listsWithProvider.indexOf(list);
  if (index >= 0) {
    let provider = listsToProvider[index];
    let pref = "browser.safebrowsing.provider." + provider + ".gethashURL";
    if ((provider == "google" || provider == "google4") &&
        (!googleKey || googleKey == "no-google-safebrowsing-api-key")) {
      is(url, "", "getHash url of " + list + " should be empty");
    } else {
      is(url, SpecialPowers.getCharPref(pref), list + " matches its gethash url");
    }
  } else {
    is(url, "", list + " should not have a gethash url");
  }
}

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