Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Confd / Utils.hs @ e09c1fa0

History | View | Annotate | Download (4.2 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
instance Arbitrary Confd.ConfdRequestType where
45
  arbitrary = elements [minBound..maxBound]
46

    
47
instance Arbitrary Confd.ConfdReqField where
48
  arbitrary = elements [minBound..maxBound]
49

    
50
instance Arbitrary Confd.ConfdReqQ where
51
  arbitrary = Confd.ConfdReqQ <$> arbitrary <*> arbitrary <*>
52
              arbitrary <*> arbitrary
53

    
54
instance Arbitrary Confd.ConfdQuery where
55
  arbitrary = oneof [ pure Confd.EmptyQuery
56
                    , Confd.PlainQuery <$> getName
57
                    , Confd.DictQuery <$> arbitrary
58
                    ]
59

    
60
instance Arbitrary Confd.ConfdRequest where
61
  arbitrary = Confd.ConfdRequest <$> arbitrary <*> arbitrary <*> arbitrary
62
              <*> arbitrary
63

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

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

    
108
testSuite "Confd/Utils"
109
  [ 'prop_req_sign
110
  , 'prop_bad_key
111
  ]