diff options
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 5 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 5 | ||||
-rw-r--r-- | tests/Network/BitTorrent/DHT/SessionSpec.hs | 16 |
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 | ||
72 | announceH :: Address ip => NodeHandler ip | 72 | announceH :: Address ip => NodeHandler ip |
73 | announceH = nodeHandler $ \ naddr @ NodeAddr {..} (Announce {..}) -> do | 73 | announceH = 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. |
352 | checkToken :: Hashable a => NodeAddr a -> Token -> DHT ip () | 352 | checkToken :: Hashable a => NodeAddr a -> Token -> DHT ip Bool |
353 | checkToken addr questionableToken = do | 353 | checkToken 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 @@ | |||
2 | module Network.BitTorrent.DHT.SessionSpec (spec) where | 2 | module Network.BitTorrent.DHT.SessionSpec (spec) where |
3 | import Control.Monad.Reader | 3 | import Control.Monad.Reader |
4 | import Data.Default | 4 | import Data.Default |
5 | import Data.List as L | ||
5 | import Test.Hspec | 6 | import Test.Hspec |
6 | import Test.QuickCheck | 7 | import 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 | ||