summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_module_LoginExport.js
blob: 6c83ff0005e71005961268509fcfddfa812e6856 (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
/* 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/. */

/**
 * Tests the LoginExport module.
 */

"use strict";

let { LoginExport } = ChromeUtils.importESModule(
  "resource://gre/modules/LoginExport.sys.mjs"
);
let { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

/**
 * Saves the logins to a temporary CSV file, reads the lines and returns the CSV lines.
 * After extracting the CSV lines, it deletes the tmp file.
 */
async function exportAsCSVInTmpFile() {
  const tmpFilePath = FileTestUtils.getTempFile("logins.csv").path;
  await LoginExport.exportAsCSV(tmpFilePath);
  const csvString = await IOUtils.readUTF8(tmpFilePath);
  await IOUtils.remove(tmpFilePath);
  // CSV uses CRLF
  return csvString.split(/\r\n/);
}

const COMMON_LOGIN_MODS = {
  guid: "{5ec0d12f-e194-4279-ae1b-d7d281bb46f0}",
  timeCreated: 1589617814635,
  timeLastUsed: 1589710449871,
  timePasswordChanged: 1589617846802,
  timesUsed: 1,
  username: "joe@example.com",
  password: "qwerty",
  origin: "https://example.com",
};

/**
 * Generates a new login object with all the form login fields populated.
 */
function exportFormLogin(modifications) {
  return LoginTestUtils.testData.formLogin({
    ...COMMON_LOGIN_MODS,
    formActionOrigin: "https://action.example.com",
    ...modifications,
  });
}

function exportAuthLogin(modifications) {
  return LoginTestUtils.testData.authLogin({
    ...COMMON_LOGIN_MODS,
    httpRealm: "My realm",
    ...modifications,
  });
}

add_setup(async () => {
  let oldLogins = Services.logins;
  Services.logins = { getAllLogins: sinon.stub() };
  registerCleanupFunction(() => {
    Services.logins = oldLogins;
  });
});

add_task(async function test_buildCSVRow() {
  let testObject = {
    null: null,
    emptyString: "",
    number: 99,
    string: "Foo",
  };
  Assert.deepEqual(
    LoginExport._buildCSVRow(testObject, [
      "null",
      "emptyString",
      "number",
      "string",
    ]),
    ["", `""`, `"99"`, `"Foo"`],
    "Check _buildCSVRow with different types"
  );
});

add_task(async function test_no_new_properties_to_export() {
  let login = exportFormLogin();
  Assert.deepEqual(
    Object.keys(login),
    [
      "QueryInterface",
      "displayOrigin",
      "origin",
      "hostname",
      "formActionOrigin",
      "formSubmitURL",
      "httpRealm",
      "username",
      "usernameField",
      "password",
      "passwordField",
      "unknownFields",
      "everSynced",
      "syncCounter",
      "init",
      "equals",
      "matches",
      "clone",
      "guid",
      "timeCreated",
      "timeLastUsed",
      "timePasswordChanged",
      "timesUsed",
    ],
    "Check that no new properties were added to a login that should maybe be exported"
  );
});

add_task(async function test_export_one_form_login() {
  let login = exportFormLogin();
  Services.logins.getAllLogins.returns([login]);

  let rows = await exportAsCSVInTmpFile();

  Assert.equal(
    rows[0],
    '"url","username","password","httpRealm","formActionOrigin","guid","timeCreated","timeLastUsed","timePasswordChanged"',
    "checking csv headers"
  );
  Assert.equal(
    rows[1],
    '"https://example.com","joe@example.com","qwerty",,"https://action.example.com","{5ec0d12f-e194-4279-ae1b-d7d281bb46f0}","1589617814635","1589710449871","1589617846802"',
    `checking login is saved as CSV row\n${JSON.stringify(login)}\n`
  );
});

add_task(async function test_export_one_auth_login() {
  let login = exportAuthLogin();
  Services.logins.getAllLogins.returns([login]);

  let rows = await exportAsCSVInTmpFile();

  Assert.equal(
    rows[0],
    '"url","username","password","httpRealm","formActionOrigin","guid","timeCreated","timeLastUsed","timePasswordChanged"',
    "checking csv headers"
  );
  Assert.equal(
    rows[1],
    '"https://example.com","joe@example.com","qwerty","My realm",,"{5ec0d12f-e194-4279-ae1b-d7d281bb46f0}","1589617814635","1589710449871","1589617846802"',
    `checking login is saved as CSV row\n${JSON.stringify(login)}\n`
  );
});

add_task(async function test_export_escapes_values() {
  let login = exportFormLogin({
    password: "!@#$%^&*()_+,'",
  });
  Services.logins.getAllLogins.returns([login]);

  let rows = await exportAsCSVInTmpFile();

  Assert.equal(
    rows[1],
    '"https://example.com","joe@example.com","!@#$%^&*()_+,\'",,"https://action.example.com","{5ec0d12f-e194-4279-ae1b-d7d281bb46f0}","1589617814635","1589710449871","1589617846802"',
    `checking login correctly escapes CSV characters \n${JSON.stringify(login)}`
  );
});

add_task(async function test_export_multiple_rows() {
  let logins = await LoginTestUtils.testData.loginList();
  // Note, because we're stubbing this method and avoiding the actual login manager logic,
  // login de-duplication does not occur
  Services.logins.getAllLogins.returns(logins);

  let actualRows = await exportAsCSVInTmpFile();
  let expectedRows = [
    '"url","username","password","httpRealm","formActionOrigin","guid","timeCreated","timeLastUsed","timePasswordChanged"',
    '"http://www.example.com","the username","the password for www.example.com",,"http://www.example.com",,,,',
    '"https://www.example.com","the username","the password for https",,"https://www.example.com",,,,',
    '"https://example.com","the username","the password for example.com",,"https://example.com",,,,',
    '"http://www3.example.com","the username","the password",,"http://www.example.com",,,,',
    '"http://www3.example.com","the username","the password",,"https://www.example.com",,,,',
    '"http://www3.example.com","the username","the password",,"http://example.com",,,,',
    '"http://www4.example.com","username one","password one",,"http://www4.example.com",,,,',
    '"http://www4.example.com","username two","password two",,"http://www4.example.com",,,,',
    '"http://www4.example.com","","password three",,"http://www4.example.com",,,,',
    '"http://www5.example.com","multi username","multi password",,"http://www5.example.com",,,,',
    '"http://www6.example.com","","12345",,"http://www6.example.com",,,,',
    '"https://www7.example.com:8080","8080_username","8080_pass",,"https://www7.example.com:8080",,,,',
    '"https://www7.example.com:8080","8080_username2","8080_pass2","My dev server",,,,,',
    '"http://www.example.org","the username","the password","The HTTP Realm",,,,,',
    '"ftp://ftp.example.org","the username","the password","ftp://ftp.example.org",,,,,',
    '"http://www2.example.org","the username","the password","The HTTP Realm",,,,,',
    '"http://www2.example.org","the username other","the password other","The HTTP Realm Other",,,,,',
    '"http://example.net","the username","the password",,"http://example.net",,,,',
    '"http://example.net","the username","the password",,"http://www.example.net",,,,',
    '"http://example.net","username two","the password",,"http://www.example.net",,,,',
    '"http://example.net","the username","the password","The HTTP Realm",,,,,',
    '"http://example.net","username two","the password","The HTTP Realm Other",,,,,',
    '"ftp://example.net","the username","the password","ftp://example.net",,,,,',
    '"http://example.net","the username","the password","",,,,,',
    '"chrome://example_extension","the username","the password one","Example Login One",,,,,',
    '"chrome://example_extension","the username","the password two","Example Login Two",,,,,',
    '"file://","file: username","file: password",,"file://",,,,',
    '"https://js.example.com","javascript: username","javascript: password",,"javascript:",,,,',
  ];

  Assert.equal(actualRows.length, expectedRows.length, "Check number of lines");
  for (let i = 0; i < logins.length; i++) {
    let login = logins[i];
    Assert.equal(
      actualRows[i],
      expectedRows[i],
      `checking CSV correctly writes row at index=${i} \n${JSON.stringify(
        login
      )}\n`
    );
  }
});