Skip to content

Commit

Permalink
tests: Add tests for pack file decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Mar 6, 2019
1 parent 0e845bd commit 9a7d29a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ test-suite unit
other-modules:
Cardano.Wallet.BinaryHelpers
, Cardano.Wallet.BinarySpec
, Cardano.Wallet.PackfileSpec
, Cardano.Wallet.PrimitiveSpec
72 changes: 72 additions & 0 deletions test/unit/Cardano/Wallet/PackfileSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.PackfileSpec (spec) where

import Prelude

import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.ByteString.Char8 as S8

import Test.Hspec
( Spec, describe, it, shouldBe, shouldSatisfy )
import Data.Either

import Cardano.Wallet.Binary.Packfile

-- version 1 header
packFileHeader :: L8.ByteString
packFileHeader = "\254CARDANOPACK\NUL\NUL\NUL\SOH"

test1 :: L8.ByteString
test1 = packFileHeader <> "\0\0\0\5hello\0\0\0"

test2 :: L8.ByteString
test2 = packFileHeader
<> "\0\0\0\11first block\0"
<> "\0\0\0\12second block"

spec :: Spec
spec = do
describe "Decoding pack file" $ do
it "should decode a packfile" $ do
-- Get this file with
-- wget -O test/data/epoch-mainnet-104 http://localhost:8080/mainnet/epoch/104
bs <- L8.readFile "test/data/epoch-mainnet-104"
let decoded = decodePackfile bs
decoded `shouldSatisfy` isRight
length (fromRight [] decoded) `shouldBe` 21600

it "first blocks in pack file should be valid" $ do
bs <- L8.readFile "test/data/epoch-mainnet-104"
let Right (first:second:_) = decodePackfile bs
S8.length first `shouldBe` 648092
S8.length second `shouldBe` 1181

it "should not decode junk" $ do
let decoded = decodePackfile "junkjunkjunkjunk"
decoded `shouldBe` Left MissingMagicError

it "should ensure pack file type" $ do
let decoded = decodePackfile "\254CARDANOYOLO\NUL\NUL\NUL\SOH"
decoded `shouldBe` Left WrongFileTypeError

it "should ensure pack file version" $ do
let decoded = decodePackfile "\254CARDANOPACK\NUL\NUL\NUL\2"
decoded `shouldBe` Left VersionTooNewError

it "should decode an empty pack file" $ do
decodePackfile packFileHeader `shouldBe` Right []

it "should decode a single blob" $ do
decodePackfile test1 `shouldBe` Right ["hello"]

it "should decode multiple blobs" $ do
decodePackfile test2 `shouldBe` Right ["first block", "second block"]

it "should not decode overly large blobs" $ do
let decoded = decodePackfile (packFileHeader <> "\64\0\0\0")
decoded `shouldBe` Left (BlobDecodeError "read block of size: 1073741824")

it "should reject extra data" $ do
let decoded = decodePackfile (packFileHeader <> "a")
decoded `shouldBe` Left (BlobDecodeError "not enough bytes")

0 comments on commit 9a7d29a

Please sign in to comment.