-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add signatureToText function and tests
- make NarSignature's Show instance more legible - add Arbitrary instance for NarSignature - add roundtrip quickcheck tests for NarSignature encoding
- Loading branch information
Showing
5 changed files
with
76 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- due to recent generic-arbitrary | ||
{-# OPTIONS_GHC -fconstraint-solver-iterations=0 #-} | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module System.Nix.Arbitrary.Signature where | ||
|
||
import qualified Crypto.PubKey.Ed25519 | ||
import Crypto.Random (drgNewTest, withDRG) | ||
import qualified Data.ByteString as BS | ||
import qualified Data.Text as Text | ||
import Test.QuickCheck.Arbitrary.Generic (GenericArbitrary(..)) | ||
import Test.QuickCheck.Instances () | ||
import Test.QuickCheck | ||
|
||
import System.Nix.Signature | ||
|
||
instance Arbitrary Crypto.PubKey.Ed25519.Signature where | ||
arbitrary = do | ||
seeds <- (,,,,) <$> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom <*> arbitraryBoundedRandom | ||
let drg = drgNewTest seeds | ||
(secretKey, _) = withDRG drg Crypto.PubKey.Ed25519.generateSecretKey | ||
publicKey = Crypto.PubKey.Ed25519.toPublic secretKey | ||
msg :: BS.ByteString = "msg" | ||
pure $ Crypto.PubKey.Ed25519.sign secretKey publicKey msg | ||
|
||
deriving via GenericArbitrary Signature | ||
instance Arbitrary Signature | ||
|
||
instance Arbitrary NarSignature where | ||
arbitrary = do | ||
name <- Text.pack . getPrintableString <$> suchThat arbitrary (\(PrintableString str) -> validName str) | ||
NarSignature name <$> arbitrary | ||
|
||
validName :: String -> Bool | ||
validName txt = not (null txt) && not (elem ':' txt) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module SignatureSpec where | ||
|
||
import Test.Hspec (Spec, describe) | ||
import Test.Hspec.Nix (roundtrips) | ||
import Test.Hspec.QuickCheck (prop) | ||
|
||
import System.Nix.Signature (signatureToText, parseSignature) | ||
import System.Nix.Arbitrary () | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "Signature" $ do | ||
prop "roundtrips" $ roundtrips signatureToText parseSignature |