-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdata_uri_test.dart
376 lines (333 loc) · 12.9 KB
/
data_uri_test.dart
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "dart:convert";
import "dart:typed_data";
main() {
testMediaType();
testRoundTrip("");
testRoundTrip("a");
testRoundTrip("ab");
testRoundTrip("abc");
testRoundTrip("abcd");
testRoundTrip("Content with special%25 characters: # ? = % # ? = %");
testRoundTrip("blåbærgrød", utf8);
testRoundTrip("blåbærgrød", latin1);
testUriEquals("data:,abc?d");
testUriEquals("DATA:,ABC?D");
testUriEquals("data:,a%20bc?d");
testUriEquals("DATA:,A%20BC?D");
testUriEquals("data:,abc?d%23e"); // # must and will be is escaped.
// Test that UriData.uri normalizes path and query.
testUtf8Encoding("\u1000\uffff");
testBytes();
testInvalidCharacters();
testNormalization();
testErrors();
}
void testMediaType() {
for (var mimeType in ["", "text/plain", "Text/PLAIN", "text/javascript"]) {
for (var charset in ["", "US-ASCII", "UTF-8"]) {
for (var base64 in ["", ";base64"]) {
bool isBase64 = base64.isNotEmpty;
// Parsing the URI from source:
var charsetParameter = charset.isEmpty ? "" : ";charset=$charset";
var text = "data:$mimeType$charsetParameter$base64,";
var uri = UriData.parse(text);
String expectedCharset = charset.isEmpty ? "US-ASCII" : charset;
String expectedMimeType = mimeType.isEmpty ? "text/plain" : mimeType;
Expect.equals(text, "$uri");
Expect.equals(expectedMimeType, uri.mimeType);
Expect.isTrue(uri.isMimeType(expectedMimeType));
Expect.isTrue(uri.isMimeType(expectedMimeType.toUpperCase()));
Expect.isTrue(uri.isMimeType(expectedMimeType.toLowerCase()));
Expect.equals(expectedCharset, uri.charset);
Expect.isTrue(uri.isCharset(expectedCharset));
Expect.isTrue(uri.isCharset(expectedCharset.toLowerCase()));
Expect.isTrue(uri.isCharset(expectedCharset.toUpperCase()));
var expectedEncoding = Encoding.getByName(expectedCharset);
if (expectedEncoding != null) {
Expect.isTrue(uri.isEncoding(expectedEncoding));
}
Expect.equals(isBase64, uri.isBase64);
// Creating the URI using a constructor:
var encoding = Encoding.getByName(charset);
uri = UriData.fromString(
"",
mimeType: mimeType,
encoding: encoding,
base64: isBase64,
);
expectedMimeType =
(mimeType.isEmpty || mimeType.toLowerCase() == "text/plain")
? "text/plain"
: mimeType;
expectedEncoding = encoding;
expectedCharset = expectedEncoding?.name ?? "US-ASCII";
var expectedText =
"data:"
"${expectedMimeType == "text/plain" ? "" : expectedMimeType}"
"${charset.isEmpty ? "" : ";charset=$expectedCharset"}"
"${isBase64 ? ";base64" : ""}"
",";
Expect.equals(expectedText, "$uri");
Expect.equals(expectedMimeType, uri.mimeType);
Expect.isTrue(uri.isMimeType(expectedMimeType));
Expect.isTrue(uri.isMimeType(expectedMimeType.toUpperCase()));
Expect.isTrue(uri.isMimeType(expectedMimeType.toLowerCase()));
Expect.equals(expectedCharset, uri.charset);
Expect.isTrue(uri.isCharset(expectedCharset));
Expect.isTrue(uri.isCharset(expectedCharset.toLowerCase()));
Expect.isTrue(uri.isCharset(expectedCharset.toUpperCase()));
if (expectedEncoding != null) {
Expect.isTrue(uri.isEncoding(expectedEncoding));
}
Expect.equals(isBase64, uri.isBase64);
}
}
}
}
void testRoundTrip(String content, [Encoding? encoding]) {
UriData dataUri = new UriData.fromString(content, encoding: encoding);
Expect.isFalse(dataUri.isBase64);
Uri uri = dataUri.uri;
expectUriEquals(new Uri.dataFromString(content, encoding: encoding), uri);
if (encoding != null) {
UriData dataUriParams = new UriData.fromString(
content,
parameters: {"charset": encoding.name},
);
Expect.equals("$dataUri", "$dataUriParams");
}
Expect.equals(encoding ?? ascii, Encoding.getByName(dataUri.charset));
Expect.equals(content, dataUri.contentAsString(encoding: encoding));
Expect.equals(content, dataUri.contentAsString());
Expect.equals(content, (encoding ?? ascii).decode(dataUri.contentAsBytes()));
uri = dataUri.uri;
Expect.equals(uri.toString(), dataUri.toString());
Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString());
dataUri = new UriData.fromBytes(content.codeUnits);
Expect.listEquals(content.codeUnits, dataUri.contentAsBytes());
Expect.equals(content, dataUri.contentAsString(encoding: latin1));
uri = dataUri.uri;
Expect.equals(uri.toString(), dataUri.toString());
Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString());
// Check that the URI is properly normalized.
expectUriEquals(uri, Uri.parse("$uri"));
}
void testUtf8Encoding(String content) {
UriData uri = new UriData.fromString(content, encoding: utf8);
Expect.equals(content, uri.contentAsString(encoding: utf8));
Expect.listEquals(utf8.encode(content), uri.contentAsBytes());
}
void testInvalidCharacters() {
// SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim)
// This contains all ASCII character that are not valid in attribute/value
// parts.
var invalid =
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f'
' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}';
var invalidNoSlash = invalid.replaceAll('/', '');
var dataUri = new UriData.fromString(
invalid,
encoding: utf8,
mimeType: "$invalidNoSlash/$invalidNoSlash",
parameters: {invalid: invalid},
);
Expect.equals(invalid, dataUri.contentAsString());
Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType);
Expect.equals(invalid, dataUri.parameters[invalid]);
var uri = dataUri.uri;
Expect.equals("$uri", "$dataUri");
expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized.
Expect.equals("$dataUri", new UriData.fromUri(uri).toString());
}
void testBytes() {
void testList(List<int> list) {
var dataUri = new UriData.fromBytes(list);
Expect.equals("application/octet-stream", dataUri.mimeType);
Expect.isTrue(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
dataUri = new UriData.fromBytes(list, percentEncoded: true);
Expect.equals("application/octet-stream", dataUri.mimeType);
Expect.isFalse(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
var string = new String.fromCharCodes(list);
dataUri = new UriData.fromString(string, encoding: latin1);
Expect.equals("text/plain", dataUri.mimeType);
Expect.isFalse(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
dataUri = new UriData.fromString(string, encoding: latin1, base64: true);
Expect.equals("text/plain", dataUri.mimeType);
Expect.isTrue(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
}
void testLists(List<int> list) {
testList(list);
for (int i = 0; i < 27; i++) {
testList(list.sublist(i, i + i)); // All lengths from 0 to 27.
}
}
var bytes = new Uint8List(512);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = i;
}
testLists(bytes);
testLists(new List.from(bytes));
testLists(new List.unmodifiable(bytes));
}
void testNormalization() {
// Base-64 normalization.
// Normalized URI-alphabet characters.
Expect.equals(
"data:;base64,AA/+",
UriData.parse("data:;base64,AA_-").toString(),
);
// Normalized escapes.
Expect.equals(
"data:;base64,AB==",
UriData.parse("data:;base64,A%42=%3D").toString(),
);
Expect.equals(
"data:;base64,/+/+",
UriData.parse("data:;base64,%5F%2D%2F%2B").toString(),
);
// Normalized padded data.
Expect.equals(
"data:;base64,AA==",
UriData.parse("data:;base64,AA%3D%3D").toString(),
);
Expect.equals(
"data:;base64,AAA=",
UriData.parse("data:;base64,AAA%3D").toString(),
);
// Normalized unpadded data.
Expect.equals(
"data:;base64,AA==",
UriData.parse("data:;base64,AA").toString(),
);
Expect.equals(
"data:;base64,AAA=",
UriData.parse("data:;base64,AAA").toString(),
);
// "URI normalization" of non-base64 content.
var uri = UriData.parse("data:,\x20\xa0");
Expect.equals("data:,%20%C2%A0", uri.toString());
uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}");
Expect.equals(
"data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85",
uri.toString(),
);
}
void testErrors() {
// Invalid constructor parameters.
Expect.throwsArgumentError(
() => new UriData.fromBytes([], mimeType: "noslash"),
);
Expect.throwsArgumentError(() => new UriData.fromBytes([257]));
Expect.throwsArgumentError(() => new UriData.fromBytes([-1]));
Expect.throwsArgumentError(() => new UriData.fromBytes([0x10000000]));
Expect.throwsArgumentError(
() => new UriData.fromString("", mimeType: "noslash"),
);
Expect.throwsArgumentError(
() => new Uri.dataFromBytes([], mimeType: "noslash"),
);
Expect.throwsArgumentError(() => new Uri.dataFromBytes([257]));
Expect.throwsArgumentError(() => new Uri.dataFromBytes([-1]));
Expect.throwsArgumentError(() => new Uri.dataFromBytes([0x10000000]));
Expect.throwsArgumentError(
() => new Uri.dataFromString("", mimeType: "noslash"),
);
// Empty parameters allowed, not an error.
var uri = new UriData.fromString("", mimeType: "", parameters: {});
Expect.equals("data:,", "$uri");
// Empty parameter key or value is an error.
Expect.throwsArgumentError(
() => new UriData.fromString("", parameters: {"": "X"}),
);
Expect.throwsArgumentError(
() => new UriData.fromString("", parameters: {"X": ""}),
);
// Not recognizing charset is an error.
uri = UriData.parse("data:;charset=arglebargle,X");
Expect.throws(() {
uri.contentAsString();
});
// Doesn't throw if we specify the encoding.
Expect.equals("X", uri.contentAsString(encoding: ascii));
// Parse format.
Expect.throwsFormatException(() => UriData.parse("notdata:,"));
Expect.throwsFormatException(() => UriData.parse("text/plain,noscheme"));
Expect.throwsFormatException(() => UriData.parse("data:noseparator"));
Expect.throwsFormatException(() => UriData.parse("data:noslash,text"));
Expect.throwsFormatException(
() => UriData.parse("data:type/sub;noequals,text"),
);
Expect.throwsFormatException(() => UriData.parse("data:type/sub;knocomma="));
Expect.throwsFormatException(
() => UriData.parse("data:type/sub;k=v;nocomma"),
);
Expect.throwsFormatException(() => UriData.parse("data:type/sub;k=nocomma"));
Expect.throwsFormatException(() => UriData.parse("data:type/sub;k=v;base64"));
void formatError(String input) {
Expect.throwsFormatException(
() => UriData.parse("data:;base64,$input"),
input,
);
}
// Invalid base64 format (detected when parsed).
for (var a = 0; a <= 4; a++) {
for (var p = 0; p <= 4; p++) {
// Base-64 encoding must have length divisible by four and no more
// than two padding characters at the end.
if (p < 3 && (a + p) % 4 == 0) continue;
if (p == 0 && a > 1) continue;
formatError("A" * a + "=" * p);
formatError("A" * a + "%3D" * p);
}
}
// Invalid base64 encoding: padding not at end.
formatError("AA=A");
formatError("A=AA");
formatError("=AAA");
formatError("A==A");
formatError("==AA");
formatError("===A");
formatError("AAA%3D=");
formatError("A%3D==");
// Invalid unpadded data.
formatError("A");
formatError("AAAAA");
// Invalid characters.
formatError("AAA*");
formatError("AAA\x00");
formatError("AAA\\");
formatError("AAA,");
// Invalid escapes.
formatError("AAA%25");
formatError("AAA%7F");
formatError("AAA%7F");
}
/// Checks that two [Uri]s are exactly the same.
expectUriEquals(Uri expect, Uri actual) {
Expect.equals(expect.scheme, actual.scheme, "scheme");
Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority");
Expect.equals(expect.userInfo, actual.userInfo, "userInfo");
Expect.equals(expect.host, actual.host, "host");
Expect.equals(expect.hasPort, actual.hasPort, "hasPort");
Expect.equals(expect.port, actual.port, "port");
Expect.equals(expect.port, actual.port, "port");
Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery");
Expect.equals(expect.query, actual.query, "query");
Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment");
Expect.equals(expect.fragment, actual.fragment, "fragment");
}
void testUriEquals(String uriText) {
var data = UriData.parse(uriText);
var uri = Uri.parse(uriText);
Expect.equals(data.uri, uri);
Expect.equals(data.toString(), uri.data.toString());
Expect.equals(data.toString(), uri.toString());
}