diff options
-rw-r--r-- | bittorrent.cabal | 2 | ||||
-rw-r--r-- | src/System/Torrent/Storage.hs | 7 | ||||
m--------- | sub/bencoding | 0 | ||||
-rw-r--r-- | tests/Data/Torrent/PieceSpec.hs | 13 | ||||
-rw-r--r-- | tests/System/Torrent/StorageSpec.hs | 42 |
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 | ||
63 | instance Exception StorageFailure | 64 | instance Exception StorageFailure |
64 | 65 | ||
@@ -76,6 +77,10 @@ open mode s l = Storage mode s <$> mmapFiles mode l | |||
76 | close :: Storage -> IO () | 77 | close :: Storage -> IO () |
77 | close Storage {..} = unmapFiles fileMap | 78 | close Storage {..} = unmapFiles fileMap |
78 | 79 | ||
80 | withStorage :: Mode -> PieceSize -> FileLayout FileSize | ||
81 | -> (Storage -> IO ()) -> IO () | ||
82 | withStorage m s l = bracket (open m s l) close | ||
83 | |||
79 | isValidIx :: PieceIx -> Storage -> Bool | 84 | isValidIx :: PieceIx -> Storage -> Bool |
80 | isValidIx i s = 0 <= i && i < undefined s | 85 | isValidIx 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 #-} | ||
2 | module Data.Torrent.PieceSpec (spec) where | ||
3 | import Control.Applicative | ||
4 | import Test.Hspec | ||
5 | import Test.QuickCheck | ||
6 | import Data.Torrent.Piece | ||
7 | |||
8 | |||
9 | instance Arbitrary a => Arbitrary (Piece a) where | ||
10 | arbitrary = Piece <$> arbitrary <*> arbitrary | ||
11 | |||
12 | spec :: Spec | ||
13 | spec = 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 @@ | |||
1 | module System.Torrent.StorageSpec (spec) where | ||
2 | import Control.Exception | ||
3 | import System.FilePath | ||
4 | import System.Directory | ||
5 | import System.IO.Unsafe | ||
6 | import Test.Hspec | ||
7 | |||
8 | import Data.Torrent.Layout | ||
9 | import Data.Torrent.Piece | ||
10 | import System.Torrent.Storage | ||
11 | |||
12 | |||
13 | layout :: FileLayout FileSize | ||
14 | layout = | ||
15 | [ (dir </> "_a", 20) | ||
16 | , (dir </> "_b", 50) | ||
17 | , (dir </> "_c", 100) | ||
18 | , (dir </> "_d", 5) | ||
19 | ] | ||
20 | where | ||
21 | dir = unsafePerformIO $ getTemporaryDirectory | ||
22 | |||
23 | createLayout :: IO () | ||
24 | createLayout = | ||
25 | bracket (open ReadWriteEx 0 layout) close (const (return ())) | ||
26 | |||
27 | spec :: Spec | ||
28 | spec = 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)) | ||