summaryrefslogtreecommitdiff
path: root/tests/System/Torrent/StorageSpec.hs
blob: 8267b7a56c537776333828a9b1f6901ad60d8dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module System.Torrent.StorageSpec (spec) where
import Control.Exception
import Data.ByteString.Lazy as BL
import System.FilePath
import System.Directory
import System.IO.Unsafe
import Test.Hspec

import Data.Torrent.Layout
import Data.Torrent.Piece
import System.Torrent.Storage


layout :: FileLayout FileSize
layout =
  [ (dir </> "_a", 20)
  , (dir </> "_b", 50)
  , (dir </> "_c", 100)
  , (dir </> "_d", 5)
  ]
  where
    dir = unsafePerformIO $ getTemporaryDirectory

createLayout :: IO ()
createLayout =
  bracket (open ReadWriteEx 0 layout) close (const (return ()))

spec :: Spec
spec = before createLayout $ do
  describe "writePiece" $ do
    it "should fail gracefully on write operation in RO mode" $ do
      s <- open ReadOnly 0 layout
      writePiece (Piece 0 "") s `shouldThrow` (== StorageIsRO)
      close s

    it "should fail if piece size do not match" $ do
      withStorage ReadWrite 1 layout $ \ s ->
        writePiece (Piece 0 "") s `shouldThrow` (== InvalidSize 0)

    it "should fail on negative index" $ do
      withStorage ReadWrite 0 layout $ \ s ->
        writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1))

    it "should fail on out of upper bound index" $ do
      withStorage ReadWrite 100 layout $ \ s -> do
        let bs = BL.replicate 100 0
        writePiece (Piece 1 bs) s
        writePiece (Piece 2 bs) s `shouldThrow` (== InvalidIndex 2)

  describe "readPiece" $ do
    it "should fail on negative index" $
      withStorage ReadOnly 0 layout $ \ s ->
        readPiece (-1) s `shouldThrow` (== InvalidIndex (-1))

    it "should fail on out of upper bound index" $ do
      withStorage ReadOnly 100 layout $ \ s -> do
        _ <- readPiece 1 s
        readPiece 2 s `shouldThrow` (== InvalidIndex 2)