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

Side by Side Diff: tests/lib/crypto/base64_test.dart

Issue 12534011: Add base64 decoder and change existing base64 encoder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address all comments from Florian and Lasse. Created 7 years, 9 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 | Annotate | Revision Log
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: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
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~', ignoreErrors: 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==@@@', ignoreErrors: true));
92 }
93
94 testUrlSafeEncodeDecode() {
95 List<int> decUrlSafe = CryptoUtils.base64StringToBytes('-_A=');
96 List<int> dec = CryptoUtils.base64StringToBytes('+/A=');
97 Expect.listEquals(decUrlSafe, dec);
98 Expect.equals('-_A=', CryptoUtils.bytesToBase64(dec, urlSafe: true));
99 Expect.equals('+/A=', CryptoUtils.bytesToBase64(dec));
100 }
101
102 testEncodeDecodeLists() {
103 for (int i = 0; i < 10; i++) {
104 for (int j = 0; j < 256 - i; j++) {
105 List<int> x = new List<int>(i);
106 for (int k = 0; k < i; k++) {
107 x[k] = j;
108 }
109 var enc = CryptoUtils.bytesToBase64(x);
110 var dec = CryptoUtils.base64StringToBytes(enc);
111 Expect.listEquals(x, dec);
112 }
113 }
114 }
115
116 fillRandom(List<int> l) {
117 var random = new Random(0xBABE);
118 for(int j=0; j < l.length; j++) {
119 l[j] = random.nextInt(255);
120 }
121 }
122
123 testPerformance() {
124 var l = new List<int>(1024);
125 var iters = 20000;
126 fillRandom(l);
127 String enc;
128 var w = new Stopwatch()..start();
129 for( int i = 0; i < iters; ++i ) {
130 enc = CryptoUtils.bytesToBase64(l);
131 }
132 int ms = w.elapsedMilliseconds;
133 int perSec = (iters * l.length) * 1000 ~/ ms;
134 // print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s");
135 w..reset();
136 for( int i = 0; i < iters; ++i ) {
137 CryptoUtils.base64StringToBytes(enc);
138 }
139 ms = w.elapsedMilliseconds;
140 perSec = (iters * l.length) * 1000 ~/ ms;
141 // print('''Decode into ${l.length} bytes for $iters
142 // times: $ms msec. $perSec b/s''');
143 }
144
145 void main() {
146 testEncoder();
147 testDecoder();
148 testDecoderForMalformedInput();
149 testEncodeDecodeLists();
150 testUrlSafeEncodeDecode();
151 testPerformance();
152 }
OLDNEW
« sdk/lib/crypto/crypto_utils.dart ('K') | « sdk/lib/crypto/crypto_utils.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698