Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.serializer_unittest.py @ 72ca1dcb

History | View | Annotate | Download (2.8 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
# 0.0510-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

    
30
import testutils
31

    
32

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

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

    
54
  def _TestSerializer(self, dump_fn, load_fn):
55
    for indent in [True, False]:
56
      for data in self._TESTDATA:
57
        self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
58
        self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
59

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

    
63
  def testJson(self):
64
    self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
65

    
66
  def testSignedMessage(self):
67
    LoadSigned = serializer.LoadSigned
68
    DumpSigned = serializer.DumpSigned
69

    
70
    for data in self._TESTDATA:
71
      self.assertEqualValues(LoadSigned(DumpSigned(data, "mykey"), "mykey"),
72
                             (data, ''))
73
      self.assertEqualValues(LoadSigned(DumpSigned(data, "myprivatekey",
74
                                                   salt="mysalt"),
75
                                        "myprivatekey"),
76
                             (data, "mysalt"))
77

    
78
      keydict = {"mykey_id": "myprivatekey"}
79
      self.assertEqualValues(LoadSigned(DumpSigned(data,
80
                                                   "myprivatekey",
81
                                                   salt="mysalt",
82
                                                   key_selector="mykey_id"),
83
                                        keydict.get),
84
                             (data, "mysalt"))
85

    
86
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
87
                      serializer.DumpSigned("test", "myprivatekey"),
88
                      "myotherkey")
89

    
90

    
91
if __name__ == '__main__':
92
  testutils.GanetiTestProgram()