From 30b6485578260a4365e42c3509d0bca4c09f0531 Mon Sep 17 00:00:00 2001 From: Ahmed Moussa Date: Mon, 25 Dec 2023 05:28:58 +0200 Subject: [PATCH] test: add a lot of unit tests to improve the overall code coverage Signed-off-by: Ahmed Moussa --- apollo/karma.config.d/timeout.js | 6 +- .../prism/apollo/utils/KMMX25519PrivateKey.kt | 2 +- .../iohk/atala/prism/apollo/PlatformTests.kt | 11 ++ .../prism/apollo/utils/KMMX25519PrivateKey.kt | 2 +- .../prism/apollo/utils/KMMX25519PrivateKey.kt | 9 +- .../atala/prism/apollo/base64/Base64Tests.kt | 75 +++++++++++++ .../derivation/BigIntegerWrapperTests.kt | 71 ++++++++++++ .../apollo/derivation/DerivationAxisTests.kt | 81 ++++++++++++++ .../apollo/derivation/DerivationPathTests.kt | 59 ++++++++++ .../apollo/derivation/HDKeyOptionsTests.kt | 101 ++++++++++++++++++ .../prism/apollo/utils/ByteArrayExtTests.kt | 69 ++++++++++++ .../apollo/utils/KMMX25519KeyPairTests.kt | 6 ++ .../prism/apollo/utils/StringExtTests.kt | 39 +++++++ .../iohk/atala/prism/apollo/PlatformTests.kt} | 4 +- .../prism/apollo/utils/KMMX25519PrivateKey.kt | 2 +- .../{PlatformTest.kt => PlatformTests.kt} | 4 +- .../prism/apollo/utils/KMMX25519PrivateKey.kt | 2 +- .../iohk/atala/prism/apollo/PlatformTests.kt | 11 ++ .../iohk/atala/prism/apollo/PlatformTests.kt | 11 ++ build.gradle.kts | 12 ++- 20 files changed, 564 insertions(+), 13 deletions(-) create mode 100644 apollo/src/androidUnitTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/BigIntegerWrapperTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationAxisTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationPathTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/HDKeyOptionsTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/ByteArrayExtTests.kt create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/StringExtTests.kt rename apollo/src/iosTest/kotlin/{io.iohk.atala.prism.apollo/PlatformTest.kt => io/iohk/atala/prism/apollo/PlatformTests.kt} (78%) rename apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/{PlatformTest.kt => PlatformTests.kt} (78%) create mode 100644 apollo/src/jvmTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt create mode 100644 apollo/src/macosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt diff --git a/apollo/karma.config.d/timeout.js b/apollo/karma.config.d/timeout.js index 2c29ef748..a8fe68233 100644 --- a/apollo/karma.config.d/timeout.js +++ b/apollo/karma.config.d/timeout.js @@ -1,9 +1,9 @@ config.set({ - browserDisconnectTimeout: 5000, - processKillTimeout: 5000, + browserDisconnectTimeout: 10000, + processKillTimeout: 10000, client: { mocha: { - timeout: 5000 + timeout: 10000 } } }); diff --git a/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 6b58540c1..55bcd5360 100644 --- a/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/apollo/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -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) diff --git a/apollo/src/androidUnitTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt b/apollo/src/androidUnitTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt new file mode 100644 index 000000000..2226eae29 --- /dev/null +++ b/apollo/src/androidUnitTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt @@ -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")) + } +} diff --git a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 3f3e832fa..f9a09533f 100644 --- a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -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") diff --git a/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 80a00c0f1..e8817ff14 100644 --- a/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/apollo/src/commonMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -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 +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/base64/Base64Tests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/base64/Base64Tests.kt index 26b262ee3..f5cdaefde 100644 --- a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/base64/Base64Tests.kt +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/base64/Base64Tests.kt @@ -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 @@ -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 + } + } } diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/BigIntegerWrapperTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/BigIntegerWrapperTests.kt new file mode 100644 index 000000000..435a7d4d8 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/BigIntegerWrapperTests.kt @@ -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()) + } +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationAxisTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationAxisTests.kt new file mode 100644 index 000000000..4be8220e4 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationAxisTests.kt @@ -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) + } +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationPathTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationPathTests.kt new file mode 100644 index 000000000..40f34bcb1 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/DerivationPathTests.kt @@ -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) + } + } +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/HDKeyOptionsTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/HDKeyOptionsTests.kt new file mode 100644 index 000000000..33d9f7058 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/HDKeyOptionsTests.kt @@ -0,0 +1,101 @@ +package io.iohk.atala.prism.apollo.derivation + +import com.ionspin.kotlin.bignum.integer.BigInteger +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class HDKeyOptionsTests { + @Test + fun propertiesShouldBeCorrectlyInitialized() { + val versions = Pair(1, 2) + val chainCode = ByteArray(32) // Arbitrary value + val depth = 5 + val parentFingerprint = 123456789 + val index = BigInteger.ONE + val privateKey = ByteArray(32) + val publicKey = ByteArray(65) + + val hdKeyOptions = HDKeyOptions(versions, chainCode, depth, parentFingerprint, index, privateKey, publicKey) + + assertEquals(versions, hdKeyOptions.versions) + assertEquals(chainCode.toList(), hdKeyOptions.chainCode.toList()) + assertEquals(depth, hdKeyOptions.depth) + assertEquals(parentFingerprint, hdKeyOptions.parentFingerprint) + assertEquals(index, hdKeyOptions.index) + assertEquals(privateKey.toList(), hdKeyOptions.privateKey!!.toList()) + assertEquals(publicKey.toList(), hdKeyOptions.publicKey!!.toList()) + } + + @Test + fun equalsShouldReturnTrueWhenComparedWithSameObjectAndFalseOtherwise() { + val hdKeyOptions1 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { it.toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + val hdKeyOptions2 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { it.toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + val hdKeyOptions3 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { (it + 1).toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + assertEquals(hdKeyOptions1, hdKeyOptions2) + assertNotEquals(hdKeyOptions1, hdKeyOptions3) + } + + @Test + fun hashCodeShouldReturnSameValueForSameObjectAndDifferentValueForDifferentObject() { + val hdKeyOptions1 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { it.toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + val hdKeyOptions2 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { it.toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + val hdKeyOptions3 = HDKeyOptions( + Pair(1, 2), + ByteArray(32) { (it + 1).toByte() }, + 5, + 123456789, + BigInteger.ONE, + ByteArray(32) { it.toByte() }, + ByteArray(65) { it.toByte() } + ) + + assertEquals(hdKeyOptions1.hashCode(), hdKeyOptions2.hashCode()) + assertNotEquals(hdKeyOptions1.hashCode(), hdKeyOptions3.hashCode()) + } +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/ByteArrayExtTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/ByteArrayExtTests.kt new file mode 100644 index 000000000..ff9b6d8e2 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/ByteArrayExtTests.kt @@ -0,0 +1,69 @@ +package io.iohk.atala.prism.apollo.utils + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ByteArrayExtTests { + @Test + fun toHexStringShouldReturnCorrectStringForSingleByteArray() { + val byteArray: ByteArray = byteArrayOf(0x1A) + + val expected = "1a" + val actual = byteArray.toHexString() + + assertEquals(expected, actual) + } + + @Test + fun toHexStringShouldReturnCorrectStringForMultipleByteArray() { + val byteArray: ByteArray = byteArrayOf(0x1A, 0x2F, 0xB1.toByte()) + + val expected = "1a2fb1" + val actual = byteArray.toHexString() + + assertEquals(expected, actual) + } + + @Test + fun toHexStringShouldReturnCorrectStringForEmptyByteArray() { + val byteArray: ByteArray = byteArrayOf() + + val expected = "" + val actual = byteArray.toHexString() + + assertEquals(expected, actual) + } + + @Test + fun testPadStartShouldReturnPaddedArrayWhenOriginalArrayLengthIsLessThanLength() { + val byteArray: ByteArray = byteArrayOf(0x1A, 0x2F) + + val expected = ByteArray(5) { 0 }.apply { + this[3] = 0x1A.toByte() + this[4] = 0x2F.toByte() + } + val actual = byteArray.padStart(5, 0) + + assertEquals(expected.toList(), actual.toList()) + } + + @Test + fun testPadStartShouldReturnOriginalArrayWhenOriginalArrayLengthIsEqualToLength() { + val byteArray: ByteArray = byteArrayOf(0x1A, 0x2F) + + val expected = byteArray + val actual = byteArray.padStart(2, 0) + + assertEquals(expected.toList(), actual.toList()) + } + + @Test + fun testPadStartShouldReturnOriginalArrayWhenOriginalArrayLengthIsGreaterThanLength() { + val byteArray: ByteArray = byteArrayOf(0x1A, 0x2F) + + val expected = byteArray + val actual = byteArray.padStart(1, 0) + + assertEquals(expected.toList(), actual.toList()) + } +} diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPairTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPairTests.kt index aefb50b48..d260fd61f 100644 --- a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPairTests.kt +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPairTests.kt @@ -12,4 +12,10 @@ class KMMX25519KeyPairTests { assertNotNull(keyPair.privateKey) assertNotNull(keyPair.publicKey) } + + @Test + fun testGetPublicKeyFromPrivateKey() { + val privateKey = KMMX25519KeyPair.generateKeyPair().privateKey + assertNotNull(privateKey.publicKey()) + } } diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/StringExtTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/StringExtTests.kt new file mode 100644 index 000000000..355a91c32 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/utils/StringExtTests.kt @@ -0,0 +1,39 @@ +package io.iohk.atala.prism.apollo.utils + +import kotlin.test.Test +import kotlin.test.assertContentEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue + +class StringExtTests { + + @Test + fun testDecodeHexShouldCorrectlyDecodeHexadecimalString() { + val hexString = "1a2fb1" + val expected = byteArrayOf(0x1A.toByte(), 0x2F.toByte(), 0xB1.toByte()) + + val actual = hexString.decodeHex() + + assertContentEquals(expected, actual) + } + + @Test + fun testDecodeHexShouldCorrectlyDecodeEmptyString() { + val hexString = "" + + val actual = hexString.decodeHex() + + assertContentEquals(byteArrayOf(), actual) + } + + @Test + fun testDecodeHexShouldThrowIllegalArgumentExceptionForOddLengthString() { + val hexString = "abc" // odd length string + + val exception = assertFailsWith { + hexString.decodeHex() + } + + assertTrue(exception.message!!.contains("even length")) + } +} diff --git a/apollo/src/iosTest/kotlin/io.iohk.atala.prism.apollo/PlatformTest.kt b/apollo/src/iosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt similarity index 78% rename from apollo/src/iosTest/kotlin/io.iohk.atala.prism.apollo/PlatformTest.kt rename to apollo/src/iosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt index 1f642857a..0e299f442 100644 --- a/apollo/src/iosTest/kotlin/io.iohk.atala.prism.apollo/PlatformTest.kt +++ b/apollo/src/iosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt @@ -3,10 +3,10 @@ package io.iohk.atala.prism.apollo import kotlin.test.Test import kotlin.test.assertTrue -class PlatformTest { +class PlatformTests { @Test - fun testPlatformName() { + fun testOS() { assertTrue(Platform.OS.contains("iOS"), "Check iOS is mentioned") } } diff --git a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 38385b049..142511a46 100644 --- a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -31,7 +31,7 @@ actual class KMMX25519PrivateKey(bytes: ByteArray) { * PublicKey associated with this PrivateKey * @return KMMX25519PublicKey */ - fun publicKey(): KMMX25519PublicKey { + actual fun publicKey(): KMMX25519PublicKey { val publicBytes = getInstance().publicKey.buffer.toByteArray() return KMMX25519PublicKey(publicBytes) diff --git a/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTest.kt b/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt similarity index 78% rename from apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTest.kt rename to apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt index 1f9cf86d1..277419f8f 100644 --- a/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTest.kt +++ b/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt @@ -3,10 +3,10 @@ package io.iohk.atala.prism.apollo import kotlin.test.Test import kotlin.test.assertTrue -class PlatformTest { +class PlatformTests { @Test - fun testPlatformName() { + fun testOS() { assertTrue(Platform.OS.contains("JS"), "Check JS is mentioned") } } diff --git a/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 31e152816..6d493d12c 100644 --- a/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/apollo/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -13,7 +13,7 @@ actual class KMMX25519PrivateKey(val raw: ByteArray) { * * @return A `KMMX25519PublicKey` object representing the generated public key. */ - fun publicKey(): KMMX25519PublicKey { + actual fun publicKey(): KMMX25519PublicKey { val private = X25519PrivateKeyParameters(raw, 0) val public = private.generatePublicKey() return KMMX25519PublicKey(public.encoded) diff --git a/apollo/src/jvmTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt b/apollo/src/jvmTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt new file mode 100644 index 000000000..dcd4e8499 --- /dev/null +++ b/apollo/src/jvmTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt @@ -0,0 +1,11 @@ +package io.iohk.atala.prism.apollo + +import kotlin.test.Test +import kotlin.test.assertTrue + +class PlatformTests { + @Test + fun testOS() { + assertTrue(Platform.OS.contains("JVM")) + } +} diff --git a/apollo/src/macosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt b/apollo/src/macosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt new file mode 100644 index 000000000..d051896f9 --- /dev/null +++ b/apollo/src/macosTest/kotlin/io/iohk/atala/prism/apollo/PlatformTests.kt @@ -0,0 +1,11 @@ +package io.iohk.atala.prism.apollo + +import kotlin.test.Test +import kotlin.test.assertTrue + +class PlatformTests { + @Test + fun testOS() { + assertTrue(Platform.OS.startsWith("macOS-")) + } +} diff --git a/build.gradle.kts b/build.gradle.kts index 9f61a5e28..9914f24d5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -161,7 +161,17 @@ subprojects { filters { excludes { classes( - "io.iohk.atala.prism.apollo.utils.bip39.wordlists.*" + "io.iohk.atala.prism.apollo.utils.bip39.wordlists.*", + "io.iohk.atala.prism.apollo.derivation.MnemonicChecksumException", + "io.iohk.atala.prism.apollo.derivation.MnemonicLengthException", + "io.iohk.atala.prism.apollo.derivation.MnemonicWordException", + "io.iohk.atala.prism.apollo.derivation.MnemonicException", + "io.iohk.atala.prism.apollo.secp256k1.Secp256k1Exception", + "io.iohk.atala.prism.apollo.utils.ECPrivateKeyDecodingException", + "io.iohk.atala.prism.apollo.utils.ECPrivateKeyException", + "io.iohk.atala.prism.apollo.utils.ECPrivateKeyInitializationException", + "io.iohk.atala.prism.apollo.utils.ECPublicKeyException", + "io.iohk.atala.prism.apollo.utils.ECPublicKeyInitializationException" ) } }