OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:convert'; | 5 import 'dart:convert'; |
6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 | 8 |
9 main() { | 9 main() { |
10 for (var list in [[], | 10 for (var list in [ |
11 [0x00], | 11 [], |
12 [0xff, 0x00], | 12 [0x00], |
13 [0xff, 0xaa, 0x55], | 13 [0xff, 0x00], |
14 [0x00, 0x01, 0x02, 0x03], | 14 [0xff, 0xaa, 0x55], |
15 new Iterable.generate(13).toList(), | 15 [0x00, 0x01, 0x02, 0x03], |
16 new Iterable.generate(254).toList(), | 16 new Iterable.generate(13).toList(), |
17 new Iterable.generate(255).toList(), | 17 new Iterable.generate(254).toList(), |
18 new Iterable.generate(256).toList()]) { | 18 new Iterable.generate(255).toList(), |
| 19 new Iterable.generate(256).toList() |
| 20 ]) { |
19 testRoundtrip(list, "List#${list.length}"); | 21 testRoundtrip(list, "List#${list.length}"); |
20 testRoundtrip(new Uint8List.fromList(list), "Uint8List#${list.length}"); | 22 testRoundtrip(new Uint8List.fromList(list), "Uint8List#${list.length}"); |
21 } | 23 } |
22 testErrors(); | 24 testErrors(); |
23 testIssue25577(); | 25 testIssue25577(); |
24 | 26 |
25 // Decoder is lenienet with mixed styles. | 27 // Decoder is lenienet with mixed styles. |
26 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA%3D=")); | 28 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA%3D=")); |
27 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA=%3D")); | 29 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA=%3D")); |
28 } | 30 } |
(...skipping 15 matching lines...) Expand all Loading... |
44 Expect.listEquals(list, result, name); | 46 Expect.listEquals(list, result, name); |
45 | 47 |
46 int increment = list.length ~/ 7 + 1; | 48 int increment = list.length ~/ 7 + 1; |
47 // Chunked. | 49 // Chunked. |
48 for (int i = 0; i < list.length; i += increment) { | 50 for (int i = 0; i < list.length; i += increment) { |
49 for (int j = i; j < list.length; j += increment) { | 51 for (int j = i; j < list.length; j += increment) { |
50 // Normal | 52 // Normal |
51 { | 53 { |
52 // Using add/close | 54 // Using add/close |
53 var results; | 55 var results; |
54 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 56 var sink = new ChunkedConversionSink.withCallback((v) { |
| 57 results = v; |
| 58 }); |
55 var encoder = BASE64.encoder.startChunkedConversion(sink); | 59 var encoder = BASE64.encoder.startChunkedConversion(sink); |
56 encoder.add(list.sublist(0, i)); | 60 encoder.add(list.sublist(0, i)); |
57 encoder.add(list.sublist(i, j)); | 61 encoder.add(list.sublist(i, j)); |
58 encoder.add(list.sublist(j, list.length)); | 62 encoder.add(list.sublist(j, list.length)); |
59 encoder.close(); | 63 encoder.close(); |
60 var name = "0-$i-$j-${list.length}: list"; | 64 var name = "0-$i-$j-${list.length}: list"; |
61 Expect.equals(encodedNormal, results.join(""), name); | 65 Expect.equals(encodedNormal, results.join(""), name); |
62 } | 66 } |
63 { | 67 { |
64 // Using addSlice | 68 // Using addSlice |
65 var results; | 69 var results; |
66 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 70 var sink = new ChunkedConversionSink.withCallback((v) { |
| 71 results = v; |
| 72 }); |
67 var encoder = BASE64.encoder.startChunkedConversion(sink); | 73 var encoder = BASE64.encoder.startChunkedConversion(sink); |
68 encoder.addSlice(list, 0, i, false); | 74 encoder.addSlice(list, 0, i, false); |
69 encoder.addSlice(list, i, j, false); | 75 encoder.addSlice(list, i, j, false); |
70 encoder.addSlice(list, j, list.length, true); | 76 encoder.addSlice(list, j, list.length, true); |
71 var name = "0-$i-$j-${list.length}: $list"; | 77 var name = "0-$i-$j-${list.length}: $list"; |
72 Expect.equals(encodedNormal, results.join(""), name); | 78 Expect.equals(encodedNormal, results.join(""), name); |
73 } | 79 } |
74 // URI | 80 // URI |
75 { | 81 { |
76 // Using add/close | 82 // Using add/close |
77 var results; | 83 var results; |
78 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 84 var sink = new ChunkedConversionSink.withCallback((v) { |
| 85 results = v; |
| 86 }); |
79 var encoder = BASE64URL.encoder.startChunkedConversion(sink); | 87 var encoder = BASE64URL.encoder.startChunkedConversion(sink); |
80 encoder.add(list.sublist(0, i)); | 88 encoder.add(list.sublist(0, i)); |
81 encoder.add(list.sublist(i, j)); | 89 encoder.add(list.sublist(i, j)); |
82 encoder.add(list.sublist(j, list.length)); | 90 encoder.add(list.sublist(j, list.length)); |
83 encoder.close(); | 91 encoder.close(); |
84 var name = "0-$i-$j-${list.length}: list"; | 92 var name = "0-$i-$j-${list.length}: list"; |
85 Expect.equals(uriEncoded, results.join(""), name); | 93 Expect.equals(uriEncoded, results.join(""), name); |
86 } | 94 } |
87 { | 95 { |
88 // Using addSlice | 96 // Using addSlice |
89 var results; | 97 var results; |
90 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 98 var sink = new ChunkedConversionSink.withCallback((v) { |
| 99 results = v; |
| 100 }); |
91 var encoder = BASE64URL.encoder.startChunkedConversion(sink); | 101 var encoder = BASE64URL.encoder.startChunkedConversion(sink); |
92 encoder.addSlice(list, 0, i, false); | 102 encoder.addSlice(list, 0, i, false); |
93 encoder.addSlice(list, i, j, false); | 103 encoder.addSlice(list, i, j, false); |
94 encoder.addSlice(list, j, list.length, true); | 104 encoder.addSlice(list, j, list.length, true); |
95 var name = "0-$i-$j-${list.length}: $list"; | 105 var name = "0-$i-$j-${list.length}: $list"; |
96 Expect.equals(uriEncoded, results.join(""), name); | 106 Expect.equals(uriEncoded, results.join(""), name); |
97 } | 107 } |
98 } | 108 } |
99 } | 109 } |
100 | 110 |
101 for (var encoded in [encodedNormal, encodedPercent, uriEncoded]) { | 111 for (var encoded in [encodedNormal, encodedPercent, uriEncoded]) { |
102 increment = encoded.length ~/ 7 + 1; | 112 increment = encoded.length ~/ 7 + 1; |
103 for (int i = 0; i < encoded.length; i += increment) { | 113 for (int i = 0; i < encoded.length; i += increment) { |
104 for (int j = i; j < encoded.length; j += increment) { | 114 for (int j = i; j < encoded.length; j += increment) { |
105 { | 115 { |
106 // Using add/close | 116 // Using add/close |
107 var results; | 117 var results; |
108 var sink = | 118 var sink = new ChunkedConversionSink.withCallback((v) { |
109 new ChunkedConversionSink.withCallback((v) { results = v; }); | 119 results = v; |
| 120 }); |
110 var decoder = BASE64.decoder.startChunkedConversion(sink); | 121 var decoder = BASE64.decoder.startChunkedConversion(sink); |
111 decoder.add(encoded.substring(0, i)); | 122 decoder.add(encoded.substring(0, i)); |
112 decoder.add(encoded.substring(i, j)); | 123 decoder.add(encoded.substring(i, j)); |
113 decoder.add(encoded.substring(j, encoded.length)); | 124 decoder.add(encoded.substring(j, encoded.length)); |
114 decoder.close(); | 125 decoder.close(); |
115 var name = "0-$i-$j-${encoded.length}: $encoded"; | 126 var name = "0-$i-$j-${encoded.length}: $encoded"; |
116 Expect.listEquals(list, results.expand((x)=>x).toList(), name); | 127 Expect.listEquals(list, results.expand((x) => x).toList(), name); |
117 } | 128 } |
118 { | 129 { |
119 // Using addSlice | 130 // Using addSlice |
120 var results; | 131 var results; |
121 var sink = | 132 var sink = new ChunkedConversionSink.withCallback((v) { |
122 new ChunkedConversionSink.withCallback((v) { results = v; }); | 133 results = v; |
| 134 }); |
123 var decoder = BASE64.decoder.startChunkedConversion(sink); | 135 var decoder = BASE64.decoder.startChunkedConversion(sink); |
124 decoder.addSlice(encoded, 0, i, false); | 136 decoder.addSlice(encoded, 0, i, false); |
125 decoder.addSlice(encoded, i, j, false); | 137 decoder.addSlice(encoded, i, j, false); |
126 decoder.addSlice(encoded, j, encoded.length, true); | 138 decoder.addSlice(encoded, j, encoded.length, true); |
127 var name = "0-$i-$j-${encoded.length}: $encoded"; | 139 var name = "0-$i-$j-${encoded.length}: $encoded"; |
128 Expect.listEquals(list, results.expand((x)=>x).toList(), name); | 140 Expect.listEquals(list, results.expand((x) => x).toList(), name); |
129 } | 141 } |
130 } | 142 } |
131 } | 143 } |
132 } | 144 } |
133 } | 145 } |
134 | 146 |
135 bool isFormatException(e) => e is FormatException; | 147 bool isFormatException(e) => e is FormatException; |
136 bool isArgumentError(e) => e is ArgumentError; | 148 bool isArgumentError(e) => e is ArgumentError; |
137 | 149 |
138 void testErrors() { | 150 void testErrors() { |
139 void badChunkDecode(List<String> list) { | 151 void badChunkDecode(List<String> list) { |
140 Expect.throws(() { | 152 Expect.throws(() { |
141 var sink = new ChunkedConversionSink.withCallback((v) { | 153 var sink = new ChunkedConversionSink.withCallback((v) { |
142 Expect.fail("Should have thrown: chunk $list"); | 154 Expect.fail("Should have thrown: chunk $list"); |
143 }); | 155 }); |
144 var c = BASE64.decoder.startChunkedConversion(sink); | 156 var c = BASE64.decoder.startChunkedConversion(sink); |
145 for (String string in list) { | 157 for (String string in list) { |
146 c.add(string); | 158 c.add(string); |
147 } | 159 } |
148 c.close(); | 160 c.close(); |
149 }, isFormatException, "chunk $list"); | 161 }, isFormatException, "chunk $list"); |
150 } | 162 } |
| 163 |
151 void badDecode(String string) { | 164 void badDecode(String string) { |
152 Expect.throws(() => BASE64.decode(string), isFormatException, string); | 165 Expect.throws(() => BASE64.decode(string), isFormatException, string); |
153 Expect.throws(() => BASE64URL.decode(string), isFormatException, string); | 166 Expect.throws(() => BASE64URL.decode(string), isFormatException, string); |
154 badChunkDecode([string]); | 167 badChunkDecode([string]); |
155 badChunkDecode(["", string]); | 168 badChunkDecode(["", string]); |
156 badChunkDecode([string, ""]); | 169 badChunkDecode([string, ""]); |
157 badChunkDecode([string, "", ""]); | 170 badChunkDecode([string, "", ""]); |
158 badChunkDecode(["", string, ""]); | 171 badChunkDecode(["", string, ""]); |
159 } | 172 } |
160 | 173 |
(...skipping 30 matching lines...) Expand all Loading... |
191 badDecode("AAA\x80"); | 204 badDecode("AAA\x80"); |
192 badDecode("AAA\xFF"); | 205 badDecode("AAA\xFF"); |
193 badDecode("AAA\u{141}"); | 206 badDecode("AAA\u{141}"); |
194 badDecode("AAA\u{1041}"); | 207 badDecode("AAA\u{1041}"); |
195 badDecode("AAA\u{10041}"); | 208 badDecode("AAA\u{10041}"); |
196 badDecode("AA\u{141}="); | 209 badDecode("AA\u{141}="); |
197 badDecode("AA\u{1041}="); | 210 badDecode("AA\u{1041}="); |
198 badDecode("AA\u{10041}="); | 211 badDecode("AA\u{10041}="); |
199 | 212 |
200 var alphabet = | 213 var alphabet = |
201 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/-_"; | 214 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/-_"; |
202 var units = alphabet.codeUnits; | 215 var units = alphabet.codeUnits; |
203 for (int i = 0; i < 128; i++) { | 216 for (int i = 0; i < 128; i++) { |
204 if (!units.contains(i)) { | 217 if (!units.contains(i)) { |
205 badDecode(new String.fromCharCode(i) * 4); | 218 badDecode(new String.fromCharCode(i) * 4); |
206 } | 219 } |
207 } | 220 } |
208 | 221 |
209 badChunkDecode(["A", "A"]); | 222 badChunkDecode(["A", "A"]); |
210 badChunkDecode(["A", "A", "A"]); | 223 badChunkDecode(["A", "A", "A"]); |
211 badChunkDecode(["A", "A", "="]); | 224 badChunkDecode(["A", "A", "="]); |
212 badChunkDecode(["A", "A", "=", ""]); | 225 badChunkDecode(["A", "A", "=", ""]); |
213 badChunkDecode(["A", "A", "=", "=", "="]); | 226 badChunkDecode(["A", "A", "=", "=", "="]); |
214 badChunkDecode(["AAA", "=="]); | 227 badChunkDecode(["AAA", "=="]); |
215 badChunkDecode(["A", "A", "A"]); | 228 badChunkDecode(["A", "A", "A"]); |
216 badChunkDecode(["AAA", ""]); | 229 badChunkDecode(["AAA", ""]); |
217 badChunkDecode(["AA=", ""]); | 230 badChunkDecode(["AA=", ""]); |
218 badChunkDecode(["AB==", ""]); | 231 badChunkDecode(["AB==", ""]); |
219 | 232 |
220 | |
221 badChunkEncode(list) { | 233 badChunkEncode(list) { |
222 for (int i = 0; i < list.length; i++) { | 234 for (int i = 0; i < list.length; i++) { |
223 for (int j = 0; j < list.length; j++) { | 235 for (int j = 0; j < list.length; j++) { |
224 Expect.throws(() { | 236 Expect.throws(() { |
225 var sink = new ChunkedConversionSink.withCallback((v) { | 237 var sink = new ChunkedConversionSink.withCallback((v) { |
226 Expect.fail("Should have thrown: chunked $list"); | 238 Expect.fail("Should have thrown: chunked $list"); |
227 }); | 239 }); |
228 var c = BASE64.encoder.startChunkedConversion(sink); | 240 var c = BASE64.encoder.startChunkedConversion(sink); |
229 c.add(list.sublist(0, i)); | 241 c.add(list.sublist(0, i)); |
230 c.add(list.sublist(i, j)); | 242 c.add(list.sublist(i, j)); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 BASE64.decoder.startChunkedConversion(new TestSink<List<int>>()); | 289 BASE64.decoder.startChunkedConversion(new TestSink<List<int>>()); |
278 ByteConversionSink encodeSink = | 290 ByteConversionSink encodeSink = |
279 BASE64.encoder.startChunkedConversion(new TestSink<String>()); | 291 BASE64.encoder.startChunkedConversion(new TestSink<String>()); |
280 } | 292 } |
281 | 293 |
282 // Implementation of Sink<T> to test type constraints. | 294 // Implementation of Sink<T> to test type constraints. |
283 class TestSink<T> implements Sink<T> { | 295 class TestSink<T> implements Sink<T> { |
284 void add(T value) {} | 296 void add(T value) {} |
285 void close() {} | 297 void close() {} |
286 } | 298 } |
OLD | NEW |