crypto-token-0.2.2: test/TokenSpec.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module TokenSpec where
import Control.Concurrent
import qualified Control.Exception as E
import Crypto.Token
import Control.Monad
import Data.Bits (xor)
import qualified Data.ByteString as BS
import Data.IORef
import Data.Word (Word64)
import GHC.Clock (getMonotonicTimeNSec)
import Test.Hspec
----------------------------------------------------------------
spec :: Spec
spec = do
describe "crypto token" $ do
it "encrypt & decrypt ByteString" $
roundTrip defaultConfig
it "lives as long as its lifetime, across rotations" $ do
mgr <- spawnTokenManager $ defaultConfig{interval = 1, tokenLifetime = 2}
threadDelay 900000
-- made just before a rotation
tf <- encryptToken mgr "Foo"
threadDelay 1200000
decryptToken mgr tf `shouldReturn` Just "Foo"
tb <- encryptToken mgr "Bar"
threadDelay 1300000
decryptToken mgr tf `shouldReturn` Nothing
decryptToken mgr tb `shouldReturn` Just "Bar"
killTokenManager mgr
it "rotates after intervals with no tokens, more than the slots" $ do
mgr <- spawnTokenManager $ defaultConfig{interval = 1, tokenLifetime = 1}
t1 <- encryptToken mgr "Foo"
threadDelay 3500000
t2 <- encryptToken mgr "Bar"
decryptToken mgr t1 `shouldReturn` Nothing
decryptToken mgr t2 `shouldReturn` Just "Bar"
it "rotates once per interval while many threads make tokens" $ do
-- two slots: a second rotation would replace the secret of
-- the tokens made before the boundary
mgr <- spawnTokenManager $ defaultConfig{interval = 2, tokenLifetime = 2}
-- from 0.6 s before a boundary to 0.6 s after it
now <- getMonotonicTimeNSec
let boundary = (now `div` 2000000000 + 1) * 2000000000
start = boundary - 600000000
when (now < start) $ sleepUntil start
dones <- replicateM 8 $ do
done <- newEmptyMVar
ref <- newIORef []
_ <- forkIO $ do
makeTokens mgr (boundary + 600000000) ref (0 :: Int)
putMVar done ()
return (done, ref)
mapM_ (takeMVar . fst) dones
ts <- concat <$> mapM (readIORef . snd) dones
any ((< boundary) . fst) ts `shouldBe` True
any ((> boundary) . fst) ts `shouldBe` True
forM_ (reverse ts) $ \(_, t) ->
decryptToken mgr t `shouldReturn` Just "Foo"
killTokenManager mgr
it "forgets its secrets when killed" $ do
mgr <- spawnTokenManager defaultConfig
t <- encryptToken mgr "Foo"
killTokenManager mgr
decryptToken mgr t `shouldReturn` Nothing
t' <- encryptToken mgr "Bar"
decryptToken mgr t' `shouldReturn` Just "Bar"
it "refuses a tampered token" $ do
mgr <- spawnTokenManager defaultConfig
t <- encryptToken mgr "Foo"
let flip1 i = BS.take i t <> BS.singleton (BS.index t i `xor` 1) <> BS.drop (i + 1) t
-- in the header, the payload and the tag
mapM_
(\i -> decryptToken mgr (flip1 i) `shouldReturn` Nothing)
[0, 20, BS.length t - 1]
decryptToken mgr (BS.take 15 t) `shouldReturn` Nothing
killTokenManager mgr
it "gives unrelated headers to consecutive tokens" $ do
mgr <- spawnTokenManager defaultConfig
t1 <- encryptToken mgr "Foo"
t2 <- encryptToken mgr "Foo"
-- with a fixed mask only the counter's low byte would differ
length (filter id $ BS.zipWith (/=) (BS.take 16 t1) (BS.take 16 t2))
`shouldSatisfy` (> 4)
killTokenManager mgr
describe "configuration" $ do
it "survives a lifetime shorter than the interval" $
roundTrip defaultConfig{interval = 10, tokenLifetime = 1}
it "survives more intervals than an index can name" $
roundTrip defaultConfig{interval = 1, tokenLifetime = 100000}
it "refuses a non-positive interval" $
spawnTokenManager defaultConfig{interval = 0}
`shouldThrow` (\(_ :: E.IOException) -> True)
-- Making tokens as fast as possible, keeping some of them.
makeTokens :: TokenManager -> Word64 -> IORef [(Word64, BS.ByteString)] -> Int -> IO ()
makeTokens mgr deadline ref i = do
t <- encryptToken mgr "Foo"
now <- getMonotonicTimeNSec
when (i `mod` 64 == 0) $ modifyIORef' ref ((now, t) :)
when (now < deadline) $ makeTokens mgr deadline ref (i + 1)
sleepUntil :: Word64 -> IO ()
sleepUntil t = do
now <- getMonotonicTimeNSec
when (now < t) $ threadDelay $ fromIntegral ((t - now) `div` 1000)
roundTrip :: Config -> IO ()
roundTrip conf =
E.bracket (spawnTokenManager conf) killTokenManager $ \mgr -> do
t <- encryptToken mgr "Foo"
decryptToken mgr t `shouldReturn` Just "Foo"