Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Mar 25, 2024
1 parent 26ae1af commit c62d3df
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions ballerina/tests/private_public_key_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ isolated function testReadPrivateKeyFromP12WithInvalidKeyStorePassword() {
}
}

@test:Config {}
isolated function testReadEcPrivateKeyFromP12WithInvalidKeyStorePassword() {
KeyStore keyStore = {
path: EC_KEYSTORE_PATH,
password: "invalid"
};
PrivateKey|Error result = decodeEcPrivateKeyFromKeyStore(keyStore, "ec-keypair", "ballerina");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlKemPrivateKeyFromP12WithInvalidKeyStorePassword() {
KeyStore keyStore = {
path: MLKEM_KEYSTORE_PATH,
password: "invalid"
};
PrivateKey|Error result = decodeMlKem768PrivateKeyFromKeyStore(keyStore, "mlkem-keypair", "ballerina");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlDsaPrivateKeyFromP12WithInvalidKeyStorePassword() {
KeyStore keyStore = {
path: MLDSA_KEYSTORE_PATH,
password: "invalid"
};
PrivateKey|Error result = decodeMlDsa65PrivateKeyFromKeyStore(keyStore, "mldsa-keypair", "ballerina");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadPrivateKeyFromP12WithInvalidAlias() {
KeyStore keyStore = {
Expand Down Expand Up @@ -254,6 +296,36 @@ isolated function testReadPrivateKeyFromNonExistingKeyFile() {
}
}

@test:Config {}
isolated function testReadEcPrivateKeyFromNonExistingKeyFile() {
PrivateKey|Error result = decodeEcPrivateKeyFromKeyFile(INVALID_PRIVATE_KEY_PATH);
if result is Error {
test:assertTrue(result.message().includes("Key file not found at:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlDsaPrivateKeyFromInvalidKeyFile() {
PrivateKey|Error result = decodeMlDsa65PrivateKeyFromKeyFile(INVALID_PRIVATE_KEY_PATH);
if result is Error {
test:assertTrue(result.message().includes("Key file not found at:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlKemPrivateKeyFromInvalidKeyFile() {
PrivateKey|Error result = decodeMlKem768PrivateKeyFromKeyFile(INVALID_PRIVATE_KEY_PATH);
if result is Error {
test:assertTrue(result.message().includes("Key file not found at:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testParsePublicKeyFromP12() returns Error? {
TrustStore trustStore = {
Expand Down Expand Up @@ -303,6 +375,48 @@ isolated function testReadPublicKeyFromP12WithInvalidTrustStorePassword() {
}
}

@test:Config {}
isolated function testReadEcPublicKeyFromP12WithInvalidTrustStorePassword() {
TrustStore trustStore = {
path: EC_KEYSTORE_PATH,
password: "invalid"
};
PublicKey|Error result = decodeEcPublicKeyFromTrustStore(trustStore, "ec-keypair");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlKemPublicKeyFromP12WithInvalidTrustStorePassword() {
TrustStore trustStore = {
path: MLKEM_KEYSTORE_PATH,
password: "invalid"
};
PublicKey|Error result = decodeMlKem768PublicKeyFromTrustStore(trustStore, "mlkem-keypair");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlDsaPublicKeyFromP12WithInvalidTrustStorePassword() {
TrustStore trustStore = {
path: MLDSA_KEYSTORE_PATH,
password: "invalid"
};
PublicKey|Error result = decodeMlDsa65PublicKeyFromTrustStore(trustStore, "mldsa-keypair");
if result is Error {
test:assertTrue(result.message().includes("Unable to open KeyStore:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadPublicKeyFromP12WithInvalidAlias() {
TrustStore trustStore = {
Expand Down Expand Up @@ -394,6 +508,16 @@ isolated function testReadPublicKeyFromNonExistingCertFile() {
}
}

@test:Config {}
isolated function testReadEcPublicKeyFromNonExistingCertFile() {
PublicKey|Error result = decodeEcPublicKeyFromCertFile(INVALID_PUBLIC_CERT_PATH);
if result is Error {
test:assertTrue(result.message().includes("Certificate file not found at:"));
} else {
test:assertFail("Expected error not found.");
}
}

@test:Config {}
isolated function testReadMlDsaPublicKeyFromInvalidCertFile() {
PublicKey|Error result = decodeMlDsa65PublicKeyFromCertFile(INVALID_PUBLIC_CERT_PATH);
Expand Down

0 comments on commit c62d3df

Please sign in to comment.