Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Akentev committed Nov 15, 2023
1 parent abfd9ba commit c00a58f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ source-repository-package
--sha256: 19pjy06xrx2siggzybcmly0qaq4ds3yzxcsvqwgs4qh9kkzh0kqh

source-repository-package
type: git
location: https://github.com/kadena-io/kadena-ethereum-bridge.git
tag: 3f1ad9c9aaf1ed16cba5b9efe05a5b3a033ee76c
--sha256: sha256-lOlHiigTWarnGW78xd8hgCD925glMnqyfUrvR7SuhCc=
type: git
location: https://github.com/kadena-io/kadena-ethereum-bridge.git
tag: ffbf20e9f0430b95448bd66c6b1b530864397fb3
--sha256: sha256-xdawv/tdjh61MbJKcBqm9Fje36+gVljuZsAxOTX1gP0=

source-repository-package
type: git
Expand Down
108 changes: 105 additions & 3 deletions test/Chainweb/Test/Pact/SPV/Hyperlane.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ module Chainweb.Test.Pact.SPV.Hyperlane
import Control.Monad.Trans.Except
import Data.Text (Text)
import Data.Default (def)
import Data.DoubleWord (Word256)

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

import Test.QuickCheck

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.1, 3.10, macOS-latest, true)

The import of ‘Test.QuickCheck’ is redundant

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-20.04, false)

The import of ‘Test.QuickCheck’ is redundant

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-22.04, false)

The import of ‘Test.QuickCheck’ is redundant

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.8.1, 3.10, ubuntu-20.04, false)

The import of ‘Test.QuickCheck’ is redundant

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.8.1, 3.10, ubuntu-22.04, false)

The import of ‘Test.QuickCheck’ is redundant

Check failure on line 27 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-20.04, true)

The import of ‘Test.QuickCheck’ is redundant
import Test.QuickCheck.Instances ()

import Chainweb.Test.Utils (prop_iso)

-- internal pact modules

Expand All @@ -35,6 +42,9 @@ import Chainweb.Pact.SPV.Hyperlane
import Chainweb.Pact.SPV.Hyperlane.Binary
import Chainweb.Utils.Serialization (runGetS)

instance Arbitrary Word256 where

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.1, 3.10, macOS-latest, true)

Orphan instance: instance Arbitrary Word256

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-20.04, false)

Orphan instance: instance Arbitrary Word256

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-22.04, false)

Orphan instance: instance Arbitrary Word256

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.8.1, 3.10, ubuntu-20.04, false)

Orphan class instance: instance Arbitrary Word256

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.8.1, 3.10, ubuntu-22.04, false)

Orphan class instance: instance Arbitrary Word256

Check failure on line 45 in test/Chainweb/Test/Pact/SPV/Hyperlane.hs

View workflow job for this annotation

GitHub Actions / Build (9.6.3, 3.10, ubuntu-20.04, true)

Orphan instance: instance Arbitrary Word256
arbitrary = fromInteger . getNonNegative <$> arbitrary

tests :: TestTree
tests = testGroup "hyperlane"
[ testCase "empty object" hyperlaneEmptyObject
Expand All @@ -43,12 +53,22 @@ tests = testGroup "hyperlane"
, testCase "decodeTokenMessageERC20" hyperlaneDecodeTokenMessageERC20

, testCase "decimalToWord" hyperlaneDecimalToWord
, testCase "decimalToWord2" hyperlaneDecimalToWord2
, testCase "wordToDecimal" hyperlaneWordToDecimal
, testCase "wordToDecimal2" hyperlaneWordToDecimal2
, testProperty "wordDecimalRoundTrip" $ prop_iso @Word256 @_ decimalToWord wordToDecimal

, testCase "encodeHyperlaneMessage" hyperlaneEncodeHyperlaneMessage
, testCase "recoverValidatorAnnouncement" hyperlaneRecoverValidatorAnnouncement

, testCase "recoverValidatorAnnouncementSuccess" hyperlaneRecoverValidatorAnnouncementSuccess
, testCase "recoverValidatorAnnouncementFailure" hyperlaneRecoverValidatorAnnouncementFailure

, testCase "verifySuccess" hyperlaneVerifySuccess
, testCase "verifySuccessMoreValidators" hyperlaneVerifySuccessMoreValidators

, testCase "verifyFailure" hyperlaneVerifyFailure
, testCase "verifyFailureIncorrectValidator" hyperlaneVerifyFailureIncorrectValidator
, testCase "verifyFailureNotEnoughSignatures" hyperlaneVerifyFailureNotEnoughSignatures
]

