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

Update tests for Crypto contract to the latest testing framework API #2856

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ jobs:
go-version: ${{ env.GO_VERSION }}
cache: true

- name: Install Flow CLI
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)" -- v1.5.0

- name: Build
run: make -j8 build

- name: Test
run: make ci

- name: Cadence Testing Framework
run: cd runtime/stdlib/contracts && flow test --cover --covercode="contracts" crypto_test.cdc

- name: Upload coverage report
uses: codecov/codecov-action@v2
with:
Expand Down
318 changes: 278 additions & 40 deletions runtime/stdlib/contracts/crypto_test.cdc
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import Test

access(all) let blockchain = Test.newEmulatorBlockchain()
access(all) let account = blockchain.createAccount()
import Crypto from "crypto.cdc"

access(all)
fun setup() {
blockchain.useConfiguration(Test.Configuration({
"Crypto": account.address
}))

let crypto = Test.readFile("crypto.cdc")
let err = blockchain.deployContract(
let err = Test.deployContract(
name: "Crypto",
code: crypto,
account: account,
path: "crypto.cdc",
arguments: []
)

Expand All @@ -22,76 +14,322 @@ fun setup() {

access(all)
fun testCryptoHash() {
let returnedValue = executeScript("./scripts/crypto_hash.cdc")
Test.assertEqual(true, returnedValue)
let hash = Crypto.hash([1, 2, 3], algorithm: HashAlgorithm.SHA3_256)
Test.assertEqual(32, hash.length)
}

access(all)
fun testCryptoHashWithTag() {
let returnedValue = executeScript("./scripts/crypto_hash_with_tag.cdc")
Test.assertEqual(true, returnedValue)
let hash = Crypto.hashWithTag(
[1, 2, 3],
tag: "v0.1.tag",
algorithm: HashAlgorithm.SHA3_256
)
Test.assertEqual(32, hash.length)
}

access(all)
fun testAddKeyToKeyList() {
let returnedValue = executeScript("./scripts/crypto_key_list_add.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 1.0
)

Test.assert(keyList.get(keyIndex: 0) != nil)
}

access(all)
fun testGetKeyFromList() {
let returnedValue = executeScript("./scripts/crypto_get_key_from_list.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 1.0
)

Test.assert(keyList.get(keyIndex: 0) != nil)
Test.assert(keyList.get(keyIndex: 2) == nil)
}

access(all)
fun testRevokeKeyFromList() {
let returnedValue = executeScript("./scripts/crypto_revoke_key_from_list.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

keyList.revoke(keyIndex: 0)
keyList.revoke(keyIndex: 2)

Test.assert(keyList.get(keyIndex: 0)!.isRevoked)
Test.assert(keyList.get(keyIndex: 2) == nil)
}

access(all)
fun testKeyListVerify() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKeyA = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyA,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let publicKeyB = PublicKey(
publicKey:
"df9609ee588dd4a6f7789df8d56f03f545d4516f0c99b200d73b9a3afafc14de5d21a4fc7a2a2015719dc95c9e756cfa44f2a445151aaf42479e7120d83df956".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyB,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"bbdc5591c3f937a730d4f6c0a6fde61a0a6ceaa531ccb367c3559335ab9734f4f2b9da8adbe371f1f7da913b5a3fdd96a871e04f078928ca89a83d841c72fadf".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

Test.assert(isValid)
}

access(all)
fun testKeyListVerifyInsufficientWeights() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify_insufficient_weights.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKeyA = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyA,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.4
)

let publicKeyB = PublicKey(
publicKey:
"df9609ee588dd4a6f7789df8d56f03f545d4516f0c99b200d73b9a3afafc14de5d21a4fc7a2a2015719dc95c9e756cfa44f2a445151aaf42479e7120d83df956".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyB,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"bbdc5591c3f937a730d4f6c0a6fde61a0a6ceaa531ccb367c3559335ab9734f4f2b9da8adbe371f1f7da913b5a3fdd96a871e04f078928ca89a83d841c72fadf".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyWithRevokedKey() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify_revoked.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

keyList.revoke(keyIndex: 0)

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyWithMissingSignature() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify_missing_signature.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyDuplicateSignature() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify_duplicate_signature.cdc")
Test.assertEqual(true, returnedValue)
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyInvalidSignature() {
let returnedValue = executeScript("./scripts/crypto_key_list_verify_invalid_signature.cdc")
Test.assertEqual(true, returnedValue)
}
let keyList = Crypto.KeyList()

access(self)
fun executeScript(_ scriptPath: String): Bool {
let script = Test.readFile(scriptPath)
let scriptResult = blockchain.executeScript(script, [])
let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"db70a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

Test.expect(scriptResult, Test.beSucceeded())
// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData
)

return scriptResult.returnValue! as! Bool
Test.assert(!isValid)
}
Loading
Loading