Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.serializer_unittest.py @ 91c17910

History | View | Annotate | Download (4.3 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for unittesting the serializer module"""
23

    
24

    
25
import unittest
26

    
27
from ganeti import serializer
28
from ganeti import errors
29
from ganeti import ht
30

    
31
import testutils
32

    
33

    
34
class TestSerializer(testutils.GanetiTestCase):
35
  """Serializer tests"""
36

    
37
  _TESTDATA = [
38
    "test",
39
    255,
40
    [1, 2, 3],
41
    (1, 2, 3),
42
    { "1": 2, "foo": "bar", },
43
    ["abc", 1, 2, 3, 999,
44
      {
45
        "a1": ("Hello", "World"),
46
        "a2": "This is only a test",
47
        "a3": None,
48
        },
49
      {
50
        "foo": "bar",
51
        },
52
      ]
53
    ]
54

    
55
  def _TestSerializer(self, dump_fn, load_fn):
56
    for data in self._TESTDATA:
57
      self.failUnless(dump_fn(data).endswith("\n"))
58
      self.assertEqualValues(load_fn(dump_fn(data)), data)
59

    
60
  def testGeneric(self):
61
    self._TestSerializer(serializer.Dump, serializer.Load)
62

    
63
  def testSignedGeneric(self):
64
    self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
65

    
66
  def testJson(self):
67
    self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
68

    
69
  def testSignedJson(self):
70
    self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
71

    
72
  def _TestSigned(self, dump_fn, load_fn):
73
    for data in self._TESTDATA:
74
      self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"),
75
                             (data, ""))
76
      self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
77
                                             salt="mysalt"),
78
                                     "myprivatekey"),
79
                             (data, "mysalt"))
80

    
81
      keydict = {
82
        "mykey_id": "myprivatekey",
83
        }
84
      self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
85
                                             salt="mysalt",
86
                                             key_selector="mykey_id"),
87
                                     keydict.get),
88
                             (data, "mysalt"))
89
      self.assertRaises(errors.SignatureError, load_fn,
90
                        dump_fn(data, "myprivatekey",
91
                                salt="mysalt",
92
                                key_selector="mykey_id"),
93
                        {}.get)
94

    
95
    self.assertRaises(errors.SignatureError, load_fn,
96
                      dump_fn("test", "myprivatekey"),
97
                      "myotherkey")
98

    
99
    self.assertRaises(errors.SignatureError, load_fn,
100
                      serializer.DumpJson("This is a test"), "mykey")
101
    self.assertRaises(errors.SignatureError, load_fn,
102
                      serializer.DumpJson({}), "mykey")
103

    
104
    # Message missing salt and HMAC
105
    tdata = { "msg": "Foo", }
106
    self.assertRaises(errors.SignatureError, load_fn,
107
                      serializer.DumpJson(tdata), "mykey")
108

    
109

    
110
class TestLoadAndVerifyJson(unittest.TestCase):
111
  def testNoJson(self):
112
    self.assertRaises(errors.ParseError, serializer.LoadAndVerifyJson,
113
                      "", NotImplemented)
114
    self.assertRaises(errors.ParseError, serializer.LoadAndVerifyJson,
115
                      "}", NotImplemented)
116

    
117
  def testVerificationFails(self):
118
    self.assertRaises(errors.ParseError, serializer.LoadAndVerifyJson,
119
                      "{}", lambda _: False)
120

    
121
    verify_fn = ht.TListOf(ht.TNonEmptyString)
122
    try:
123
      serializer.LoadAndVerifyJson("{}", verify_fn)
124
    except errors.ParseError, err:
125
      self.assertTrue(str(err).endswith(str(verify_fn)))
126
    else:
127
      self.fail("Exception not raised")
128

    
129
  def testSuccess(self):
130
    self.assertEqual(serializer.LoadAndVerifyJson("{}", ht.TAny), {})
131
    self.assertEqual(serializer.LoadAndVerifyJson("\"Foo\"", ht.TAny), "Foo")
132

    
133

    
134
if __name__ == "__main__":
135
  testutils.GanetiTestProgram()