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 |
| index 41f229085d143e2f86ab26cc165053e5b0aa516d..8e5c4161f8c54f0b9576b39ef90948027712c5ee 100644 |
| --- a/tests/corelib/data_uri_test.dart |
| +++ b/tests/corelib/data_uri_test.dart |
| @@ -18,16 +18,18 @@ main() { |
| testRoundTrip("blåbærgrød", UTF8); |
| testRoundTrip("blåbærgrød", LATIN1); |
| - testUriEquals("data:,abc?d#e"); |
| - testUriEquals("DATA:,ABC?D#E"); |
| - testUriEquals("data:,a%20bc?d#e"); |
| - testUriEquals("DATA:,A%20BC?D#E"); |
| - testUriEquals("data:,a%62c?d#e"); |
| - testUriEquals("DATA:,A%42C?D#E"); |
| + 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. |
|
floitsch
2017/02/15 17:03:09
-is-
|
| + |
| + // Test that UriData.uri normalizes path and query. |
| testUtf8Encoding("\u1000\uffff"); |
| testBytes(); |
| testInvalidCharacters(); |
| + testNormalization(); |
| testErrors(); |
| } |
| @@ -158,6 +160,16 @@ void testBytes() { |
| testLists(new List.unmodifiable(bytes)); |
| } |
| +void testNormalization() { |
| + // "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()); |
| +} |
| + |
| bool badArgument(e) => e is ArgumentError; |
| bool badFormat(e) => e is FormatException; |
| @@ -218,29 +230,55 @@ void testErrors() { |
| Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");}, |
| badFormat); |
| - // Invalid base64 format (only detected when decodeing). |
| + void formatError(String input) { |
| + Expect.throws(() => UriData.parse("data:;base64,$input"), badFormat, 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; |
| - uri = UriData.parse("data:;base64," + "A" * a + "=" * p); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| + if (p == 0 && a > 1) continue; |
| + formatError("A" * a + "=" * p); |
| + formatError("A" * a + "%3D" * p); |
| } |
| } |
| // Invalid base64 encoding: padding not at end. |
| - uri = UriData.parse("data:;base64,AA=A"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| - uri = UriData.parse("data:;base64,A=AA"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| - uri = UriData.parse("data:;base64,=AAA"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| - uri = UriData.parse("data:;base64,A==A"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| - uri = UriData.parse("data:;base64,==AA"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| - uri = UriData.parse("data:;base64,===A"); |
| - Expect.throws(uri.contentAsBytes, badFormat); |
| + formatError("AA=A"); |
| + formatError("A=AA"); |
| + formatError("=AAA"); |
| + formatError("A==A"); |
| + formatError("==AA"); |
| + formatError("===A"); |
| + formatError("AAA%3D="); |
| + formatError("A%3D=="); |
| + |
| + // 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()); |
| + |
| + // Invalid unpadded data. |
| + formatError("A"); |
| + formatError("AAAAA"); |
| + // 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()); |
| + |
| + // Invalid characters. |
| + formatError("AAA*"); |
| + formatError("AAA\x00"); |
| + formatError("AAA\\"); |
| + formatError("AAA,"); |
| + |
| + // Normalized URI-alphabet characters. |
| + Expect.equals("data:;base64,AA/+", |
| + UriData.parse("data:;base64,AA_-").toString()); |
| } |
| /// Checks that two [Uri]s are exactly the same. |