summaryrefslogtreecommitdiff
path: root/tests/Network/BitTorrent/Core/NodeInfoSpec.hs
blob: fb7774406123a02c28af2a0a4b212197ca2ec497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# OPTIONS -fno-warn-orphans #-}
module Network.BitTorrent.Core.NodeInfoSpec (spec) where
import Control.Applicative
import Data.Serialize as S
import Data.String
import Test.Hspec
import Test.QuickCheck

import Network.BitTorrent.Core
import Network.BitTorrent.Core.PeerAddrSpec ()

instance Arbitrary NodeId where
  arbitrary = fromString <$> vector 20

instance Arbitrary a => Arbitrary (NodeAddr a) where
  arbitrary = NodeAddr <$> arbitrary <*> arbitrary

instance Arbitrary a => Arbitrary (NodeInfo a) where
  arbitrary = NodeInfo <$> arbitrary <*> arbitrary

spec :: Spec
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)