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

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 comments. 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' ];
15 16
16 var longLine = 17 var longLine =
17 "Man is distinguished, not only by his reason, but by this singular " 18 "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 " 19 "passion from other animals, which is a lust of the mind, that by a "
(...skipping 17 matching lines...) Expand all
36 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" 37 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
37 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" 38 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci"
38 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" 39 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
39 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" 40 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm"
40 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" 41 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
41 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" 42 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX"
42 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" 43 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
43 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" 44 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
44 "5hbCBwbGVhc3VyZS4="; 45 "5hbCBwbGVhc3VyZS4=";
45 46
46 void main() { 47 void testEncoder() {
47 for (var i = 0; i < inputs.length; i++) { 48 for (var i = 0; i < inputs.length; i++) {
48 var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits); 49 var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits);
49 Expect.equals(results[i], enc); 50 Expect.equals(results[i], enc);
50 } 51 }
51 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, 76), 52 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, addLineSeparator : true),
floitsch 2013/03/22 17:22:05 long line.
mdakin1 2013/03/25 17:05:02 Done.
52 longLineResult); 53 longLineResult);
53 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits), 54 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits),
54 longLineResultNoBreak); 55 longLineResultNoBreak);
55 } 56 }
57
58 void testDecoder() {
59 for (var i = 0; i < results.length; i++) {
60 List<int> dec = CryptoUtils.base64StringToBytes(results[i]);
61 Expect.equals(inputs[i], new String.fromCharCodes(dec));
62 }
63 var longLineDecoded = CryptoUtils.base64StringToBytes(longLineResult);
64 Expect.equals(new String.fromCharCodes(longLineDecoded), longLine);
65 var longLineResultNoBreak = CryptoUtils.base64StringToBytes(longLineResult);
66 Expect.equals(new String.fromCharCodes(longLineResultNoBreak), longLine);
67 }
68
69 void testDecoderForMalformedInput() {
70 try {
71 CryptoUtils.base64StringToBytes("AB~", ignoreErrors : false);
floitsch 2013/03/22 17:22:05 We usually use: Expect.throws(() { CryptoUtils.bas
mdakin1 2013/03/25 17:05:02 Done.
72 Expect.fail("Should have failed on Malformed input.");
73 } on FormatException catch(e) {
74 // pass
75 }
76 try {
77 // Missing padding.
78 CryptoUtils.base64StringToBytes("Zg=", ignoreErrors : false);
79 Expect.fail("Should have failed on Malformed input.");
80 } on FormatException catch(e) {
81 // pass
82 }
83 }
84
85 void testUrlSafeEncodeDecode() {
86 CryptoUtils.base64StringToBytes("AB~", ignoreErrors : false);
87 CryptoUtils.base64StringToBytes("Zg=", ignoreErrors : false);
88 }
89
90 void testEncodeDecodeLists() {
91 for (int i = 0; i < 10; i++) {
92 for (int j = 0; j < 256 - i; j++) {
93 List<int> x = new List<int>(i);
94 for (int k = 0; k < i; k++) {
95 x[k] = j;
96 }
97 var enc = CryptoUtils.bytesToBase64(x);
98 var dec = CryptoUtils.base64StringToBytes(enc);
99 Expect.listEquals(x, dec);
100 }
101 }
102 }
103
104 fillRandom(List<int> l) {
105 var random = new Random(0xBABE);
106 for(int j=0; j < l.length; j++) {
107 l[j] = random.nextInt(255);
108 }
109 }
110 void testPerformance() {
111 var l = new List<int>(1024);
112 var iters = 50000;
113 fillRandom(l);
114 String enc;
115 var w = new Stopwatch()..start();
116 for( int i = 0; i < iters; ++i ) {
117 enc = CryptoUtils.bytesToBase64(l);
118 }
119 int ms = w.elapsedMilliseconds;
120 int perSec = (iters * l.length) * 1000 ~/ ms;
121 print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s");
floitsch 2013/03/22 17:22:05 Don't print in tests. But you can leave it behind
mdakin1 2013/03/25 17:05:02 Uh, this was meant to be removed before uploading.
122 w..reset();
123 for( int i = 0; i < iters; ++i ) {
124 CryptoUtils.base64StringToBytes(enc);
125 }
126 ms = w.elapsedMilliseconds;
127 perSec = (iters * l.length) * 1000 ~/ ms;
128 print("Decode into ${l.length} bytes for $iters times: $ms msec. $perSec b/s ");
floitsch 2013/03/22 17:22:05 80 chars.
mdakin1 2013/03/25 17:05:02 Done.
129 }
130
131 void main() {
132 testEncoder();
133 testDecoder();
134 testDecoderForMalformedInput();
135 testEncodeDecodeLists();
136 // testUrlSafeEncodeDecode();
137 testPerformance();
138 }
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