Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Confd / Utils.hs @ 7022db83

History | View | Annotate | Download (3.8 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2
{-# OPTIONS_GHC -fno-warn-orphans #-}
3

    
4
{-| Unittests for ganeti-htools.
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11

    
12
This program is free software; you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation; either version 2 of the License, or
15
(at your option) any later version.
16

    
17
This program is distributed in the hope that it will be useful, but
18
WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
General Public License for more details.
21

    
22
You should have received a copy of the GNU General Public License
23
along with this program; if not, write to the Free Software
24
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
02110-1301, USA.
26

    
27
-}
28

    
29
module Test.Ganeti.Confd.Utils (testConfd_Utils) where
30

    
31
import Control.Applicative
32
import Test.QuickCheck
33
import qualified Text.JSON as J
34

    
35
import Test.Ganeti.TestHelper
36
import Test.Ganeti.TestCommon
37

    
38
import qualified Ganeti.BasicTypes as BasicTypes
39
import qualified Ganeti.Confd as Confd
40
import qualified Ganeti.Confd.Utils as Confd.Utils
41
import qualified Ganeti.Constants as C
42
import qualified Ganeti.Hash as Hash
43

    
44
$(genArbitrary ''Confd.ConfdRequestType)
45

    
46
$(genArbitrary ''Confd.ConfdReqField)
47

    
48
$(genArbitrary ''Confd.ConfdReqQ)
49

    
50
instance Arbitrary Confd.ConfdQuery where
51
  arbitrary = oneof [ pure Confd.EmptyQuery
52
                    , Confd.PlainQuery <$> getName
53
                    , Confd.DictQuery <$> arbitrary
54
                    ]
55

    
56
$(genArbitrary ''Confd.ConfdRequest)
57

    
58
-- | Test that signing messages and checking signatures is correct. It
59
-- also tests, indirectly the serialisation of messages so we don't
60
-- need a separate test for that.
61
prop_req_sign :: Hash.HashKey        -- ^ The hash key
62
              -> NonNegative Integer -- ^ The base timestamp
63
              -> Positive Integer    -- ^ Delta for out of window
64
              -> Bool                -- ^ Whether delta should be + or -
65
              -> Confd.ConfdRequest
66
              -> Property
67
prop_req_sign key (NonNegative timestamp) (Positive bad_delta)
68
                         pm crq =
69
  forAll (choose (0, fromIntegral C.confdMaxClockSkew)) $ \ good_delta ->
70
  let encoded = J.encode crq
71
      salt = show timestamp
72
      signed = J.encode $ Confd.Utils.signMessage key salt encoded
73
      good_timestamp = timestamp + if pm then good_delta else (-good_delta)
74
      bad_delta' = fromIntegral C.confdMaxClockSkew + bad_delta
75
      bad_timestamp = timestamp + if pm then bad_delta' else (-bad_delta')
76
      ts_ok = Confd.Utils.parseMessage key signed good_timestamp
77
      ts_bad = Confd.Utils.parseMessage key signed bad_timestamp
78
  in printTestCase "Failed to parse good message"
79
       (ts_ok ==? BasicTypes.Ok (encoded, crq)) .&&.
80
     printTestCase ("Managed to deserialise message with bad\
81
                    \ timestamp, got " ++ show ts_bad)
82
       (ts_bad ==? BasicTypes.Bad "Too old/too new timestamp or clock skew")
83

    
84
-- | Tests that signing with a different key fails detects failure
85
-- correctly.
86
prop_bad_key :: String             -- ^ Salt
87
             -> Confd.ConfdRequest -- ^ Request
88
             -> Property
89
prop_bad_key salt crq =
90
  -- fixme: we hardcode here the expected length of a sha1 key, as
91
  -- otherwise we could have two short keys that differ only in the
92
  -- final zero elements count, and those will be expanded to be the
93
  -- same
94
  forAll (vector 20) $ \key_sign ->
95
  forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify ->
96
  let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
97
      encoded = J.encode signed
98
  in printTestCase ("Accepted message signed with different key" ++ encoded) $
99
    BasicTypes.Bad "HMAC verification failed" ==?
100
     Confd.Utils.parseRequest key_verify encoded
101

    
102
testSuite "Confd/Utils"
103
  [ 'prop_req_sign
104
  , 'prop_bad_key
105
  ]