summaryrefslogtreecommitdiffstats
path: root/toolkit/components/reputationservice/test/unit/test_login_rep.js
blob: 14f09189684cc2c013b482f7a419749c75eadc03 (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
180
181
182
183
/* -*- 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/. */

const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.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();
      },
    }
  );
});