OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Library tag to allow the test to run on Dartium. |
| 6 library base64_test; |
| 7 |
| 8 import 'dart:math'; |
| 9 import 'dart:async'; |
| 10 |
| 11 import "package:crypto/crypto.dart"; |
| 12 import "package:test/test.dart"; |
| 13 |
| 14 void main() { |
| 15 test('encoder', _testEncoder); |
| 16 test('decoder', _testDecoder); |
| 17 test('decoder for malformed input', _testDecoderForMalformedInput); |
| 18 test('encode decode lists', _testEncodeDecodeLists); |
| 19 test('url safe encode-decode', _testUrlSafeEncodeDecode); |
| 20 test('percent-encoded padding character encode-decode', |
| 21 _testPaddingCharacter); |
| 22 test('streaming encoder', _testStreamingEncoder); |
| 23 test('streaming decoder', _testStreamingDecoder); |
| 24 test('streaming decoder for malformed input', |
| 25 _testStreamingDecoderForMalformedInput); |
| 26 test('streaming encoder for different decompositions of a list of bytes', |
| 27 _testStreamingEncoderForDecompositions); |
| 28 test('streaming decoder for different decompositions of a string', |
| 29 _testStreamingDecoderForDecompositions); |
| 30 test('streaming for encoded padding character', |
| 31 _testStreamingForEncodedPadding); |
| 32 test('old api', _testOldApi); |
| 33 test('url safe streaming encoder/decoder', _testUrlSafeStreaming); |
| 34 test('performance', _testPerformance); |
| 35 |
| 36 |
| 37 } |
| 38 |
| 39 // Data from http://tools.ietf.org/html/rfc4648. |
| 40 const _INPUTS = |
| 41 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; |
| 42 const _RESULTS = |
| 43 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; |
| 44 |
| 45 const _PADDING_INPUT = const [2, 8]; |
| 46 |
| 47 var _STREAMING_ENCODER_INPUT = |
| 48 [[102, 102], [111, 102], |
| 49 [111, 111, 102, 111, 111, 98, 102, 111, |
| 50 111, 98, 97, 102, 111, 111, 98, 97, 114]]; |
| 51 |
| 52 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy'; |
| 53 const _STREAMING_DECODER_INPUT = |
| 54 const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI=']; |
| 55 const _STREAMING_DECODED = |
| 56 const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114]; |
| 57 const _STREAMING_DECODER_INPUT_FOR_ZEROES = |
| 58 const ['AAAA', 'AAA=', 'AA==', '']; |
| 59 var _STREAMING_DECODED_ZEROES = [0, 0, 0, 0, 0, 0]; |
| 60 |
| 61 var _DECOMPOSITIONS_FOR_DECODING = [ |
| 62 ["A", "", "BCD"], ["A", "BCD", "", ""], ["A", "B", "", "", "CD", ""], |
| 63 ["", "A", "BC", "", "D"], ["", "AB", "C", "", "", "D"], ["AB", "CD", ""], |
| 64 ["", "ABC", "", "D"], ["", "ABC", "D", ""], ["", "", "ABCD", ""], |
| 65 ["A", "B", "C", "D"], ["", "A", "B", "C", "D", ""], |
| 66 ["", "A", "B", "", "", "C", "", "D", ""]]; |
| 67 |
| 68 const _DECOMPOSITION_DECODED = const [0, 16, 131]; |
| 69 |
| 70 var _DECOMPOSITIONS_FOR_ENCODING = [ |
| 71 [[196, 16], [], [158], [196]], |
| 72 [[196, 16], [158, 196], [], []], |
| 73 [[196], [], [16], [], [], [158], [], [196]], |
| 74 [[196], [], [16], [158, 196], [], []], |
| 75 [[], [196], [], [], [16, 158], [], [196]], |
| 76 [[], [196], [16, 158, 196], []], |
| 77 [[196, 16, 158], [], [], [196]], |
| 78 [[196, 16, 158], [], [196], []], |
| 79 [[196, 16, 158, 196], [], [], []]]; |
| 80 |
| 81 const _DECOMPOSITION_ENCODED = 'xBCexA=='; |
| 82 |
| 83 // Test data with only zeroes. |
| 84 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; |
| 85 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; |
| 86 |
| 87 const _LONG_LINE = |
| 88 "Man is distinguished, not only by his reason, but by this singular " |
| 89 "passion from other animals, which is a lust of the mind, that by a " |
| 90 "perseverance of delight in the continued and indefatigable generation " |
| 91 "of knowledge, exceeds the short vehemence of any carnal pleasure."; |
| 92 |
| 93 const _LONG_LINE_RESULT = |
| 94 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" |
| 95 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" |
| 96 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" |
| 97 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n" |
| 98 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" |
| 99 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n" |
| 100 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" |
| 101 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n" |
| 102 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" |
| 103 "5hbCBwbGVhc3VyZS4="; |
| 104 |
| 105 const _LONG_LINE_RESULT_NO_BREAK = |
| 106 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" |
| 107 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" |
| 108 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" |
| 109 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" |
| 110 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" |
| 111 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" |
| 112 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" |
| 113 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" |
| 114 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" |
| 115 "5hbCBwbGVhc3VyZS4="; |
| 116 |
| 117 void _testEncoder() { |
| 118 for (var i = 0; i < _INPUTS.length; i++) { |
| 119 expect(BASE64.encode(_INPUTS[i].codeUnits), _RESULTS[i]); |
| 120 } |
| 121 for (var i = 0; i < inputsWithZeroes.length; i++) { |
| 122 expect(BASE64.encode(inputsWithZeroes[i]), |
| 123 _RESULTS_WITH_ZEROS[i]); |
| 124 } |
| 125 expect(BASE64.encode(_LONG_LINE.codeUnits, addLineSeparator : true), |
| 126 _LONG_LINE_RESULT); |
| 127 expect(BASE64.encode(_LONG_LINE.codeUnits), |
| 128 _LONG_LINE_RESULT_NO_BREAK); |
| 129 } |
| 130 |
| 131 void _testDecoder() { |
| 132 for (var i = 0; i < _RESULTS.length; i++) { |
| 133 expect( |
| 134 new String.fromCharCodes(BASE64.decode(_RESULTS[i])), |
| 135 _INPUTS[i]); |
| 136 } |
| 137 |
| 138 for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) { |
| 139 expect(BASE64.decode(_RESULTS_WITH_ZEROS[i]), |
| 140 inputsWithZeroes[i]); |
| 141 } |
| 142 |
| 143 var longLineDecoded = BASE64.decode(_LONG_LINE_RESULT); |
| 144 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); |
| 145 |
| 146 var longLineResultNoBreak = BASE64.decode(_LONG_LINE_RESULT); |
| 147 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); |
| 148 } |
| 149 |
| 150 void _testPaddingCharacter() { |
| 151 var encoded = BASE64.encode(_PADDING_INPUT, encodePaddingCharacter: true); |
| 152 expect(encoded, 'Agg%3D'); |
| 153 expect(BASE64.decode(encoded), _PADDING_INPUT); |
| 154 } |
| 155 |
| 156 Future _testStreamingEncoder() async { |
| 157 expect( |
| 158 await new Stream.fromIterable(_STREAMING_ENCODER_INPUT) |
| 159 .transform(BASE64.encoder) |
| 160 .join(), |
| 161 _STREAMING_ENCODED); |
| 162 } |
| 163 |
| 164 Future _testStreamingDecoder() async { |
| 165 expect( |
| 166 await new Stream.fromIterable(_STREAMING_DECODER_INPUT) |
| 167 .transform(BASE64.decoder) |
| 168 .expand((l) => l) |
| 169 .toList(), |
| 170 _STREAMING_DECODED); |
| 171 |
| 172 expect( |
| 173 await new Stream.fromIterable(_STREAMING_DECODER_INPUT_FOR_ZEROES) |
| 174 .transform(BASE64.decoder) |
| 175 .expand((l) => l) |
| 176 .toList(), |
| 177 _STREAMING_DECODED_ZEROES); |
| 178 } |
| 179 |
| 180 Future _testStreamingDecoderForMalformedInput() async { |
| 181 expect(new Stream.fromIterable(['ABz']) |
| 182 .transform(BASE64.decoder) |
| 183 .toList(), |
| 184 throwsFormatException); |
| 185 |
| 186 expect(new Stream.fromIterable(['AB', 'Lx', 'z', 'xx']) |
| 187 .transform(BASE64.decoder) |
| 188 .toList(), |
| 189 throwsFormatException); |
| 190 } |
| 191 |
| 192 Future _testStreamingEncoderForDecompositions() async { |
| 193 for(var decomposition in _DECOMPOSITIONS_FOR_ENCODING) { |
| 194 expect( |
| 195 await new Stream.fromIterable(decomposition) |
| 196 .transform(BASE64.encoder) |
| 197 .join(), |
| 198 _DECOMPOSITION_ENCODED); |
| 199 } |
| 200 } |
| 201 |
| 202 Future _testStreamingDecoderForDecompositions() async { |
| 203 for(var decomposition in _DECOMPOSITIONS_FOR_DECODING) { |
| 204 expect( |
| 205 await new Stream.fromIterable(decomposition) |
| 206 .transform(BASE64.decoder) |
| 207 .expand((x) => x) |
| 208 .toList(), |
| 209 _DECOMPOSITION_DECODED); |
| 210 } |
| 211 } |
| 212 |
| 213 void _testDecoderForMalformedInput() { |
| 214 expect(() { |
| 215 BASE64.decode('AB~'); |
| 216 }, throwsFormatException); |
| 217 |
| 218 expect(() { |
| 219 BASE64.decode('A'); |
| 220 }, throwsFormatException); |
| 221 } |
| 222 |
| 223 Future _testUrlSafeStreaming() async { |
| 224 String encUrlSafe = '-_A='; |
| 225 List<List<int>> dec = [BASE64.decode('+/A=')]; |
| 226 var streamedResult = await new Stream.fromIterable(dec) |
| 227 .transform(new Base64Encoder(urlSafe: true)).join(); |
| 228 |
| 229 expect(streamedResult, encUrlSafe); |
| 230 } |
| 231 |
| 232 Future _testStreamingForEncodedPadding() async { |
| 233 List<String> withEncodedPadding = ['AA%', '3D', '%', '3', 'DEFGZ']; |
| 234 List<int> decoded = BASE64.decode('AA==EFGZ'); |
| 235 var streamedResult = await new Stream.fromIterable(withEncodedPadding) |
| 236 .transform(BASE64.decoder).expand((x) => x).toList(); |
| 237 |
| 238 expect(streamedResult, decoded); |
| 239 } |
| 240 |
| 241 void _testUrlSafeEncodeDecode() { |
| 242 List<int> decUrlSafe = BASE64.decode('-_A='); |
| 243 List<int> dec = BASE64.decode('+/A='); |
| 244 expect(decUrlSafe, orderedEquals(dec)); |
| 245 expect(BASE64.encode(dec, urlSafe: true), '-_A='); |
| 246 expect(BASE64.encode(dec), '+/A='); |
| 247 } |
| 248 |
| 249 void _testEncodeDecodeLists() { |
| 250 for (int i = 0; i < 10; i++) { |
| 251 for (int j = 0; j < 256 - i; j++) { |
| 252 List<int> x = new List<int>(i); |
| 253 for (int k = 0; k < i; k++) { |
| 254 x[k] = j; |
| 255 } |
| 256 var enc = BASE64.encode(x); |
| 257 var dec = BASE64.decode(enc); |
| 258 expect(dec, orderedEquals(x)); |
| 259 } |
| 260 } |
| 261 } |
| 262 |
| 263 void _fillRandom(List<int> l) { |
| 264 var random = new Random(0xBABE); |
| 265 for (int j = 0; j < l.length; j++) { |
| 266 l[j] = random.nextInt(255); |
| 267 } |
| 268 } |
| 269 |
| 270 void _testOldApi() { |
| 271 for (int i = 0; i < _INPUTS.length; i++) { |
| 272 expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]); |
| 273 expect(CryptoUtils.base64StringToBytes(_RESULTS[i]), _INPUTS[i].codeUnits); |
| 274 } |
| 275 } |
| 276 |
| 277 void _testPerformance() { |
| 278 var l = new List<int>(1024); |
| 279 var iters = 5000; |
| 280 _fillRandom(l); |
| 281 String enc; |
| 282 var w = new Stopwatch()..start(); |
| 283 for( int i = 0; i < iters; ++i ) { |
| 284 enc = BASE64.encode(l); |
| 285 } |
| 286 int ms = w.elapsedMilliseconds; |
| 287 int perSec = (iters * l.length) * 1000 ~/ ms; |
| 288 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); |
| 289 w..reset(); |
| 290 for( int i = 0; i < iters; ++i ) { |
| 291 BASE64.decode(enc); |
| 292 } |
| 293 ms = w.elapsedMilliseconds; |
| 294 perSec = (iters * l.length) * 1000 ~/ ms; |
| 295 // ('''Decode into ${l.length} bytes for $iters |
| 296 // times: $ms msec. $perSec b/s'''); |
| 297 } |
OLD | NEW |