packages feed

hstratus-auth-0.2.0.4 / test / HStratus / PBKDF2Spec.hs

{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE OverloadedStrings #-}{- |Module      : HStratus.PBKDF2SpecCopyright   : (c) 2023 Tim EmiolaMaintainer  : Tim Emiola <adetokunbo@emio.la>SPDX-License-Identifier: BSD3-}module HStratus.PBKDF2Spec (spec) whereimport qualified Crypto.Hash.SHA1 as SHA1import qualified Crypto.Hash.SHA256 as SHA256import qualified Crypto.Hash.SHA512 as SHA512import Data.Aeson  ( FromJSON (..)  , Object  , Value  , eitherDecodeFileStrict  , withArray  , withObject  , (.:)  )import Data.Aeson.Types (Parser)import Data.ByteString (ByteString)import Data.ByteString.Base16 (decode)import Data.Foldable (toList)import Data.Text.Encoding (encodeUtf8)import Data.Word (Word32, Word64, Word8)import Network.HStratus.Internal.PBKDF2 (PseudoRandomF, deriveKey, wrap)import Paths_hstratus_auth (getDataFileName)import Test.Hspec (Spec, context, describe, it, runIO, shouldBe)import Test.Hspec.Runner (SpecWith)spec :: Specspec = describe "module Network.HStratus.PBKDF2" $ do  describe "deriveKey" $ do    context "when using the wycheProof test cases" $ do      specFrom "sha1" SHA1.hmac      specFrom "sha256" SHA256.hmac      specFrom "sha512" SHA512.hmacspecFrom :: String -> PseudoRandomF -> SpecspecFrom shaName pseudo = do  context ("with " ++ shaName ++ ".hmac as the pseudorandom function") $ do    testData <- runIO $ namedDataPath shaName >>= loadTestData    mapM_ (specWithFrom pseudo) testDataspecWithFrom :: PseudoRandomF -> TestDescription -> SpecWith ()specWithFrom pseudo td = do  let TestDescription{tdSalt, tdIterationCount = count, tdDerivedKey = key} = td  it ("test " ++ show (tdId td) ++ " should succeed") $ do    let pseudo' = wrap pseudo (tdDerivedLength td)        calc x = deriveKey x (tdPassword td) tdSalt count    calc <$> pseudo' `shouldBe` Right keyloadTestData :: FilePath -> IO [TestDescription]loadTestData src =  let decodeDataFile aPath = fmap unTestFile <$> eitherDecodeFileStrict aPath   in decodeDataFile src >>= either fail purenamedDataPath :: String -> IO FilePathnamedDataPath shaName =  let path = "testdata/pbkdf2_hmac" ++ shaName ++ "_test.json"   in getDataFileName pathnewtype TestFile = TestFile {unTestFile :: [TestDescription]}instance FromJSON TestFile where  parseJSON = fmap TestFile . parseTestFileparseTestFile :: Value -> Parser [TestDescription]parseTestFile =  let oneTest = withObject "test" parseTestDescription      manyTests = traverse oneTest . toList      testsInTestGroup o = o .: "tests" >>= withArray "[test]" manyTests      oneTestGroup = withObject "testsInTestGroup" testsInTestGroup      manyTestGroups = traverse oneTestGroup . toList      testGroupsAtTop o = o .: "testGroups" >>= withArray "[testGroups]" manyTestGroups   in withObject "dataFile" (fmap concat . testGroupsAtTop)data TestDescription = TestDescription  { tdId :: !Word8  , tdPassword :: !ByteString  , tdSalt :: !ByteString  , tdIterationCount :: !Word64  , tdDerivedLength :: !Word32  , tdDerivedKey :: !ByteString  }  deriving (Eq, Show)parseTestDescription :: Object -> Parser TestDescriptionparseTestDescription o = do  let parseBase16Bytes = either fail pure . decode . encodeUtf8  tdId <- o .: "tcId"  tdPassword <- o .: "password" >>= parseBase16Bytes  tdSalt <- o .: "salt" >>= parseBase16Bytes  tdIterationCount <- o .: "iterationCount"  tdDerivedLength <- o .: "dkLen"  tdDerivedKey <- o .: "dk" >>= parseBase16Bytes  pure    TestDescription      { tdId      , tdPassword      , tdSalt      , tdIterationCount      , tdDerivedKey      , tdDerivedLength      }