pkg/crypto: move tests to unittest package
R=sgjesse@google.com
Review URL: https://codereview.chromium.org//169363008
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/crypto@32742 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/lib/src/crypto_utils.dart b/lib/src/crypto_utils.dart
index b593c0d..515c1e9 100644
--- a/lib/src/crypto_utils.dart
+++ b/lib/src/crypto_utils.dart
@@ -78,9 +78,9 @@
out[j++] = lookup.codeUnitAt(x & 0x3f);
// Add optional line separator for each 76 char output.
if (addLineSeparator && ++c == 19 && j < outputLen - 2) {
- out[j++] = CR;
- out[j++] = LF;
- c = 0;
+ out[j++] = CR;
+ out[j++] = LF;
+ c = 0;
}
}
@@ -117,7 +117,7 @@
int c = _decodeTable[input.codeUnitAt(i)];
if (c < 0) {
extrasLen++;
- if(c == -2) {
+ if (c == -2) {
throw new FormatException('Invalid character: ${input[i]}');
}
}
@@ -138,10 +138,10 @@
int outputLen = (((len - extrasLen) * 6) >> 3) - padLength;
List<int> out = new List<int>(outputLen);
- for (int i = 0, o = 0; o < outputLen;) {
+ for (int i = 0, o = 0; o < outputLen; ) {
// Accumulate 4 valid 6 bit Base 64 characters into an int.
int x = 0;
- for (int j = 4; j > 0;) {
+ for (int j = 4; j > 0; ) {
int c = _decodeTable[input.codeUnitAt(i++)];
if (c >= 0) {
x = ((x << 6) & 0xFFFFFF) | c;
diff --git a/lib/src/hash_utils.dart b/lib/src/hash_utils.dart
index 84df9d0..1d90e69 100644
--- a/lib/src/hash_utils.dart
+++ b/lib/src/hash_utils.dart
@@ -22,6 +22,15 @@
// Base class encapsulating common behavior for cryptographic hash
// functions.
abstract class _HashBase implements Hash {
+ final int _chunkSizeInWords;
+ final int _digestSizeInWords;
+ final bool _bigEndianWords;
+ final List<int> _currentChunk;
+ final List<int> _h;
+ int _lengthInBytes = 0;
+ List<int> _pendingData;
+ bool _digestCalled = false;
+
_HashBase(int chunkSizeInWords,
int digestSizeInWords,
bool this._bigEndianWords)
@@ -139,14 +148,4 @@
_pendingData.addAll(_wordToBytes(0));
}
}
-
- // Hasher state.
- final int _chunkSizeInWords;
- final int _digestSizeInWords;
- final bool _bigEndianWords;
- int _lengthInBytes = 0;
- List<int> _pendingData;
- final List<int> _currentChunk;
- final List<int> _h;
- bool _digestCalled = false;
}
diff --git a/lib/src/hmac.dart b/lib/src/hmac.dart
index ba6cc11..a4ea193 100644
--- a/lib/src/hmac.dart
+++ b/lib/src/hmac.dart
@@ -12,12 +12,15 @@
*/
// TODO(floitsch): make Hash implement Sink, EventSink or similar.
class HMAC {
+ final List<int> _message;
+ Hash _hash;
+ List<int> _key;
bool _isClosed = false;
/**
* Create an [HMAC] object from a [Hash] and a key.
*/
- HMAC(Hash this._hash, List<int> this._key) : _message = [];
+ HMAC(Hash this._hash, List<int> this._key): _message = [];
/**
* Add a list of bytes to the message.
@@ -106,9 +109,4 @@
}
return result == 0;
}
-
- // HMAC internal state.
- Hash _hash;
- List<int> _key;
- final List<int> _message;
}
diff --git a/lib/src/md5.dart b/lib/src/md5.dart
index 6acf384..07b567e 100644
--- a/lib/src/md5.dart
+++ b/lib/src/md5.dart
@@ -11,7 +11,7 @@
* required for backwards compatibility.
*/
class MD5 extends _HashBase {
- MD5() : super(16, 4, false) {
+ MD5(): super(16, 4, false) {
_h[0] = 0x67452301;
_h[1] = 0xefcdab89;
_h[2] = 0x98badcfe;
diff --git a/lib/src/sha1.dart b/lib/src/sha1.dart
index f08c987..0b55b8d 100644
--- a/lib/src/sha1.dart
+++ b/lib/src/sha1.dart
@@ -8,6 +8,8 @@
* SHA1 hash function implementation.
*/
class SHA1 extends _HashBase {
+ final List<int> _w;
+
// Construct a SHA1 hasher object.
SHA1() : _w = new List(80), super(16, 5, true) {
_h[0] = 0x67452301;
@@ -64,6 +66,4 @@
_h[3] = _add32(d, _h[3]);
_h[4] = _add32(e, _h[4]);
}
-
- List<int> _w;
}
diff --git a/lib/src/sha256.dart b/lib/src/sha256.dart
index b429b8f..88b701e 100644
--- a/lib/src/sha256.dart
+++ b/lib/src/sha256.dart
@@ -8,6 +8,8 @@
* SHA256 hash function implementation.
*/
class SHA256 extends _HashBase {
+ final List<int> _w;
+
// Construct a SHA256 hasher object.
SHA256() : _w = new List(64), super(16, 8, true) {
// Initial value of the hash parts. First 32 bits of the fractional parts
@@ -102,6 +104,4 @@
_h[6] = _add32(g, _h[6]);
_h[7] = _add32(h, _h[7]);
}
-
- final List<int> _w;
}
diff --git a/pubspec.yaml b/pubspec.yaml
index f9d7d93..ff692ac 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,9 +1,10 @@
name: crypto
-version: 0.9.0
-author: "Dart Team <misc@dartlang.org>"
+version: 0.9.1-dev
+author: Dart Team <misc@dartlang.org>
+description: Library of cryptographic functions.
homepage: http://www.dartlang.org
-documentation: http://api.dartlang.org/docs/pkg/crypto
-description: >
- Library of cryptographic functions.
environment:
- sdk: ">=0.8.10+6 <2.0.0"
+ sdk: '>=0.8.10+6 <2.0.0'
+documentation: http://api.dartlang.org/docs/pkg/crypto
+dev_dependencies:
+ unittest: '>=0.10.0 <0.11.0'
diff --git a/test/base64_test.dart b/test/base64_test.dart
index 9a58e3b..4215af9 100644
--- a/test/base64_test.dart
+++ b/test/base64_test.dart
@@ -5,27 +5,37 @@
// Library tag to allow the test to run on Dartium.
library base64_test;
-import "package:expect/expect.dart";
-import "package:crypto/crypto.dart";
import 'dart:math';
+import "package:crypto/crypto.dart";
+import "package:unittest/unittest.dart";
+
+void main() {
+ test('encoder', _testEncoder);
+ test('decoder', _testDecoder);
+ test('decoder for malformed input', _testDecoderForMalformedInput);
+ test('encode decode lists', _testEncodeDecodeLists);
+ test('url safe encode-decode', _testUrlSafeEncodeDecode);
+ test('performance', _testPerformance);
+}
+
// Data from http://tools.ietf.org/html/rfc4648.
-var inputs =
+const _INPUTS =
const [ '', 'f', 'fo', 'foo', 'foob', 'fooba', 'foobar'];
-var results =
+const _RESULTS =
const [ '', 'Zg==', 'Zm8=', 'Zm9v', 'Zm9vYg==', 'Zm9vYmE=', 'Zm9vYmFy'];
// Test data with only zeroes.
var inputsWithZeroes = [[0, 0, 0], [0, 0], [0], []];
-var resultsWithZeroes = ['AAAA', 'AAA=', 'AA==', ''];
+const _RESULTS_WITH_ZEROS = const ['AAAA', 'AAA=', 'AA==', ''];
-var longLine =
+const _LONG_LINE =
"Man is distinguished, not only by his reason, but by this singular "
"passion from other animals, which is a lust of the mind, that by a "
"perseverance of delight in the continued and indefatigable generation "
"of knowledge, exceeds the short vehemence of any carnal pleasure.";
-var longLineResult =
+const _LONG_LINE_RESULT =
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm"
"x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"
"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci"
@@ -37,7 +47,7 @@
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
"5hbCBwbGVhc3VyZS4=";
-var longLineResultNoBreak =
+const _LONG_LINE_RESULT_NO_BREAK =
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbm"
"x5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"
"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci"
@@ -49,55 +59,57 @@
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm"
"5hbCBwbGVhc3VyZS4=";
-testEncoder() {
- for (var i = 0; i < inputs.length; i++) {
- Expect.equals(results[i], CryptoUtils.bytesToBase64(inputs[i].codeUnits));
+void _testEncoder() {
+ for (var i = 0; i < _INPUTS.length; i++) {
+ expect(CryptoUtils.bytesToBase64(_INPUTS[i].codeUnits), _RESULTS[i]);
}
for (var i = 0; i < inputsWithZeroes.length; i++) {
- Expect.equals(resultsWithZeroes[i],
- CryptoUtils.bytesToBase64(inputsWithZeroes[i]));
+ expect(CryptoUtils.bytesToBase64(inputsWithZeroes[i]),
+ _RESULTS_WITH_ZEROS[i]);
}
- Expect.equals(
- CryptoUtils.bytesToBase64(longLine.codeUnits, addLineSeparator : true),
- longLineResult);
- Expect.equals(CryptoUtils.bytesToBase64(longLine.codeUnits),
- longLineResultNoBreak);
+ expect(
+ CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits, addLineSeparator : true),
+ _LONG_LINE_RESULT);
+ expect(CryptoUtils.bytesToBase64(_LONG_LINE.codeUnits),
+ _LONG_LINE_RESULT_NO_BREAK);
}
-testDecoder() {
- for (var i = 0; i < results.length; i++) {
- Expect.equals(inputs[i],
- new String.fromCharCodes(CryptoUtils.base64StringToBytes(results[i])));
+void _testDecoder() {
+ for (var i = 0; i < _RESULTS.length; i++) {
+ expect(
+ new String.fromCharCodes(CryptoUtils.base64StringToBytes(_RESULTS[i])),
+ _INPUTS[i]);
}
- for (var i = 0; i < resultsWithZeroes.length; i++) {
- Expect.listEquals(inputsWithZeroes[i],
- CryptoUtils.base64StringToBytes(resultsWithZeroes[i]));
+ for (var i = 0; i < _RESULTS_WITH_ZEROS.length; i++) {
+ expect(CryptoUtils.base64StringToBytes(_RESULTS_WITH_ZEROS[i]),
+ inputsWithZeroes[i]);
}
- var longLineDecoded = CryptoUtils.base64StringToBytes(longLineResult);
- Expect.equals(new String.fromCharCodes(longLineDecoded), longLine);
- var longLineResultNoBreak = CryptoUtils.base64StringToBytes(longLineResult);
- Expect.equals(new String.fromCharCodes(longLineResultNoBreak), longLine);
+ var longLineDecoded = CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT);
+ expect(new String.fromCharCodes(longLineDecoded), _LONG_LINE);
+ var longLineResultNoBreak =
+ CryptoUtils.base64StringToBytes(_LONG_LINE_RESULT);
+ expect(new String.fromCharCodes(longLineResultNoBreak), _LONG_LINE);
}
-testDecoderForMalformedInput() {
- Expect.throws(() {
- CryptoUtils.base64StringToBytes('AB~');
- }, (e) => e is FormatException);
+void _testDecoderForMalformedInput() {
+ expect(() {
+ CryptoUtils.base64StringToBytes('AB~');
+ }, throwsFormatException);
- Expect.throws(() {
+ expect(() {
CryptoUtils.base64StringToBytes('A');
- }, (e) => e is FormatException);
+ }, throwsFormatException);
}
-testUrlSafeEncodeDecode() {
+void _testUrlSafeEncodeDecode() {
List<int> decUrlSafe = CryptoUtils.base64StringToBytes('-_A=');
List<int> dec = CryptoUtils.base64StringToBytes('+/A=');
- Expect.listEquals(decUrlSafe, dec);
- Expect.equals('-_A=', CryptoUtils.bytesToBase64(dec, urlSafe: true));
- Expect.equals('+/A=', CryptoUtils.bytesToBase64(dec));
+ expect(decUrlSafe, dec);
+ expect(CryptoUtils.bytesToBase64(dec, urlSafe: true), '-_A=');
+ expect(CryptoUtils.bytesToBase64(dec), '+/A=');
}
-testEncodeDecodeLists() {
+void _testEncodeDecodeLists() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 256 - i; j++) {
List<int> x = new List<int>(i);
@@ -106,22 +118,22 @@
}
var enc = CryptoUtils.bytesToBase64(x);
var dec = CryptoUtils.base64StringToBytes(enc);
- Expect.listEquals(x, dec);
+ expect(dec, x);
}
}
}
-fillRandom(List<int> l) {
+void _fillRandom(List<int> l) {
var random = new Random(0xBABE);
- for(int j=0; j < l.length; j++) {
+ for (int j = 0; j < l.length; j++) {
l[j] = random.nextInt(255);
}
}
-testPerformance() {
+void _testPerformance() {
var l = new List<int>(1024);
var iters = 5000;
- fillRandom(l);
+ _fillRandom(l);
String enc;
var w = new Stopwatch()..start();
for( int i = 0; i < iters; ++i ) {
@@ -139,12 +151,3 @@
// print('''Decode into ${l.length} bytes for $iters
// times: $ms msec. $perSec b/s''');
}
-
-void main() {
- testEncoder();
- testDecoder();
- testDecoderForMalformedInput();
- testEncodeDecodeLists();
- testUrlSafeEncodeDecode();
- testPerformance();
-}
diff --git a/test/hmac_md5_test.dart b/test/hmac_md5_test.dart
index 2d717b9..15b941f 100644
--- a/test/hmac_md5_test.dart
+++ b/test/hmac_md5_test.dart
@@ -5,11 +5,30 @@
// Library tag to allow the test to run on Dartium.
library hmac_md5_test;
-import "package:expect/expect.dart";
import "package:crypto/crypto.dart";
+import "package:unittest/unittest.dart";
+
+void main() {
+ test('standard vectors', () {
+ _testStandardVectors(_HMAC_MD5_INPUTS, _HMAC_MD5_KEYS,
+ _HMAC_MD5_STRING_MACS, _HMAC_MD5_MACS);
+ });
+}
+
+void _testStandardVectors(inputs, keys, string_macs, macs) {
+ for (var i = 0; i < inputs.length; i++) {
+ var h = new HMAC(new MD5(), keys[i]);
+ h.add(inputs[i]);
+ var d = h.close();
+ expect(CryptoUtils.bytesToHex(d).startsWith(string_macs[i]), isTrue);
+ expect(h.verify(macs[i]), isTrue);
+ expect(h.verify(macs[(i + 1) % macs.length]), isFalse);
+ expect(() => h.verify([]), throws);
+ }
+}
// Data from http://tools.ietf.org/html/rfc2202.
-var hmac_md5_inputs =
+const _HMAC_MD5_INPUTS =
const [
const [ 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 ],
const [ 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61,
@@ -42,7 +61,7 @@
0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x44,
0x61, 0x74, 0x61 ] ];
-var hmac_md5_keys =
+const _HMAC_MD5_KEYS =
const [ const [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
const [ 0x4a, 0x65, 0x66, 0x65 ],
@@ -72,7 +91,7 @@
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa ] ];
-var hmac_md5_string_macs =
+const _HMAC_MD5_STRING_MACS =
const [ '9294727a3638bb1c13f48ef8158bfc9d',
'750c783e6ab0b503eaa86e310a5db738',
'56be34521d144c88dbb8c733f0e8b3f6',
@@ -81,7 +100,7 @@
'6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd',
'6f630fad67cda0ee1fb1f562db3aa53e' ];
-var hmac_md5_macs =
+const _HMAC_MD5_MACS =
const [ const [0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4,
0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d],
const [0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8,
@@ -96,20 +115,3 @@
0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd],
const [0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1,
0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e]];
-
-void testStandardVectors(inputs, keys, string_macs, macs) {
- for (var i = 0; i < inputs.length; i++) {
- var h = new HMAC(new MD5(), keys[i]);
- h.add(inputs[i]);
- var d = h.close();
- Expect.isTrue(CryptoUtils.bytesToHex(d).startsWith(string_macs[i]), '$i');
- Expect.isTrue(h.verify(macs[i]));
- Expect.isFalse(h.verify(macs[(i+1)%macs.length]));
- Expect.throws(() => h.verify([]));
- }
-}
-
-void main() {
- testStandardVectors(hmac_md5_inputs, hmac_md5_keys,
- hmac_md5_string_macs, hmac_md5_macs);
-}
diff --git a/test/hmac_sha1_test.dart b/test/hmac_sha1_test.dart
index de79470..13ff34a 100644
--- a/test/hmac_sha1_test.dart
+++ b/test/hmac_sha1_test.dart
@@ -5,20 +5,23 @@
// Library tag to allow the test to run on Dartium.
library hmac_sha1_test;
-import "package:expect/expect.dart";
import "package:crypto/crypto.dart";
+import "package:unittest/unittest.dart";
part 'hmac_sha1_test_vectors.dart';
-void testStandardVectors(inputs, keys, macs) {
+
+void main() {
+ test('standard vectors', () {
+ _testStandardVectors(hmac_sha1_inputs, hmac_sha1_keys, hmac_sha1_macs);
+ });
+}
+
+void _testStandardVectors(inputs, keys, macs) {
for (var i = 0; i < inputs.length; i++) {
var hmac = new HMAC(new SHA1(), keys[i]);
hmac.add(inputs[i]);
var d = hmac.close();
- Expect.isTrue(CryptoUtils.bytesToHex(d).startsWith(macs[i]), '$i');
+ expect(CryptoUtils.bytesToHex(d).startsWith(macs[i]), isTrue);
}
}
-
-void main() {
- testStandardVectors(hmac_sha1_inputs, hmac_sha1_keys, hmac_sha1_macs);
-}
diff --git a/test/hmac_sha256_test.dart b/test/hmac_sha256_test.dart
index 73fa561..fa31420 100644
--- a/test/hmac_sha256_test.dart
+++ b/test/hmac_sha256_test.dart
@@ -5,20 +5,23 @@
// Library tag to allow the test to run on Dartium.
library hmac_sha256_test;
-import "package:expect/expect.dart";
+import "package:unittest/unittest.dart";
import "package:crypto/crypto.dart";
part 'hmac_sha256_test_vectors.dart';
-void testStandardVectors(inputs, keys, macs) {
+void main() {
+ test('standard vectors', () {
+ _testStandardVectors(hmac_sha256_inputs, hmac_sha256_keys,
+ hmac_sha256_macs);
+ });
+}
+
+void _testStandardVectors(inputs, keys, macs) {
for (var i = 0; i < inputs.length; i++) {
var hmac = new HMAC(new SHA256(), keys[i]);
hmac.add(inputs[i]);
var d = hmac.close();
- Expect.isTrue(CryptoUtils.bytesToHex(d).startsWith(macs[i]), '$i');
+ expect(CryptoUtils.bytesToHex(d).startsWith(macs[i]), isTrue);
}
}
-
-void main() {
- testStandardVectors(hmac_sha256_inputs, hmac_sha256_keys, hmac_sha256_macs);
-}
diff --git a/test/sha1_test.dart b/test/sha1_test.dart
index 7ce49c3..acfdad8 100644
--- a/test/sha1_test.dart
+++ b/test/sha1_test.dart
@@ -5,22 +5,27 @@
// Library tag to allow dartium to run the test.
library sha1_test;
-import "package:expect/expect.dart";
import "package:crypto/crypto.dart";
+import "package:unittest/unittest.dart";
part 'sha1_long_test_vectors.dart';
part 'sha1_short_test_vectors.dart';
-List<int> createTestArr(int len) {
- var arr = new List<int>(len);
- for (var i = 0; i < len; i++) {
- arr[i] = i;
- }
- return arr;
+
+void main() {
+ test('expected values', _testExpectedValues);
+ test('invalid use', _testInvalidUse);
+ test('repeated digest', _testRepeatedDigest);
+ test('long inputs', () {
+ _testStandardVectors(sha1_long_inputs, sha1_long_mds);
+ });
+ test('short inputs', () {
+ _testStandardVectors(sha1_short_inputs, sha1_short_mds);
+ });
}
-void test() {
- final expected_values = const [
+void _testExpectedValues() {
+ var expectedValues = const [
"da39a3ee5e6b4b0d3255bfef95601890afd80709",
"5ba93c9db0cff93f52b521d7420e43f6eda2784f",
"3f29546453678b855931c174a97d6c0894b8f546",
@@ -534,39 +539,31 @@
"3dce8306f3c1810d5d81ed5ebb0ccea947277a61",
"11bca5b61fc1f6d59078ec5354bc6d9adecc0c5d",
];
- for (var i = 0; i < expected_values.length; i++) {
+ for (var i = 0; i < expectedValues.length; i++) {
var hash = new SHA1();
- hash.add(createTestArr(i));
+ hash.add(new List<int>.generate(i, (j) => j, growable: false));
var digest = hash.close();
- Expect.equals(expected_values[i], CryptoUtils.bytesToHex(digest));
+ expect(expectedValues[i], CryptoUtils.bytesToHex(digest));
}
}
-void testInvalidUse() {
+void _testInvalidUse() {
var sha = new SHA1();
sha.close();
- Expect.throws(() => sha.add([0]), (e) => e is StateError);
+ expect(() => sha.add([0]), throwsStateError);
}
-void testRepeatedDigest() {
+void _testRepeatedDigest() {
var sha = new SHA1();
var digest = sha.close();
- Expect.listEquals(digest, sha.close());
+ expect(digest, sha.close());
}
-void testStandardVectors(inputs, mds) {
+void _testStandardVectors(inputs, mds) {
for (var i = 0; i < inputs.length; i++) {
var hash = new SHA1();
hash.add(inputs[i]);
var d = hash.close();
- Expect.equals(mds[i], CryptoUtils.bytesToHex(d), '$i');
+ expect(mds[i], CryptoUtils.bytesToHex(d), reason: '$i');
}
}
-
-void main() {
- test();
- testInvalidUse();
- testRepeatedDigest();
- testStandardVectors(sha1_long_inputs, sha1_long_mds);
- testStandardVectors(sha1_short_inputs, sha1_short_mds);
-}
diff --git a/test/sha256_test.dart b/test/sha256_test.dart
index 01bb4b8..e86a322 100644
--- a/test/sha256_test.dart
+++ b/test/sha256_test.dart
@@ -5,22 +5,25 @@
// Library tag to allow Dartium to run the tests.
library sha256_test;
-import "package:expect/expect.dart";
+import "package:unittest/unittest.dart";
import "package:crypto/crypto.dart";
part 'sha256_long_test_vectors.dart';
part 'sha256_short_test_vectors.dart';
-List<int> createTestArr(int len) {
- var arr = new List<int>(len);
- for (var i = 0; i < len; i++) {
- arr[i] = i;
- }
- return arr;
+
+void main() {
+ test('expected values', _testExpectedValues);
+ test('invalid use', _testInvalidUse);
+ test('repeated digest', _testRepeatedDigest);
+ test('long inputs',
+ () => _testStandardVectors(sha256_long_inputs, sha256_long_mds));
+ test('short inputs',
+ () => _testStandardVectors(sha256_short_inputs, sha256_short_mds));
}
-void test() {
- final expected_values = const [
+void _testExpectedValues() {
+ var expectedValues = const [
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
'6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d',
'b413f47d13ee2fe6c845b2ee141af81de858df4ec549a58b7970bb96645bc8d2',
@@ -278,40 +281,31 @@
'd6a8bdb01e763fb64f3a02512e7be905679a5add6bb408f8750d679d17cad92f',
'3f8591112c6bbe5c963965954e293108b7208ed2af893e500d859368c654eabe' ];
- for (var i = 0; i < expected_values.length; i++) {
+ for (var i = 0; i < expectedValues.length; i++) {
var hash = new SHA256();
- hash.add(createTestArr(i));
+ hash.add(new List<int>.generate(i, (j) => j, growable: false));
var d = hash.close();
- Expect.equals(expected_values[i], CryptoUtils.bytesToHex(d), '$i');
+ expect(expectedValues[i], CryptoUtils.bytesToHex(d), reason: '$i');
}
}
-void testInvalidUse() {
+void _testInvalidUse() {
var sha = new SHA256();
sha.close();
- Expect.throws(() => sha.add([0]), (e) => e is StateError);
+ expect(() => sha.add([0]), throwsStateError);
}
-void testRepeatedDigest() {
+void _testRepeatedDigest() {
var sha = new SHA256();
var digest = sha.close();
- Expect.listEquals(digest, sha.close());
+ expect(digest, sha.close());
}
-void testStandardVectors(inputs, mds) {
+void _testStandardVectors(inputs, mds) {
for (var i = 0; i < inputs.length; i++) {
var hash = new SHA256();
hash.add(inputs[i]);
var d = hash.close();
- Expect.equals(mds[i], CryptoUtils.bytesToHex(d), '$i');
+ expect(mds[i], CryptoUtils.bytesToHex(d), reason: '$i');
}
}
-
-void main() {
- test();
- testInvalidUse();
- testRepeatedDigest();
- testStandardVectors(sha256_long_inputs, sha256_long_mds);
- testStandardVectors(sha256_short_inputs, sha256_short_mds);
-}
-