From 46e883226896c3dd99b06d2575093ae4ed2f3e45 Mon Sep 17 00:00:00 2001 From: Rodney Lorrimar Date: Tue, 12 Mar 2019 15:57:48 +1000 Subject: [PATCH 1/2] tests: Add a couple negative tests for block decoding @KtorZ -> Fix line-length to be below 80 --- test/unit/Cardano/Wallet/BinarySpec.hs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/test/unit/Cardano/Wallet/BinarySpec.hs b/test/unit/Cardano/Wallet/BinarySpec.hs index dbbd42e1a06..e36272c78df 100644 --- a/test/unit/Cardano/Wallet/BinarySpec.hs +++ b/test/unit/Cardano/Wallet/BinarySpec.hs @@ -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 @@ -62,6 +64,18 @@ spec = do let decoded = unsafeDeserialiseFromBytes decodeBlock bs decoded `shouldBe` block3 + 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 @@ -77,12 +91,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 From 97f3fadc532fc37bc54be440f3de2a71b21453b5 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Tue, 12 Mar 2019 12:25:13 +0100 Subject: [PATCH 2/2] add test case to unserialize block with more than one transaction --- test/data/Cardano/Wallet/BinarySpec-block-4 | Bin 0 -> 1389 bytes test/unit/Cardano/Wallet/BinarySpec.hs | 73 +++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/data/Cardano/Wallet/BinarySpec-block-4 diff --git a/test/data/Cardano/Wallet/BinarySpec-block-4 b/test/data/Cardano/Wallet/BinarySpec-block-4 new file mode 100644 index 0000000000000000000000000000000000000000..bae1019d70cacf0df213a0be8b572677fab27db0 GIT binary patch literal 1389 zcmZo-Y;Kj(o#w?Eq3}h+!sT_vsr+Q)4eVc+n_d^0dF@=y39*(eW-GG_?@KdVnwcUL zw6A7WdQVgA8AO+NF9(Y-0J_la6}>*5*VgtUquoMZ)@1n!l~z zn<9&=D!UTu*S*^0e~{sIo9wR4>CzDjerXy!H+>@oicDs|;WC)8)M}+`nBMPPj;ndW zsos1JGg_MXgd!Yf zQ>w<9SK|>n!TZ01MF9H_Il0tYz6gicN2YD{VLv1KxnJ?$){8z<_N6hiT6DE)CnmjZ zpQyKLZhCW#;E#NUEfRk`*K>Th_B^}GU{!PANxNPp2QIg)<18)B3=9lSS;>h-DT#Uc zy2Uw63nCP}S86R*IL38m*7cps{Pk{y9=Y^J;~C4Zk8K6r{yctt_gm&SHP3HixFHdt z(iEZaQ)rTbtoO!k4;;>&{qJBfb++fpxY~6btgsnzo`kR)~Pu{CRN3n zZ&UihM|Otg^L<5oQ`TNOb-*d_*9K<|A=ZVA5poeSr|d#)<|H4LdN|k6`QqR0@}*Ua z4_-WS(`vTjazzHI&JVliOEWMq*Tu{^!v!~W;paH6zyyKmlk;X~FCo6e6Lbo zYp!a&IKIT__J+^D zT2pqc<&%opk26*im8aj`c=D)p+wWlHKt3u_AFv>Nf`x4OUh`azmytI-H_ZPvL2`D@ zZLa4ifowL_Hp2O)599x3PrB~THDFVVO{fxLkeWJo{~}Nz*I4H>Uxo*=C;w6&f&14k zp4(m7;3oA$@ANMHFa5cOv0N>$lpv;-@qcLbX*wjdzjm*+Y44ws*p}MpFVbrKEYa2P z7^LRENsR)U%D{AB$N4SD0nIECD#2;G^ws`vj!~7?_g!F-|D@je;S-NJ4iOFux~82nI5Kz9^iM%wm)oDX zuqTt(ddutod&TUvHhi>Md-RFPiKXWR#KR(53^KWQPyexUQ}NO@y3%Ko4HYf-z4c`O T-^_56k*NWM=l^d4;>HC41l@b- literal 0 HcmV?d00001 diff --git a/test/unit/Cardano/Wallet/BinarySpec.hs b/test/unit/Cardano/Wallet/BinarySpec.hs index e36272c78df..f4f7b6174ce 100644 --- a/test/unit/Cardano/Wallet/BinarySpec.hs +++ b/test/unit/Cardano/Wallet/BinarySpec.hs @@ -64,6 +64,11 @@ 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 @@ -155,7 +160,6 @@ block2 = Block "DdzFFzCqrhsmxmuQpgjUrvRwF5ZKnyQ7pGrS4q53u5B516wcc26m\ \aHz9M4myYAkQVc5m9E4DKJjRDjPxuDdK3ZsHb1Dnqf3XorZ1PnzX" - -- A testnet block with a transaction block3 :: Block block3 = Block @@ -189,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.