Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / Confd / Utils.hs @ 5006418e

History | View | Annotate | Download (3.9 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 Control.Applicative
32 2733df51 Iustin Pop
import Test.QuickCheck
33 2733df51 Iustin Pop
import qualified Text.JSON as J
34 2733df51 Iustin Pop
35 2733df51 Iustin Pop
import Test.Ganeti.TestHelper
36 2733df51 Iustin Pop
import Test.Ganeti.TestCommon
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 7022db83 Iustin Pop
$(genArbitrary ''Confd.ConfdRequestType)
45 2733df51 Iustin Pop
46 7022db83 Iustin Pop
$(genArbitrary ''Confd.ConfdReqField)
47 2733df51 Iustin Pop
48 7022db83 Iustin Pop
$(genArbitrary ''Confd.ConfdReqQ)
49 2733df51 Iustin Pop
50 2733df51 Iustin Pop
instance Arbitrary Confd.ConfdQuery where
51 2733df51 Iustin Pop
  arbitrary = oneof [ pure Confd.EmptyQuery
52 5006418e Iustin Pop
                    , Confd.PlainQuery <$> genName
53 2733df51 Iustin Pop
                    , Confd.DictQuery <$> arbitrary
54 2733df51 Iustin Pop
                    ]
55 2733df51 Iustin Pop
56 7022db83 Iustin Pop
$(genArbitrary ''Confd.ConfdRequest)
57 2733df51 Iustin Pop
58 2733df51 Iustin Pop
-- | Test that signing messages and checking signatures is correct. It
59 2733df51 Iustin Pop
-- also tests, indirectly the serialisation of messages so we don't
60 2733df51 Iustin Pop
-- need a separate test for that.
61 20bc5360 Iustin Pop
prop_req_sign :: Hash.HashKey        -- ^ The hash key
62 20bc5360 Iustin Pop
              -> NonNegative Integer -- ^ The base timestamp
63 20bc5360 Iustin Pop
              -> Positive Integer    -- ^ Delta for out of window
64 20bc5360 Iustin Pop
              -> Bool                -- ^ Whether delta should be + or -
65 20bc5360 Iustin Pop
              -> Confd.ConfdRequest
66 20bc5360 Iustin Pop
              -> Property
67 20bc5360 Iustin Pop
prop_req_sign key (NonNegative timestamp) (Positive bad_delta)
68 2733df51 Iustin Pop
                         pm crq =
69 2733df51 Iustin Pop
  forAll (choose (0, fromIntegral C.confdMaxClockSkew)) $ \ good_delta ->
70 2733df51 Iustin Pop
  let encoded = J.encode crq
71 2733df51 Iustin Pop
      salt = show timestamp
72 2733df51 Iustin Pop
      signed = J.encode $ Confd.Utils.signMessage key salt encoded
73 2733df51 Iustin Pop
      good_timestamp = timestamp + if pm then good_delta else (-good_delta)
74 2733df51 Iustin Pop
      bad_delta' = fromIntegral C.confdMaxClockSkew + bad_delta
75 2733df51 Iustin Pop
      bad_timestamp = timestamp + if pm then bad_delta' else (-bad_delta')
76 2733df51 Iustin Pop
      ts_ok = Confd.Utils.parseMessage key signed good_timestamp
77 2733df51 Iustin Pop
      ts_bad = Confd.Utils.parseMessage key signed bad_timestamp
78 2733df51 Iustin Pop
  in printTestCase "Failed to parse good message"
79 2733df51 Iustin Pop
       (ts_ok ==? BasicTypes.Ok (encoded, crq)) .&&.
80 2733df51 Iustin Pop
     printTestCase ("Managed to deserialise message with bad\
81 2733df51 Iustin Pop
                    \ timestamp, got " ++ show ts_bad)
82 2733df51 Iustin Pop
       (ts_bad ==? BasicTypes.Bad "Too old/too new timestamp or clock skew")
83 2733df51 Iustin Pop
84 2733df51 Iustin Pop
-- | Tests that signing with a different key fails detects failure
85 2733df51 Iustin Pop
-- correctly.
86 20bc5360 Iustin Pop
prop_bad_key :: String             -- ^ Salt
87 20bc5360 Iustin Pop
             -> Confd.ConfdRequest -- ^ Request
88 20bc5360 Iustin Pop
             -> Property
89 20bc5360 Iustin Pop
prop_bad_key salt crq =
90 2733df51 Iustin Pop
  -- fixme: we hardcode here the expected length of a sha1 key, as
91 2733df51 Iustin Pop
  -- otherwise we could have two short keys that differ only in the
92 2733df51 Iustin Pop
  -- final zero elements count, and those will be expanded to be the
93 2733df51 Iustin Pop
  -- same
94 2733df51 Iustin Pop
  forAll (vector 20) $ \key_sign ->
95 2733df51 Iustin Pop
  forAll (vector 20 `suchThat` (/= key_sign)) $ \key_verify ->
96 2733df51 Iustin Pop
  let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
97 2733df51 Iustin Pop
      encoded = J.encode signed
98 2733df51 Iustin Pop
  in printTestCase ("Accepted message signed with different key" ++ encoded) $
99 41eb900e Iustin Pop
     Confd.Utils.parseRequest key_verify encoded ==?
100 41eb900e Iustin Pop
       BasicTypes.Bad "HMAC verification failed"
101 2733df51 Iustin Pop
102 e09c1fa0 Iustin Pop
testSuite "Confd/Utils"
103 20bc5360 Iustin Pop
  [ 'prop_req_sign
104 20bc5360 Iustin Pop
  , 'prop_bad_key
105 2733df51 Iustin Pop
  ]