-
Notifications
You must be signed in to change notification settings - Fork 65
Add tests for Import Key Exception #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,9 @@ class _TestCase { | |
// Parameters for key import (always required) | ||
final Map<String, dynamic>? importKeyParams; | ||
|
||
// Parameters for import key exception (optional, if there is an exception) | ||
final Map<String, dynamic>? importKeyException; | ||
|
||
// Parameters for sign/verify (required, if there is a signature) | ||
final Map<String, dynamic>? signVerifyParams; | ||
|
||
|
@@ -72,6 +75,20 @@ class _TestCase { | |
// Parameters for deriveBits (required, if there is a derivedBits) | ||
final Map<String, dynamic>? deriveParams; | ||
|
||
String? get pkcs8ImportKeyException { | ||
if (importKeyException != null) { | ||
return importKeyException?['pkcs8Exception']; | ||
} | ||
return null; | ||
} | ||
|
||
String? get jwkImportKeyException { | ||
if (importKeyException != null) { | ||
return importKeyException?['jwkException']; | ||
} | ||
return null; | ||
} | ||
|
||
_TestCase( | ||
this.name, { | ||
this.generateKeyParams, | ||
|
@@ -90,6 +107,7 @@ class _TestCase { | |
this.signVerifyParams, | ||
this.encryptDecryptParams, | ||
this.deriveParams, | ||
this.importKeyException, | ||
}); | ||
|
||
factory _TestCase.fromJson(Map json) { | ||
|
@@ -110,6 +128,7 @@ class _TestCase { | |
derivedBits: _optionalBase64Decode(json['derivedBits']), | ||
derivedLength: json['derivedLength'] as int?, | ||
importKeyParams: _optionalStringMapDecode(json['importKeyParams']), | ||
importKeyException: _optionalStringMapDecode(json['importKeyException']), | ||
signVerifyParams: _optionalStringMapDecode(json['signVerifyParams']), | ||
encryptDecryptParams: | ||
_optionalStringMapDecode(json['encryptDecryptParams']), | ||
|
@@ -746,6 +765,23 @@ void _runTests<PrivateKey, PublicKey>( | |
publicKey = pair.publicKey; | ||
privateKey = pair.privateKey; | ||
}); | ||
} else if (c.importKeyException != null) { | ||
test('pkcs8 import exception', () async { | ||
try { | ||
|
||
await r._importPrivatePkcs8Key!(c.privatePkcs8KeyData!, {'curve': 'p-521'}); | ||
check(false, 'Expected an exception for P-512 import'); | ||
} catch (e) { | ||
check(e.toString().contains(c.pkcs8ImportKeyException!)); | ||
} | ||
}); | ||
test('jwk import exception', () async { | ||
try { | ||
await r._importPrivateJsonWebKey!(c.privateJsonWebKeyData!, {'curve': 'p-521'}); | ||
check(false, 'Expected an exception for P-512 import'); | ||
} catch (e) { | ||
check(e.toString().contains(c.jwkImportKeyException!)); | ||
} | ||
}); | ||
|
||
} else { | ||
test('import key-pair', () async { | ||
// Get a privateKey | ||
|
@@ -876,7 +912,8 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
test('validated derivedBits', () async { | ||
if (c.pkcs8ImportKeyException == null){ | ||
test('validated derivedBits', () async { | ||
final derived = await r._deriveBits( | ||
_KeyPair( | ||
privateKey: privateKey as PrivateKey, | ||
|
@@ -889,6 +926,7 @@ void _runTests<PrivateKey, PublicKey>( | |
'failed to derivedBits are not consistent', | ||
); | ||
}); | ||
} | ||
} | ||
|
||
//------------------------------ Utilities for testing | ||
|
@@ -1021,7 +1059,7 @@ void _runTests<PrivateKey, PublicKey>( | |
|
||
//------------------------------ Test import public key | ||
|
||
if (c.publicRawKeyData != null) { | ||
if (c.publicRawKeyData != null && c.pkcs8ImportKeyException == null) { | ||
assert(!r._isSymmetric && r._importPublicRawKey != null); | ||
|
||
test('importPublicRawKey()', () async { | ||
|
@@ -1033,7 +1071,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (c.publicSpkiKeyData != null) { | ||
if (c.publicSpkiKeyData != null && c.pkcs8ImportKeyException == null) { | ||
assert(!r._isSymmetric && r._importPublicSpkiKey != null); | ||
|
||
test('importPublicSpkiKey()', () async { | ||
|
@@ -1045,7 +1083,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (c.publicJsonWebKeyData != null) { | ||
if (c.publicJsonWebKeyData != null && c.jwkImportKeyException == null) { | ||
assert(!r._isSymmetric && r._importPublicJsonWebKey != null); | ||
|
||
test('importPublicJsonWebKey()', () async { | ||
|
@@ -1069,7 +1107,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (c.privatePkcs8KeyData != null) { | ||
if (c.privatePkcs8KeyData != null && c.pkcs8ImportKeyException == null) { | ||
test('importPrivatePkcs8Key()', () async { | ||
final key = await r._importPrivatePkcs8Key!( | ||
c.privatePkcs8KeyData!, | ||
|
@@ -1079,7 +1117,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (c.privateJsonWebKeyData != null) { | ||
if (c.privateJsonWebKeyData != null && c.jwkImportKeyException == null) { | ||
test('importPrivateJsonWebKey()', () async { | ||
final key = await r._importPrivateJsonWebKey!( | ||
c.privateJsonWebKeyData!, | ||
|
@@ -1360,7 +1398,7 @@ void _runTests<PrivateKey, PublicKey>( | |
} | ||
|
||
//------------------------------ Test derivedBits | ||
if (r._deriveBits != null) { | ||
if (r._deriveBits != null && (c.pkcs8ImportKeyException == null || c.jwkImportKeyException == null)) { | ||
test('deriveBits', () async { | ||
final derived = await r._deriveBits( | ||
_KeyPair( | ||
|
@@ -1374,7 +1412,7 @@ void _runTests<PrivateKey, PublicKey>( | |
} | ||
|
||
//------------------------------ export/import private key | ||
if (r._exportPrivateRawKey != null) { | ||
if (r._exportPrivateRawKey != null && (c.pkcs8ImportKeyException == null || c.jwkImportKeyException == null)) { | ||
test('export/import raw private key', () async { | ||
final keyData = await r._exportPrivateRawKey(privateKey as PrivateKey); | ||
check(keyData.isNotEmpty, 'exported key is empty'); | ||
|
@@ -1384,7 +1422,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (r._exportPrivatePkcs8Key != null) { | ||
if (r._exportPrivatePkcs8Key != null && c.pkcs8ImportKeyException == null) { | ||
test('export/import pkcs8 private key', () async { | ||
final keyData = await r._exportPrivatePkcs8Key(privateKey as PrivateKey); | ||
check(keyData.isNotEmpty, 'exported key is empty'); | ||
|
@@ -1394,7 +1432,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (r._exportPrivateJsonWebKey != null) { | ||
if (r._exportPrivateJsonWebKey != null && c.jwkImportKeyException == null) { | ||
test('export/import jwk private key', () async { | ||
final jwk = await r._exportPrivateJsonWebKey(privateKey as PrivateKey); | ||
check(jwk.isNotEmpty, 'exported key is empty'); | ||
|
@@ -1406,7 +1444,7 @@ void _runTests<PrivateKey, PublicKey>( | |
|
||
//------------------------------ export/import public key | ||
|
||
if (r._exportPublicRawKey != null) { | ||
if (r._exportPublicRawKey != null && (c.pkcs8ImportKeyException == null || c.jwkImportKeyException == null)) { | ||
assert(!r._isSymmetric && r._importPublicRawKey != null); | ||
|
||
test('export/import raw public key', () async { | ||
|
@@ -1418,7 +1456,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (r._exportPublicSpkiKey != null) { | ||
if (r._exportPublicSpkiKey != null && c.pkcs8ImportKeyException == null) { | ||
assert(!r._isSymmetric && r._importPublicSpkiKey != null); | ||
|
||
test('export/import pkcs8 public key', () async { | ||
|
@@ -1430,7 +1468,7 @@ void _runTests<PrivateKey, PublicKey>( | |
}); | ||
} | ||
|
||
if (r._exportPublicJsonWebKey != null) { | ||
if (r._exportPublicJsonWebKey != null && c.jwkImportKeyException == null) { | ||
assert(!r._isSymmetric && r._importPublicJsonWebKey != null); | ||
|
||
test('export/import jwk public key', () async { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,36 @@ final _testData = [ | |
"importKeyParams": {"curve": "p-256"}, | ||
"deriveParams": {} | ||
}, | ||
{ | ||
"name": "generated on boringssl/linux (import key exception) at 2020-01-22T23:24:34", | ||
"privatePkcs8KeyData": | ||
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3aTiZ7odKAODYk4BpZlzulBCB/BptmxjtvrzyXI71UyhRANCAATl0GVa8O1sXXf2NV5qGJ/9/Vq8PVWCZuezADa1F0Vr2TaB8BseZIW+rhmEmLC2FfCdxj9NmLp00SilRTm40Hxm", | ||
"privateJsonWebKeyData": { | ||
"kty": "EC", | ||
"crv": "P-256", | ||
"x": "5dBlWvDtbF139jVeahif_f1avD1VgmbnswA2tRdFa9k", | ||
"y": "NoHwGx5khb6uGYSYsLYV8J3GP02YunTRKKVFObjQfGY", | ||
"d": "3aTiZ7odKAODYk4BpZlzulBCB_BptmxjtvrzyXI71Uw" | ||
}, | ||
"publicRawKeyData": | ||
"BHiIXxrwhM92v4ueDrj3x1JJY4uS+II/IJPjqMvaKj/QfoOllnEkrnaOW1owBYRBMnP0pPouPkqbVfPACMUsfKs=", | ||
"publicSpkiKeyData": | ||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeIhfGvCEz3a/i54OuPfHUklji5L4gj8gk+Ooy9oqP9B+g6WWcSSudo5bWjAFhEEyc/Sk+i4+SptV88AIxSx8qw==", | ||
"publicJsonWebKeyData": { | ||
"kty": "EC", | ||
"crv": "P-256", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This curve doesn't look wrong? |
||
"x": "eIhfGvCEz3a_i54OuPfHUklji5L4gj8gk-Ooy9oqP9A", | ||
"y": "foOllnEkrnaOW1owBYRBMnP0pPouPkqbVfPACMUsfKs" | ||
}, | ||
"derivedBits": "WA==", | ||
"derivedLength": 7, | ||
"importKeyParams": {"curve": "p-256"}, | ||
"importKeyException": { | ||
"pkcs8Exception": "FormatException: incorrect elliptic curve", | ||
"jwkException": "JWK property \"crv\" is not" | ||
}, | ||
"deriveParams": {} | ||
}, | ||
{ | ||
"name": "generated on chrome/linux at 2020-01-22T23:24:39", | ||
"privatePkcs8KeyData": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this simpler.
Instead of making a test case that contains different strings for
pkcs8Exception
andjwkException
, we just make two test cases. One that tests the exception when importing a PKCS8 key and on that tests it when importing a JWK key.Notice that when specifying a test case, you don't have to specify both
privatePkcs8KeyData
andprivateJsonWebKeyData
, it's enough to specify one of them. So if we want to test different error messages (from different formats), we really should just make two test cases.That way, we also avoid having an absolute explosion of properties on the test case objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to update
_validateTestCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with one string
importKeyException
. Made necessary changes to_validateTestCase