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

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

Powered by Google App Engine
This is Rietveld 408576698