-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from onflow/signature-algorithm-test
Unit tests: Signer, SignatureAlgorithm
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/test/kotlin/com/nftco/flow/sdk/models/SignatureAlgorithmTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.nftco.flow.sdk.models | ||
|
||
import com.nftco.flow.sdk.SignatureAlgorithm | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class SignatureAlgorithmTest { | ||
@Test | ||
fun `Test fromCode`() { | ||
assertEquals(SignatureAlgorithm.ECDSA_P256, SignatureAlgorithm.fromCode(2)) | ||
assertEquals(SignatureAlgorithm.ECDSA_SECP256k1, SignatureAlgorithm.fromCode(3)) | ||
assertEquals(SignatureAlgorithm.UNKNOWN, SignatureAlgorithm.fromCode(-1)) | ||
} | ||
|
||
@Test | ||
fun `Test fromCadenceIndex`() { | ||
assertEquals(SignatureAlgorithm.ECDSA_P256, SignatureAlgorithm.fromCadenceIndex(1)) | ||
assertEquals(SignatureAlgorithm.ECDSA_SECP256k1, SignatureAlgorithm.fromCadenceIndex(2)) | ||
assertEquals(SignatureAlgorithm.UNKNOWN, SignatureAlgorithm.fromCadenceIndex(-1)) | ||
} | ||
|
||
@Test | ||
fun `Test fromCode with invalid code`() { | ||
assertEquals(SignatureAlgorithm.UNKNOWN, SignatureAlgorithm.fromCode(0)) | ||
} | ||
|
||
@Test | ||
fun `Test fromCadenceIndex with invalid index`() { | ||
assertEquals(SignatureAlgorithm.UNKNOWN, SignatureAlgorithm.fromCadenceIndex(4)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.nftco.flow.sdk.models | ||
|
||
import com.nftco.flow.sdk.DomainTag | ||
import org.junit.jupiter.api.Assertions.assertArrayEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import com.nftco.flow.sdk.Signer | ||
import com.nftco.flow.sdk.Hasher | ||
|
||
class SignerTest { | ||
lateinit var hasher: Hasher | ||
|
||
lateinit var signer: Signer | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
signer = object : Signer { | ||
override val hasher: Hasher | ||
get() = this@SignerTest.hasher | ||
|
||
override fun sign(bytes: ByteArray): ByteArray { | ||
// Mock sign function to return bytes * 2 | ||
return bytes + bytes | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `Test signWithDomain`() { | ||
val inputBytes = byteArrayOf(0x01, 0x02, 0x03) | ||
val domainBytes = byteArrayOf(0x04, 0x05) | ||
val expectedBytes = signer.sign(domainBytes + inputBytes) | ||
|
||
val actualBytes = signer.signWithDomain(inputBytes, domainBytes) | ||
assertArrayEquals(expectedBytes, actualBytes) | ||
} | ||
|
||
@Test | ||
fun `Test signAsUser`() { | ||
val inputBytes = byteArrayOf(0x01, 0x02, 0x03) | ||
val expectedBytes = signer.sign(DomainTag.USER_DOMAIN_TAG + inputBytes) | ||
|
||
val actualBytes = signer.signAsUser(inputBytes) | ||
assertArrayEquals(expectedBytes, actualBytes) | ||
} | ||
|
||
@Test | ||
fun `Test signAsTransaction`() { | ||
val inputBytes = byteArrayOf(0x01, 0x02, 0x03) | ||
val expectedBytes = signer.sign(DomainTag.TRANSACTION_DOMAIN_TAG + inputBytes) | ||
|
||
val actualBytes = signer.signAsTransaction(inputBytes) | ||
assertArrayEquals(expectedBytes, actualBytes) | ||
} | ||
} |