4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 """Script for unittesting the serializer module"""
28 from ganeti import serializer
29 from ganeti import errors
30 from ganeti import compat
35 class TestSerializer(testutils.GanetiTestCase):
36 """Serializer tests"""
43 { "1": 2, "foo": "bar", },
46 "a1": ("Hello", "World"),
47 "a2": "This is only a test",
56 def _TestSerializer(self, dump_fn, load_fn):
57 for indent in [True, False]:
58 for data in self._TESTDATA:
59 self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
60 self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
62 def testGeneric(self):
63 self._TestSerializer(serializer.Dump, serializer.Load)
65 def testSignedGeneric(self):
66 self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
69 self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
71 def testSignedJson(self):
72 self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
74 def _TestSigned(self, dump_fn, load_fn):
75 for data in self._TESTDATA:
76 self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"),
78 self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
84 "mykey_id": "myprivatekey",
86 self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
88 key_selector="mykey_id"),
91 self.assertRaises(errors.SignatureError, load_fn,
92 dump_fn(data, "myprivatekey",
94 key_selector="mykey_id"),
97 self.assertRaises(errors.SignatureError, load_fn,
98 dump_fn("test", "myprivatekey"),
101 self.assertRaises(errors.SignatureError, load_fn,
102 serializer.DumpJson("This is a test"), "mykey")
103 self.assertRaises(errors.SignatureError, load_fn,
104 serializer.DumpJson({}), "mykey")
106 # Message missing salt and HMAC
107 tdata = { "msg": "Foo", }
108 self.assertRaises(errors.SignatureError, load_fn,
109 serializer.DumpJson(tdata), "mykey")
112 class TestInvalidDictionaryKey(unittest.TestCase):
113 def _Test(self, data):
114 if serializer._OLD_SIMPLEJSON:
115 # Using old "simplejson", can't really test
116 warnings.warn("This test requires Python 2.6 or above to function"
118 self.assertTrue(serializer.DumpJson(data))
120 self.assertRaises(ValueError, serializer.DumpJson, data)
123 for value in [123, 1.1, -1, -9492.1123, -3234e-4]:
124 self._Test({value: ""})
126 def testAllowed(self):
127 for value in ["", "Hello World", None, True, False]:
128 self.assertTrue(serializer.DumpJson({value: ""}))
131 if __name__ == '__main__':
132 testutils.GanetiTestProgram()