summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal2
-rw-r--r--src/System/Torrent/Storage.hs7
m---------sub/bencoding0
-rw-r--r--tests/Data/Torrent/PieceSpec.hs13
-rw-r--r--tests/System/Torrent/StorageSpec.hs42
5 files changed, 63 insertions, 1 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index 2f83c77b..c1ffd12d 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -159,6 +159,7 @@ test-suite spec
159 Data.Torrent.LayoutSpec 159 Data.Torrent.LayoutSpec
160 Data.Torrent.MagnetSpec 160 Data.Torrent.MagnetSpec
161 Data.Torrent.MetainfoSpec 161 Data.Torrent.MetainfoSpec
162 Data.Torrent.PieceSpec
162 Data.Torrent.ProgressSpec 163 Data.Torrent.ProgressSpec
163 Network.BitTorrent.CoreSpec 164 Network.BitTorrent.CoreSpec
164 Network.BitTorrent.Core.PeerIdSpec 165 Network.BitTorrent.Core.PeerIdSpec
@@ -168,6 +169,7 @@ test-suite spec
168 Network.BitTorrent.Tracker.RPC.HTTPSpec 169 Network.BitTorrent.Tracker.RPC.HTTPSpec
169 Network.BitTorrent.Tracker.RPC.UDPSpec 170 Network.BitTorrent.Tracker.RPC.UDPSpec
170 Network.BitTorrent.Exchange.MessageSpec 171 Network.BitTorrent.Exchange.MessageSpec
172 System.Torrent.StorageSpec
171 System.Torrent.FileMapSpec 173 System.Torrent.FileMapSpec
172 build-depends: base == 4.* 174 build-depends: base == 4.*
173 , bytestring 175 , bytestring
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index 71e0616b..bf44d7bf 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -25,6 +25,7 @@ module System.Torrent.Storage
25 , def 25 , def
26 , open 26 , open
27 , close 27 , close
28 , withStorage
28 29
29 -- * Query 30 -- * Query
30 , genPieceInfo 31 , genPieceInfo
@@ -58,7 +59,7 @@ data StorageFailure
58 -- | Piece size do not match with one passed to the 'open' 59 -- | Piece size do not match with one passed to the 'open'
59 -- function. 60 -- function.
60 | InvalidSize PieceSize 61 | InvalidSize PieceSize
61 deriving (Show, Typeable) 62 deriving (Show, Eq, Typeable)
62 63
63instance Exception StorageFailure 64instance Exception StorageFailure
64 65
@@ -76,6 +77,10 @@ open mode s l = Storage mode s <$> mmapFiles mode l
76close :: Storage -> IO () 77close :: Storage -> IO ()
77close Storage {..} = unmapFiles fileMap 78close Storage {..} = unmapFiles fileMap
78 79
80withStorage :: Mode -> PieceSize -> FileLayout FileSize
81 -> (Storage -> IO ()) -> IO ()
82withStorage m s l = bracket (open m s l) close
83
79isValidIx :: PieceIx -> Storage -> Bool 84isValidIx :: PieceIx -> Storage -> Bool
80isValidIx i s = 0 <= i && i < undefined s 85isValidIx i s = 0 <= i && i < undefined s
81 86
diff --git a/sub/bencoding b/sub/bencoding
Subproject 04d5e90177f9edbc49c5392f471950ebd0bfea4 Subproject fa7861cc092fb3d423d6e3c05df36d3651068de
diff --git a/tests/Data/Torrent/PieceSpec.hs b/tests/Data/Torrent/PieceSpec.hs
new file mode 100644
index 00000000..ef1f2938
--- /dev/null
+++ b/tests/Data/Torrent/PieceSpec.hs
@@ -0,0 +1,13 @@
1{-# OPTIONS_GHC -fno-warn-orphans #-}
2module Data.Torrent.PieceSpec (spec) where
3import Control.Applicative
4import Test.Hspec
5import Test.QuickCheck
6import Data.Torrent.Piece
7
8
9instance Arbitrary a => Arbitrary (Piece a) where
10 arbitrary = Piece <$> arbitrary <*> arbitrary
11
12spec :: Spec
13spec = return () \ No newline at end of file
diff --git a/tests/System/Torrent/StorageSpec.hs b/tests/System/Torrent/StorageSpec.hs
new file mode 100644
index 00000000..8d9dfd8f
--- /dev/null
+++ b/tests/System/Torrent/StorageSpec.hs
@@ -0,0 +1,42 @@
1module System.Torrent.StorageSpec (spec) where
2import Control.Exception
3import System.FilePath
4import System.Directory
5import System.IO.Unsafe
6import Test.Hspec
7
8import Data.Torrent.Layout
9import Data.Torrent.Piece
10import System.Torrent.Storage
11
12
13layout :: FileLayout FileSize
14layout =
15 [ (dir </> "_a", 20)
16 , (dir </> "_b", 50)
17 , (dir </> "_c", 100)
18 , (dir </> "_d", 5)
19 ]
20 where
21 dir = unsafePerformIO $ getTemporaryDirectory
22
23createLayout :: IO ()
24createLayout =
25 bracket (open ReadWriteEx 0 layout) close (const (return ()))
26
27spec :: Spec
28spec = before createLayout $ do
29 describe "writePiece" $ do
30 it "should fail gracefully on write operation in RO mode" $ do
31 s <- open ReadOnly 0 layout
32 writePiece (Piece 0 "") s `shouldThrow` (== StorageIsRO)
33 close s
34
35 it "should fail on negative index" $ do
36 withStorage ReadWrite 0 layout $ \ s ->
37 writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1))
38
39 describe "readPiece" $ do
40 it "should fail on negative index" $
41 withStorage ReadOnly 0 layout $ \ s ->
42 readPiece (-1) s `shouldThrow` (== InvalidIndex (-1))