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