Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Confd / Utils.hs @ 497f5cbf

History | View | Annotate | Download (3.6 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 Test.QuickCheck
32
import qualified Text.JSON as J
33

    
34
import Test.Ganeti.TestHelper
35
import Test.Ganeti.TestCommon
36
import Test.Ganeti.Confd.Types ()
37

    
38
import qualified Ganeti.BasicTypes as BasicTypes
39
import qualified Ganeti.Confd.Types 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
-- | Test that signing messages and checking signatures is correct. It
45
-- also tests, indirectly the serialisation of messages so we don't
46
-- need a separate test for that.
47
prop_req_sign :: Hash.HashKey        -- ^ The hash key
48
              -> NonNegative Integer -- ^ The base timestamp
49
              -> Positive Integer    -- ^ Delta for out of window
50
              -> Bool                -- ^ Whether delta should be + or -
51
              -> Confd.ConfdRequest
52
              -> Property
53
prop_req_sign key (NonNegative timestamp) (Positive bad_delta)
54
                         pm crq =
55
  forAll (choose (0, fromIntegral C.confdMaxClockSkew)) $ \ good_delta ->
56
  let encoded = J.encode crq
57
      salt = show timestamp
58
      signed = J.encode $ Confd.Utils.signMessage key salt encoded
59
      good_timestamp = timestamp + if pm then good_delta else (-good_delta)
60
      bad_delta' = fromIntegral C.confdMaxClockSkew + bad_delta
61
      bad_timestamp = timestamp + if pm then bad_delta' else (-bad_delta')
62
      ts_ok = Confd.Utils.parseRequest key signed good_timestamp
63
      ts_bad = Confd.Utils.parseRequest key signed bad_timestamp
64
  in printTestCase "Failed to parse good message"
65
       (ts_ok ==? BasicTypes.Ok (encoded, crq)) .&&.
66
     printTestCase ("Managed to deserialise message with bad\
67
                    \ timestamp, got " ++ show ts_bad)
68
       (ts_bad ==? BasicTypes.Bad "Too old/too new timestamp or clock skew")
69

    
70
-- | Tests that signing with a different key fails detects failure
71
-- correctly.
72
prop_bad_key :: String             -- ^ Salt
73
             -> Confd.ConfdRequest -- ^ Request
74
             -> Property
75
prop_bad_key salt crq =
76
  -- fixme: we hardcode here the expected length of a sha1 key, as
77
  -- otherwise we could have two short keys that differ only in the
78
  -- final zero elements count, and those will be expanded to be the
79
  -- same
80
  forAll (vector 20) $ \key_sign ->
81
  forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify ->
82
  let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
83
      encoded = J.encode signed
84
  in printTestCase ("Accepted message signed with different key" ++ encoded) $
85
     (Confd.Utils.parseSignedMessage key_verify encoded
86
      :: BasicTypes.Result (String, String, Confd.ConfdRequest)) ==?
87
       BasicTypes.Bad "HMAC verification failed"
88

    
89
testSuite "Confd/Utils"
90
  [ 'prop_req_sign
91
  , 'prop_bad_key
92
  ]