Skip to content
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

test: add a lot of unit tests to improve the overall code coverage #143

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apollo/karma.config.d/timeout.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
config.set({
browserDisconnectTimeout: 5000,
processKillTimeout: 5000,
browserDisconnectTimeout: 10000,
processKillTimeout: 10000,
client: {
mocha: {
timeout: 5000
timeout: 10000
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ actual class KMMX25519PrivateKey(val raw: ByteArray) {
*
* @return The generated public key.
*/
fun publicKey(): KMMX25519PublicKey {
actual fun publicKey(): KMMX25519PublicKey {
val private = X25519PrivateKeyParameters(raw, 0)
val public = private.generatePublicKey()
return KMMX25519PublicKey(public.encoded)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.iohk.atala.prism.apollo

import org.junit.Test
import kotlin.test.assertTrue

class PlatformTests {
@Test
fun testOS() {
assertTrue(Platform.OS.contains("Android"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ actual class KMMX25519PrivateKey(val raw: ByteArray) {
* @throws RuntimeException if there is an error generating the public key.
*/
@Throws(RuntimeException::class)
public fun publicKey(): KMMX25519PublicKey {
public actual fun publicKey(): KMMX25519PublicKey {
val result = X25519.publicKeyWithPrivateKey(raw.toNSData())
result.failure()?.let { throw RuntimeException(it.localizedDescription()) }
val publicRaw = result.success()?.toByteArray() ?: throw RuntimeException("Null result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ package io.iohk.atala.prism.apollo.utils
/**
* Definition of the KMMX25519PrivateKey functionality
*/
expect class KMMX25519PrivateKey
expect class KMMX25519PrivateKey {
/**
* Generates a public key from the given private key.
*
* @return The generated public key.
*/
fun publicKey(): KMMX25519PublicKey
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.iohk.atala.prism.apollo.base64
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class Base64Tests {
// Extension
Expand Down Expand Up @@ -191,4 +192,78 @@ class Base64Tests {
fun testEncodeBase16RFC_4648_6() {
assertEquals("Zm9vYmFy", "foobar".base64PadEncoded)
}

@Test
fun testStringBase64Encoded() {
assertEquals("aGVsbG8", "hello".base64Encoded)
}

@Test
fun testStringBase64Decoded() {
assertEquals("hello", "aGVsbG8".base64Decoded)
}
hamada147 marked this conversation as resolved.
Show resolved Hide resolved

@Test
fun testStringBase64UrlPadEncoded() {
assertEquals("aGVsbG8=", "hello".base64UrlPadEncoded)
}

@Test
fun testByteArrayBase64Encoded() {
assertEquals(
"SGVsbG8gd29ybGQh",
"Hello world!".encodeToByteArray().base64Encoded
)
}

@Test
fun testByteArrayBase64Decoded() {
assertEquals(
"Hello world!",
"SGVsbG8gd29ybGQh".encodeToByteArray().base64Decoded
)
}

@Test
fun testByteArrayBase64PadDecoded() {
assertEquals(
"Hello world!",
"SGVsbG8gd29ybGQh".encodeToByteArray().base64PadDecoded
)
}

@Test
fun testByteArrayBase64UrlDecoded() {
assertEquals(
"Hello, world!",
"SGVsbG8sIHdvcmxkIQ==".encodeToByteArray().base64UrlDecoded
)
}

@Test
fun testByteArrayBase64UrlPadEncoded() {
assertEquals(
"aGVsbG8=",
"hello".encodeToByteArray().base64UrlPadEncoded
)
}

@Test
fun asCharArrayShouldReturnEmptyCharArrayForEmptyByteArray() {
val byteArray: ByteArray = byteArrayOf()
val charArray = byteArray.asCharArray()

assertEquals(0, charArray.size)
}

@Test
fun asCharArrayShouldReturnCorrectCharArrayForVariousByteArray() {
val byteArray: ByteArray = byteArrayOf(72, 101, 108, 108, 111) // ASCII values for "Hello"
val charArray = byteArray.asCharArray()
val expectedCharArray: CharArray = charArrayOf('H', 'e', 'l', 'l', 'o')

assertTrue {
charArray contentEquals expectedCharArray
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.iohk.atala.prism.apollo.derivation

import com.ionspin.kotlin.bignum.integer.toBigInteger
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class BigIntegerWrapperTests {
@Test
fun initFromIntShouldInitializeValueCorrectly() {
val intVal = Random.nextInt()
val bigIntegerWrapper = BigIntegerWrapper(intVal)
assertEquals(intVal.toBigInteger(), bigIntegerWrapper.value)
}

@Test
fun initFromLongShouldInitializeValueCorrectly() {
val longVal = Random.nextLong()
val bigIntegerWrapper = BigIntegerWrapper(longVal)
assertEquals(longVal.toBigInteger(), bigIntegerWrapper.value)
}

@Test
fun initFromShortShouldInitializeValueCorrectly() {
val shortVal = Random.nextInt(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort()
val bigIntegerWrapper = BigIntegerWrapper(shortVal)
assertEquals(shortVal.toInt().toBigInteger(), bigIntegerWrapper.value)
}

@Test
fun initFromByteShouldInitializeValueCorrectly() {
val byteVal = Random.nextInt(Byte.MIN_VALUE.toInt(), Byte.MAX_VALUE.toInt()).toByte()
val bigIntegerWrapper = BigIntegerWrapper(byteVal)
assertEquals(byteVal.toInt().toBigInteger(), bigIntegerWrapper.value)
}

@Test
fun initFromStringShouldInitializeValueCorrectly() {
val strVal = Random.nextLong().toString()
val bigIntegerWrapper = BigIntegerWrapper(strVal)
assertEquals(strVal.toBigInteger(), bigIntegerWrapper.value)
}

@Test
fun initFromBigIntegerShouldInitializeValueCorrectly() {
val bigIntVal = Random.nextLong().toBigInteger()
val bigIntegerWrapper = BigIntegerWrapper(bigIntVal)
assertEquals(bigIntVal, bigIntegerWrapper.value)
}

@Test
fun equalsShouldReturnTrueForSameObjectAndFalseForDifferentObject() {
val bigIntegerWrapper1 = BigIntegerWrapper(Random.nextInt())
val bigIntegerWrapper2 = BigIntegerWrapper(bigIntegerWrapper1.value)
val bigIntegerWrapper3 = BigIntegerWrapper(Random.nextInt())

assertEquals(bigIntegerWrapper1, bigIntegerWrapper2)
assertNotEquals(bigIntegerWrapper1, bigIntegerWrapper3)
}

@Test
fun hashCodeShouldReturnSameValueForSameObjectAndDifferentValueForDifferentObject() {
val bigIntegerWrapper1 = BigIntegerWrapper(Random.nextInt())
val bigIntegerWrapper2 = BigIntegerWrapper(bigIntegerWrapper1.value)
val bigIntegerWrapper3 = BigIntegerWrapper(Random.nextInt())

assertEquals(bigIntegerWrapper1.hashCode(), bigIntegerWrapper2.hashCode())
assertNotEquals(bigIntegerWrapper1.hashCode(), bigIntegerWrapper3.hashCode())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.iohk.atala.prism.apollo.derivation

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue

class DerivationAxisTests {
@Test
fun hardenedShouldInitializeCorrectly() {
val num = 15 // Just an arbitrary number for testing

val hardenedDerivationAxis = DerivationAxis.hardened(num)

assertEquals(num or (1 shl 31), hardenedDerivationAxis.i)
assertTrue(hardenedDerivationAxis.hardened)
assertEquals(num, hardenedDerivationAxis.number)
}

@Test
fun hardenedShouldFailOnNegativeInput() {
val num = -10
assertFailsWith(IllegalArgumentException::class) {
DerivationAxis.hardened(num)
}
}

@Test
fun normalShouldInitializeCorrectly() {
val num = 20 // Just an arbitrary number for testing

val normalDerivationAxis = DerivationAxis.normal(num)

assertEquals(num, normalDerivationAxis.i)
assertFalse(normalDerivationAxis.hardened)
assertEquals(num, normalDerivationAxis.number)
}

@Test
fun normalShouldFailOnNegativeInput() {
val num = -10
assertFailsWith(IllegalArgumentException::class) {
DerivationAxis.hardened(num)
}
}

@Test
fun toStringShouldRenderCorrectly() {
val num = 25

val hardened = DerivationAxis.hardened(num)
val normal = DerivationAxis.normal(num)

assertEquals("$num'", hardened.toString())
assertEquals(num.toString(), normal.toString())
}

@Test
fun hashCodeShouldCalculateCorrectly() {
val num = 30
val derivationAxis = DerivationAxis.normal(num)

assertEquals(num.hashCode(), derivationAxis.hashCode())
}

@Test
fun equalsShouldWorkCorrectly() {
val num1 = 35
val num2 = 36

val axis1 = DerivationAxis.normal(num1)
val axisHardened1 = DerivationAxis.hardened(num2)
val axis2 = DerivationAxis.normal(num2)

assertEquals(axis1, axis1)
assertNotEquals(axis1, axisHardened1)
assertNotEquals(axis1, axis2)
}
}
Comment on lines +10 to +81
Copy link
Contributor

@goncalo-frade-iohk goncalo-frade-iohk Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok now with this tests, HDKeyTests, DerivationPathTests the same applies that ive said before.
Yes its great to add tests but we cannot just test for the sake of coverage ;), we need to add tests that are specifically targeting a test vector on a RFC or in this case a BIP, specially like in this case its something well specified on BIP 32.

BIP 32 specifies quite a few test vectors for Derivation paths and Hardened keys. This in part will make Apollo much more secure and reliable having those tests.

https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, I will add these tests.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.iohk.atala.prism.apollo.derivation

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

class DerivationPathTests {

@Test
fun deriveShouldReturnNewDerivationPathWithAddedAxis() {
val path = DerivationPath.empty()
val axis = DerivationAxis.normal(1)

val derivedPath = path.derive(axis)

assertEquals(path.axes + axis, derivedPath.axes)
}

@Test
fun emptyShouldReturnNewDerivationPathWithNoAxes() {
val path = DerivationPath.empty()

assertTrue(path.axes.isEmpty())
}

@Test
fun toStringShouldReturnCorrectStringRepresentation() {
val axes = listOf(DerivationAxis.normal(1), DerivationAxis.hardened(2))
val path = DerivationPath(axes)

assertEquals("m/1/2'", path.toString())
}

@Test
fun fromPathShouldParseStringAndReturnCorrespondingDerivationPath() {
val pathStr = "m/1/2'"
val path = DerivationPath.fromPath(pathStr)

assertEquals(listOf(DerivationAxis.normal(1), DerivationAxis.hardened(2)), path.axes)
}

@Test
fun fromPathShouldThrowExceptionOnIncorrectFormat() {
val pathStr = "m'/1/2"

assertFailsWith(IllegalArgumentException::class) {
DerivationPath.fromPath(pathStr)
}
}

@Test
fun fromPathShouldThrowExceptionOnNegativeOrNonIntegerInput() {
val pathStr = "m/-1/1.5"
assertFailsWith(IllegalArgumentException::class) {
DerivationPath.fromPath(pathStr)
}
}
}
Loading