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