File: Tests.hs

package info (click to toggle)
haskell-cryptostore 0.3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: haskell: 8,241; makefile: 3
file content (74 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- | X.509 tests.
module X509.Tests (x509Tests) where

import qualified Data.ByteString as B
import           Data.X509

import Crypto.Store.X509

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck

import Util
import X509.Instances ()

keyTests :: TestName -> String -> Int -> TestTree
keyTests name prefix count =
    testGroup name
        [ testCase "read public key" $ do
              keys <- readPubKeyFile fKey
              length keys @?= count
        , testCase "read certificate" $ do
              cert <- readSignedObject fCert :: IO [SignedCertificate]
              length cert @?= 1
        , testCase "same key" $ do
              cert <- readSignedObject fCert :: IO [SignedCertificate]
              keys <- readPubKeyFile fKey
              assertBool "keys differ" $
                  let [c] = cert
                      key = certPubKey (signedObject (getSigned c))
                   in all (== key) keys
        , testCase "write certificate" $ do
              bs <- B.readFile fCert
              let objs = readSignedObjectFromMemory bs :: [SignedCertificate]
              writeSignedObjectToMemory objs @?= bs
        , testCase "write public key" $ do
              bs <- B.readFile fKey
              let (key : _) = readPubKeyFileFromMemory bs
              assertBool "first key differs" $
                  writePubKeyFileToMemory [key] `B.isPrefixOf` bs
        ]
  where
    fCert = testFile (prefix ++ "-self-signed-cert.pem")
    fKey  = testFile (prefix ++ "-public.pem")

propertyTests :: TestTree
propertyTests = localOption (QuickCheckMaxSize 5) $ testGroup "properties"
    [ testProperty "marshalling public keys" $ \keys ->
          keys === readPubKeyFileFromMemory (writePubKeyFileToMemory keys)
    , testProperty "marshalling certificates" $ \objs ->
          asCerts objs === writeReadObjs objs
    , testProperty "marshalling CRLs" $ \objs ->
          asCRLs objs === writeReadObjs objs
    ]
  where
    writeReadObjs :: SignedObject a => [SignedExact a] -> [SignedExact a]
    writeReadObjs = readSignedObjectFromMemory . writeSignedObjectToMemory

    asCerts = id :: [SignedCertificate] -> [SignedCertificate]
    asCRLs  = id :: [SignedCRL] -> [SignedCRL]

x509Tests :: TestTree
x509Tests =
    testGroup "X509"
        [ keyTests "RSA"                        "rsa"        2
        , keyTests "DSA"                        "dsa"        1
        , keyTests "EC (named curve)"           "ecdsa-p256" 1
--        , keyTests "EC (explicit prime curve)"  "ecdsa-epc"  1
        , keyTests "X25519"                     "x25519"     1
        , keyTests "X448"                       "x448"       1
        , keyTests "Ed25519"                    "ed25519"    1
        , keyTests "Ed448"                      "ed448"      1
        , propertyTests
        ]