summaryrefslogtreecommitdiff
path: root/bittorrent/tests/Network/BitTorrent/DHT/TokenSpec.hs
blob: a45d2212ddb1567e01e5e24b42d6ccfdcbb8947d (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
{-# LANGUAGE ScopedTypeVariables  #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Network.BitTorrent.DHT.TokenSpec (spec) where
import Control.Applicative
import Data.List as L
import Data.String
import Test.Hspec
import Test.QuickCheck

import Network.BitTorrent.Address
import Network.BitTorrent.CoreSpec ()
import Network.BitTorrent.DHT.Token as T


instance Arbitrary Token where
  arbitrary = fromString <$> arbitrary

instance Arbitrary TokenMap where
  arbitrary = tokens <$> arbitrary

repeatN :: Int -> (a -> a) -> (a -> a)
repeatN n f = L.foldr (.) id $ L.replicate n f

spec :: Spec
spec = do
  describe "Token" $ do
    return ()

  describe "TokenMap" $ do
    it "is keeping any granted token in current session" $
      property $ \ (addr :: NodeAddr IPv4) m ->
        T.member addr (T.lookup addr m) m

    it "is keeping any granted token in next session" $
      property $ \ (addr :: NodeAddr IPv4) m ->
        T.member addr (T.lookup addr m) (T.update m)

    -- can fail with some small probability
    it "is rejecting any outdated tokens" $
      property $ \ (addr :: NodeAddr IPv4) m k -> not $
        let n = min 100 (abs k + 2) in
        T.member addr (T.lookup addr m) (repeatN n T.update m)