root / test / hs / Test / Ganeti / Confd / Utils.hs @ 14933c17
History | View | Annotate | Download (4.3 kB)
1 | 2733df51 | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | 2733df51 | Iustin Pop | {-# OPTIONS_GHC -fno-warn-orphans #-} |
3 | 2733df51 | Iustin Pop | |
4 | 2733df51 | Iustin Pop | {-| Unittests for ganeti-htools. |
5 | 2733df51 | Iustin Pop | |
6 | 2733df51 | Iustin Pop | -} |
7 | 2733df51 | Iustin Pop | |
8 | 2733df51 | Iustin Pop | {- |
9 | 2733df51 | Iustin Pop | |
10 | 2733df51 | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
11 | 2733df51 | Iustin Pop | |
12 | 2733df51 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
13 | 2733df51 | Iustin Pop | it under the terms of the GNU General Public License as published by |
14 | 2733df51 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
15 | 2733df51 | Iustin Pop | (at your option) any later version. |
16 | 2733df51 | Iustin Pop | |
17 | 2733df51 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
18 | 2733df51 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | 2733df51 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | 2733df51 | Iustin Pop | General Public License for more details. |
21 | 2733df51 | Iustin Pop | |
22 | 2733df51 | Iustin Pop | You should have received a copy of the GNU General Public License |
23 | 2733df51 | Iustin Pop | along with this program; if not, write to the Free Software |
24 | 2733df51 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | 2733df51 | Iustin Pop | 02110-1301, USA. |
26 | 2733df51 | Iustin Pop | |
27 | 2733df51 | Iustin Pop | -} |
28 | 2733df51 | Iustin Pop | |
29 | e09c1fa0 | Iustin Pop | module Test.Ganeti.Confd.Utils (testConfd_Utils) where |
30 | 2733df51 | Iustin Pop | |
31 | 2733df51 | Iustin Pop | import Test.QuickCheck |
32 | 2733df51 | Iustin Pop | import qualified Text.JSON as J |
33 | 2733df51 | Iustin Pop | |
34 | 2733df51 | Iustin Pop | import Test.Ganeti.TestHelper |
35 | 2733df51 | Iustin Pop | import Test.Ganeti.TestCommon |
36 | fef919b7 | Iustin Pop | import Test.Ganeti.Confd.Types () |
37 | 2733df51 | Iustin Pop | |
38 | 2733df51 | Iustin Pop | import qualified Ganeti.BasicTypes as BasicTypes |
39 | cdc2392b | Iustin Pop | import qualified Ganeti.Confd.Types as Confd |
40 | 2733df51 | Iustin Pop | import qualified Ganeti.Confd.Utils as Confd.Utils |
41 | 2733df51 | Iustin Pop | import qualified Ganeti.Constants as C |
42 | 2733df51 | Iustin Pop | import qualified Ganeti.Hash as Hash |
43 | 2733df51 | Iustin Pop | |
44 | 2733df51 | Iustin Pop | -- | Test that signing messages and checking signatures is correct. It |
45 | 2733df51 | Iustin Pop | -- also tests, indirectly the serialisation of messages so we don't |
46 | 2733df51 | Iustin Pop | -- need a separate test for that. |
47 | 20bc5360 | Iustin Pop | prop_req_sign :: Hash.HashKey -- ^ The hash key |
48 | 20bc5360 | Iustin Pop | -> NonNegative Integer -- ^ The base timestamp |
49 | 20bc5360 | Iustin Pop | -> Positive Integer -- ^ Delta for out of window |
50 | 20bc5360 | Iustin Pop | -> Bool -- ^ Whether delta should be + or - |
51 | 20bc5360 | Iustin Pop | -> Confd.ConfdRequest |
52 | 20bc5360 | Iustin Pop | -> Property |
53 | 20bc5360 | Iustin Pop | prop_req_sign key (NonNegative timestamp) (Positive bad_delta) |
54 | 2733df51 | Iustin Pop | pm crq = |
55 | 2733df51 | Iustin Pop | forAll (choose (0, fromIntegral C.confdMaxClockSkew)) $ \ good_delta -> |
56 | 2733df51 | Iustin Pop | let encoded = J.encode crq |
57 | 2733df51 | Iustin Pop | salt = show timestamp |
58 | 2733df51 | Iustin Pop | signed = J.encode $ Confd.Utils.signMessage key salt encoded |
59 | 2733df51 | Iustin Pop | good_timestamp = timestamp + if pm then good_delta else (-good_delta) |
60 | 2733df51 | Iustin Pop | bad_delta' = fromIntegral C.confdMaxClockSkew + bad_delta |
61 | 2733df51 | Iustin Pop | bad_timestamp = timestamp + if pm then bad_delta' else (-bad_delta') |
62 | 497f5cbf | Michele Tartara | ts_ok = Confd.Utils.parseRequest key signed good_timestamp |
63 | 497f5cbf | Michele Tartara | ts_bad = Confd.Utils.parseRequest key signed bad_timestamp |
64 | 2733df51 | Iustin Pop | in printTestCase "Failed to parse good message" |
65 | 2733df51 | Iustin Pop | (ts_ok ==? BasicTypes.Ok (encoded, crq)) .&&. |
66 | 2733df51 | Iustin Pop | printTestCase ("Managed to deserialise message with bad\ |
67 | 2733df51 | Iustin Pop | \ timestamp, got " ++ show ts_bad) |
68 | 2733df51 | Iustin Pop | (ts_bad ==? BasicTypes.Bad "Too old/too new timestamp or clock skew") |
69 | 2733df51 | Iustin Pop | |
70 | 1a0defea | Michele Tartara | -- | Tests that a ConfdReply can be properly encoded, signed and parsed using |
71 | 1a0defea | Michele Tartara | -- the proper salt, but fails parsing with the wrong salt. |
72 | 1a0defea | Michele Tartara | prop_rep_salt :: Hash.HashKey -- ^ The hash key |
73 | 1a0defea | Michele Tartara | -> Confd.ConfdReply -- ^ A Confd reply |
74 | 1a0defea | Michele Tartara | -> Property |
75 | 1a0defea | Michele Tartara | prop_rep_salt hmac reply = |
76 | 1a0defea | Michele Tartara | forAll arbitrary $ \salt1 -> |
77 | 1a0defea | Michele Tartara | forAll (arbitrary `suchThat` (/= salt1)) $ \salt2 -> |
78 | 1a0defea | Michele Tartara | let innerMsg = J.encode reply |
79 | 1a0defea | Michele Tartara | msg = J.encode $ Confd.Utils.signMessage hmac salt1 innerMsg |
80 | 1a0defea | Michele Tartara | in |
81 | 1a0defea | Michele Tartara | Confd.Utils.parseReply hmac msg salt1 ==? BasicTypes.Ok (innerMsg, reply) |
82 | 1a0defea | Michele Tartara | .&&. Confd.Utils.parseReply hmac msg salt2 ==? |
83 | 1a0defea | Michele Tartara | BasicTypes.Bad "The received salt differs from the expected salt" |
84 | 1a0defea | Michele Tartara | |
85 | 2733df51 | Iustin Pop | -- | Tests that signing with a different key fails detects failure |
86 | 2733df51 | Iustin Pop | -- correctly. |
87 | 20bc5360 | Iustin Pop | prop_bad_key :: String -- ^ Salt |
88 | 20bc5360 | Iustin Pop | -> Confd.ConfdRequest -- ^ Request |
89 | 20bc5360 | Iustin Pop | -> Property |
90 | 20bc5360 | Iustin Pop | prop_bad_key salt crq = |
91 | 2733df51 | Iustin Pop | -- fixme: we hardcode here the expected length of a sha1 key, as |
92 | 2733df51 | Iustin Pop | -- otherwise we could have two short keys that differ only in the |
93 | 2733df51 | Iustin Pop | -- final zero elements count, and those will be expanded to be the |
94 | 2733df51 | Iustin Pop | -- same |
95 | 2733df51 | Iustin Pop | forAll (vector 20) $ \key_sign -> |
96 | 2733df51 | Iustin Pop | forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify -> |
97 | 2733df51 | Iustin Pop | let signed = Confd.Utils.signMessage key_sign salt (J.encode crq) |
98 | 2733df51 | Iustin Pop | encoded = J.encode signed |
99 | 2733df51 | Iustin Pop | in printTestCase ("Accepted message signed with different key" ++ encoded) $ |
100 | 5bfcd75f | Michele Tartara | (Confd.Utils.parseSignedMessage key_verify encoded |
101 | 5bfcd75f | Michele Tartara | :: BasicTypes.Result (String, String, Confd.ConfdRequest)) ==? |
102 | 41eb900e | Iustin Pop | BasicTypes.Bad "HMAC verification failed" |
103 | 2733df51 | Iustin Pop | |
104 | e09c1fa0 | Iustin Pop | testSuite "Confd/Utils" |
105 | 20bc5360 | Iustin Pop | [ 'prop_req_sign |
106 | 1a0defea | Michele Tartara | , 'prop_rep_salt |
107 | 20bc5360 | Iustin Pop | , 'prop_bad_key |
108 | 2733df51 | Iustin Pop | ] |