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

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: minor fix 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 9
10 // Data from http://tools.ietf.org/html/rfc4648. 10 // Data from http://tools.ietf.org/html/rfc4648.
11 var inputs = 11 var inputs =
12 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar' ]; 12 const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar' ];
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Please add inputs that end in '\0' and '\0\0' too,
mdakin1 2013/03/25 17:05:02 I am not sure significance of '\0' , it is interpr
floitsch 2013/03/25 17:45:37 I think you could have written '\x00' in the strin
13 var results = 13 var results =
14 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy' ]; 14 const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy' ];
15 15
16 var longLine = 16 var longLine =
17 "Man is distinguished, not only by his reason, but by this singular " 17 "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 " 18 "passion from other animals, which is a lust of the mind, that by a "
19 "perseverance of delight in the continued and indefatigable generation " 19 "perseverance of delight in the continued and indefatigable generation "
20 "of knowledge, exceeds the short vehemence of any carnal pleasure."; 20 "of knowledge, exceeds the short vehemence of any carnal pleasure.";
21 21
22 var longLineResult = 22 var longLineResult =
(...skipping 13 matching lines...) Expand all
36 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" 36 "x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
37 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci" 37 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci"
38 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" 38 "BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"
39 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm" 39 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcm"
40 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" 40 "FuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"
41 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX" 41 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYX"
42 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" 42 "Rpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"
43 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm" 43 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
44 "5hbCBwbGVhc3VyZS4="; 44 "5hbCBwbGVhc3VyZS4=";
45 45
46 void main() { 46 void testEncoder() {
47 for (var i = 0; i < inputs.length; i++) { 47 for (var i = 0; i < inputs.length; i++) {
48 var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits); 48 var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits);
49 Expect.equals(results[i], enc); 49 Expect.equals(results[i], enc);
50 } 50 }
51 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, 76), 51 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, addLineSeparator : true),
52 longLineResult); 52 longLineResult);
53 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits), 53 Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits),
54 longLineResultNoBreak); 54 longLineResultNoBreak);
55 } 55 }
56
57 void testDecoder() {
58 for (var i = 0; i < results.length; i++) {
59 List<int> dec = CryptoUtils.base64StringToBytes(results[i]);
60 Expect.equals(inputs[i], new String.fromCharCodes(dec));
61 }
62 var longLineDecoded = CryptoUtils.base64StringToBytes(longLineResult);
63 Expect.equals(new String.fromCharCodes(longLineDecoded), longLine);
64 var longLineResultNoBreak = CryptoUtils.base64StringToBytes(longLineResult);
65 Expect.equals(new String.fromCharCodes(longLineResultNoBreak), longLine);
66 }
67
68 void testDecoderForMalformedInput() {
69 try {
70 CryptoUtils.base64StringToBytes("AB~", ignoreErrors : false);
71 Expect.fail("Should have failed on Malformed input.");
72 } on FormatException catch(e) {
73 // pass
74 }
75 try {
76 // Missing padding.
77 CryptoUtils.base64StringToBytes("Zg=", ignoreErrors : false);
78 Expect.fail("Should have failed on Malformed input.");
79 } on FormatException catch(e) {
80 // pass
81 }
82 }
83
84 void testEncodeDecode() {
85 for (int i = 0; i < 10; i++) {
86 for (int j = 0; j < 256 - i; j++) {
87 List<int> x = new List<int>(i);
88 for (int k = 0; k < i; k++) {
89 x[k] = j;
90 }
91 var enc = CryptoUtils.bytesToBase64(x);
92 var dec = CryptoUtils.base64StringToBytes(enc);
93 Expect.listEquals(x, dec);
94 }
95 }
96 }
97
98 void main() {
99 testEncoder();
100 testDecoder();
101 testDecoderForMalformedInput();
102 testEncodeDecode();
Lasse Reichstein Nielsen 2013/03/22 09:16:56 Add tests for url-safe and ignoreErrors too.
mdakin1 2013/03/25 17:05:02 Done.
103 }
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