<!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>
