summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/BitTorrent/DHT/Query.hs5
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs5
-rw-r--r--tests/Network/BitTorrent/DHT/SessionSpec.hs16
3 files changed, 16 insertions, 10 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs
index dcb217d6..07fb80c2 100644
--- a/src/Network/BitTorrent/DHT/Query.hs
+++ b/src/Network/BitTorrent/DHT/Query.hs
@@ -71,7 +71,10 @@ getPeersH = nodeHandler $ \ naddr (GetPeers ih) -> do
71 71
72announceH :: Address ip => NodeHandler ip 72announceH :: Address ip => NodeHandler ip
73announceH = nodeHandler $ \ naddr @ NodeAddr {..} (Announce {..}) -> do 73announceH = nodeHandler $ \ naddr @ NodeAddr {..} (Announce {..}) -> do
74 checkToken naddr sessionToken 74 valid <- checkToken naddr sessionToken
75 unless valid $ do
76 throwIO $ InvalidParameter "token"
77
75 let annPort = if impliedPort then nodePort else port 78 let annPort = if impliedPort then nodePort else port
76 let peerAddr = PeerAddr Nothing nodeHost annPort 79 let peerAddr = PeerAddr Nothing nodeHost annPort
77 insertPeer topic peerAddr 80 insertPeer topic peerAddr
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 6f13cf78..10f3e9e4 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -349,12 +349,11 @@ grantToken addr = do
349 349
350-- | Throws 'HandlerError' if the token is invalid or already 350-- | Throws 'HandlerError' if the token is invalid or already
351-- expired. See 'TokenMap' for details. 351-- expired. See 'TokenMap' for details.
352checkToken :: Hashable a => NodeAddr a -> Token -> DHT ip () 352checkToken :: Hashable a => NodeAddr a -> Token -> DHT ip Bool
353checkToken addr questionableToken = do 353checkToken addr questionableToken = do
354 tryUpdateSecret 354 tryUpdateSecret
355 toks <- asks sessionTokens >>= liftIO . readTVarIO 355 toks <- asks sessionTokens >>= liftIO . readTVarIO
356 unless (T.member addr questionableToken (tokenMap toks)) $ 356 return $ T.member addr questionableToken (tokenMap toks)
357 throwIO $ InvalidParameter "token"
358 357
359{----------------------------------------------------------------------- 358{-----------------------------------------------------------------------
360-- Routing table 359-- Routing table
diff --git a/tests/Network/BitTorrent/DHT/SessionSpec.hs b/tests/Network/BitTorrent/DHT/SessionSpec.hs
index 75c95462..3e197875 100644
--- a/tests/Network/BitTorrent/DHT/SessionSpec.hs
+++ b/tests/Network/BitTorrent/DHT/SessionSpec.hs
@@ -2,6 +2,7 @@
2module Network.BitTorrent.DHT.SessionSpec (spec) where 2module Network.BitTorrent.DHT.SessionSpec (spec) where
3import Control.Monad.Reader 3import Control.Monad.Reader
4import Data.Default 4import Data.Default
5import Data.List as L
5import Test.Hspec 6import Test.Hspec
6import Test.QuickCheck 7import Test.QuickCheck
7 8
@@ -33,14 +34,17 @@ spec = do
33 describe "tokens" $ do 34 describe "tokens" $ do
34 it "should not complain about valid token" $ 35 it "should not complain about valid token" $
35 property $ \ (addrs :: [NodeAddr IPv4]) -> do 36 property $ \ (addrs :: [NodeAddr IPv4]) -> do
36 simpleDHT $ do 37 isOks <- simpleDHT $ do
37 forM_ addrs $ \ addr -> do 38 forM addrs $ \ addr -> do
38 token <- grantToken addr 39 token <- grantToken addr
39 checkToken addr token 40 checkToken addr token
40{- 41 L.and isOks `shouldBe` True
41 it "" $ property $ \ (addr :: NodeAddr IPv4) token -> do 42
42 simpleDHT (checkToken addr token) `shouldThrow` (== undefined) 43 it "should complain about invalid token" $
43-} 44 property $ \ (addr :: NodeAddr IPv4) token -> do
45 isOk <- simpleDHT (checkToken addr token)
46 isOk `shouldBe` False
47
44 describe "routing table" $ do 48 describe "routing table" $ do
45 return () 49 return ()
46 50