summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Data/Torrent/Layout.hs1
-rw-r--r--src/System/Torrent/Storage.hs6
-rw-r--r--tests/System/Torrent/StorageSpec.hs16
3 files changed, 21 insertions, 2 deletions
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs
index 453c0d4f..a4b55b3d 100644
--- a/src/Data/Torrent/Layout.hs
+++ b/src/Data/Torrent/Layout.hs
@@ -56,6 +56,7 @@ module Data.Torrent.Layout
56 , fileOffset 56 , fileOffset
57 57
58 -- * Internal 58 -- * Internal
59 , sizeInBase
59 , getLayoutInfo 60 , getLayoutInfo
60 , putLayoutInfo 61 , putLayoutInfo
61 ) where 62 ) where
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index bf44d7bf..8ad4d3e5 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -45,7 +45,7 @@ import Data.Typeable
45import Data.Torrent.Bitfield 45import Data.Torrent.Bitfield
46import Data.Torrent.Layout 46import Data.Torrent.Layout
47import Data.Torrent.Piece 47import Data.Torrent.Piece
48import System.Torrent.FileMap 48import System.Torrent.FileMap as FM
49 49
50 50
51data StorageFailure 51data StorageFailure
@@ -82,7 +82,9 @@ withStorage :: Mode -> PieceSize -> FileLayout FileSize
82withStorage m s l = bracket (open m s l) close 82withStorage m s l = bracket (open m s l) close
83 83
84isValidIx :: PieceIx -> Storage -> Bool 84isValidIx :: PieceIx -> Storage -> Bool
85isValidIx i s = 0 <= i && i < undefined s 85isValidIx i Storage {..} = 0 <= i && i < pcount
86 where
87 pcount = FM.size fileMap `sizeInBase` pieceLen
86 88
87writePiece :: Piece BL.ByteString -> Storage -> IO () 89writePiece :: Piece BL.ByteString -> Storage -> IO ()
88writePiece p @ Piece {..} s @ Storage {..} 90writePiece p @ Piece {..} s @ Storage {..}
diff --git a/tests/System/Torrent/StorageSpec.hs b/tests/System/Torrent/StorageSpec.hs
index 8d9dfd8f..8267b7a5 100644
--- a/tests/System/Torrent/StorageSpec.hs
+++ b/tests/System/Torrent/StorageSpec.hs
@@ -1,5 +1,6 @@
1module System.Torrent.StorageSpec (spec) where 1module System.Torrent.StorageSpec (spec) where
2import Control.Exception 2import Control.Exception
3import Data.ByteString.Lazy as BL
3import System.FilePath 4import System.FilePath
4import System.Directory 5import System.Directory
5import System.IO.Unsafe 6import System.IO.Unsafe
@@ -32,11 +33,26 @@ spec = before createLayout $ do
32 writePiece (Piece 0 "") s `shouldThrow` (== StorageIsRO) 33 writePiece (Piece 0 "") s `shouldThrow` (== StorageIsRO)
33 close s 34 close s
34 35
36 it "should fail if piece size do not match" $ do
37 withStorage ReadWrite 1 layout $ \ s ->
38 writePiece (Piece 0 "") s `shouldThrow` (== InvalidSize 0)
39
35 it "should fail on negative index" $ do 40 it "should fail on negative index" $ do
36 withStorage ReadWrite 0 layout $ \ s -> 41 withStorage ReadWrite 0 layout $ \ s ->
37 writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1)) 42 writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1))
38 43
44 it "should fail on out of upper bound index" $ do
45 withStorage ReadWrite 100 layout $ \ s -> do
46 let bs = BL.replicate 100 0
47 writePiece (Piece 1 bs) s
48 writePiece (Piece 2 bs) s `shouldThrow` (== InvalidIndex 2)
49
39 describe "readPiece" $ do 50 describe "readPiece" $ do
40 it "should fail on negative index" $ 51 it "should fail on negative index" $
41 withStorage ReadOnly 0 layout $ \ s -> 52 withStorage ReadOnly 0 layout $ \ s ->
42 readPiece (-1) s `shouldThrow` (== InvalidIndex (-1)) 53 readPiece (-1) s `shouldThrow` (== InvalidIndex (-1))
54
55 it "should fail on out of upper bound index" $ do
56 withStorage ReadOnly 100 layout $ \ s -> do
57 _ <- readPiece 1 s
58 readPiece 2 s `shouldThrow` (== InvalidIndex 2)