summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/unit/test_CSVParser.js
blob: d680d8daf251c71211b30e460b1ce2bd263cf349 (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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { CSV } = ChromeUtils.importESModule(
  "resource://gre/modules/CSV.sys.mjs"
);

const TEST_CASES = [
  {
    description:
      "string with fields with no special characters gets parsed correctly",
    csvString: `
url,username,password
https://example.com/,testusername,testpassword
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description:
      "string with fields enclosed in quotes with no special characters gets parsed correctly",
    csvString: `
"url","username","password"
"https://example.com/","testusername","testpassword"
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description: "empty fields gets parsed correctly",
    csvString: `
"url","username","password"
"https://example.com/","",""
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "",
        password: "",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description: "string with commas in fields gets parsed correctly",
    csvString: `
url,username,password
https://example.com/,"test,usern,ame","tes,,tpassword"
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "test,usern,ame",
        password: "tes,,tpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description: "string with line break in fields gets parsed correctly",
    csvString: `
url,username,password
https://example.com/,"test\nusername","\ntestpass\n\nword"
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "test\nusername",
        password: "\ntestpass\n\nword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description: "string with quotation mark in fields gets parsed correctly",
    csvString: `
url,username,password
https://example.com/,"testusern""ame","test""""pass""word"
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: 'testusern"ame',
        password: 'test""pass"word',
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description: "tsv string with tab as delimiter gets parsed correctly",
    csvString: `
url\tusername\tpassword
https://example.com/\ttestusername\ttestpassword
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: "\t",
    throwsError: false,
  },
  {
    description: "string with CR LF as line breaks gets parsed correctly",
    csvString:
      "url,username,password\r\nhttps://example.com/,testusername,testpassword\r\n",
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description:
      "string without line break at the end of the file gets parsed correctly",
    csvString: `
url,username,password
https://example.com/,testusername,testpassword`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description:
      "multiple line breaks at the beginning, in the middle or at the end of a string are trimmed and not parsed as empty rows",
    csvString: `
\r\r
url,username,password
\n\n
https://example.com/,testusername,testpassword
\n\r
`,
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [
      {
        url: "https://example.com/",
        username: "testusername",
        password: "testpassword",
      },
    ],
    delimiter: ",",
    throwsError: false,
  },
  {
    description:
      "throws error when after a field, that is enclosed in quotes, follow any invalid characters (doesn't follow csv standard - RFC 4180)",
    csvString: `
  url,username,password
  https://example.com/,"testusername"outside,testpassword
  `,
    delimiter: ",",
    throwsError: true,
  },
  {
    description:
      "throws error when the closing quotation mark for a field is missing (doesn't follow csv standard - RFC 4180)",
    csvString: `
url,"username,password
https://example.com/,testusername,testpassword
`,
    delimiter: ",",
    throwsError: true,
  },
  {
    description:
      "parsing empty csv file results in empty header line and empty parsedLines",
    csvString: "",
    expectedHeaderLine: [],
    expectedParsedLines: [],
    delimiter: ",",
    throwsError: false,
  },
  {
    description:
      "parsing csv file with only header line results in empty parsedLines",
    csvString: "url,username,password\n",
    expectedHeaderLine: ["url", "username", "password"],
    expectedParsedLines: [],
    delimiter: ",",
    throwsError: false,
  },
];

async function parseCSVStringAndValidateResult(test) {
  info(`Test case: ${test.description}`);
  if (test.throwsError) {
    Assert.throws(
      () => CSV.parse(test.csvString, test.delimiter),
      /Stopped parsing because of wrong csv format/
    );
  } else {
    let [resultHeaderLine, resultParsedLines] = CSV.parse(
      test.csvString,
      test.delimiter
    );
    Assert.deepEqual(
      resultHeaderLine,
      test.expectedHeaderLine,
      "Header line check"
    );
    Assert.deepEqual(
      resultParsedLines,
      test.expectedParsedLines,
      "Parsed lines check"
    );
  }
}

add_task(function test_csv_parsing_results() {
  TEST_CASES.forEach(testCase => {
    parseCSVStringAndValidateResult(testCase);
  });
});