-
Notifications
You must be signed in to change notification settings - Fork 53
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 all commits
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,10 @@ class _TestCase { | |
// Parameters for key import (always required) | ||
final Map<String, dynamic>? importKeyParams; | ||
|
||
// If not `null`, then importing private or public key MUST throw | ||
// an exception that contains this string. | ||
final String? importKeyException; | ||
|
||
// Parameters for sign/verify (required, if there is a signature) | ||
final Map<String, dynamic>? signVerifyParams; | ||
|
||
|
@@ -90,6 +94,7 @@ class _TestCase { | |
this.signVerifyParams, | ||
this.encryptDecryptParams, | ||
this.deriveParams, | ||
this.importKeyException, | ||
}); | ||
|
||
factory _TestCase.fromJson(Map json) { | ||
|
@@ -110,6 +115,7 @@ class _TestCase { | |
derivedBits: _optionalBase64Decode(json['derivedBits']), | ||
derivedLength: json['derivedLength'] as int?, | ||
importKeyParams: _optionalStringMapDecode(json['importKeyParams']), | ||
importKeyException: json['importKeyException'] as String?, | ||
signVerifyParams: _optionalStringMapDecode(json['signVerifyParams']), | ||
encryptDecryptParams: | ||
_optionalStringMapDecode(json['encryptDecryptParams']), | ||
|
@@ -691,7 +697,14 @@ void _validateTestCase<PrivateKey, PublicKey>( | |
check(c.importKeyParams != null); | ||
check((c.signVerifyParams != null) == (r._signBytes != null)); | ||
check((c.encryptDecryptParams != null) == (r._encryptBytes != null)); | ||
check((c.deriveParams != null) == (r._deriveBits != null)); | ||
|
||
if (c.deriveParams != null) { | ||
check((c.deriveParams != null) == (r._deriveBits != null)); | ||
|
||
if (r._deriveBits != null) { | ||
check(c.derivedLength != null); | ||
} | ||
} | ||
if (c.signature != null) { | ||
check(r._signBytes != null); | ||
} | ||
|
@@ -701,9 +714,6 @@ void _validateTestCase<PrivateKey, PublicKey>( | |
if (c.derivedBits != null) { | ||
check(r._deriveBits != null); | ||
} | ||
if (r._deriveBits != null) { | ||
check(c.derivedLength != null); | ||
} | ||
|
||
// Check that data matches the methods we have in the runner. | ||
check(r._importPrivateRawKey != null || c.privateRawKeyData == null); | ||
|
@@ -712,6 +722,30 @@ void _validateTestCase<PrivateKey, PublicKey>( | |
check(r._importPublicRawKey != null || c.publicRawKeyData == null); | ||
check(r._importPublicSpkiKey != null || c.publicSpkiKeyData == null); | ||
check(r._importPublicJsonWebKey != null || c.publicJsonWebKeyData == null); | ||
|
||
if (c.generateKeyParams != null) { | ||
check(c.importKeyException == null, | ||
'importKeyException must be null when generateKeyParams is provided'); | ||
} | ||
|
||
if (c.importKeyException != null) { | ||
check(c.plaintext == null, | ||
'plaintext must be null when importKeyException is provided'); | ||
check(c.signature == null, | ||
'signature must be null when importKeyException is provided'); | ||
check(c.ciphertext == null, | ||
'ciphertext must be null when importKeyException is provided'); | ||
check(c.derivedBits == null, | ||
'derivedBits must be null when importKeyException is provided'); | ||
check(c.derivedLength == null, | ||
'derivedLength must be null when importKeyException is provided'); | ||
check(c.signVerifyParams == null, | ||
'signVerifyParams must be null when importKeyException is provided'); | ||
check(c.encryptDecryptParams == null, | ||
'encryptDecryptParams must be null when importKeyException is provided'); | ||
check(c.deriveParams == null, | ||
'deriveParams must be null when importKeyException is provided'); | ||
} | ||
} | ||
|
||
void _runTests<PrivateKey, PublicKey>( | ||
|
@@ -746,6 +780,30 @@ void _runTests<PrivateKey, PublicKey>( | |
publicKey = pair.publicKey; | ||
privateKey = pair.privateKey; | ||
}); | ||
} else if (c.importKeyException != null) { | ||
if (c.privatePkcs8KeyData != 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.importKeyException!)); | ||
} | ||
}); | ||
} | ||
if (c.privateJsonWebKeyData != null) { | ||
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.importKeyException!)); | ||
} | ||
}); | ||
} | ||
Comment on lines
+784
to
+805
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. We need a test case for each kind of key possible:
Also I'd suggest naming the test cases like: if (c.publicRawKeyData != null) {
test('importPublicRawKey() throws', () async {
...
});
}
if (c.publicJsonWebKeyData != null) {
test('importPublicJsonWebKey() throws', () async {
...
});
}
...
// This test case is testing failure to import a key
// further tests will not be conducted, since they don't
// make sense after failing to import a key.
return; |
||
return; | ||
} else { | ||
test('import key-pair', () async { | ||
// Get a privateKey | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,39 @@ final _testData = [ | |
"importKeyParams": {"curve": "p-256"}, | ||
"deriveParams": {} | ||
}, | ||
{ | ||
"name": "generated on boringssl/linux (pkcs8 import key exception) at 2020-01-22T23:24:34", | ||
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. I think we can do a better "name" here. For exceptions, we shouldn't really care where the test case was generated. So better make the test case name say: "import incorrect curve" or something like that. |
||
"privatePkcs8KeyData": | ||
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3aTiZ7odKAODYk4BpZlzulBCB/BptmxjtvrzyXI71UyhRANCAATl0GVa8O1sXXf2NV5qGJ/9/Vq8PVWCZuezADa1F0Vr2TaB8BseZIW+rhmEmLC2FfCdxj9NmLp00SilRTm40Hxm", | ||
"publicRawKeyData": | ||
"BHiIXxrwhM92v4ueDrj3x1JJY4uS+II/IJPjqMvaKj/QfoOllnEkrnaOW1owBYRBMnP0pPouPkqbVfPACMUsfKs=", | ||
"publicSpkiKeyData": | ||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeIhfGvCEz3a/i54OuPfHUklji5L4gj8gk+Ooy9oqP9B+g6WWcSSudo5bWjAFhEEyc/Sk+i4+SptV88AIxSx8qw==", | ||
Comment on lines
+116
to
+121
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. We should change the Right now a test case is require to have one of:
We should want that if Because, in some cases you may want to check an exception that can only be produced when trying to import a private key (or trying to import a public key). Example: trying to import a public key will never complain about |
||
"importKeyParams": {"curve": "p-256"}, | ||
"importKeyException": "FormatException: incorrect elliptic curve" | ||
}, | ||
{ | ||
"name": "generated on boringssl/linux (jwk import key exception) at 2020-01-22T23:24:34", | ||
"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" | ||
}, | ||
"importKeyParams": {"curve": "p-256"}, | ||
"importKeyException": "JWK property \"crv\" is not" | ||
}, | ||
{ | ||
"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.
Is this unrelated to this change, if so let's make it a different PR