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 // Library tag to allow the test to run on Dartium. | 5 // Library tag to allow the test to run on Dartium. |
6 library base64_test; | 6 library base64_test; |
7 | 7 |
8 import 'dart:math'; | 8 import 'dart:math'; |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 | 10 |
11 import "package:crypto/crypto.dart"; | 11 import "package:crypto/crypto.dart"; |
12 import "package:test/test.dart"; | 12 import "package:test/test.dart"; |
13 | 13 |
14 void main() { | 14 void main() { |
15 test('encoder', _testEncoder); | 15 test('encoder', _testEncoder); |
16 test('decoder', _testDecoder); | 16 test('decoder', _testDecoder); |
17 test('decoder for malformed input', _testDecoderForMalformedInput); | 17 test('decoder for malformed input', _testDecoderForMalformedInput); |
18 test('encode decode lists', _testEncodeDecodeLists); | 18 test('encode decode lists', _testEncodeDecodeLists); |
19 test('url safe encode-decode', _testUrlSafeEncodeDecode); | 19 test('url safe encode-decode', _testUrlSafeEncodeDecode); |
20 test('consistent safe/unsafe character decoding', | 20 test('consistent safe/unsafe character decoding', |
21 _testConsistentSafeUnsafeDecode); | 21 _testConsistentSafeUnsafeDecode); |
| 22 test('percent-encoded padding character encode-decode', |
| 23 _testPaddingCharacter); |
22 test('streaming encoder', _testStreamingEncoder); | 24 test('streaming encoder', _testStreamingEncoder); |
23 test('streaming decoder', _testStreamingDecoder); | 25 test('streaming decoder', _testStreamingDecoder); |
24 test('streaming decoder for malformed input', | 26 test('streaming decoder for malformed input', |
25 _testStreamingDecoderForMalformedInput); | 27 _testStreamingDecoderForMalformedInput); |
26 test('streaming encoder for different decompositions of a list of bytes', | 28 test('streaming encoder for different decompositions of a list of bytes', |
27 _testStreamingEncoderForDecompositions); | 29 _testStreamingEncoderForDecompositions); |
28 test('streaming decoder for different decompositions of a string', | 30 test('streaming decoder for different decompositions of a string', |
29 _testStreamingDecoderForDecompositions); | 31 _testStreamingDecoderForDecompositions); |
30 test('consistent safe/unsafe character streaming decoding', | 32 test('consistent safe/unsafe character streaming decoding', |
31 _testConsistentSafeUnsafeStreamDecode); | 33 _testConsistentSafeUnsafeStreamDecode); |
| 34 test('streaming for encoded padding character', |
| 35 _testStreamingForEncodedPadding); |
32 test('old api', _testOldApi); | 36 test('old api', _testOldApi); |
33 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); | 37 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); |
34 test('performance', _testPerformance); | 38 test('performance', _testPerformance); |
35 | 39 |
36 | 40 |
37 } | 41 } |
38 | 42 |
39 // Data from http://tools.ietf.org/html/rfc4648. | 43 // Data from http://tools.ietf.org/html/rfc4648. |
40 const _INPUTS = | 44 const _INPUTS = |
41 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; | 45 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; |
42 const _RESULTS = | 46 const _RESULTS = |
43 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; | 47 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; |
| 48 |
| 49 const _PADDING_INPUT = const [2, 8]; |
| 50 |
44 var _STREAMING_ENCODER_INPUT = | 51 var _STREAMING_ENCODER_INPUT = |
45 [[102, 102], [111, 102], | 52 [[102, 102], [111, 102], |
46 [111, 111, 102, 111, 111, 98, 102, 111, | 53 [111, 111, 102, 111, 111, 98, 102, 111, |
47 111, 98, 97, 102, 111, 111, 98, 97, 114]]; | 54 111, 98, 97, 102, 111, 111, 98, 97, 114]]; |
48 | 55 |
49 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy'; | 56 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy'; |
50 const _STREAMING_DECODER_INPUT = | 57 const _STREAMING_DECODER_INPUT = |
51 const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI=']; | 58 const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI=']; |
52 const _STREAMING_DECODED = | 59 const _STREAMING_DECODED = |
53 const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114]; | 60 const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114]; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 inputsWithZeroes[i]); | 148 inputsWithZeroes[i]); |
142 } | 149 } |
143 | 150 |
144 var longLineDecoded = BASE64.decode(_LONG_LINE_RESULT); | 151 var longLineDecoded = BASE64.decode(_LONG_LINE_RESULT); |
145 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); | 152 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); |
146 | 153 |
147 var longLineResultNoBreak = BASE64.decode(_LONG_LINE_RESULT); | 154 var longLineResultNoBreak = BASE64.decode(_LONG_LINE_RESULT); |
148 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); | 155 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); |
149 } | 156 } |
150 | 157 |
| 158 void _testPaddingCharacter() { |
| 159 var encoded = BASE64.encode(_PADDING_INPUT, encodePaddingCharacter: true); |
| 160 expect(encoded, 'Agg%3D'); |
| 161 expect(BASE64.decode(encoded), _PADDING_INPUT); |
| 162 } |
| 163 |
151 Future _testStreamingEncoder() async { | 164 Future _testStreamingEncoder() async { |
152 expect( | 165 expect( |
153 await new Stream.fromIterable(_STREAMING_ENCODER_INPUT) | 166 await new Stream.fromIterable(_STREAMING_ENCODER_INPUT) |
154 .transform(BASE64.encoder) | 167 .transform(BASE64.encoder) |
155 .join(), | 168 .join(), |
156 _STREAMING_ENCODED); | 169 _STREAMING_ENCODED); |
157 } | 170 } |
158 | 171 |
159 Future _testStreamingDecoder() async { | 172 Future _testStreamingDecoder() async { |
160 expect( | 173 expect( |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 }, throwsFormatException); | 243 }, throwsFormatException); |
231 } | 244 } |
232 | 245 |
233 Future _testConsistentSafeUnsafeStreamDecode() { | 246 Future _testConsistentSafeUnsafeStreamDecode() { |
234 expect(new Stream.fromIterable(_INCONSISTENT_SAFE_STREAMING_RESULT) | 247 expect(new Stream.fromIterable(_INCONSISTENT_SAFE_STREAMING_RESULT) |
235 .transform(BASE64.decoder) | 248 .transform(BASE64.decoder) |
236 .toList(), | 249 .toList(), |
237 throwsFormatException); | 250 throwsFormatException); |
238 } | 251 } |
239 | 252 |
| 253 Future _testStreamingForEncodedPadding() async { |
| 254 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; |
| 255 List<int> decoded = BASE64.decode('AA==EFGZ'); |
| 256 var streamedResult = await new Stream.fromIterable(withEncodedPadding) |
| 257 .transform(BASE64.decoder).expand((x) => x).toList(); |
| 258 |
| 259 expect(streamedResult, decoded); |
| 260 } |
| 261 |
240 void _testUrlSafeEncodeDecode() { | 262 void _testUrlSafeEncodeDecode() { |
241 List<int> decUrlSafe = BASE64.decode('-_A='); | 263 List<int> decUrlSafe = BASE64.decode('-_A='); |
242 List<int> dec = BASE64.decode('+/A='); | 264 List<int> dec = BASE64.decode('+/A='); |
243 expect(decUrlSafe, orderedEquals(dec)); | 265 expect(decUrlSafe, orderedEquals(dec)); |
244 expect(BASE64.encode(dec, urlSafe: true), '-_A='); | 266 expect(BASE64.encode(dec, urlSafe: true), '-_A='); |
245 expect(BASE64.encode(dec), '+/A='); | 267 expect(BASE64.encode(dec), '+/A='); |
246 } | 268 } |
247 | 269 |
248 void _testEncodeDecodeLists() { | 270 void _testEncodeDecodeLists() { |
249 for (int i = 0; i < 10; i++) { | 271 for (int i = 0; i < 10; i++) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); | 309 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); |
288 w..reset(); | 310 w..reset(); |
289 for( int i = 0; i < iters; ++i ) { | 311 for( int i = 0; i < iters; ++i ) { |
290 BASE64.decode(enc); | 312 BASE64.decode(enc); |
291 } | 313 } |
292 ms = w.elapsedMilliseconds; | 314 ms = w.elapsedMilliseconds; |
293 perSec = (iters * l.length) * 1000 ~/ ms; | 315 perSec = (iters * l.length) * 1000 ~/ ms; |
294 // ('''Decode into ${l.length} bytes for $iters | 316 // ('''Decode into ${l.length} bytes for $iters |
295 // times: $ms msec. $perSec b/s'''); | 317 // times: $ms msec. $perSec b/s'''); |
296 } | 318 } |
OLD | NEW |