From 0ec645b8e89c9b0177377812f7e4035c2c94ce1b Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Tue, 26 Nov 2013 06:25:25 +0400 Subject: Add Arbitrary instance for AnnounceQuery --- bittorrent.cabal | 9 +++-- src/Network/BitTorrent/Tracker/Message.hs | 2 ++ tests/Data/Torrent/ProgressSpec.hs | 13 +++++++ tests/Network/BitTorrent/Core/PeerIdSpec.hs | 15 ++++++++ tests/Network/BitTorrent/Tracker/MessageSpec.hs | 47 +++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/Data/Torrent/ProgressSpec.hs create mode 100644 tests/Network/BitTorrent/Core/PeerIdSpec.hs create mode 100644 tests/Network/BitTorrent/Tracker/MessageSpec.hs diff --git a/bittorrent.cabal b/bittorrent.cabal index 403837ba..dbc9d96a 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal @@ -143,10 +143,13 @@ test-suite spec type: exitcode-stdio-1.0 hs-source-dirs: tests main-is: Spec.hs - other-modules: Data.Torrent.MagnetSpec + other-modules: Data.Torrent.ClientSpec Data.Torrent.InfoHashSpec + Data.Torrent.MagnetSpec Data.Torrent.MetainfoSpec - Data.Torrent.ClientSpec + Data.Torrent.ProgressSpec + Network.BitTorrent.Core.PeerIdSpec + Network.BitTorrent.Tracker.MessageSpec build-depends: base == 4.* , bytestring , directory @@ -161,7 +164,7 @@ test-suite spec , hspec , QuickCheck , quickcheck-instances - + , http-types , bencoding , bittorrent ghc-options: -Wall -fno-warn-orphans diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs index e49a9e58..53d7a946 100644 --- a/src/Network/BitTorrent/Tracker/Message.hs +++ b/src/Network/BitTorrent/Tracker/Message.hs @@ -417,6 +417,8 @@ missingOffset = 101 invalidOffset :: Int invalidOffset = 150 +-- TODO use Network.HTTP.Types.Status + -- | Get HTTP response error code from a announce params parse -- failure. -- diff --git a/tests/Data/Torrent/ProgressSpec.hs b/tests/Data/Torrent/ProgressSpec.hs new file mode 100644 index 00000000..32efbd7a --- /dev/null +++ b/tests/Data/Torrent/ProgressSpec.hs @@ -0,0 +1,13 @@ +{-# OPTIONS -fno-warn-orphans #-} +module Data.Torrent.ProgressSpec (spec) where +import Control.Applicative +import Test.Hspec +import Test.QuickCheck +import Data.Torrent.Progress + + +instance Arbitrary Progress where + arbitrary = Progress <$> arbitrary <*> arbitrary <*> arbitrary + +spec :: Spec +spec = return () diff --git a/tests/Network/BitTorrent/Core/PeerIdSpec.hs b/tests/Network/BitTorrent/Core/PeerIdSpec.hs new file mode 100644 index 00000000..7ed8a976 --- /dev/null +++ b/tests/Network/BitTorrent/Core/PeerIdSpec.hs @@ -0,0 +1,15 @@ +module Network.BitTorrent.Core.PeerIdSpec (spec) where +import Control.Applicative +import Test.Hspec +import Test.QuickCheck +import Network.BitTorrent.Core.PeerId + + +instance Arbitrary PeerId where + arbitrary = oneof + [ azureusStyle defaultClientId defaultVersionNumber <$> arbitrary + , shadowStyle 'X' defaultVersionNumber <$> arbitrary + ] + +spec :: Spec +spec = return () \ No newline at end of file diff --git a/tests/Network/BitTorrent/Tracker/MessageSpec.hs b/tests/Network/BitTorrent/Tracker/MessageSpec.hs new file mode 100644 index 00000000..62b4ab04 --- /dev/null +++ b/tests/Network/BitTorrent/Tracker/MessageSpec.hs @@ -0,0 +1,47 @@ +{-# OPTIONS -fno-warn-orphans #-} +module Network.BitTorrent.Tracker.MessageSpec (spec) where + +import Control.Applicative +import Data.BEncode as BE +import Data.ByteString.Lazy as BL +import Data.Maybe +import Data.Word +import Network +import Network.URI +import Test.Hspec +import Test.QuickCheck +import Network.HTTP.Types.URI + +import Data.Torrent.InfoHashSpec hiding (spec) +import Data.Torrent.ProgressSpec hiding (spec) +import Network.BitTorrent.Core.PeerIdSpec hiding (spec) + +import Network.BitTorrent.Tracker.Message + + +prop_bencode :: Eq a => BEncode a => a -> Bool +prop_bencode a = BE.decode (BL.toStrict (BE.encode a)) == return a + +--prop_urlencode :: Eq a => URLDecoded a => URLEncoded a => a -> Bool +--prop_urlencode a = urlDecode (T.pack (urlEncode a)) == a + +instance Arbitrary Event where + arbitrary = elements [minBound..maxBound] + +instance Arbitrary PortNumber where + arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) + +instance Arbitrary AnnounceQuery where + arbitrary = AnnounceQuery + <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary + <*> arbitrary <*> arbitrary <*> arbitrary + +baseURI :: URI +baseURI = fromJust $ parseURI "http://a" + +spec :: Spec +spec = do + describe "Announce" $ do + return () + describe "Scrape" $ do + return () -- cgit v1.2.3