summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/unit/test_sdr.js
blob: e9e477efc5e6f21d856936519c9336aea6281c5f (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
"use strict";

// Tests various aspects of the nsISecretDecoderRing implementation.

do_get_profile();

let gSetPasswordShownCount = 0;

// Mock implementation of nsITokenPasswordDialogs.
const gTokenPasswordDialogs = {
  setPassword(ctx, tokenName) {
    gSetPasswordShownCount++;
    info(`setPassword() called; shown ${gSetPasswordShownCount} times`);
    info(`tokenName: ${tokenName}`);
    return false; // Returning false means "the user didn't cancel".
  },

  QueryInterface: ChromeUtils.generateQI(["nsITokenPasswordDialogs"]),
};

let gMockPrompter = {
  promptPassword(dialogTitle, text, password, checkMsg, checkValue) {
    // Returning false simulates the user canceling the password prompt.
    return false;
  },

  QueryInterface: ChromeUtils.generateQI(["nsIPrompt"]),
};

// Mock nsIWindowWatcher. PSM calls getNewPrompter on this to get an nsIPrompt
// to call promptPassword. We return the mock one, above.
let gWindowWatcher = {
  getNewPrompter: () => gMockPrompter,
  QueryInterface: ChromeUtils.generateQI(["nsIWindowWatcher"]),
};

add_task(function setup() {
  let windowWatcherCID = MockRegistrar.register(
    "@mozilla.org/embedcomp/window-watcher;1",
    gWindowWatcher
  );
  registerCleanupFunction(() => {
    MockRegistrar.unregister(windowWatcherCID);
  });
});

add_task(function testEncryptString() {
  let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
    Ci.nsISecretDecoderRing
  );

  // Test valid inputs for encryptString() and decryptString().
  let inputs = [
    "",
    " ", // First printable latin1 character (code point 32).
    "foo",
    "1234567890`~!@#$%^&*()-_=+{[}]|\\:;'\",<.>/?",
    "¡äöüÿ", // Misc + last printable latin1 character (code point 255).
    "aaa 一二三", // Includes Unicode with code points outside [0, 255].
  ];
  for (let input of inputs) {
    let converter = Cc[
      "@mozilla.org/intl/scriptableunicodeconverter"
    ].createInstance(Ci.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";

    let convertedInput = converter.ConvertFromUnicode(input);
    convertedInput += converter.Finish();

    let encrypted = sdr.encryptString(convertedInput);

    notEqual(
      convertedInput,
      encrypted,
      "Encrypted input should not just be the input itself"
    );

    try {
      atob(encrypted);
    } catch (e) {
      ok(false, `encryptString() should have returned Base64: ${e}`);
    }

    equal(
      convertedInput,
      sdr.decryptString(encrypted),
      "decryptString(encryptString(input)) should return input"
    );
  }

  // Test invalid inputs for decryptString().
  throws(
    () => sdr.decryptString("*"),
    /NS_ERROR_ILLEGAL_VALUE/,
    "decryptString() should throw if given non-Base64 input"
  );

  // Test calling changePassword() pops up the appropriate dialog.
  // Note: On Android, nsITokenPasswordDialogs is apparently not implemented,
  //       which also seems to prevent us from mocking out the interface.
  if (AppConstants.platform != "android") {
    let tokenPasswordDialogsCID = MockRegistrar.register(
      "@mozilla.org/nsTokenPasswordDialogs;1",
      gTokenPasswordDialogs
    );
    registerCleanupFunction(() => {
      MockRegistrar.unregister(tokenPasswordDialogsCID);
    });

    equal(
      gSetPasswordShownCount,
      0,
      "changePassword() dialog should have been shown zero times"
    );
    sdr.changePassword();
    equal(
      gSetPasswordShownCount,
      1,
      "changePassword() dialog should have been shown exactly once"
    );
  }
});

add_task(async function testAsyncEncryptStrings() {
  let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
    Ci.nsISecretDecoderRing
  );

  // Test valid inputs for encryptString() and decryptString().
  let inputs = [
    "",
    " ", // First printable latin1 character (code point 32).
    "foo",
    "1234567890`~!@#$%^&*()-_=+{[}]|\\:;'\",<.>/?",
    "¡äöüÿ", // Misc + last printable latin1 character (code point 255).
    "aaa 一二三", // Includes Unicode with code points outside [0, 255].
  ];

  let encrypteds = await sdr.asyncEncryptStrings(inputs);
  for (let i = 0; i < inputs.length; i++) {
    let encrypted = encrypteds[i];
    let input = inputs[i];
    let converter = Cc[
      "@mozilla.org/intl/scriptableunicodeconverter"
    ].createInstance(Ci.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";

    let convertedInput = converter.ConvertFromUnicode(input);
    convertedInput += converter.Finish();
    notEqual(
      convertedInput,
      encrypted,
      "Encrypted input should not just be the input itself"
    );

    try {
      atob(encrypted);
    } catch (e) {
      ok(false, `encryptString() should have returned Base64: ${e}`);
    }

    equal(
      convertedInput,
      sdr.decryptString(encrypted),
      "decryptString(encryptString(input)) should return input"
    );
  }
});

add_task(async function testAsyncDecryptStrings() {
  let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
    Ci.nsISecretDecoderRing
  );

  // Test valid inputs for encryptString() and decryptString().
  let testCases = [
    "",
    " ", // First printable latin1 character (code point 32).
    "foo",
    "1234567890`~!@#$%^&*()-_=+{[}]|\\:;'\",<.>/?",
    "¡äöüÿ", // Misc + last printable latin1 character (code point 255).
    "aaa 一二三", // Includes Unicode with code points outside [0, 255].
  ];

  let convertedTestCases = testCases.map(tc => {
    let converter = Cc[
      "@mozilla.org/intl/scriptableunicodeconverter"
    ].createInstance(Ci.nsIScriptableUnicodeConverter);
    converter.charset = "UTF-8";

    let convertedInput = converter.ConvertFromUnicode(tc);
    convertedInput += converter.Finish();
    return convertedInput;
  });

  let encryptedStrings = convertedTestCases.map(tc => sdr.encryptString(tc));
  let decrypteds = await sdr.asyncDecryptStrings(encryptedStrings);
  for (let i = 0; i < encryptedStrings.length; i++) {
    let decrypted = decrypteds[i];

    equal(
      decrypted,
      testCases[i],
      "decrypted string should match expected value"
    );
    equal(
      sdr.decryptString(encryptedStrings[i]),
      convertedTestCases[i],
      "decryptString(encryptString(input)) should return the initial decrypted string value"
    );
  }
});

