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

Unified Diff: test/base64_test.dart

Issue 1159093002: Implement a Base64 codec (Closed) Base URL: https://github.com/dart-lang/crypto.git@master
Patch Set: Created 5 years, 7 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
« lib/src/crypto_utils.dart ('K') | « lib/src/crypto_utils.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/base64_test.dart
diff --git a/test/base64_test.dart b/test/base64_test.dart
index b5647b035bf4cfe55091663915d38c9f01fa5b64..454f1b72e76d9e68dd75b32681bb0a502b45e369 100644
--- a/test/base64_test.dart
+++ b/test/base64_test.dart
@@ -6,6 +6,7 @@
library base64_test;
import 'dart:math';
+import 'dart:async';
import "package:crypto/crypto.dart";
import "package:test/test.dart";
@@ -16,7 +17,12 @@ void main() {
test('decoder for malformed input', _testDecoderForMalformedInput);
test('encode decode lists', _testEncodeDecodeLists);
test('url safe encode-decode', _testUrlSafeEncodeDecode);
+ test('streaming encoder', _testStreamingEncoder);
+ test('streaming decoder', _testStreamingDecoder);
+ test('streaming decoder for malformed input', _testStreamingDecoderForMalformedInput);
+ test('url safe streaming encoder/decoder', _testUrlSafeStreaming);
test('performance', _testPerformance);
+
}
// Data from http://tools.ietf.org/html/rfc4648.
@@ -24,6 +30,17 @@ const _INPUTS =
const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar'];
const _RESULTS =
const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy'];
+var _STREAMING_ENCODER_INPUT =
+ [[102, 102], [111, 102], [111, 111, 102, 111, 111, 98, 102, 111, 111, 98, 97, 102, 111, 111, 98, 97, 114]];
Lasse Reichstein Nielsen 2015/05/29 06:48:43 long line.
Alexander Ivanov 2015/05/29 13:12:06 Done.
+
+const _STREAMING_ENCODED = 'ZmZvZm9vZm9vYmZvb2JhZm9vYmFy';
+const _STREAMING_DECODER_INPUT =
+ const ['YmFz', 'ZTY', '0I', 'GRlY29kZXI='];
Lasse Reichstein Nielsen 2015/05/29 06:48:43 indent by four.
Alexander Ivanov 2015/05/29 13:12:06 Done.
+const _STREAMING_DECODED =
+ const [98, 97, 115, 101, 54, 52, 32, 100, 101, 99, 111, 100, 101, 114];
+const _STREAMING_DECODER_INPUT_FOR_ZEROES =
+ const ['AAAA', 'AAA=', 'AA==', ''];
+var _STREAMING_DECODED_ZEROES = [0, 0, 0, 0, 0, 0];
// Test data with only zeroes.
var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []];
@@ -61,52 +78,96 @@ const _LONG_LINE_RESULT_NO_BREAK =
void _testEncoder() {
for (var i = 0; i < _INPUTS.length; i++) {
- expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]);
+ expect(Base64.encode(_INPUTS[i].codeUnits), _RESULTS[i]);
}
for (var i = 0; i < inputsWithZeroes.length; i++) {
- expect(CryptoUtils.bytesToBase64(inputsWithZeroes[i]),
+ expect(Base64.encode(inputsWithZeroes[i]),
_RESULTS_WITH_ZEROS[i]);
}
expect(
- CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits, addLineSeparator : true),
+ Base64.encode(_LONG_LINE.codeUnits, addLineSeparator : true),
_LONG_LINE_RESULT);
- expect(CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits),
+ expect(Base64.encode(_LONG_LINE.codeUnits),
_LONG_LINE_RESULT_NO_BREAK);
}
void _testDecoder() {
for (var i = 0; i < _RESULTS.length; i++) {
expect(
- new String.fromCharCodes(CryptoUtils.base64StringToBytes(_RESULTS[i])),
+ new String.fromCharCodes(Base64.decode(_RESULTS[i])),
_INPUTS[i]);
}
+
for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) {
- expect(CryptoUtils.base64StringToBytes(_RESULTS_WITH_ZEROS[i]),
+ expect(Base64.decode(_RESULTS_WITH_ZEROS[i]),
inputsWithZeroes[i]);
}
- var longLineDecoded = CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT);
+
+ var longLineDecoded = Base64.decode(_LONG_LINE_RESULT);
expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE);
- var longLineResultNoBreak =
- CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT);
+
+ var longLineResultNoBreak = Base64.decode(_LONG_LINE_RESULT);
expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE);
}
+Future _testStreamingEncoder() async {
+ expect(await new Stream.fromIterable(_STREAMING_ENCODER_INPUT).transform(Base64.encoder).join(),
+ _STREAMING_ENCODED);
+}
+
+Future _testStreamingDecoder() async {
+ expect(
+ await new Stream.fromIterable(_STREAMING_DECODER_INPUT)
+ .transform(Base64.decoder)
+ .expand((l) => l)
+ .toList(),
+ _STREAMING_DECODED);
+
+ expect(
+ await new Stream.fromIterable(_STREAMING_DECODER_INPUT_FOR_ZEROES)
+ .transform(Base64.decoder)
+ .expand((l) => l)
+ .toList(),
+ _STREAMING_DECODED_ZEROES);
Lasse Reichstein Nielsen 2015/05/29 07:15:16 I would like a test of chunked encoding/decoding t
Alexander Ivanov 2015/05/29 13:12:06 Done.
+}
+
+Future _testStreamingDecoderForMalformedInput() async {
+ expect(() async {
+ await new Stream.fromIterable(['ABz'])
+ .transform(Base64.decoder).toList();
+ }(), throwsFormatException);
+
+ expect(() async {
+ await new Stream.fromIterable(['AB', 'Lx', 'z', 'xx'])
+ .transform(Base64.decoder).toList();
Lasse Reichstein Nielsen 2015/05/29 06:48:43 indent by four (or up to other ".").
Alexander Ivanov 2015/05/29 13:12:06 Done.
+ }(), throwsFormatException);
+}
+
void _testDecoderForMalformedInput() {
expect(() {
- CryptoUtils.base64StringToBytes('AB~');
+ Base64.decode('AB~');
}, throwsFormatException);
expect(() {
- CryptoUtils.base64StringToBytes('A');
+ Base64.decode('A');
}, throwsFormatException);
}
+Future _testUrlSafeStreaming() async {
+ String encUrlSafe = '-_A=';
+ List<List<int>> dec = [Base64.decode('+/A=')];
+ var streamed_result = await new Stream.fromIterable(dec)
+ .transform(new Base64Encoder(urlSafe: true)).join();
+
+ expect(streamed_result, encUrlSafe);
+}
+
void _testUrlSafeEncodeDecode() {
- List<int> decUrlSafe = CryptoUtils.base64StringToBytes('-_A=');
- List<int> dec = CryptoUtils.base64StringToBytes('+/A=');
+ List<int> decUrlSafe = Base64.decode('-_A=');
+ List<int> dec = Base64.decode('+/A=');
expect(decUrlSafe, orderedEquals(dec));
- expect(CryptoUtils.bytesToBase64(dec, urlSafe: true), '-_A=');
- expect(CryptoUtils.bytesToBase64(dec), '+/A=');
+ expect(Base64.encode(dec, urlSafe: true), '-_A=');
+ expect(Base64.encode(dec), '+/A=');
}
void _testEncodeDecodeLists() {
@@ -116,8 +177,8 @@ void _testEncodeDecodeLists() {
for (int k = 0; k < i; k++) {
x[k] = j;
}
- var enc = CryptoUtils.bytesToBase64(x);
- var dec = CryptoUtils.base64StringToBytes(enc);
+ var enc = Base64.encode(x);
+ var dec = Base64.decode(enc);
expect(dec, orderedEquals(x));
}
}
@@ -137,17 +198,18 @@ void _testPerformance() {
String enc;
var w = new Stopwatch()..start();
for( int i = 0; i < iters; ++i ) {
- enc = CryptoUtils.bytesToBase64(l);
+ enc = Base64.encode(l);
}
int ms = w.elapsedMilliseconds;
int perSec = (iters * l.length) * 1000 ~/ ms;
// print("Encode 1024 bytes for $iters times: $ms msec. $perSec b/s");
w..reset();
for( int i = 0; i < iters; ++i ) {
- CryptoUtils.base64StringToBytes(enc);
+ Base64.decode(enc);
}
ms = w.elapsedMilliseconds;
perSec = (iters * l.length) * 1000 ~/ ms;
- // print('''Decode into ${l.length} bytes for $iters
+ // gst
Lasse Reichstein Nielsen 2015/05/29 06:48:43 Bad edit?
Alexander Ivanov 2015/05/29 13:12:06 yes
+ ('''Decode into ${l.length} bytes for $iters
// times: $ms msec. $perSec b/s''');
}
« lib/src/crypto_utils.dart ('K') | « lib/src/crypto_utils.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698