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', | |
21 _testConsistentSafeUnsafeDecode); | |
22 test('percent-encoded padding character encode-decode', | 20 test('percent-encoded padding character encode-decode', |
23 _testPaddingCharacter); | 21 _testPaddingCharacter); |
24 test('streaming encoder', _testStreamingEncoder); | 22 test('streaming encoder', _testStreamingEncoder); |
25 test('streaming decoder', _testStreamingDecoder); | 23 test('streaming decoder', _testStreamingDecoder); |
26 test('streaming decoder for malformed input', | 24 test('streaming decoder for malformed input', |
27 _testStreamingDecoderForMalformedInput); | 25 _testStreamingDecoderForMalformedInput); |
28 test('streaming encoder for different decompositions of a list of bytes', | 26 test('streaming encoder for different decompositions of a list of bytes', |
29 _testStreamingEncoderForDecompositions); | 27 _testStreamingEncoderForDecompositions); |
30 test('streaming decoder for different decompositions of a string', | 28 test('streaming decoder for different decompositions of a string', |
31 _testStreamingDecoderForDecompositions); | 29 _testStreamingDecoderForDecompositions); |
32 test('consistent safe/unsafe character streaming decoding', | |
33 _testConsistentSafeUnsafeStreamDecode); | |
34 test('streaming for encoded padding character', | 30 test('streaming for encoded padding character', |
35 _testStreamingForEncodedPadding); | 31 _testStreamingForEncodedPadding); |
36 test('old api', _testOldApi); | 32 test('old api', _testOldApi); |
37 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); | 33 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); |
38 test('performance', _testPerformance); | 34 test('performance', _testPerformance); |
39 | 35 |
40 | 36 |
41 } | 37 } |
42 | 38 |
43 // Data from http://tools.ietf.org/html/rfc4648. | 39 // Data from http://tools.ietf.org/html/rfc4648. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 [[196], [], [16], [], [], [158], [], [196]], | 73 [[196], [], [16], [], [], [158], [], [196]], |
78 [[196], [], [16], [158, 196], [], []], | 74 [[196], [], [16], [158, 196], [], []], |
79 [[], [196], [], [], [16, 158], [], [196]], | 75 [[], [196], [], [], [16, 158], [], [196]], |
80 [[], [196], [16, 158, 196], []], | 76 [[], [196], [16, 158, 196], []], |
81 [[196, 16, 158], [], [], [196]], | 77 [[196, 16, 158], [], [], [196]], |
82 [[196, 16, 158], [], [196], []], | 78 [[196, 16, 158], [], [196], []], |
83 [[196, 16, 158, 196], [], [], []]]; | 79 [[196, 16, 158, 196], [], [], []]]; |
84 | 80 |
85 const _DECOMPOSITION_ENCODED = 'xBCexA=='; | 81 const _DECOMPOSITION_ENCODED = 'xBCexA=='; |
86 | 82 |
87 const _INCONSISTENT_SAFE_RESULT = 'A+_x'; | 83 const _INCONSISTENT_SAFE_RESULT = 'A+_x'; |
Søren Gjesse
2015/08/05 07:18:33
Remove this constant and the next as well.
| |
88 | 84 |
89 const _INCONSISTENT_SAFE_STREAMING_RESULT = const ['A+AAA', '_x=']; | 85 const _INCONSISTENT_SAFE_STREAMING_RESULT = const ['A+AAA', '_x=']; |
90 | 86 |
91 // Test data with only zeroes. | 87 // Test data with only zeroes. |
92 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; | 88 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; |
93 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; | 89 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; |
94 | 90 |
95 const _LONG_LINE = | 91 const _LONG_LINE = |
96 "Man is distinguished, not only by his reason, but by this singular " | 92 "Man is distinguished, not only by his reason, but by this singular " |
97 "passion from other animals, which is a lust of the mind, that by a " | 93 "passion from other animals, which is a lust of the mind, that by a " |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 | 226 |
231 Future _testUrlSafeStreaming() async { | 227 Future _testUrlSafeStreaming() async { |
232 String encUrlSafe = '-_A='; | 228 String encUrlSafe = '-_A='; |
233 List<List<int>> dec = [BASE64.decode('+/A=')]; | 229 List<List<int>> dec = [BASE64.decode('+/A=')]; |
234 var streamedResult = await new Stream.fromIterable(dec) | 230 var streamedResult = await new Stream.fromIterable(dec) |
235 .transform(new Base64Encoder(urlSafe: true)).join(); | 231 .transform(new Base64Encoder(urlSafe: true)).join(); |
236 | 232 |
237 expect(streamedResult, encUrlSafe); | 233 expect(streamedResult, encUrlSafe); |
238 } | 234 } |
239 | 235 |
240 void _testConsistentSafeUnsafeDecode() { | |
241 expect(() { | |
242 BASE64.decode(_INCONSISTENT_SAFE_RESULT); | |
243 }, throwsFormatException); | |
244 } | |
245 | |
246 Future _testConsistentSafeUnsafeStreamDecode() { | |
247 expect(new Stream.fromIterable(_INCONSISTENT_SAFE_STREAMING_RESULT) | |
248 .transform(BASE64.decoder) | |
249 .toList(), | |
250 throwsFormatException); | |
251 } | |
252 | |
253 Future _testStreamingForEncodedPadding() async { | 236 Future _testStreamingForEncodedPadding() async { |
254 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; | 237 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; |
255 List<int> decoded = BASE64.decode('AA==EFGZ'); | 238 List<int> decoded = BASE64.decode('AA==EFGZ'); |
256 var streamedResult = await new Stream.fromIterable(withEncodedPadding) | 239 var streamedResult = await new Stream.fromIterable(withEncodedPadding) |
257 .transform(BASE64.decoder).expand((x) => x).toList(); | 240 .transform(BASE64.decoder).expand((x) => x).toList(); |
258 | 241 |
259 expect(streamedResult, decoded); | 242 expect(streamedResult, decoded); |
260 } | 243 } |
261 | 244 |
262 void _testUrlSafeEncodeDecode() { | 245 void _testUrlSafeEncodeDecode() { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); | 292 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); |
310 w..reset(); | 293 w..reset(); |
311 for( int i = 0; i < iters; ++i ) { | 294 for( int i = 0; i < iters; ++i ) { |
312 BASE64.decode(enc); | 295 BASE64.decode(enc); |
313 } | 296 } |
314 ms = w.elapsedMilliseconds; | 297 ms = w.elapsedMilliseconds; |
315 perSec = (iters * l.length) * 1000 ~/ ms; | 298 perSec = (iters * l.length) * 1000 ~/ ms; |
316 // ('''Decode into ${l.length} bytes for $iters | 299 // ('''Decode into ${l.length} bytes for $iters |
317 // times: $ms msec. $perSec b/s'''); | 300 // times: $ms msec. $perSec b/s'''); |
318 } | 301 } |
OLD | NEW |