Debian Codegraph @

test/hs/Test/Ganeti/Confd/Utils.hs

raw
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 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Confd.Utils (testConfd_Utils) where
39
40 import Test.QuickCheck
41 import qualified Text.JSON as J
42
43 import Test.Ganeti.TestHelper
44 import Test.Ganeti.TestCommon
45 import Test.Ganeti.Confd.Types ()
46
47 import qualified Ganeti.BasicTypes as BasicTypes
48 import qualified Ganeti.Confd.Types as Confd
49 import qualified Ganeti.Confd.Utils as Confd.Utils
50 import qualified Ganeti.Constants as C
51 import qualified Ganeti.Hash as Hash
52
53 -- | Test that signing messages and checking signatures is correct. It
54 -- also tests, indirectly the serialisation of messages so we don't
55 -- need a separate test for that.
56 prop_req_sign :: Hash.HashKey -- ^ The hash key
57 -> NonNegative Integer -- ^ The base timestamp
58 -> Positive Integer -- ^ Delta for out of window
59 -> Bool -- ^ Whether delta should be + or -
60 -> Confd.ConfdRequest
61 -> Property
62 prop_req_sign key (NonNegative timestamp) (Positive bad_delta)
63 pm crq =
64 forAll (choose (0, fromIntegral C.confdMaxClockSkew)) $ \ good_delta ->
65 let encoded = J.encode crq
66 salt = show timestamp
67 signed = J.encode $ Confd.Utils.signMessage key salt encoded
68 good_timestamp = timestamp + if pm then good_delta else (-good_delta)
69 bad_delta' = fromIntegral C.confdMaxClockSkew + bad_delta
70 bad_timestamp = timestamp + if pm then bad_delta' else (-bad_delta')
71 ts_ok = Confd.Utils.parseRequest key signed good_timestamp
72 ts_bad = Confd.Utils.parseRequest key signed bad_timestamp
73 in counterexample "Failed to parse good message"
74 (ts_ok ==? BasicTypes.Ok (encoded, crq)) .&&.
75 counterexample ("Managed to deserialise message with bad\
76 \ timestamp, got " ++ show ts_bad)
77 (ts_bad ==? BasicTypes.Bad "Too old/too new timestamp or clock skew")
78
79 -- | Tests that a ConfdReply can be properly encoded, signed and parsed using
80 -- the proper salt, but fails parsing with the wrong salt.
81 prop_rep_salt :: Hash.HashKey -- ^ The hash key
82 -> Confd.ConfdReply -- ^ A Confd reply
83 -> Property
84 prop_rep_salt hmac reply =
85 forAll arbitrary $ \salt1 ->
86 forAll (arbitrary `suchThat` (/= salt1)) $ \salt2 ->
87 let innerMsg = J.encode reply
88 msg = J.encode $ Confd.Utils.signMessage hmac salt1 innerMsg
89 in
90 Confd.Utils.parseReply hmac msg salt1 ==? BasicTypes.Ok (innerMsg, reply)
91 .&&. Confd.Utils.parseReply hmac msg salt2 ==?
92 BasicTypes.Bad "The received salt differs from the expected salt"
93
94 -- | Tests that signing with a different key fails detects failure
95 -- correctly.
96 prop_bad_key :: String -- ^ Salt
97 -> Confd.ConfdRequest -- ^ Request
98 -> Property
99 prop_bad_key salt crq =
100 -- fixme: we hardcode here the expected length of a sha1 key, as
101 -- otherwise we could have two short keys that differ only in the
102 -- final zero elements count, and those will be expanded to be the
103 -- same
104 forAll (vector 20) $ \key_sign ->
105 forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify ->
106 let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
107 encoded = J.encode signed
108 in counterexample ("Accepted message signed with different key" ++ encoded) $
109 (Confd.Utils.parseSignedMessage key_verify encoded
110 :: BasicTypes.Result (String, String, Confd.ConfdRequest)) ==?
111 BasicTypes.Bad "HMAC verification failed"
112
113 testSuite "Confd/Utils"
114 [ 'prop_req_sign
115 , 'prop_rep_salt
116 , 'prop_bad_key
117 ]