Chromium Code Reviews| Index: tests/corelib/data_uri_test.dart |
| diff --git a/tests/corelib/data_uri_test.dart b/tests/corelib/data_uri_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..911480f5f8772665b5ee6f41c6125c62e6f10b75 |
| --- /dev/null |
| +++ b/tests/corelib/data_uri_test.dart |
| @@ -0,0 +1,137 @@ |
| +// 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() { |
| + testRoundTrip(""); |
| + testRoundTrip("a"); |
| + testRoundTrip("ab"); |
| + testRoundTrip("abc"); |
| + testRoundTrip("abcd"); |
| + |
| + testUtf8Encoding("\u1000\uffff"); |
| + testBytes(); |
| + testInvalidCharacters(); |
| + testErrors(); |
| +} |
|
nweiz
2015/10/15 21:09:04
Also test:
* Percent-encoding non-token character
Lasse Reichstein Nielsen
2015/11/03 18:02:52
Good points. Also, I should test the "base64" and
|
| + |
| +void testRoundTrip(String content) { |
| + DataUri dataUri = new DataUri.fromString(content); |
| + Expect.equals(content, dataUri.contentAsString()); |
| + Expect.listEquals(content.codeUnits, dataUri.contentAsBytes()); |
| + |
| + Uri uri = dataUri.toUri(); |
| + Expect.equals(uri.toString(), dataUri.toString()); |
| + Expect.equals(dataUri.toString(), new DataUri.fromUri(uri).toString()); |
| + |
| + dataUri = new DataUri.fromBytes(content.codeUnits); |
| + Expect.listEquals(content.codeUnits, dataUri.contentAsBytes()); |
| + Expect.equals(content, dataUri.contentAsString(encoding: ASCII)); |
| + |
| + uri = dataUri.toUri(); |
| + Expect.equals(uri.toString(), dataUri.toString()); |
| + Expect.equals(dataUri.toString(), new DataUri.fromUri(uri).toString()); |
| +} |
| + |
| +void testUtf8Encoding(String content) { |
| + DataUri uri = new DataUri.fromString(content); |
| + 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) |
| + var invalid = |
| + '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f' |
| + ' ()<>@,;:"/[]?=%#'; |
| + var invalidNoSlash = invalid.replaceAll('/', ''); |
| + var dataUri = new DataUri.fromString( |
| + invalid, |
| + mimeType: "$invalidNoSlash/$invalidNoSlash", |
| + parameters: DataUriParameter.fromMap({invalid: invalid})); |
| + |
| + Expect.equals(invalid, dataUri.contentAsString()); |
| + Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType); |
| + Expect.equals(invalid, dataUri.parameters.first.key); |
| + Expect.equals(invalid, dataUri.parameters.first.value); |
| + |
| + var uri = dataUri.toUri(); |
| + Expect.equals("$uri", "$dataUri"); |
| + Expect.equals("$dataUri", new DataUri.fromUri(uri).toString()); |
| +} |
| + |
| +void testBytes() { |
| + void testList(List<int> list) { |
| + var dataUri = new DataUri.fromBytes(list); |
| + Expect.equals("application/octet-stream", dataUri.mimeType); |
| + 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; |
|
nweiz
2015/10/15 21:09:04
Won't the latter half of this list be outside the
Lasse Reichstein Nielsen
2015/11/03 18:02:52
Storing into an Uint8List will truncate, so I get
|
| + } |
| + testLists(bytes); |
| + testLists(new List.from(bytes)); |
| + testLists(new List.unmodifiable(bytes)); |
| +} |
| + |
| +bool badArgument(e) => e is ArgumentError; |
| +bool badFormat(e) => e is FormatException; |
| + |
| +void testErrors() { |
| + // Invalid constructor parameters. |
| + Expect.throws(() { new DataUri.fromBytes([], mimeType: "noslash"); }, |
| + badArgument); |
| + Expect.throws(() { new DataUri.fromBytes([257]); }, |
| + badArgument); |
| + Expect.throws(() { new DataUri.fromBytes([-1]); }, |
| + badArgument); |
| + Expect.throws(() { new DataUri.fromBytes([0x100000000]); }, |
| + badArgument); |
| + Expect.throws(() { new DataUri.fromString("", mimeType: "noslash"); }, |
| + badArgument); |
| + |
| + // Empty parameters allowed, not an error. |
| + var uri = new DataUri.fromString("", mimeType: "", parameters: []); |
| + Expect.equals("data:,", uri.text); |
| + |
| + // Parse format. |
| + Expect.throws(() { DataUri.parse("notdata:,");}, badFormat); |
| + Expect.throws(() { DataUri.parse("text/plain,noscheme");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:noseparator");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:noslash,text");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:type/sub;noequals,text");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:type/sub;knocomma=");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:type/sub;k=v;nocomma");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:type/sub;k=nocomma");}, badFormat); |
| + Expect.throws(() { DataUri.parse("data:type/sub;k=v;base64");}, badFormat); |
| + |
| + uri = DataUri.parse("data:,"); // Minimal valid. |
| + Expect.equals("data:,", uri.text); |
| + uri = DataUri.parse("data:;base64,"); // Minimal valid base64. |
| + Expect.equals("data:;base64,", uri.text); |
|
nweiz
2015/10/15 21:09:04
Why are these in testErrors?
Lasse Reichstein Nielsen
2015/11/03 18:02:52
Mostly as sanity checks.
I'll move them to main a
|
| + |
| + // Invalid base64 format (only detected when decodeing). |
| + uri = DataUri.parse("data:;base64,AAA"); |
| + Expect.throws(uri.contentAsBytes, badFormat); |
| + uri = DataUri.parse("data:;base64,AA="); |
| + Expect.throws(uri.contentAsBytes, badFormat); |
| + |
| + // Accepts encoded '=' in base64. |
| + uri = DataUri.parse("data:;base64,AA=="); |
| + Expect.listEquals([0], uri.contentAsBytes()); |
| + uri = DataUri.parse("data:;base64,AA%3D%3D"); |
| + Expect.listEquals([0], uri.contentAsBytes()); |
|
nweiz
2015/10/15 21:09:04
Also these?
Lasse Reichstein Nielsen
2015/11/03 18:02:52
That's really base-64 decoding now, so I'll just r
|
| +} |