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

Unified 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/crypto/crypto_utils.dart ('K') | « sdk/lib/crypto/crypto_utils.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/crypto/base64_test.dart
diff --git a/tests/lib/crypto/base64_test.dart b/tests/lib/crypto/base64_test.dart
index 0070a50666be48e41f373caf5fc4622308670fad..650cdcead8fbd0e6480d8eac0b96d2c17eba5217 100644
--- a/tests/lib/crypto/base64_test.dart
+++ b/tests/lib/crypto/base64_test.dart
@@ -6,6 +6,7 @@
library base64_test;
import 'dart:crypto';
+import 'dart:math';
// Data from http://tools.ietf.org/html/rfc4648.
var inputs =
@@ -43,13 +44,95 @@ var longLineResultNoBreak =
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
"5hbCBwbGVhc3VyZS4=";
-void main() {
+void testEncoder() {
for (var i = 0; i < inputs.length; i++) {
var enc = CryptoUtils.bytesToBase64(inputs[i].codeUnits);
Expect.equals(results[i], enc);
}
- Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits, 76),
+ 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.
longLineResult);
Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits),
longLineResultNoBreak);
}
+
+void testDecoder() {
+ for (var i = 0; i < results.length; i++) {
+ List<int> dec = CryptoUtils.base64StringToBytes(results[i]);
+ Expect.equals(inputs[i], new String.fromCharCodes(dec));
+ }
+ var longLineDecoded = CryptoUtils.base64StringToBytes(longLineResult);
+ Expect.equals(new String.fromCharCodes(longLineDecoded), longLine);
+ var longLineResultNoBreak = CryptoUtils.base64StringToBytes(longLineResult);
+ Expect.equals(new String.fromCharCodes(longLineResultNoBreak), longLine);
+}
+
+void testDecoderForMalformedInput() {
+ try {
+ 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.
+ Expect.fail("Should have failed on Malformed input.");
+ } on FormatException catch(e) {
+ // pass
+ }
+ try {
+ // Missing padding.
+ CryptoUtils.base64StringToBytes("Zg=", ignoreErrors : false);
+ Expect.fail("Should have failed on Malformed input.");
+ } on FormatException catch(e) {
+ // pass
+ }
+}
+
+void testUrlSafeEncodeDecode() {
+ CryptoUtils.base64StringToBytes("AB~", ignoreErrors : false);
+ CryptoUtils.base64StringToBytes("Zg=", ignoreErrors : false);
+}
+
+void testEncodeDecodeLists() {
+ for (int i = 0; i < 10; i++) {
+ for (int j = 0; j < 256 - i; j++) {
+ List<int> x = new List<int>(i);
+ for (int k = 0; k < i; k++) {
+ x[k] = j;
+ }
+ var enc = CryptoUtils.bytesToBase64(x);
+ var dec = CryptoUtils.base64StringToBytes(enc);
+ Expect.listEquals(x, dec);
+ }
+ }
+}
+
+fillRandom(List<int> l) {
+ var random = new Random(0xBABE);
+ for(int j=0; j < l.length; j++) {
+ l[j] = random.nextInt(255);
+ }
+}
+void testPerformance() {
+ var l = new List<int>(1024);
+ var iters = 50000;
+ fillRandom(l);
+ String enc;
+ var w = new Stopwatch()..start();
+ for( int i = 0; i < iters; ++i ) {
+ enc = CryptoUtils.bytesToBase64(l);
+ }
+ int ms = w.elapsedMilliseconds;
+ int perSec = (iters * l.length) * 1000 ~/ ms;
+ 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.
+ w..reset();
+ for( int i = 0; i < iters; ++i ) {
+ CryptoUtils.base64StringToBytes(enc);
+ }
+ ms = w.elapsedMilliseconds;
+ perSec = (iters * l.length) * 1000 ~/ ms;
+ 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.
+}
+
+void main() {
+ testEncoder();
+ testDecoder();
+ testDecoderForMalformedInput();
+ testEncodeDecodeLists();
+// testUrlSafeEncodeDecode();
+ testPerformance();
+}
« 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