Bug 1407879 - Add testcase for login reputation service. r=francois
authorDimiL <dlee@mozilla.com>
Thu, 04 Jan 2018 09:14:15 +0800
changeset 452578 b3f26bc2b5a90ca8f913ebee3b1e130901287fa6
parent 452577 bb521dc27954a4d583f1407a0fdd269e7ae15560
child 452579 f0fc447f97238c68f4482cdf9f02a3ca9e96c1ee
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1407879
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1407879 - Add testcase for login reputation service. r=francois MozReview-Commit-ID: HhvPqTTz7pg
toolkit/components/reputationservice/test/unit/test_login_rep.js
toolkit/components/reputationservice/test/unit/xpcshell.ini
toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
new file mode 100644
--- /dev/null
+++ b/toolkit/components/reputationservice/test/unit/test_login_rep.js
@@ -0,0 +1,159 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+Cu.import("resource://gre/modules/NetUtil.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+// LoginReputationService
+const gLoginRep = Cc["@mozilla.org/reputationservice/login-reputation-service;1"].
+                  getService(Ci.nsILoginReputationService);
+
+let gListManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
+                     .getService(Ci.nsIUrlListManager);
+
+var gHttpServ = null;
+
+const whitelistedURI = "http://foo:bar@whitelisted.com/index.htm#junk";
+const exampleURI = "http://user:password@example.com/i.html?foo=bar";
+
+const LOCAL_WHITELIST_DATA = {
+  tableName: "test-passwordwhite-proto",
+  providerName: "test",
+  updateUrl: "http://localhost:5555/safebrowsing/update?",
+  gethashUrl: "",
+};
+
+add_task(async function test_setup() {
+
+  // Enable login reputation service
+  Services.prefs.setBoolPref("browser.safebrowsing.passwords.enabled", true);
+  gLoginRep.init();
+
+  // Setup local whitelist table.
+  Services.prefs.setCharPref("urlclassifier.passwordAllowTable", "test-passwordwhite-proto");
+  gListManager.registerTable(LOCAL_WHITELIST_DATA.tableName,
+                             LOCAL_WHITELIST_DATA.providerName,
+                             LOCAL_WHITELIST_DATA.updateUrl,
+                             LOCAL_WHITELIST_DATA.gethashUrl);
+
+  registerCleanupFunction(function() {
+    gListManager.unregisterTable(LOCAL_WHITELIST_DATA.tableName);
+
+    Services.prefs.clearUserPref("browser.safebrowsing.passwords.enabled");
+    Services.prefs.clearUserPref("urlclassifier.passwordAllowTable");
+  });
+});
+
+add_test(function test_setup_local_whitelist() {
+  // Setup the http server for SafeBrowsing update, so we can save SAFE entries
+  // to SafeBrowsing database while update.
+  gHttpServ = new HttpServer();
+  gHttpServ.registerDirectory("/", do_get_cwd());
+  gHttpServ.registerPathHandler("/safebrowsing/update", function(request, response) {
+    response.setHeader("Content-Type",
+                       "application/vnd.google.safebrowsing-update", false);
+
+    response.setStatusLine(request.httpVersion, 200, "OK");
+
+    // The protobuf binary represention of response:
+    //
+    // [
+    //   {
+    //     'threat_type': 8, // CSD_WHITELIST
+    //     'response_type': 2, // FULL_UPDATE
+    //     'new_client_state': 'sta\x00te', // NEW_CLIENT_STATE
+    //     'checksum': { "sha256": CHECKSUM }, // CHECKSUM
+    //     'additions': { 'compression_type': RAW,
+    //                    'prefix_size': 32,
+    //                    'raw_hashes': "whitelisted.com/index.htm"}
+    //   }
+    // ]
+    //
+    let content = "\x0A\x36\x08\x08\x20\x02\x2A\x28\x08\x01\x12\x24\x08" +
+                  "\x20\x12\x20\x0F\xE4\x66\xBB\xDD\x34\xAB\x1E\xF7\x8F" +
+                  "\xDD\x9D\x8C\xF8\x9F\x4E\x42\x97\x92\x86\x02\x03\xE0" +
+                  "\xE9\x60\xBD\xD6\x3A\x85\xCD\x08\xD0\x3A\x06\x73\x74" +
+                  "\x61\x00\x74\x65\x12\x04\x08\x0C\x10\x0A";
+
+    response.bodyOutputStream.write(content, content.length);
+  });
+
+  gHttpServ.start(5555);
+
+  // Trigger the update once http server is ready.
+  Services.obs.addObserver(function(aSubject, aTopic, aData) {
+    if (aData.startsWith("success")) {
+      run_next_test();
+    } else {
+      do_throw("update fail");
+    }
+  }, "safebrowsing-update-finished");
+  gListManager.forceUpdates(LOCAL_WHITELIST_DATA.tableName);
+
+  registerCleanupFunction(function() {
+    return (async function() {
+      await new Promise(resolve => {
+        gHttpServ.stop(resolve);
+      });
+    })();
+  });
+});
+
+add_test(function test_disable() {
+  Services.prefs.setBoolPref("browser.safebrowsing.passwords.enabled", false);
+
+  gLoginRep.queryReputation({
+    formURI: NetUtil.newURI("http://example.com"),
+  }, {
+    onComplete(aStatus, aVerdict) {
+      Assert.equal(aStatus, Cr.NS_ERROR_ABORT);
+      Assert.equal(aVerdict, Ci.nsILoginReputationVerdictType.UNSPECIFIED);
+
+      Services.prefs.setBoolPref("browser.safebrowsing.passwords.enabled", true);
+
+      run_next_test();
+    }
+  });
+});
+
+add_test(function test_nullQuery() {
+  try {
+    gLoginRep.queryReputation(null, {
+      onComplete(aStatus, aVerdict) {}
+    });
+    do_throw("Query parameter cannot be null");
+  } catch (ex) {
+    Assert.equal(ex.result, Cr.NS_ERROR_INVALID_POINTER);
+
+    run_next_test();
+  }
+});
+
+add_test(function test_local_whitelist() {
+  gLoginRep.queryReputation({
+    formURI: NetUtil.newURI(whitelistedURI),
+  }, {
+    onComplete(aStatus, aVerdict) {
+      Assert.equal(aStatus, Cr.NS_OK);
+      Assert.equal(aVerdict, Ci.nsILoginReputationVerdictType.SAFE);
+
+      run_next_test();
+    }
+  });
+});
+
+add_test(function test_notin_local_whitelist() {
+  gLoginRep.queryReputation({
+    formURI: NetUtil.newURI(exampleURI),
+  }, {
+    onComplete(aStatus, aVerdict) {
+      Assert.equal(aStatus, Cr.NS_OK);
+      Assert.equal(aVerdict, Ci.nsILoginReputationVerdictType.UNSPECIFIED);
+
+      run_next_test();
+    }
+  });
+});
--- a/toolkit/components/reputationservice/test/unit/xpcshell.ini
+++ b/toolkit/components/reputationservice/test/unit/xpcshell.ini
@@ -6,8 +6,9 @@ support-files =
   data/block_digest.chunk
   data/signed_win.exe
 
 [test_app_rep.js]
 [test_app_rep_windows.js]
 skip-if = os != "win"
 [test_app_rep_maclinux.js]
 skip-if = os == "win"
+[test_login_rep.js]
--- a/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierUtils.cpp
@@ -262,16 +262,17 @@ static const struct {
   { "goog-downloadwhite-proto", CSD_DOWNLOAD_WHITELIST},  // 9
 
   // For login reputation
   { "goog-passwordwhite-proto", CSD_WHITELIST}, // 8
 
   // For testing purpose.
   { "test-phish-proto",    SOCIAL_ENGINEERING_PUBLIC}, // 2
   { "test-unwanted-proto", UNWANTED_SOFTWARE},         // 3
+  { "test-passwordwhite-proto", CSD_WHITELIST},        // 8
 };
 
 NS_IMETHODIMP
 nsUrlClassifierUtils::ConvertThreatTypeToListNames(uint32_t aThreatType,
                                                    nsACString& aListNames)
 {
   for (uint32_t i = 0; i < ArrayLength(THREAT_TYPE_CONV_TABLE); i++) {
     if (aThreatType == THREAT_TYPE_CONV_TABLE[i].mThreatType) {