hyperlaneEmptyObject :: Assertion
Expand Down Expand Up @@ -84,9 +104,15 @@ hyperlaneDecodeTokenMessageERC20 = do
hyperlaneDecimalToWord :: Assertion
hyperlaneDecimalToWord = assertEqual "" 10000000000000000000000000000000000000 (decimalToWord 10000000000000000000)

hyperlaneDecimalToWord2 :: Assertion
hyperlaneDecimalToWord2 = assertEqual "" 10333333333333333000 (decimalToWord 10.333333333333333)

hyperlaneWordToDecimal :: Assertion
hyperlaneWordToDecimal = assertEqual "" 3333333333333333333.333333333333333333 (wordToDecimal 3333333333333333333333333333333333333)

hyperlaneWordToDecimal2 :: Assertion
hyperlaneWordToDecimal2 = assertEqual "" 10.333333333333333 (wordToDecimal 10333333333333333000)

hyperlaneEncodeHyperlaneMessage :: Assertion
hyperlaneEncodeHyperlaneMessage = do
let
Expand Down Expand Up @@ -116,8 +142,8 @@ hyperlaneEncodeHyperlaneMessage = do
]
in assertEqual "Should get encoded message" expectedObject o

hyperlaneRecoverValidatorAnnouncement :: Assertion
hyperlaneRecoverValidatorAnnouncement = do
hyperlaneRecoverValidatorAnnouncementSuccess :: Assertion
hyperlaneRecoverValidatorAnnouncementSuccess = do
let
obj' = mkObject
[ ("storageLocation", tStr $ asString ("storagelocation" :: Text))
Expand All @@ -133,6 +159,18 @@ hyperlaneRecoverValidatorAnnouncement = do
]
in assertEqual "Should get encoded message" expectedObject o

hyperlaneRecoverValidatorAnnouncementFailure :: Assertion
hyperlaneRecoverValidatorAnnouncementFailure = do
let
obj' = mkObject
[ ("storageLocation", tStr $ asString ("storagelocation" :: Text))
, ("signature", tStr $ asString ("0x43ba1fb621a19fbae9589c9d3fab7414a4ad75c45ddb6ddaf2e493a8a8ecf0af27256ed4f38b7304e80f653b462a79dcc22bbc975d7ce6f077f1cefe3afedabc1c" :: Text))
]
res <- runExceptT $ evalHyperlaneCommand obj'
case res of
Left err -> assertEqual "Address recover failure" "Failed to recover address" err
Right _ -> assertFailure "Should fail"

hyperlaneVerifySuccess :: Assertion
hyperlaneVerifySuccess = do
let
Expand Down Expand Up @@ -164,11 +202,44 @@ hyperlaneVerifySuccess = do
]
in assertEqual "Should get encoded message" expectedObject o

hyperlaneVerifySuccessMoreValidators :: Assertion
hyperlaneVerifySuccessMoreValidators = do
let
obj' = mkObject
[ ("message", tStr $ asString ("0x01000001450000027200000000000000000000000000000000006b622d746f6b656e2d726f757465720000000100000000000000000000000071c7656ec7ab88b098defb751b7401b5f6d8976f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000002a30783731433736353645433761623838623039386465664237353142373430314235663664383937364600000000000000000000000000000000000000000000" :: Text))
, ("validators", toTList tTyString def $
map (tStr . asString) ["0x5DD34992E0994E9d3c53c1CCfe5C2e38d907338e", "0x4BD34992E0994E9d3c53c1CCfe5C2e38d907338e" :: Text])
, ("metadata", tStr $ asString ("0x0000000000000000000000002e234dae75c793f67a35089c9d99245e1c58470b00000000000000000000000000000000000000000000000000000000000000ad0000000f0e1c8be19e9e2bd14665599b8e8ed1f3dbca562788e5844975770eb31380b3ae5de03487e89a1d3c42fad8aac486a06e1af6b3478ec0d148c0c8566c404537291b" :: Text))
, ("threshold", tLit $ LInteger 1)
]
res <- runExceptT $ evalHyperlaneCommand obj'
case res of
Left err -> assertFailure $ "Should get the result" ++ show err
Right o ->
let
expectedObject = mkObject
[ ("message", obj
[ ("version", tLit $ LInteger 1)
, ("nonce", tLit $ LInteger 325)
, ("originDomain", tLit $ LInteger 626)
, ("sender", tStr $ asString ("0x00000000006b622d746f6b656e2d726f75746572" :: Text))
, ("destinationDomain", tLit $ LInteger 1)
, ("recipient", tStr $ asString ("0x71c7656ec7ab88b098defb751b7401b5f6d8976f" :: Text))
, ("tokenMessage", obj
[ ("recipient", tStr $ asString ("0x71C7656EC7ab88b098defB751B7401B5f6d8976F" :: Text))
, ("amount", tLit $ LDecimal 10) ]
)
])
, ("messageId", tStr $ asString ("0x3b2cc397eb65735078b967c5497a028ee35a0ebe89478a2417b2e9c7e891e0f3" :: Text))
]
in assertEqual "Should get encoded message" expectedObject o

