-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add tests for pack file decoding
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |