Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Nov 18, 2024
1 parent 13ad188 commit df4fe43
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/vauthenticator/server/keys/KeyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class KeyConfig {
kmsClient: KmsClient,
storage: KeyGeneratorMasterKeyStorage
): KeyGenerator = JavaSecurityKeyGenerator(
KeyCryptographicOperations(
JavaSecurityCryptographicOperations(
KeyGeneratorMasterKeyRepository(storage)
)
)
Expand All @@ -45,7 +45,7 @@ class KeyConfig {
storage: KeyGeneratorMasterKeyStorage
): KeyDecrypter = JavaSecurityKeyDecrypter(
maserKid,
KeyCryptographicOperations(
JavaSecurityCryptographicOperations(
KeyGeneratorMasterKeyRepository(storage)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec


class KeyCryptographicOperations(
class JavaSecurityCryptographicOperations(
private val repository: KeyGeneratorMasterKeyRepository
) {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.vauthenticator.server.keys.domain.MasterKid

class JavaSecurityKeyDecrypter(
private val maserKid: String,
private val keyCryptographicOperations: KeyCryptographicOperations
private val javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations
) : KeyDecrypter {
override fun decryptKey(encrypted: String): String {
return encoder.encode(keyCryptographicOperations.decryptKeyWith(MasterKid(maserKid), encrypted.toByteArray()))
return encoder.encode(javaSecurityCryptographicOperations.decryptKeyWith(MasterKid(maserKid), encrypted.toByteArray()))
.decodeToString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import java.util.*


class JavaSecurityKeyGenerator(
private val keyCryptographicOperations: KeyCryptographicOperations
private val javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations
) : KeyGenerator {


override fun dataKeyPairFor(masterKid: MasterKid): DataKey {
val generateRSAKeyPair = keyCryptographicOperations.generateRSAKeyPair()
val generateRSAKeyPair = javaSecurityCryptographicOperations.generateRSAKeyPair()
return DataKey(
keyCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded),
javaSecurityCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded),
Optional.of(generateRSAKeyPair.public.encoded)
)
}

override fun dataKeyFor(masterKid: MasterKid): DataKey {
val generateRSAKeyPair = keyCryptographicOperations.generateRSAKeyPair()
val generateRSAKeyPair = javaSecurityCryptographicOperations.generateRSAKeyPair()
return DataKey(
keyCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded),
javaSecurityCryptographicOperations.encryptKeyWith(masterKid, generateRSAKeyPair.private.encoded),
Optional.empty()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.vauthenticator.server.keys.adapter.java

import com.vauthenticator.server.extentions.decoder
import com.vauthenticator.server.support.KeysUtils
import io.mockk.*
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.spec.RSAKeyGenParameterSpec
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

@ExtendWith(MockKExtension::class)
class JavaSecurityCryptographicOperationsTest {

@MockK
lateinit var repository: KeyGeneratorMasterKeyRepository

lateinit var uut: JavaSecurityCryptographicOperations

@BeforeEach
fun setUp() {
uut = JavaSecurityCryptographicOperations(repository)
}

@Test
fun `when a new rsa key pair is created`() {
mockkStatic(KeyPairGenerator::class)
val expected = mockk<KeyPair>()
val generator = mockk<KeyPairGenerator>(relaxed = true)
every { KeyPairGenerator.getInstance("RSA", "BC") } returns generator
every { generator.initialize(RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4)) } just runs
every { generator.generateKeyPair() } returns expected

val actual = uut.generateRSAKeyPair()
Assertions.assertEquals(expected, actual)
}

@Test
fun `when an encoded plain text is encrypted with some master key`() {
val expected = "ENCRYPTED_DATA".toByteArray()
val encodedPlainText = "INPUT_TEXT".toByteArray()
val masterKeyValue = "QV9LRVk="
val key = SecretKeySpec(decoder.decode(masterKeyValue), "AES")
val cipher = mockk<Cipher>(relaxed = true)
mockkStatic(Cipher::class)

every { repository.maskerKeyFor(KeysUtils.aMasterKey) } returns masterKeyValue
every { Cipher.getInstance("AES") } returns cipher
every { cipher.init(Cipher.ENCRYPT_MODE, key) } just runs
every { cipher.doFinal(encodedPlainText) } returns expected

val actual = uut.encryptKeyWith(KeysUtils.aMasterKey, encodedPlainText)
Assertions.assertEquals(expected, actual)
}

@Test
fun `when an encoded encrypted text is decrypted with some master key`() {
val expected = "DECRYPTED_DATA".toByteArray()
val encodedEncryptedText = "RU5DUllQVEVEX0lOUFVUX1RFWFQ=".toByteArray()
val masterKeyValue = "QV9LRVk="
val key = SecretKeySpec(decoder.decode(masterKeyValue), "AES")
val cipher = mockk<Cipher>(relaxed = true)
mockkStatic(Cipher::class)

every { repository.maskerKeyFor(KeysUtils.aMasterKey) } returns masterKeyValue
every { Cipher.getInstance("AES") } returns cipher
every { cipher.init(Cipher.DECRYPT_MODE, key) } just runs
every { cipher.doFinal(decoder.decode(encodedEncryptedText)) } returns expected

val actual = uut.decryptKeyWith(KeysUtils.aMasterKey, encodedEncryptedText)
Assertions.assertEquals(expected, actual)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import org.junit.jupiter.api.extension.ExtendWith
class JavaSecurityKeyDecrypterTest {

@MockK
lateinit var keyCryptographicOperations: KeyCryptographicOperations
lateinit var javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations

@Test
fun `happy path`() {
val encrypted = "AN_ENCRYPTED_VALUE"
val decrypted = "AN_UNENCRYPTED_VALUE".toByteArray()
val maserKid = "A_MASTER_KEY"

val uut = JavaSecurityKeyDecrypter(maserKid, keyCryptographicOperations)
val uut = JavaSecurityKeyDecrypter(maserKid, javaSecurityCryptographicOperations)

every { keyCryptographicOperations.decryptKeyWith(MasterKid(maserKid), encrypted.toByteArray()) } returns decrypted
every { javaSecurityCryptographicOperations.decryptKeyWith(MasterKid(maserKid), encrypted.toByteArray()) } returns decrypted

val actual = uut.decryptKey(encrypted)
val expected = encoder.encode(decrypted).decodeToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.security.PublicKey
class JavaSecurityKeyGeneratorTest {

@MockK
lateinit var keyCryptographicOperations: KeyCryptographicOperations
lateinit var javaSecurityCryptographicOperations: JavaSecurityCryptographicOperations

private val masterKid = MasterKid("A_MASTER_KEY")
private val anEncryptedPrivateKEyValueAsByteArray = "AN_ENCRYPTED_PRIVATE_KEY_VALUE".toByteArray()
Expand All @@ -29,20 +29,20 @@ class JavaSecurityKeyGeneratorTest {

@BeforeEach
fun setUp() {
uut = JavaSecurityKeyGenerator(keyCryptographicOperations)
uut = JavaSecurityKeyGenerator(javaSecurityCryptographicOperations)
}

@Test
fun `when a new data key is created`() {
val keyPair = mockk<KeyPair>()
val privateKey = mockk<PrivateKey>()

every { keyCryptographicOperations.generateRSAKeyPair() } returns keyPair
every { javaSecurityCryptographicOperations.generateRSAKeyPair() } returns keyPair
every { keyPair.private } returns privateKey
every { privateKey.encoded } returns anEncryptedPrivateKEyValueAsByteArray

every {
keyCryptographicOperations.encryptKeyWith(
javaSecurityCryptographicOperations.encryptKeyWith(
masterKid,
anEncryptedPrivateKEyValueAsByteArray
)
Expand All @@ -60,15 +60,15 @@ class JavaSecurityKeyGeneratorTest {
val privateKey = mockk<PrivateKey>()
val publicKey = mockk<PublicKey>()

every { keyCryptographicOperations.generateRSAKeyPair() } returns keyPair
every { javaSecurityCryptographicOperations.generateRSAKeyPair() } returns keyPair
every { keyPair.private } returns privateKey
every { privateKey.encoded } returns anEncryptedPrivateKEyValueAsByteArray

every { keyPair.public } returns publicKey
every { publicKey.encoded } returns aPublicKeyValueAsByteArray

every {
keyCryptographicOperations.encryptKeyWith(
javaSecurityCryptographicOperations.encryptKeyWith(
masterKid,
anEncryptedPrivateKEyValueAsByteArray
)
Expand Down

This file was deleted.

0 comments on commit df4fe43

Please sign in to comment.