summaryrefslogtreecommitdiff
path: root/tests/System/Torrent/StorageSpec.hs
blob: 8d9dfd8fefa67fe90ffb1d1d4980d1a5d714323e (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
module System.Torrent.StorageSpec (spec) where
import Control.Exception
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 on negative index" $ do
      withStorage ReadWrite 0 layout $ \ s ->
        writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1))

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