OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 import "dart:convert"; | |
7 import "dart:typed_data"; | |
8 | |
9 main() { | |
10 testMediaType(); | |
11 | |
12 testRoundTrip(""); | |
13 testRoundTrip("a"); | |
14 testRoundTrip("ab"); | |
15 testRoundTrip("abc"); | |
16 testRoundTrip("abcd"); | |
17 testRoundTrip("Content with special%25 characters: # ? = % # ? = %"); | |
18 testRoundTrip("blåbærgrød", UTF8); | |
19 testRoundTrip("blåbærgrød", LATIN1); | |
20 | |
21 testUtf8Encoding("\u1000\uffff"); | |
22 testBytes(); | |
23 testInvalidCharacters(); | |
24 testErrors(); | |
25 } | |
26 | |
27 void testMediaType() { | |
28 for (var mimeType in ["", "text/plain", "text/javascript"]) { | |
29 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) { | |
30 for (var base64 in ["", ";base64"]) { | |
31 bool isBase64 = base64.isNotEmpty; | |
32 var text = "data:$mimeType$charset$base64,"; | |
33 var uri = UriData.parse(text); | |
34 | |
35 String expectedCharset = | |
36 charset.isEmpty ? "US-ASCII" : charset.substring(9); | |
37 String expectedMimeType = mimeType.isEmpty ? "text/plain" : mimeType; | |
38 | |
39 Expect.equals(text, "$uri"); | |
40 Expect.equals(expectedMimeType, uri.mimeType); | |
41 Expect.equals(expectedCharset, uri.charset); | |
42 Expect.equals(isBase64, uri.isBase64); | |
43 } | |
44 } | |
45 } | |
46 } | |
47 | |
48 void testRoundTrip(String content, [Encoding encoding]) { | |
49 UriData dataUri = new UriData.fromString(content, encoding: encoding); | |
50 Expect.isFalse(dataUri.isBase64); | |
51 Uri uri = dataUri.uri; | |
52 expectUriEquals(new Uri.dataFromString(content, encoding: encoding), uri); | |
53 | |
54 if (encoding != null) { | |
55 UriData dataUriParams = | |
56 new UriData.fromString(content, parameters: {"charset": encoding.name}); | |
57 Expect.equals("$dataUri", "$dataUriParams"); | |
58 } | |
59 | |
60 Expect.equals(encoding ?? ASCII, Encoding.getByName(dataUri.charset)); | |
61 Expect.equals(content, dataUri.contentAsString(encoding: encoding)); | |
62 Expect.equals(content, dataUri.contentAsString()); | |
63 Expect.equals(content, (encoding ?? ASCII).decode(dataUri.contentAsBytes())); | |
64 | |
65 uri = dataUri.uri; | |
66 Expect.equals(uri.toString(), dataUri.toString()); | |
67 Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString()); | |
68 | |
69 dataUri = new UriData.fromBytes(content.codeUnits); | |
70 Expect.listEquals(content.codeUnits, dataUri.contentAsBytes()); | |
71 Expect.equals(content, dataUri.contentAsString(encoding: LATIN1)); | |
72 | |
73 uri = dataUri.uri; | |
74 Expect.equals(uri.toString(), dataUri.toString()); | |
75 Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString()); | |
76 // Check that the URI is properly normalized. | |
77 expectUriEquals(uri, Uri.parse("$uri")); | |
78 } | |
79 | |
80 void testUtf8Encoding(String content) { | |
81 UriData uri = new UriData.fromString(content, encoding: UTF8); | |
82 Expect.equals(content, uri.contentAsString(encoding: UTF8)); | |
83 Expect.listEquals(UTF8.encode(content), uri.contentAsBytes()); | |
84 } | |
85 | |
86 void testInvalidCharacters() { | |
87 // SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim) | |
88 // This contains all ASCII character that are not valid in attribute/value | |
89 // parts. | |
90 var invalid = | |
91 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f' | |
92 ' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}'; | |
93 var invalidNoSlash = invalid.replaceAll('/', ''); | |
94 var dataUri = new UriData.fromString(invalid, | |
95 encoding: UTF8, | |
96 mimeType: "$invalidNoSlash/$invalidNoSlash", | |
97 parameters: {invalid: invalid}); | |
98 | |
99 Expect.equals(invalid, dataUri.contentAsString()); | |
100 Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType); | |
101 Expect.equals(invalid, dataUri.parameters[invalid]); | |
102 | |
103 var uri = dataUri.uri; | |
104 Expect.equals("$uri", "$dataUri"); | |
105 expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized. | |
106 Expect.equals("$dataUri", new UriData.fromUri(uri).toString()); | |
107 } | |
108 | |
109 void testBytes() { | |
110 void testList(List<int> list) { | |
111 var dataUri = new UriData.fromBytes(list); | |
112 Expect.equals("application/octet-stream", dataUri.mimeType); | |
113 Expect.isTrue(dataUri.isBase64); | |
114 Expect.listEquals(list, dataUri.contentAsBytes()); | |
115 | |
116 dataUri = new UriData.fromBytes(list, percentEncoded: true); | |
117 Expect.equals("application/octet-stream", dataUri.mimeType); | |
118 Expect.isFalse(dataUri.isBase64); | |
119 Expect.listEquals(list, dataUri.contentAsBytes()); | |
120 | |
121 var string = new String.fromCharCodes(list); | |
122 | |
123 dataUri = new UriData.fromString(string, encoding: LATIN1); | |
124 Expect.equals("text/plain", dataUri.mimeType); | |
125 Expect.isFalse(dataUri.isBase64); | |
126 Expect.listEquals(list, dataUri.contentAsBytes()); | |
127 | |
128 dataUri = new UriData.fromString(string, encoding: LATIN1, base64: true); | |
129 Expect.equals("text/plain", dataUri.mimeType); | |
130 Expect.isTrue(dataUri.isBase64); | |
131 Expect.listEquals(list, dataUri.contentAsBytes()); | |
132 } | |
133 | |
134 void testLists(List<int> list) { | |
135 testList(list); | |
136 for (int i = 0; i < 27; i++) { | |
137 testList(list.sublist(i, i + i)); // All lengths from 0 to 27. | |
138 } | |
139 } | |
140 | |
141 var bytes = new Uint8List(512); | |
142 for (int i = 0; i < bytes.length; i++) { | |
143 bytes[i] = i; | |
144 } | |
145 testLists(bytes); | |
146 testLists(new List.from(bytes)); | |
147 testLists(new List.unmodifiable(bytes)); | |
148 } | |
149 | |
150 bool badArgument(e) => e is ArgumentError; | |
151 bool badFormat(e) => e is FormatException; | |
152 | |
153 void testErrors() { | |
154 // Invalid constructor parameters. | |
155 Expect.throws(() { | |
156 new UriData.fromBytes([], mimeType: "noslash"); | |
157 }, badArgument); | |
158 Expect.throws(() { | |
159 new UriData.fromBytes([257]); | |
160 }, badArgument); | |
161 Expect.throws(() { | |
162 new UriData.fromBytes([-1]); | |
163 }, badArgument); | |
164 Expect.throws(() { | |
165 new UriData.fromBytes([0x10000000]); | |
166 }, badArgument); | |
167 Expect.throws(() { | |
168 new UriData.fromString("", mimeType: "noslash"); | |
169 }, badArgument); | |
170 | |
171 Expect.throws(() { | |
172 new Uri.dataFromBytes([], mimeType: "noslash"); | |
173 }, badArgument); | |
174 Expect.throws(() { | |
175 new Uri.dataFromBytes([257]); | |
176 }, badArgument); | |
177 Expect.throws(() { | |
178 new Uri.dataFromBytes([-1]); | |
179 }, badArgument); | |
180 Expect.throws(() { | |
181 new Uri.dataFromBytes([0x10000000]); | |
182 }, badArgument); | |
183 Expect.throws(() { | |
184 new Uri.dataFromString("", mimeType: "noslash"); | |
185 }, badArgument); | |
186 | |
187 // Empty parameters allowed, not an error. | |
188 var uri = new UriData.fromString("", mimeType: "", parameters: {}); | |
189 Expect.equals("data:,", "$uri"); | |
190 // Empty parameter key or value is an error. | |
191 Expect.throws( | |
192 () => new UriData.fromString("", parameters: {"": "X"}), badArgument); | |
193 Expect.throws( | |
194 () => new UriData.fromString("", parameters: {"X": ""}), badArgument); | |
195 | |
196 // Not recognizing charset is an error. | |
197 uri = UriData.parse("data:;charset=arglebargle,X"); | |
198 Expect.throws(() { | |
199 uri.contentAsString(); | |
200 }); | |
201 // Doesn't throw if we specify the encoding. | |
202 Expect.equals("X", uri.contentAsString(encoding: ASCII)); | |
203 | |
204 // Parse format. | |
205 Expect.throws(() { | |
206 UriData.parse("notdata:,"); | |
207 }, badFormat); | |
208 Expect.throws(() { | |
209 UriData.parse("text/plain,noscheme"); | |
210 }, badFormat); | |
211 Expect.throws(() { | |
212 UriData.parse("data:noseparator"); | |
213 }, badFormat); | |
214 Expect.throws(() { | |
215 UriData.parse("data:noslash,text"); | |
216 }, badFormat); | |
217 Expect.throws(() { | |
218 UriData.parse("data:type/sub;noequals,text"); | |
219 }, badFormat); | |
220 Expect.throws(() { | |
221 UriData.parse("data:type/sub;knocomma="); | |
222 }, badFormat); | |
223 Expect.throws(() { | |
224 UriData.parse("data:type/sub;k=v;nocomma"); | |
225 }, badFormat); | |
226 Expect.throws(() { | |
227 UriData.parse("data:type/sub;k=nocomma"); | |
228 }, badFormat); | |
229 Expect.throws(() { | |
230 UriData.parse("data:type/sub;k=v;base64"); | |
231 }, badFormat); | |
232 | |
233 // Invalid base64 format (only detected when decodeing). | |
234 for (var a = 0; a <= 4; a++) { | |
235 for (var p = 0; p <= 4; p++) { | |
236 // Base-64 encoding must have length divisible by four and no more | |
237 // than two padding characters at the end. | |
238 if (p < 3 && (a + p) % 4 == 0) continue; | |
239 uri = UriData.parse("data:;base64," + "A" * a + "=" * p); | |
240 Expect.throws(uri.contentAsBytes, badFormat); | |
241 } | |
242 } | |
243 // Invalid base64 encoding: padding not at end. | |
244 uri = UriData.parse("data:;base64,AA=A"); | |
245 Expect.throws(uri.contentAsBytes, badFormat); | |
246 uri = UriData.parse("data:;base64,A=AA"); | |
247 Expect.throws(uri.contentAsBytes, badFormat); | |
248 uri = UriData.parse("data:;base64,=AAA"); | |
249 Expect.throws(uri.contentAsBytes, badFormat); | |
250 uri = UriData.parse("data:;base64,A==A"); | |
251 Expect.throws(uri.contentAsBytes, badFormat); | |
252 uri = UriData.parse("data:;base64,==AA"); | |
253 Expect.throws(uri.contentAsBytes, badFormat); | |
254 uri = UriData.parse("data:;base64,===A"); | |
255 Expect.throws(uri.contentAsBytes, badFormat); | |
256 } | |
257 | |
258 /// Checks that two [Uri]s are exactly the same. | |
259 expectUriEquals(Uri expect, Uri actual) { | |
260 Expect.equals(expect.scheme, actual.scheme, "scheme"); | |
261 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority"); | |
262 Expect.equals(expect.userInfo, actual.userInfo, "userInfo"); | |
263 Expect.equals(expect.host, actual.host, "host"); | |
264 Expect.equals(expect.hasPort, actual.hasPort, "hasPort"); | |
265 Expect.equals(expect.port, actual.port, "port"); | |
266 Expect.equals(expect.port, actual.port, "port"); | |
267 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery"); | |
268 Expect.equals(expect.query, actual.query, "query"); | |
269 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment"); | |
270 Expect.equals(expect.fragment, actual.fragment, "fragment"); | |
271 } | |
OLD | NEW |