Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: test/base64_test.dart

Issue 1159093002: Implement a Base64 codec (Closed) Base URL: https://github.com/dart-lang/crypto.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« lib/src/crypto_utils.dart ('K') | « lib/src/crypto_utils.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10
10 import "package:crypto/crypto.dart"; 11 import "package:crypto/crypto.dart";
11 import "package:test/test.dart"; 12 import "package:test/test.dart";
12 13
13 void main() { 14 void main() {
14 test('encoder', _testEncoder); 15 test('encoder', _testEncoder);
15 test('decoder', _testDecoder); 16 test('decoder', _testDecoder);
16 test('decoder for malformed input', _testDecoderForMalformedInput); 17 test('decoder for malformed input', _testDecoderForMalformedInput);
17 test('encode decode lists', _testEncodeDecodeLists); 18 test('encode decode lists', _testEncodeDecodeLists);
18 test('url safe encode-decode', _testUrlSafeEncodeDecode); 19 test('url safe encode-decode', _testUrlSafeEncodeDecode);
20 test('streaming encoder', _testStreamingEncoder);
21 test('streaming decoder', _testStreamingDecoder);
22 test('streaming decoder for malformed input', _testStreamingDecoderForMalforme dInput);
23 test('url safe streaming encoder/decoder', _testUrlSafeStreaming);
19 test('performance', _testPerformance); 24 test('performance', _testPerformance);
25
20 } 26 }
21 27
22 // Data from http://tools.ietf.org/html/rfc4648. 28 // Data from http://tools.ietf.org/html/rfc4648.
23 const _INPUTS = 29 const _INPUTS =
24 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar']; 30 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar'];
25 const _RESULTS = 31 const _RESULTS =
26 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy']; 32 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy'];
33 var _STREAMING_ENCODER_INPUT =
34 [[102, 102], [111, 102], [111, 111, 102, 111, 111, 98, 102, 111, 111, 98, 97 , 102, 111, 111, 98, 97, 114]];
Lasse Reichstein Nielsen 2015/05/29 06:48:43 long line.
Alexander Ivanov 2015/05/29 13:12:06 Done.
35
36 const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy';
37 const _STREAMING_DECODER_INPUT =
38 const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI='];
Lasse Reichstein Nielsen 2015/05/29 06:48:43 indent by four.
Alexander Ivanov 2015/05/29 13:12:06 Done.
39 const _STREAMING_DECODED =
40 const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114];
41 const _STREAMING_DECODER_INPUT_FOR_ZEROES =
42 const ['AAAA', 'AAA=', 'AA==', ''];
43 var _STREAMING_DECODED_ZEROES = [0, 0, 0, 0, 0, 0];
27 44
28 // Test data with only zeroes. 45 // Test data with only zeroes.
29 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []]; 46 var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []];
30 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', '']; 47 const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', ''];
31 48
32 const _LONG_LINE = 49 const _LONG_LINE =
33 "Man is distinguished, not only by his reason, but by this singular " 50 "Man is distinguished, not only by his reason, but by this singular "
34 "passion from other animals, which is a lust of the mind, that by a " 51 "passion from other animals, which is a lust of the mind, that by a "
35 "perseverance of delight in the continued and indefatigable generation " 52 "perseverance of delight in the continued and indefatigable generation "
36 "of knowledge, exceeds the short vehemence of any carnal pleasure."; 53 "of knowledge, exceeds the short vehemence of any carnal pleasure.";
(...skipping 17 matching lines...) Expand all
54 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" 71 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
55 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" 72 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm"
56 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" 73 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
57 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" 74 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX"
58 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" 75 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
59 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" 76 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
60 "5hbCBwbGVhc3VyZS4="; 77 "5hbCBwbGVhc3VyZS4=";
61 78
62 void _testEncoder() { 79 void _testEncoder() {
63 for (var i = 0; i < _INPUTS.length; i++) { 80 for (var i = 0; i < _INPUTS.length; i++) {
64 expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]); 81 expect(Base64.encode(_INPUTS[i].codeUnits), _RESULTS[i]);
65 } 82 }
66 for (var i = 0; i < inputsWithZeroes.length; i++) { 83 for (var i = 0; i < inputsWithZeroes.length; i++) {
67 expect(CryptoUtils.bytesToBase64(inputsWithZeroes[i]), 84 expect(Base64.encode(inputsWithZeroes[i]),
68 _RESULTS_WITH_ZEROS[i]); 85 _RESULTS_WITH_ZEROS[i]);
69 } 86 }
70 expect( 87 expect(
71 CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits, addLineSeparator : true), 88 Base64.encode(_LONG_LINE.codeUnits, addLineSeparator : true),
72 _LONG_LINE_RESULT); 89 _LONG_LINE_RESULT);
73 expect(CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits), 90 expect(Base64.encode(_LONG_LINE.codeUnits),
74 _LONG_LINE_RESULT_NO_BREAK); 91 _LONG_LINE_RESULT_NO_BREAK);
75 } 92 }
76 93
77 void _testDecoder() { 94 void _testDecoder() {
78 for (var i = 0; i < _RESULTS.length; i++) { 95 for (var i = 0; i < _RESULTS.length; i++) {
79 expect( 96 expect(
80 new String.fromCharCodes(CryptoUtils.base64StringToBytes(_RESULTS[i])), 97 new String.fromCharCodes(Base64.decode(_RESULTS[i])),
81 _INPUTS[i]); 98 _INPUTS[i]);
82 } 99 }
100
83 for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) { 101 for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) {
84 expect(CryptoUtils.base64StringToBytes(_RESULTS_WITH_ZEROS[i]), 102 expect(Base64.decode(_RESULTS_WITH_ZEROS[i]),
85 inputsWithZeroes[i]); 103 inputsWithZeroes[i]);
86 } 104 }
87 var longLineDecoded = CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT); 105
106 var longLineDecoded = Base64.decode(_LONG_LINE_RESULT);
88 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE); 107 expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE);
89 var longLineResultNoBreak = 108
90 CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT); 109 var longLineResultNoBreak = Base64.decode(_LONG_LINE_RESULT);
91 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE); 110 expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE);
92 } 111 }
93 112
113 Future _testStreamingEncoder() async {
114 expect(await new Stream.fromIterable(_STREAMING_ENCODER_INPUT).transform(Base6 4.encoder).join(),
115 _STREAMING_ENCODED);
116 }
117
118 Future _testStreamingDecoder() async {
119 expect(
120 await new Stream.fromIterable(_STREAMING_DECODER_INPUT)
121 .transform(Base64.decoder)
122 .expand((l) => l)
123 .toList(),
124 _STREAMING_DECODED);
125
126 expect(
127 await new Stream.fromIterable(_STREAMING_DECODER_INPUT_FOR_ZEROES)
128 .transform(Base64.decoder)
129 .expand((l) => l)
130 .toList(),
131 _STREAMING_DECODED_ZEROES);
Lasse Reichstein Nielsen 2015/05/29 07:15:16 I would like a test of chunked encoding/decoding t
Alexander Ivanov 2015/05/29 13:12:06 Done.
132 }
133
134 Future _testStreamingDecoderForMalformedInput() async {
135 expect(() async {
136 await new Stream.fromIterable(['ABz'])
137 .transform(Base64.decoder).toList();
138 }(), throwsFormatException);
139
140 expect(() async {
141 await new Stream.fromIterable(['AB', 'Lx', 'z', 'xx'])
142 .transform(Base64.decoder).toList();
Lasse Reichstein Nielsen 2015/05/29 06:48:43 indent by four (or up to other ".").
Alexander Ivanov 2015/05/29 13:12:06 Done.
143 }(), throwsFormatException);
144 }
145
94 void _testDecoderForMalformedInput() { 146 void _testDecoderForMalformedInput() {
95 expect(() { 147 expect(() {
96 CryptoUtils.base64StringToBytes('AB~'); 148 Base64.decode('AB~');
97 }, throwsFormatException); 149 }, throwsFormatException);
98 150
99 expect(() { 151 expect(() {
100 CryptoUtils.base64StringToBytes('A'); 152 Base64.decode('A');
101 }, throwsFormatException); 153 }, throwsFormatException);
102 } 154 }
103 155
156 Future _testUrlSafeStreaming() async {
157 String encUrlSafe = '-_A=';
158 List<List<int>> dec = [Base64.decode('+/A=')];
159 var streamed_result = await new Stream.fromIterable(dec)
160 .transform(new Base64Encoder(urlSafe: true)).join();
161
162 expect(streamed_result, encUrlSafe);
163 }
164
104 void _testUrlSafeEncodeDecode() { 165 void _testUrlSafeEncodeDecode() {
105 List<int> decUrlSafe = CryptoUtils.base64StringToBytes('-_A='); 166 List<int> decUrlSafe = Base64.decode('-_A=');
106 List<int> dec = CryptoUtils.base64StringToBytes('+/A='); 167 List<int> dec = Base64.decode('+/A=');
107 expect(decUrlSafe, orderedEquals(dec)); 168 expect(decUrlSafe, orderedEquals(dec));
108 expect(CryptoUtils.bytesToBase64(dec, urlSafe: true), '-_A='); 169 expect(Base64.encode(dec, urlSafe: true), '-_A=');
109 expect(CryptoUtils.bytesToBase64(dec), '+/A='); 170 expect(Base64.encode(dec), '+/A=');
110 } 171 }
111 172
112 void _testEncodeDecodeLists() { 173 void _testEncodeDecodeLists() {
113 for (int i = 0; i < 10; i++) { 174 for (int i = 0; i < 10; i++) {
114 for (int j = 0; j < 256 - i; j++) { 175 for (int j = 0; j < 256 - i; j++) {
115 List<int> x = new List<int>(i); 176 List<int> x = new List<int>(i);
116 for (int k = 0; k < i; k++) { 177 for (int k = 0; k < i; k++) {
117 x[k] = j; 178 x[k] = j;
118 } 179 }
119 var enc = CryptoUtils.bytesToBase64(x); 180 var enc = Base64.encode(x);
120 var dec = CryptoUtils.base64StringToBytes(enc); 181 var dec = Base64.decode(enc);
121 expect(dec, orderedEquals(x)); 182 expect(dec, orderedEquals(x));
122 } 183 }
123 } 184 }
124 } 185 }
125 186
126 void _fillRandom(List<int> l) { 187 void _fillRandom(List<int> l) {
127 var random = new Random(0xBABE); 188 var random = new Random(0xBABE);
128 for (int j = 0; j < l.length; j++) { 189 for (int j = 0; j < l.length; j++) {
129 l[j] = random.nextInt(255); 190 l[j] = random.nextInt(255);
130 } 191 }
131 } 192 }
132 193
133 void _testPerformance() { 194 void _testPerformance() {
134 var l = new List<int>(1024); 195 var l = new List<int>(1024);
135 var iters = 5000; 196 var iters = 5000;
136 _fillRandom(l); 197 _fillRandom(l);
137 String enc; 198 String enc;
138 var w = new Stopwatch()..start(); 199 var w = new Stopwatch()..start();
139 for( int i = 0; i < iters; ++i ) { 200 for( int i = 0; i < iters; ++i ) {
140 enc = CryptoUtils.bytesToBase64(l); 201 enc = Base64.encode(l);
141 } 202 }
142 int ms = w.elapsedMilliseconds; 203 int ms = w.elapsedMilliseconds;
143 int perSec = (iters * l.length) * 1000 ~/ ms; 204 int perSec = (iters * l.length) * 1000 ~/ ms;
144 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s"); 205 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s");
145 w..reset(); 206 w..reset();
146 for( int i = 0; i < iters; ++i ) { 207 for( int i = 0; i < iters; ++i ) {
147 CryptoUtils.base64StringToBytes(enc); 208 Base64.decode(enc);
148 } 209 }
149 ms = w.elapsedMilliseconds; 210 ms = w.elapsedMilliseconds;
150 perSec = (iters * l.length) * 1000 ~/ ms; 211 perSec = (iters * l.length) * 1000 ~/ ms;
151 // print('''Decode into ${l.length} bytes for $iters 212 // gst
Lasse Reichstein Nielsen 2015/05/29 06:48:43 Bad edit?
Alexander Ivanov 2015/05/29 13:12:06 yes
213 ('''Decode into ${l.length} bytes for $iters
152 // times: $ms msec. $perSec b/s'''); 214 // times: $ms msec. $perSec b/s''');
153 } 215 }
OLDNEW
« lib/src/crypto_utils.dart ('K') | « lib/src/crypto_utils.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698