Skip to content

Commit

Permalink
Merge pull request #82 from blackheaven/hf/goldan
Browse files Browse the repository at this point in the history
chore: add golden tests (#75)
  • Loading branch information
Vlix authored Oct 20, 2024
2 parents 1359a9f + 4beda0f commit 573dfc8
Show file tree
Hide file tree
Showing 23 changed files with 90 additions and 1 deletion.
2 changes: 2 additions & 0 deletions password/password.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ build-type: Custom
extra-source-files:
README.md
ChangeLog.md
test/golden/*.golden

flag argon2
description: Compile with Argon2 support?
Expand Down Expand Up @@ -192,6 +193,7 @@ test-suite password-tasty
, quickcheck-instances
, tasty
, tasty-hunit
, tasty-golden
, tasty-quickcheck
, template-haskell
, text
Expand Down
1 change: 1 addition & 0 deletions password/test/golden/Argon_defaultParams.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ0Mg$w9lYrby3BP07ISb/6DWrawOF7lP9EZZfSBV2SiukgSE
1 change: 1 addition & 0 deletions password/test/golden/Argon_otherSalt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQw$4ZXadpbypzQxRQe2bef8kyFJc0PWKBHXoA5FN4iXf20
1 change: 1 addition & 0 deletions password/test/golden/Argon_outputLength24.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ0Mg$jr2U2k4fMCMYkjLZTi8264BWKmovscHJ
1 change: 1 addition & 0 deletions password/test/golden/Argon_variantArgon2i.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ0Mg$wTEHt+UirwtEfvpsYyNNCosby+3w651waXa3dtyF3Fo
1 change: 1 addition & 0 deletions password/test/golden/Bcrypt_cost16.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$2b$16$a07rXVLfZFOuKB.uKB.yKeckTbSjjG2z2Hz8Kb2s8Jsc82snjBHxm
1 change: 1 addition & 0 deletions password/test/golden/Bcrypt_cost4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$2b$04$a07rXVLfZFOuKB.uKB.yKe1sTwXsMijp7DB/Yz0JJk2mmZqbRc6p2
1 change: 1 addition & 0 deletions password/test/golden/Bcrypt_defaultParams.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$2b$10$a07rXVLfZFOuKB.uKB.yKe1tQQIacGVaKJqgpYw212LZlYsLIMonK
1 change: 1 addition & 0 deletions password/test/golden/Bcrypt_otherSalt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$2b$10$a07rXVLfZFOuKB.uKB.uK.yahNLJ/XzWDg41m2qdAMMEpkL7RrFG.
1 change: 1 addition & 0 deletions password/test/golden/PBKDF2_algorithmMD5.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
md5:25000:c29tZXNhbHQ0Mg==:jiOmE1hpAp2eJwrJFbJPLw==
1 change: 1 addition & 0 deletions password/test/golden/PBKDF2_defaultParams.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512:25000:c29tZXNhbHQ0Mg==:RUT4QuEfyBnGIUR3R1jXbD+MjSTU+dNOLWX0mNtLKGI03PIjmxYSIN6USII99OMMhI3KR4o0tVJ1DH8XZaCtbA==
1 change: 1 addition & 0 deletions password/test/golden/PBKDF2_iterCounts1000.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512:1000:c29tZXNhbHQ0Mg==:1gWOWLAgx5ojc3rh9uegFTRyUgKYHS84GPARbqZYIyHeAZdRQzVOLaEAg00JxeglImCAYZbBZ/dBh0Al8q1d8Q==
1 change: 1 addition & 0 deletions password/test/golden/PBKDF2_otherSalt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512:25000:c29tZXNhbHQw:SjeECOnuJvRIgLwPtL/akEzSS/+5vI4ZNRxEnZUOHnDuubUg7FcIuhnwm8a9cvuOXQrqsCZPHGgh8LNpxLEvRg==
1 change: 1 addition & 0 deletions password/test/golden/PBKDF2_outputLength16.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sha512:25000:c29tZXNhbHQ0Mg==:RUT4QuEfyBnGIUR3R1jXbA==
1 change: 1 addition & 0 deletions password/test/golden/Scrypt_defaultParams.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14|8|1|c29tZXNhbHQ0Mg==|byRql1IUuO27GSmCJ0a6ce18VJ6DwIdEcVNvvdEDFBFXosA4Vqh3SES/rR0+pY6ntCTWq4GZlQGTY67TWvWI1A==
1 change: 1 addition & 0 deletions password/test/golden/Scrypt_otherSalt.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14|8|1|c29tZXNhbHQw|us1zh+3aNbwanYjy6Ig+Ft8yAv741HCMOemJpSEP2OJSic/hwr75xCeGVGqU9yG5NUnDAZ1arPuwBkVRgdLemA==
1 change: 1 addition & 0 deletions password/test/golden/Scrypt_outputLength16.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14|8|1|c29tZXNhbHQ0Mg==|byRql1IUuO27GSmCJ0a6cQ==
1 change: 1 addition & 0 deletions password/test/golden/Scrypt_rounds8.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8|8|1|c29tZXNhbHQ0Mg==|XgnxMJ3Ecu5nfvZUyHX7sCxRWRwmeyXeRpqwMy1jCGyPZCYJdGaS0fSJbenFgRoWTFqRWaJ1SoxnpWIiUIyrPw==
1 change: 1 addition & 0 deletions password/test/golden/Scrypt_saltLength16.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14|8|1|c29tZXNhbHQ0Mg==|byRql1IUuO27GSmCJ0a6ce18VJ6DwIdEcVNvvdEDFBFXosA4Vqh3SES/rR0+pY6ntCTWq4GZlQGTY67TWvWI1A==
17 changes: 17 additions & 0 deletions password/test/tasty/Argon2.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{-# LANGUAGE OverloadedStrings #-}
module Argon2 (testArgon2) where

import Data.ByteString (fromStrict)
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (assertBool, assertEqual, testCase)
import Test.Tasty.Golden (goldenVsString)

import Data.Password.Argon2

Expand All @@ -24,6 +27,7 @@ testArgon2 = testGroup "Argon2"
, testWithParams "Argon2 (Argon2d)" (fastParams{ argon2Variant = Argon2d })
, paddingTests
, omittedVersionTest
, testGolden
]
where
testWithParams s params =
Expand Down Expand Up @@ -86,3 +90,16 @@ referenceHashWithoutVersion = PasswordHash "$argon2i$m=65536,t=2,p=4$c29tZXNhbHQ
v10Hash, v10HashWithoutVersion :: PasswordHash Argon2
v10Hash = PasswordHash "$argon2i$v=16$m=65536,t=2,p=1$Kx1BEcpIg0Ey5GyXq5do2w$0qRfWHw09EdqQkSsaG57O/ou8v/E6Vc83w"
v10HashWithoutVersion = PasswordHash "$argon2i$m=65536,t=2,p=1$Kx1BEcpIg0Ey5GyXq5do2w$0qRfWHw09EdqQkSsaG57O/ou8v/E6Vc83w"

testGolden :: TestTree
testGolden = testGroup "Golden tests"
[ go "defaultParams" "Argon_defaultParams" defaultParams "somesalt42"
, go "variant = Argon2i" "Argon_variantArgon2i" defaultParams{argon2Variant = Argon2i} "somesalt42"
, go "output length = 24" "Argon_outputLength24" defaultParams{argon2OutputLength = 24} "somesalt42"
, go "other salt" "Argon_otherSalt" defaultParams "somesalt0"
]
where go testName fileName params salt =
goldenVsString
testName
("test/golden/" <> fileName <> ".golden")
(return $ fromStrict $ encodeUtf8 $ unPasswordHash $ hashPasswordWithSalt params (Salt salt) $ mkPassword "password")
17 changes: 17 additions & 0 deletions password/test/tasty/Bcrypt.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{-# LANGUAGE OverloadedStrings #-}
module Bcrypt where

import Data.ByteString (fromStrict)
import Data.Text (pack)
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty
import Test.Tasty.HUnit (testCase, assertEqual)
import Test.Tasty.Golden (goldenVsString)

import Data.Password.Bcrypt

Expand All @@ -22,6 +25,7 @@ testBcrypt = testGroup "bcrypt"
-- These apparently do not work.
-- , testPassword "2x"
-- , testPassword "2"
, testGolden
]

-- | Tests if the version does not matter for matching the hash.
Expand All @@ -34,3 +38,16 @@ testPassword s =
where
hash v = PasswordHash $
"$" <> pack v <> "$10$v0mMyoUN2ZvDqsJFPH6ft.FcpX67hdXhh7tWf8/hwWZrKZ8U2Phs6"

testGolden :: TestTree
testGolden = testGroup "Golden tests"
[ go "defaultParams" "Bcrypt_defaultParams" defaultParams "somesalt00000042"
, go "cost = 16" "Bcrypt_cost16" 16 "somesalt00000042"
, go "cost = 4" "Bcrypt_cost4" 4 "somesalt00000042"
, go "other salt" "Bcrypt_otherSalt" defaultParams "somesalt00000000"
]
where go testName fileName params salt =
goldenVsString
testName
("test/golden/" <> fileName <> ".golden")
(return $ fromStrict $ encodeUtf8 $ unPasswordHash $ hashPasswordWithSalt params (Salt salt) $ mkPassword "password")
19 changes: 18 additions & 1 deletion password/test/tasty/PBKDF2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module PBKDF2 where

import Crypto.Hash.Algorithms as Crypto (HashAlgorithm, SHA1(..), SHA256(..), SHA512(..))
import Crypto.KDF.PBKDF2 as PBKDF2
import Data.ByteString (ByteString)
import Data.ByteString (ByteString, fromStrict)
#if !MIN_VERSION_base(4,13,0)
import Data.Semigroup ((<>))
#endif
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty
import Test.Tasty.Golden (goldenVsString)
import Test.Tasty.QuickCheck
import Test.QuickCheck.Instances.ByteString ()
import Test.QuickCheck.Instances.Text ()
Expand Down Expand Up @@ -43,6 +45,7 @@ testPBKDF2 = testGroup "PBKDF2"
(\pass (PasswordHash hash) -> checkPassword pass . PasswordHash $ "pbkdf2:" <> hash)
extractParams
testParams
, testGolden
]
where
testIt s params = testCorrectPassword s (hashPasswordWithParams params) checkPassword extractParams params
Expand All @@ -65,3 +68,17 @@ cryptoParams i = PBKDF2.Parameters {
PBKDF2.iterCounts = 2000,
PBKDF2.outputLength = i
}

testGolden :: TestTree
testGolden = testGroup "Golden tests"
[ go "defaultParams" "PBKDF2_defaultParams" defaultParams "somesalt42"
, go "algorithm = MD5" "PBKDF2_algorithmMD5" defaultParams{pbkdf2Algorithm = PBKDF2_MD5} "somesalt42"
, go "output length = 16" "PBKDF2_outputLength16" defaultParams{pbkdf2OutputLength = 16} "somesalt42"
, go "iterCounts = 1000" "PBKDF2_iterCounts1000" defaultParams{pbkdf2Iterations = 1000} "somesalt42"
, go "other salt" "PBKDF2_otherSalt" defaultParams "somesalt0"
]
where go testName fileName params salt =
goldenVsString
testName
("test/golden/" <> fileName <> ".golden")
(return $ fromStrict $ encodeUtf8 $ unPasswordHash $ hashPasswordWithSalt params (Salt salt) $ mkPassword "password")
18 changes: 18 additions & 0 deletions password/test/tasty/Scrypt.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Scrypt where

import Data.ByteString (fromStrict)
import Data.Maybe (fromJust)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Test.Tasty
import Test.Tasty.Golden (goldenVsString)
import Test.Tasty.QuickCheck
import Test.QuickCheck.Instances.Text ()

Expand All @@ -30,6 +33,7 @@ testScrypt = testGroup "scrypt"
#ifndef IS_MAC_OS
, testProperty "scrypt <-> cryptonite" $ withMaxSuccess 10 checkScrypt
#endif
, testGolden
]
where
hash8Rounds = hashPasswordWithParams testsParams8Rounds
Expand All @@ -48,3 +52,17 @@ checkScrypt pass = ioProperty $ do
hashPasswordWithSalt defaultParams{ scryptRounds = 8 } (Salt salt) $ mkPassword pass
return $ scryptHash === encodeUtf8 ourHash
#endif

testGolden :: TestTree
testGolden = testGroup "Golden tests"
[ go "defaultParams" "Scrypt_defaultParams" defaultParams "somesalt42"
, go "rounds = 8" "Scrypt_rounds8" defaultParams{scryptRounds = 8} "somesalt42"
, go "output length = 16" "Scrypt_outputLength16" defaultParams{scryptOutputLength = 16} "somesalt42"
, go "scrypt length = 16" "Scrypt_saltLength16" defaultParams{scryptSalt = 16} "somesalt42"
, go "Other salt" "Scrypt_otherSalt" defaultParams "somesalt0"
]
where go testName fileName params salt =
goldenVsString
testName
("test/golden/" <> fileName <> ".golden")
(return $ fromStrict $ encodeUtf8 $ unPasswordHash $ hashPasswordWithSalt params (Salt salt) $ mkPassword "password")

0 comments on commit 573dfc8

Please sign in to comment.