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"""
27 from ganeti import serializer
28 from ganeti import errors
33 class TestSerializer(testutils.GanetiTestCase):
34 """Serializer tests"""
41 { "1": 2, "foo": "bar", },
44 "a1": ("Hello", "World"),
45 "a2": "This is only a test",
54 def _TestSerializer(self, dump_fn, load_fn):
55 for data in self._TESTDATA:
56 self.failUnless(dump_fn(data).endswith("\n"))
57 self.assertEqualValues(load_fn(dump_fn(data)), data)
59 def testGeneric(self):
60 self._TestSerializer(serializer.Dump, serializer.Load)
62 def testSignedGeneric(self):
63 self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
66 self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
68 def testSignedJson(self):
69 self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
71 def _TestSigned(self, dump_fn, load_fn):
72 for data in self._TESTDATA:
73 self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"),
75 self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
81 "mykey_id": "myprivatekey",
83 self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
85 key_selector="mykey_id"),
88 self.assertRaises(errors.SignatureError, load_fn,
89 dump_fn(data, "myprivatekey",
91 key_selector="mykey_id"),
94 self.assertRaises(errors.SignatureError, load_fn,
95 dump_fn("test", "myprivatekey"),
98 self.assertRaises(errors.SignatureError, load_fn,
99 serializer.DumpJson("This is a test"), "mykey")
100 self.assertRaises(errors.SignatureError, load_fn,
101 serializer.DumpJson({}), "mykey")
103 # Message missing salt and HMAC
104 tdata = { "msg": "Foo", }
105 self.assertRaises(errors.SignatureError, load_fn,
106 serializer.DumpJson(tdata), "mykey")
109 if __name__ == '__main__':
110 testutils.GanetiTestProgram()