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

tests: Add a couple negative tests for block decoding #48

Merged
merged 2 commits into from
Mar 12, 2019
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
Binary file added test/data/Cardano/Wallet/BinarySpec-block-4
Binary file not shown.
97 changes: 93 additions & 4 deletions test/unit/Cardano/Wallet/BinarySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, decodeBase58 )
import Data.Either
( isLeft )
import Test.Hspec
( Spec, describe, it, shouldBe )
( Spec, describe, it, shouldBe, shouldSatisfy )

import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Read as CBOR
Expand Down Expand Up @@ -62,6 +64,23 @@ spec = do
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block3

it "should decode a block with many transactions" $ do
bs <- L8.readFile "test/data/Cardano/Wallet/BinarySpec-block-4"
let decoded = unsafeDeserialiseFromBytes decodeBlock bs
decoded `shouldBe` block4

it "should fail to decode a junk block" $ do
let junk = mconcat (replicate 100 "junk")
decoded = CBOR.deserialiseFromBytes decodeBlock junk
decoded `shouldSatisfy` isLeft

it "should fail to decode a block with block header data" $ do
bs <- L8.readFile
"test/data/Cardano/Wallet/BinarySpec-block-header-1"
let decoded = CBOR.deserialiseFromBytes decodeBlock bs
decoded `shouldBe`
Left (CBOR.DeserialiseFailure 3 "expected list of length 3")

describe "Encoding Tx" $ do
let txs = Set.toList (transactions block2 <> transactions block3)
let roundTripTx tx = do
Expand All @@ -77,12 +96,16 @@ spec = do

it "should compute correct txId (1)" $ do
let hash = txId (txs !! 0)
let hash' = hash16 "c470563001e448e61ff1268c2a6eb458ace1d04011a02cb262b6d709d66c23d0"
let hash' = hash16
"c470563001e448e61ff1268c2a6eb458\
\ace1d04011a02cb262b6d709d66c23d0"
hash `shouldBe` hash'

it "should compute correct txId (2)" $ do
let hash = txId (txs !! 1)
let hash' = hash16 "d30d37f1f8674c6c33052826fdc5bc198e3e95c150364fd775d4bc663ae6a9e6"
let hash' = hash16
"d30d37f1f8674c6c33052826fdc5bc19\
\8e3e95c150364fd775d4bc663ae6a9e6"
hash `shouldBe` hash'

-- A mainnet block header
Expand Down Expand Up @@ -137,7 +160,6 @@ block2 = Block
"DdzFFzCqrhsmxmuQpgjUrvRwF5ZKnyQ7pGrS4q53u5B516wcc26m\
\aHz9M4myYAkQVc5m9E4DKJjRDjPxuDdK3ZsHb1Dnqf3XorZ1PnzX"


-- A testnet block with a transaction
block3 :: Block
block3 = Block
Expand Down Expand Up @@ -171,6 +193,73 @@ block3 = Block
"37btjrVyb4KD5Ne4yvGAHGbQuHUYQX1VPsXh85rBh3UrGSMWdRSFxBYQ9\
\RQRHCMezN6AMLd3uYTC5hbeVTUiPzfQUTCEogg2HrSJKQUjAgsoYZHwT3"

-- A mainnet block with multiple transactions
block4 :: Block
block4 = Block
{ header = BlockHeader
{ epochIndex = 14
, slotNumber = 18
, prevBlockHash = prevBlockHash0
}
, transactions = Set.fromList
[ Tx
{ inputs =
[ TxIn
{ inputId = inputId0
, inputIx = 0
}
]
, outputs =
[ TxOut
{ address = addr0
, coin = Coin 3841254542346
}
, TxOut
{ address = addr1
, coin = Coin 2700667457
}
]
}
, Tx
{ inputs =
[ TxIn
{ inputId = inputId1
, inputIx = 0
}
]
, outputs =
[ TxOut
{ address = addr2
, coin = Coin 3832107959251
}
, TxOut
{ address = addr3
, coin = Coin 11823271860
}
]
}
]
}
where
prevBlockHash0 = hash16
"f4283844eb78ca6f6333b007f5a735d71499d6ce7cc816846a033a36784bd299"
inputId0 = hash16
"f91292301d4bb1b6e040cecdff4030959b49c95e7dae087782dd558bebb6668a"
addr0 = addr58
"DdzFFzCqrhss1h6EV6KqcSkJNoC2UdtnuP1gGsoxT5Gv8GPfsReX\
\8QhVZWXeZLTidYPi3Fu5ZXG4gvfq3zbwD4nboD1HxoCPCFJLWpMc"
addr1 = addr58
"DdzFFzCqrhszy7cUVphbFGaPBG7yn5nNPzjGDYaPsPuEG84KRjtK\
\RhhNhwjNENx8LT9XGbYfAJzptwotbp7ySho5LGeCc2ALq3cQX2JM"
inputId1 = hash16
"96e170491afb6ebd579fd57c76c684f22436f8cc3a912397ddb1c9c51b86fb53"
addr2 = addr58
"DdzFFzCqrht5ZoBwGocznhhpr4yXRWy1RaMqguNBPjVGhwZcNMnh\
\sDUtEETzjQVVc1TBuL3en6yA8JcKVXx1cuoea5rozjcaFid3pkV7"
addr3 = addr58
"DdzFFzCqrhsoLchqT8AwxFH9srQzvH78dUAD1BwHneqGHvA7BV89\
\Mj87XFPDSU2tJCMiWpi7vf1U5CqE835Xz2kpnyzFTuYQLkZev4qw"

-- * Helpers

-- | Make a Hash from a Base16 encoded string, without error handling.
Expand Down