| 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:crypto'; | 8 import 'dart:crypto'; |
| 9 import 'dart:math'; |
| 9 | 10 |
| 10 // Data from http://tools.ietf.org/html/rfc4648. | 11 // Data from http://tools.ietf.org/html/rfc4648. |
| 11 var inputs = | 12 var inputs = |
| 12 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar' ]; | 13 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; |
| 13 var results = | 14 var results = |
| 14 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy' ]; | 15 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; |
| 16 |
| 17 // Test data with only zeroes. |
| 18 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; |
| 19 var resultsWithZeroes = ['AAAA', 'AAA=', 'AA==', '']; |
| 15 | 20 |
| 16 var longLine = | 21 var longLine = |
| 17 "Man is distinguished, not only by his reason, but by this singular " | 22 "Man is distinguished, not only by his reason, but by this singular " |
| 18 "passion from other animals, which is a lust of the mind, that by a " | 23 "passion from other animals, which is a lust of the mind, that by a " |
| 19 "perseverance of delight in the continued and indefatigable generation " | 24 "perseverance of delight in the continued and indefatigable generation " |
| 20 "of knowledge, exceeds the short vehemence of any carnal pleasure."; | 25 "of knowledge, exceeds the short vehemence of any carnal pleasure."; |
| 21 | 26 |
| 22 var longLineResult = | 27 var longLineResult = |
| 23 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" | 28 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm" |
| 24 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" | 29 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" | 41 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" |
| 37 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" | 42 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" |
| 38 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" | 43 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" |
| 39 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" | 44 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" |
| 40 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" | 45 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" |
| 41 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" | 46 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" |
| 42 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" | 47 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" |
| 43 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" | 48 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" |
| 44 "5hbCBwbGVhc3VyZS4="; | 49 "5hbCBwbGVhc3VyZS4="; |
| 45 | 50 |
| 46 void main() { | 51 testEncoder() { |
| 47 for (var i = 0; i < inputs.length; i++) { | 52 for (var i = 0; i < inputs.length; i++) { |
| 48 var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits); | 53 Expect.equals(results[i], CryptoUtils.bytesToBase64(inputs[i].codeUnits)); |
| 49 Expect.equals(results[i], enc); | |
| 50 } | 54 } |
| 51 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, 76), | 55 for (var i = 0; i < inputsWithZeroes.length; i++) { |
| 52 longLineResult); | 56 Expect.equals(resultsWithZeroes[i], |
| 57 CryptoUtils.bytesToBase64(inputsWithZeroes[i])); |
| 58 } |
| 59 Expect.equals( |
| 60 CryptoUtils.bytesToBase64(longLine.codeUnits, addLineSeparator : true), |
| 61 longLineResult); |
| 53 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits), | 62 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits), |
| 54 longLineResultNoBreak); | 63 longLineResultNoBreak); |
| 55 } | 64 } |
| 65 |
| 66 testDecoder() { |
| 67 for (var i = 0; i < results.length; i++) { |
| 68 Expect.equals(inputs[i], |
| 69 new String.fromCharCodes(CryptoUtils.base64StringToBytes(results[i]))); |
| 70 } |
| 71 for (var i = 0; i < resultsWithZeroes.length; i++) { |
| 72 Expect.listEquals(inputsWithZeroes[i], |
| 73 CryptoUtils.base64StringToBytes(resultsWithZeroes[i])); |
| 74 } |
| 75 var longLineDecoded = CryptoUtils.base64StringToBytes(longLineResult); |
| 76 Expect.equals(new String.fromCharCodes(longLineDecoded), longLine); |
| 77 var longLineResultNoBreak = CryptoUtils.base64StringToBytes(longLineResult); |
| 78 Expect.equals(new String.fromCharCodes(longLineResultNoBreak), longLine); |
| 79 } |
| 80 |
| 81 testDecoderForMalformedInput() { |
| 82 Expect.throws(() { |
| 83 CryptoUtils.base64StringToBytes('AB~', ignoreInvalidCharacters: false); |
| 84 }, (e) => e is FormatException); |
| 85 |
| 86 Expect.throws(() { |
| 87 CryptoUtils.base64StringToBytes('A'); |
| 88 }, (e) => e is FormatException); |
| 89 |
| 90 Expect.listEquals('f'.codeUnits, |
| 91 CryptoUtils.base64StringToBytes('~~Zg==@@@', |
| 92 ignoreInvalidCharacters: true)); |
| 93 } |
| 94 |
| 95 testUrlSafeEncodeDecode() { |
| 96 List<int> decUrlSafe = CryptoUtils.base64StringToBytes('-_A='); |
| 97 List<int> dec = CryptoUtils.base64StringToBytes('+/A='); |
| 98 Expect.listEquals(decUrlSafe, dec); |
| 99 Expect.equals('-_A=', CryptoUtils.bytesToBase64(dec, urlSafe: true)); |
| 100 Expect.equals('+/A=', CryptoUtils.bytesToBase64(dec)); |
| 101 } |
| 102 |
| 103 testEncodeDecodeLists() { |
| 104 for (int i = 0; i < 10; i++) { |
| 105 for (int j = 0; j < 256 - i; j++) { |
| 106 List<int> x = new List<int>(i); |
| 107 for (int k = 0; k < i; k++) { |
| 108 x[k] = j; |
| 109 } |
| 110 var enc = CryptoUtils.bytesToBase64(x); |
| 111 var dec = CryptoUtils.base64StringToBytes(enc); |
| 112 Expect.listEquals(x, dec); |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 fillRandom(List<int> l) { |
| 118 var random = new Random(0xBABE); |
| 119 for(int j=0; j < l.length; j++) { |
| 120 l[j] = random.nextInt(255); |
| 121 } |
| 122 } |
| 123 |
| 124 testPerformance() { |
| 125 var l = new List<int>(1024); |
| 126 var iters = 5000; |
| 127 fillRandom(l); |
| 128 String enc; |
| 129 var w = new Stopwatch()..start(); |
| 130 for( int i = 0; i < iters; ++i ) { |
| 131 enc = CryptoUtils.bytesToBase64(l); |
| 132 } |
| 133 int ms = w.elapsedMilliseconds; |
| 134 int perSec = (iters * l.length) * 1000 ~/ ms; |
| 135 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); |
| 136 w..reset(); |
| 137 for( int i = 0; i < iters; ++i ) { |
| 138 CryptoUtils.base64StringToBytes(enc); |
| 139 } |
| 140 ms = w.elapsedMilliseconds; |
| 141 perSec = (iters * l.length) * 1000 ~/ ms; |
| 142 // print('''Decode into ${l.length} bytes for $iters |
| 143 // times: $ms msec. $perSec b/s'''); |
| 144 } |
| 145 |
| 146 void main() { |
| 147 testEncoder(); |
| 148 testDecoder(); |
| 149 testDecoderForMalformedInput(); |
| 150 testEncodeDecodeLists(); |
| 151 testUrlSafeEncodeDecode(); |
| 152 testPerformance(); |
| 153 } |
| OLD | NEW |