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 [0x00], |
12 [0xff, 0x00], | 12 [0xff, 0x00], |
13 [0xff, 0xaa, 0x55], | 13 [0xff, 0xaa, 0x55], |
14 [0x00, 0x01, 0x02, 0x03], | 14 [0x00, 0x01, 0x02, 0x03], |
15 new Iterable.generate(13).toList(), | 15 new Iterable.generate(13).toList(), |
16 new Iterable.generate(254).toList(), | 16 new Iterable.generate(254).toList(), |
17 new Iterable.generate(255).toList(), | 17 new Iterable.generate(255).toList(), |
18 new Iterable.generate(256).toList()]) { | 18 new Iterable.generate(256).toList()]) { |
19 testRoundtrip(list, "List#${list.length}"); | 19 testRoundtrip(list, "List#${list.length}"); |
20 testRoundtrip(new Uint8List.fromList(list), "Uint8List#${list.length}"); | 20 testRoundtrip(new Uint8List.fromList(list), "Uint8List#${list.length}"); |
21 } | 21 } |
22 testErrors(); | 22 testErrors(); |
| 23 |
| 24 // Decoder is lenienet with mixed styles. |
| 25 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA%3D=")); |
| 26 Expect.listEquals([0xfb, 0xff, 0xbf, 0x00], BASE64.decode("-_+/AA=%3D")); |
23 } | 27 } |
24 | 28 |
25 void testRoundtrip(list, name) { | 29 void testRoundtrip(list, name) { |
26 // Direct. | 30 // Direct. |
27 String encoded = BASE64.encode(list); | 31 String encodedNormal = BASE64.encode(list); |
28 List result = BASE64.decode(encoded); | 32 String encodedPercent = encodedNormal.replaceAll("=", "%3D"); |
| 33 String uriEncoded = encodedNormal.replaceAll("+", "-").replaceAll("/", "_"); |
| 34 List result = BASE64.decode(encodedNormal); |
| 35 Expect.listEquals(list, result, name); |
| 36 result = BASE64.decode(encodedPercent); |
29 Expect.listEquals(list, result, name); | 37 Expect.listEquals(list, result, name); |
30 | 38 |
31 int increment = list.length ~/ 7 + 1; | 39 int increment = list.length ~/ 7 + 1; |
32 // Chunked. | 40 // Chunked. |
33 for (int i = 0; i < list.length; i += increment) { | 41 for (int i = 0; i < list.length; i += increment) { |
34 for (int j = i; j < list.length; j += increment) { | 42 for (int j = i; j < list.length; j += increment) { |
35 { | 43 { |
36 // Using add/close | 44 // Using add/close |
37 var results; | 45 var results; |
38 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 46 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); |
39 var encoder = BASE64.encoder.startChunkedConversion(sink); | 47 var encoder = BASE64.encoder.startChunkedConversion(sink); |
40 encoder.add(list.sublist(0, i)); | 48 encoder.add(list.sublist(0, i)); |
41 encoder.add(list.sublist(i, j)); | 49 encoder.add(list.sublist(i, j)); |
42 encoder.add(list.sublist(j, list.length)); | 50 encoder.add(list.sublist(j, list.length)); |
43 encoder.close(); | 51 encoder.close(); |
44 var name = "0-$i-$j-${list.length}: list"; | 52 var name = "0-$i-$j-${list.length}: list"; |
45 Expect.equals(encoded, results.join(""), name); | 53 Expect.equals(encodedNormal, results.join(""), name); |
46 } | 54 } |
47 { | 55 { |
48 // Using addSlice | 56 // Using addSlice |
49 var results; | 57 var results; |
50 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 58 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); |
51 var encoder = BASE64.encoder.startChunkedConversion(sink); | 59 var encoder = BASE64.encoder.startChunkedConversion(sink); |
52 encoder.addSlice(list, 0, i, false); | 60 encoder.addSlice(list, 0, i, false); |
53 encoder.addSlice(list, i, j, false); | 61 encoder.addSlice(list, i, j, false); |
54 encoder.addSlice(list, j, list.length, true); | 62 encoder.addSlice(list, j, list.length, true); |
55 var name = "0-$i-$j-${list.length}: $list"; | 63 var name = "0-$i-$j-${list.length}: $list"; |
56 Expect.equals(encoded, results.join(""), name); | 64 Expect.equals(encodedNormal, results.join(""), name); |
57 } | 65 } |
58 } | 66 } |
59 } | 67 } |
60 | 68 |
61 increment = encoded.length ~/ 7 + 1; | 69 for (var encoded in [encodedNormal, encodedPercent, uriEncoded]) { |
62 for (int i = 0; i < encoded.length; i += increment) { | 70 increment = encoded.length ~/ 7 + 1; |
63 for (int j = i; j < encoded.length; j += increment) { | 71 for (int i = 0; i < encoded.length; i += increment) { |
64 { | 72 for (int j = i; j < encoded.length; j += increment) { |
65 // Using add/close | 73 { |
66 var results; | 74 // Using add/close |
67 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 75 var results; |
68 var decoder = BASE64.decoder.startChunkedConversion(sink); | 76 var sink = |
69 decoder.add(encoded.substring(0, i)); | 77 new ChunkedConversionSink.withCallback((v) { results = v; }); |
70 decoder.add(encoded.substring(i, j)); | 78 var decoder = BASE64.decoder.startChunkedConversion(sink); |
71 decoder.add(encoded.substring(j, encoded.length)); | 79 decoder.add(encoded.substring(0, i)); |
72 decoder.close(); | 80 decoder.add(encoded.substring(i, j)); |
73 var name = "0-$i-$j-${encoded.length}: $encoded"; | 81 decoder.add(encoded.substring(j, encoded.length)); |
74 Expect.listEquals(list, results.expand((x)=>x).toList(), name); | 82 decoder.close(); |
75 } | 83 var name = "0-$i-$j-${encoded.length}: $encoded"; |
76 { | 84 Expect.listEquals(list, results.expand((x)=>x).toList(), name); |
77 // Using addSlice | 85 } |
78 var results; | 86 { |
79 var sink = new ChunkedConversionSink.withCallback((v) { results = v; }); | 87 // Using addSlice |
80 var decoder = BASE64.decoder.startChunkedConversion(sink); | 88 var results; |
81 decoder.addSlice(encoded, 0, i, false); | 89 var sink = |
82 decoder.addSlice(encoded, i, j, false); | 90 new ChunkedConversionSink.withCallback((v) { results = v; }); |
83 decoder.addSlice(encoded, j, encoded.length, true); | 91 var decoder = BASE64.decoder.startChunkedConversion(sink); |
84 var name = "0-$i-$j-${encoded.length}: $encoded"; | 92 decoder.addSlice(encoded, 0, i, false); |
85 Expect.listEquals(list, results.expand((x)=>x).toList(), name); | 93 decoder.addSlice(encoded, i, j, false); |
| 94 decoder.addSlice(encoded, j, encoded.length, true); |
| 95 var name = "0-$i-$j-${encoded.length}: $encoded"; |
| 96 Expect.listEquals(list, results.expand((x)=>x).toList(), name); |
| 97 } |
86 } | 98 } |
87 } | 99 } |
88 } | 100 } |
89 } | 101 } |
90 | 102 |
91 bool isFormatException(e) => e is FormatException; | 103 bool isFormatException(e) => e is FormatException; |
92 bool isArgumentError(e) => e is ArgumentError; | 104 bool isArgumentError(e) => e is ArgumentError; |
93 | 105 |
94 void testErrors() { | 106 void testErrors() { |
95 void badChunkDecode(List<String> list) { | 107 void badChunkDecode(List<String> list) { |
(...skipping 20 matching lines...) Expand all Loading... |
116 badDecode("A"); | 128 badDecode("A"); |
117 badDecode("AA"); | 129 badDecode("AA"); |
118 badDecode("AAA"); | 130 badDecode("AAA"); |
119 badDecode("AAAAA"); | 131 badDecode("AAAAA"); |
120 badDecode("AAAAAA"); | 132 badDecode("AAAAAA"); |
121 badDecode("AAAAAAA"); | 133 badDecode("AAAAAAA"); |
122 badDecode("AAAA="); | 134 badDecode("AAAA="); |
123 badDecode("AAAA=="); | 135 badDecode("AAAA=="); |
124 badDecode("AAAA==="); | 136 badDecode("AAAA==="); |
125 badDecode("AAAA===="); | 137 badDecode("AAAA===="); |
| 138 badDecode("AAAA%"); |
| 139 badDecode("AAAA%3"); |
| 140 badDecode("AAAA%3D"); |
| 141 badDecode("AAA%3D%"); |
| 142 badDecode("AAA%3D="); |
126 badDecode("A="); | 143 badDecode("A="); |
127 badDecode("A=A"); | 144 badDecode("A=A"); |
128 badDecode("A=="); | 145 badDecode("A=="); |
129 badDecode("A==A"); | 146 badDecode("A==A"); |
130 badDecode("A==="); | 147 badDecode("A==="); |
131 badDecode("===="); | 148 badDecode("===="); |
132 badDecode("AA="); | 149 badDecode("AA="); |
| 150 badDecode("AA%="); |
| 151 badDecode("AA%3"); |
| 152 badDecode("AA%3D"); |
133 badDecode("AA==="); | 153 badDecode("AA==="); |
134 badDecode("AAA=="); | 154 badDecode("AAA=="); |
135 badDecode("AAA=AAAA"); | 155 badDecode("AAA=AAAA"); |
136 badDecode("AAA-"); | |
137 badDecode("AAA_"); | |
138 badDecode("AAA\x00"); | 156 badDecode("AAA\x00"); |
139 badDecode("AAA=\x00"); | 157 badDecode("AAA=\x00"); |
140 badDecode("AAA\x80"); | 158 badDecode("AAA\x80"); |
141 badDecode("AAA\xFF"); | 159 badDecode("AAA\xFF"); |
142 badDecode("AAA\u{141}"); | 160 badDecode("AAA\u{141}"); |
143 badDecode("AAA\u{1041}"); | 161 badDecode("AAA\u{1041}"); |
144 badDecode("AAA\u{10041}"); | 162 badDecode("AAA\u{10041}"); |
| 163 badDecode("AA\u{141}="); |
| 164 badDecode("AA\u{1041}="); |
| 165 badDecode("AA\u{10041}="); |
145 | 166 |
146 var alphabet = | 167 var alphabet = |
147 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; | 168 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/-_"; |
148 var units = alphabet.codeUnits; | 169 var units = alphabet.codeUnits; |
149 for (int i = 0; i < 128; i++) { | 170 for (int i = 0; i < 128; i++) { |
150 if (!units.contains(i)) { | 171 if (!units.contains(i)) { |
151 badDecode(new String.fromCharCode(i) * 4); | 172 badDecode(new String.fromCharCode(i) * 4); |
152 } | 173 } |
153 } | 174 } |
154 | 175 |
155 badChunkDecode(["A", "A"]); | 176 badChunkDecode(["A", "A"]); |
156 badChunkDecode(["A", "A", "A"]); | 177 badChunkDecode(["A", "A", "A"]); |
157 badChunkDecode(["A", "A", "="]); | 178 badChunkDecode(["A", "A", "="]); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 badChunkEncode([invalid, 0, 0]); | 229 badChunkEncode([invalid, 0, 0]); |
209 } | 230 } |
210 | 231 |
211 badEncode(-1); | 232 badEncode(-1); |
212 badEncode(0x100); | 233 badEncode(0x100); |
213 badEncode(0x1000); | 234 badEncode(0x1000); |
214 badEncode(0x10000); | 235 badEncode(0x10000); |
215 badEncode(0x100000000); /// 01: ok | 236 badEncode(0x100000000); /// 01: ok |
216 badEncode(0x10000000000000000); /// 01: continued | 237 badEncode(0x10000000000000000); /// 01: continued |
217 } | 238 } |
OLD | NEW |