Skip to content

Commit

Permalink
Add negative test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhashinee committed Nov 28, 2023
1 parent 8d22708 commit 0a1b654
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions ballerina/tests/sign_verify_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,59 @@ isolated function testVerifySha384withEcdsa() returns Error? {
byte[] sha384withEcdsaSignature = check signSha384withEcdsa(payload, privateKey);
test:assertTrue(check verifySha384withEcdsaSignature(payload, sha384withEcdsaSignature, publicKey));
}

@test:Config {}
isolated function testDecodeRsaPrivateKeyError() returns Error? {
KeyStore keyStore = {
path: EC_KEYSTORE_PATH,
password: "ballerina"
};
PrivateKey|Error privateKey = decodeRsaPrivateKeyFromKeyStore(keyStore, "ec-keypair", "ballerina");
if privateKey is Error {
test:assertEquals(privateKey.message(), "Not a valid RSA key.");
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testDecodeEcPrivateKeyError() returns Error? {
KeyStore keyStore = {
path: KEYSTORE_PATH,
password: "ballerina"
};
PrivateKey|Error privateKey = decodeEcPrivateKeyFromKeyStore(keyStore, "ballerina", "ballerina");
if privateKey is Error {
test:assertEquals(privateKey.message(), "Not a valid EC key.");
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testDecodeEcPublicKeyError() returns Error? {
KeyStore keyStore = {
path: KEYSTORE_PATH,
password: "ballerina"
};
PublicKey|Error publicKey = decodeEcPublicKeyFromTrustStore(keyStore, "ballerina");
if publicKey is Error {
test:assertEquals(publicKey.message(), "Not a valid EC public key.");
} else {
test:assertFail("Expected error not found");
}
}

@test:Config {}
isolated function testDecodeRsaPublicKeyError() returns Error? {
KeyStore keyStore = {
path: EC_KEYSTORE_PATH,
password: "ballerina"
};
PublicKey|Error publicKey = decodeRsaPublicKeyFromTrustStore(keyStore, "ec-keypair");
if publicKey is Error {
test:assertEquals(publicKey.message(), "Not a valid RSA public key.");
} else {
test:assertFail("Expected error not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static Object buildRsaPublicKeyRecord(Certificate certificate) {
if (publicKey.getAlgorithm().equals(Constants.RSA_ALGORITHM)) {
return getPublicKeyRecord(certificate, certificateBMap, publicKey);
}
return CryptoUtils.createError("Not a valid RSA key.");
return CryptoUtils.createError("Not a valid RSA public key.");
}

private static Object buildEcPublicKeyRecord(Certificate certificate) {
Expand All @@ -256,7 +256,7 @@ private static Object buildEcPublicKeyRecord(Certificate certificate) {
if (publicKey.getAlgorithm().equals(Constants.EC_ALGORITHM)) {
return getPublicKeyRecord(certificate, certificateBMap, publicKey);
}
return CryptoUtils.createError("Not a valid EC key.");
return CryptoUtils.createError("Not a valid EC public key.");
}

private static Object getPublicKeyRecord(Certificate certificate, BMap<BString, Object> certificateBMap,
Expand Down

0 comments on commit 0a1b654

Please sign in to comment.