Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.serializer_unittest.py @ f4a2f532

History | View | Annotate | Download (3.6 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

    
31
class SimplejsonMock(object):
32
  def dumps(self, data, indent=None):
33
    return repr(data)
34

    
35
  def loads(self, data):
36
    return eval(data)
37

    
38

    
39
class TestSerializer(unittest.TestCase):
40
  """Serializer tests"""
41

    
42
  _TESTDATA = [
43
    "test",
44
    255,
45
    [1, 2, 3],
46
    (1, 2, 3),
47
    { 1: 2, "foo": "bar", },
48
    ]
49

    
50
  def setUp(self):
51
    self._orig_simplejson = serializer.simplejson
52
    serializer.simplejson = SimplejsonMock()
53

    
54
  def tearDown(self):
55
    serializer.simplejson = self._orig_simplejson
56

    
57
  def testGeneric(self):
58
    return self._TestSerializer(serializer.Dump, serializer.Load)
59

    
60
  def testJson(self):
61
    return self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
62

    
63
  def testSignedMessage(self):
64
    LoadSigned = serializer.LoadSigned
65
    DumpSigned = serializer.DumpSigned
66
    SaltEqualTo = serializer.SaltEqualTo
67
    SaltIn = serializer.SaltIn
68
    SaltInRange = serializer.SaltInRange
69

    
70
    for data in self._TESTDATA:
71
      self.assertEqual(LoadSigned(DumpSigned(data, "mykey"), "mykey"), data)
72
      self.assertEqual(LoadSigned(
73
                         DumpSigned(data, "myprivatekey", "mysalt"),
74
                         "myprivatekey", SaltEqualTo("mysalt")), data)
75
      self.assertEqual(LoadSigned(
76
                         DumpSigned(data, "myprivatekey", "mysalt"),
77
                         "myprivatekey", SaltIn(["notmysalt", "mysalt"])), data)
78
      self.assertEqual(LoadSigned(
79
                         DumpSigned(data, "myprivatekey", "12345"),
80
                         "myprivatekey", SaltInRange(12340, 12346)), data)
81
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
82
                      serializer.DumpSigned("test", "myprivatekey"),
83
                      "myotherkey")
84
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
85
                      serializer.DumpSigned("test", "myprivatekey", "salt"),
86
                      "myprivatekey")
87
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
88
                      serializer.DumpSigned("test", "myprivatekey", "salt"),
89
                      "myprivatekey", SaltIn(["notmysalt", "morenotmysalt"]))
90
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
91
                      serializer.DumpSigned("test", "myprivatekey", "salt"),
92
                      "myprivatekey", SaltInRange(1, 2))
93
    self.assertRaises(errors.SignatureError, serializer.LoadSigned,
94
                      serializer.DumpSigned("test", "myprivatekey", "12345"),
95
                      "myprivatekey", SaltInRange(1, 2))
96

    
97
  def _TestSerializer(self, dump_fn, load_fn):
98
    for data in self._TESTDATA:
99
      self.failUnless(dump_fn(data).endswith("\n"))
100
      self.failUnlessEqual(load_fn(dump_fn(data)), data)
101

    
102

    
103
if __name__ == '__main__':
104
  unittest.main()