Move python test files to test/py
[ganeti-local] / htest / Test / Ganeti / Confd / Utils.hs
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 a ConfdReply can be properly encoded, signed and parsed using
71 -- the proper salt, but fails parsing with the wrong salt.
72 prop_rep_salt :: Hash.HashKey     -- ^ The hash key
73               -> Confd.ConfdReply -- ^ A Confd reply
74               -> Property
75 prop_rep_salt hmac reply =
76   forAll arbitrary $ \salt1 ->
77   forAll (arbitrary `suchThat` (/= salt1)) $ \salt2 ->
78   let innerMsg = J.encode reply
79       msg = J.encode $ Confd.Utils.signMessage hmac salt1 innerMsg
80   in
81     Confd.Utils.parseReply hmac msg salt1 ==? BasicTypes.Ok (innerMsg, reply)
82       .&&. Confd.Utils.parseReply hmac msg salt2 ==?
83            BasicTypes.Bad "The received salt differs from the expected salt"
84
85 -- | Tests that signing with a different key fails detects failure
86 -- correctly.
87 prop_bad_key :: String             -- ^ Salt
88              -> Confd.ConfdRequest -- ^ Request
89              -> Property
90 prop_bad_key salt crq =
91   -- fixme: we hardcode here the expected length of a sha1 key, as
92   -- otherwise we could have two short keys that differ only in the
93   -- final zero elements count, and those will be expanded to be the
94   -- same
95   forAll (vector 20) $ \key_sign ->
96   forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify ->
97   let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
98       encoded = J.encode signed
99   in printTestCase ("Accepted message signed with different key" ++ encoded) $
100      (Confd.Utils.parseSignedMessage key_verify encoded
101       :: BasicTypes.Result (String, String, Confd.ConfdRequest)) ==?
102        BasicTypes.Bad "HMAC verification failed"
103
104 testSuite "Confd/Utils"
105   [ 'prop_req_sign
106   , 'prop_rep_salt
107   , 'prop_bad_key
108   ]