add_task(async function testAsyncDecryptInvalidStrings() {
  let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
    Ci.nsISecretDecoderRing
  );

  // Test invalid inputs for sdr.asyncDecryptStrings
  let testCases = [
    "~bmV0cGxheQ==", // invalid base64 encoding
    "bmV0cGxheQ==", // valid base64 characters but not encrypted
    "https://www.example.com", // website address from erroneous migration
  ];

  let decrypteds = await sdr.asyncDecryptStrings(testCases);
  equal(
    decrypteds.length,
    testCases.length,
    "each testcase should still return a response"
  );
  for (let i = 0; i < decrypteds.length; i++) {
    let decrypted = decrypteds[i];

    equal(
      decrypted,
      "",
      "decrypted string should be empty when trying to decrypt an invalid input with asyncDecryptStrings"
    );

    Assert.throws(
      () => sdr.decryptString(testCases[i]),
      /NS_ERROR_ILLEGAL_VALUE|NS_ERROR_FAILURE/,
      `Check testcase would have thrown: ${testCases[i]}`
    );
  }
});

add_task(async function testAsyncDecryptLoggedOut() {
  // Set a master password.
  let token = Cc["@mozilla.org/security/pk11tokendb;1"]
    .getService(Ci.nsIPK11TokenDB)
    .getInternalKeyToken();
  token.initPassword("password");
  token.logoutSimple();

  let sdr = Cc["@mozilla.org/security/sdr;1"].getService(
    Ci.nsISecretDecoderRing
  );

  await Assert.rejects(
    sdr.asyncDecryptStrings(["irrelevant"]),
    /NS_ERROR_NOT_AVAILABLE/,
    "Check error is thrown instead of returning empty strings"
  );

  token.reset();
  token.initPassword("");
});