Skip to content

Commit

Permalink
test: add a lot of unit tests to improve the overall code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed Moussa <[email protected]>
  • Loading branch information
hamada147 committed Dec 25, 2023
1 parent d0d7d06 commit 92881f3
Show file tree
Hide file tree
Showing 19 changed files with 558 additions and 13 deletions.
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)
}

@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)
}
}
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

0 comments on commit 92881f3

Please sign in to comment.