From 4a231f96101e9b00db0328c7fbcd16884df33325 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Mon, 30 Dec 2013 14:06:31 +0400 Subject: Add basic spec for token map --- tests/Network/BitTorrent/DHT/TokenSpec.hs | 32 +++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'tests/Network') diff --git a/tests/Network/BitTorrent/DHT/TokenSpec.hs b/tests/Network/BitTorrent/DHT/TokenSpec.hs index 90ff0f03..6353a24c 100644 --- a/tests/Network/BitTorrent/DHT/TokenSpec.hs +++ b/tests/Network/BitTorrent/DHT/TokenSpec.hs @@ -1,14 +1,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.DHT.Token + +import Network.BitTorrent.Core +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 = return () \ No newline at end of file +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) \ No newline at end of file -- cgit v1.2.3