hyperlaneVerifyFailure :: Assertion
hyperlaneVerifyFailure = do
let
obj' = mkObject
[ ("message", tStr $ asString ("0x01000001450000027200000000000000000000000000000000006b622d746f6b656e2d726f757465720000000100000000000000000000000071c7656ec7ab88b098defb751b7401b5f6d8976f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000002a30783731433736353645433761623838623039386465664237353142373430314235663664383937364600000000000000000000000000000000000000000000" :: Text))
-- different validator
, ("validators", toTList tTyString def $ map (tStr . asString) [("0x5DD34992E0994E9d3c53c1CCfe5C2e38d907338e" :: Text)])
, ("metadata", tStr $ asString ("0x0000000000000000000000002e234dae75c793f67a35089c9d99245e1c58470b00000000000000000000000000000000000000000000000000000000000000ad0000000f0e1c8be19e9e2bd14665599b8e8ed1f3dbca562788e5844975770eb31380b3ae5de03487e89a1d3c42fad8aac486a06e1af6b3478ec0d148c0c8566c404537291b" :: Text))
, ("threshold", tLit $ LInteger 1)
Expand All @@ -178,3 +249,34 @@ hyperlaneVerifyFailure = do
case res of
Left err -> assertEqual "Verification should fail" "Verification failed" err
Right _ -> assertFailure "Should fail"

hyperlaneVerifyFailureIncorrectValidator :: Assertion
hyperlaneVerifyFailureIncorrectValidator = do
let
obj' = mkObject
[ ("message", tStr $ asString ("0x01000001450000027200000000000000000000000000000000006b622d746f6b656e2d726f757465720000000100000000000000000000000071c7656ec7ab88b098defb751b7401b5f6d8976f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000002a30783731433736353645433761623838623039386465664237353142373430314235663664383937364600000000000000000000000000000000000000000000" :: Text))
, ("validators", toTList tTyString def $ map (tStr . asString) [("badValidator" :: Text)])
, ("metadata", tStr $ asString ("0x0000000000000000000000002e234dae75c793f67a35089c9d99245e1c58470b00000000000000000000000000000000000000000000000000000000000000ad0000000f0e1c8be19e9e2bd14665599b8e8ed1f3dbca562788e5844975770eb31380b3ae5de03487e89a1d3c42fad8aac486a06e1af6b3478ec0d148c0c8566c404537291b" :: Text))
, ("threshold", tLit $ LInteger 1)
]
res <- runExceptT $ evalHyperlaneCommand obj'

case res of
Left err -> assertEqual "Verification should fail" "Failed to decode a validator (badValidator):decodeHex: does not start with 0x" err
Right _ -> assertFailure "Should fail"

hyperlaneVerifyFailureNotEnoughSignatures :: Assertion
hyperlaneVerifyFailureNotEnoughSignatures = do
let
obj' = mkObject
[ ("message", tStr $ asString ("0x01000001450000027200000000000000000000000000000000006b622d746f6b656e2d726f757465720000000100000000000000000000000071c7656ec7ab88b098defb751b7401b5f6d8976f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000002a30783731433736353645433761623838623039386465664237353142373430314235663664383937364600000000000000000000000000000000000000000000" :: Text))
, ("validators", toTList tTyString def $ map (tStr . asString) [("0x5DD34992E0994E9d3c53c1CCfe5C2e38d907338e" :: Text)])
-- metadata without signatures
, ("metadata", tStr $ asString ("0x0000000000000000000000002e234dae75c793f67a35089c9d99245e1c58470b00000000000000000000000000000000000000000000000000000000000000ad0000000f" :: Text))
, ("threshold", tLit $ LInteger 1)
]
res <- runExceptT $ evalHyperlaneCommand obj'

case res of
Left err -> assertEqual "Verification should fail" "The number of recovered addresses from the signatures is less than threshold: 1" err
Right _ -> assertFailure "Should fail"

0 comments on commit c00a58f

Please sign in to comment.