From bec54f38a42f1f48aa9577a853c0288d46eec3ff Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 26 Dec 2013 07:40:18 +0400 Subject: Add tests for NodeInfo module --- tests/Network/BitTorrent/Core/NodeSpec.hs | 39 ++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'tests/Network/BitTorrent/Core') diff --git a/tests/Network/BitTorrent/Core/NodeSpec.hs b/tests/Network/BitTorrent/Core/NodeSpec.hs index b3bdf134..3f27f9eb 100644 --- a/tests/Network/BitTorrent/Core/NodeSpec.hs +++ b/tests/Network/BitTorrent/Core/NodeSpec.hs @@ -1,13 +1,16 @@ +{-# OPTIONS -fno-warn-orphans #-} module Network.BitTorrent.Core.NodeSpec (spec) where import Control.Applicative +import Data.Serialize as S +import Data.String import Test.Hspec import Test.QuickCheck -import Network.BitTorrent.Core.Node +import Network.BitTorrent.Core import Network.BitTorrent.Core.PeerAddrSpec () instance Arbitrary NodeId where - arbitrary = undefined + arbitrary = fromString <$> vector 20 instance Arbitrary a => Arbitrary (NodeAddr a) where arbitrary = NodeAddr <$> arbitrary <*> arbitrary @@ -16,4 +19,34 @@ instance Arbitrary a => Arbitrary (NodeInfo a) where arbitrary = NodeInfo <$> arbitrary <*> arbitrary spec :: Spec -spec = return () \ No newline at end of file +spec = do + describe "NodeId" $ do + it "properly serialized" $ do + S.decode "mnopqrstuvwxyz123456" + `shouldBe` Right ("mnopqrstuvwxyz123456" :: NodeId) + + S.encode ("mnopqrstuvwxyz123456" :: NodeId) + `shouldBe` "mnopqrstuvwxyz123456" + + it "properly serialized (iso)" $ property $ \ nid -> + S.decode (S.encode nid) `shouldBe` + Right (nid :: NodeId) + + describe "NodeAddr" $ do + it "properly serialized" $ do + S.decode "\127\0\0\1\1\2" `shouldBe` + Right ("127.0.0.1:258" :: NodeAddr IPv4) + + it "properly serialized (iso)" $ property $ \ nid -> + S.decode (S.encode nid) `shouldBe` + Right (nid :: NodeAddr IPv4) + + describe "NodeInfo" $ do + it "properly serialized" $ do + S.decode "mnopqrstuvwxyz123456\ + \\127\0\0\1\1\2" `shouldBe` Right + (NodeInfo "mnopqrstuvwxyz123456" "127.0.0.1:258" :: NodeInfo IPv4) + + it "properly serialized (iso)" $ property $ \ nid -> + S.decode (S.encode nid) `shouldBe` + Right (nid :: NodeInfo IPv4) -- cgit v1.2.3