root / test / ganeti.serializer_unittest.py @ 5aab242c
History | View | Annotate | Download (2.4 kB)
1 | 501c95a2 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 501c95a2 | Michael Hanselmann | #
|
3 | 501c95a2 | Michael Hanselmann | |
4 | 501c95a2 | Michael Hanselmann | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | 501c95a2 | Michael Hanselmann | #
|
6 | 501c95a2 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 501c95a2 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 501c95a2 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 501c95a2 | Michael Hanselmann | # (at your option) any later version.
|
10 | 501c95a2 | Michael Hanselmann | #
|
11 | 501c95a2 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 501c95a2 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 501c95a2 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 501c95a2 | Michael Hanselmann | # General Public License for more details.
|
15 | 501c95a2 | Michael Hanselmann | #
|
16 | 501c95a2 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 501c95a2 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 501c95a2 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 501c95a2 | Michael Hanselmann | # 0.0510-1301, USA.
|
20 | 501c95a2 | Michael Hanselmann | |
21 | 501c95a2 | Michael Hanselmann | |
22 | 501c95a2 | Michael Hanselmann | """Script for unittesting the serializer module"""
|
23 | 501c95a2 | Michael Hanselmann | |
24 | 501c95a2 | Michael Hanselmann | |
25 | 501c95a2 | Michael Hanselmann | import unittest |
26 | 501c95a2 | Michael Hanselmann | |
27 | 501c95a2 | Michael Hanselmann | from ganeti import serializer |
28 | f4a2f532 | Guido Trotter | from ganeti import errors |
29 | 501c95a2 | Michael Hanselmann | |
30 | 501c95a2 | Michael Hanselmann | |
31 | 501c95a2 | Michael Hanselmann | class SimplejsonMock(object): |
32 | 501c95a2 | Michael Hanselmann | def dumps(self, data, indent=None): |
33 | 501c95a2 | Michael Hanselmann | return repr(data) |
34 | 501c95a2 | Michael Hanselmann | |
35 | 501c95a2 | Michael Hanselmann | def loads(self, data): |
36 | 501c95a2 | Michael Hanselmann | return eval(data) |
37 | 501c95a2 | Michael Hanselmann | |
38 | 501c95a2 | Michael Hanselmann | |
39 | 501c95a2 | Michael Hanselmann | class TestSerializer(unittest.TestCase): |
40 | 501c95a2 | Michael Hanselmann | """Serializer tests"""
|
41 | 501c95a2 | Michael Hanselmann | |
42 | 501c95a2 | Michael Hanselmann | _TESTDATA = [ |
43 | 501c95a2 | Michael Hanselmann | "test",
|
44 | 501c95a2 | Michael Hanselmann | 255,
|
45 | 501c95a2 | Michael Hanselmann | [1, 2, 3], |
46 | 501c95a2 | Michael Hanselmann | (1, 2, 3), |
47 | 501c95a2 | Michael Hanselmann | { 1: 2, "foo": "bar", }, |
48 | 501c95a2 | Michael Hanselmann | ] |
49 | 501c95a2 | Michael Hanselmann | |
50 | 501c95a2 | Michael Hanselmann | def setUp(self): |
51 | 501c95a2 | Michael Hanselmann | self._orig_simplejson = serializer.simplejson
|
52 | 501c95a2 | Michael Hanselmann | serializer.simplejson = SimplejsonMock() |
53 | 501c95a2 | Michael Hanselmann | |
54 | 501c95a2 | Michael Hanselmann | def tearDown(self): |
55 | 501c95a2 | Michael Hanselmann | serializer.simplejson = self._orig_simplejson
|
56 | 501c95a2 | Michael Hanselmann | |
57 | 501c95a2 | Michael Hanselmann | def testGeneric(self): |
58 | 501c95a2 | Michael Hanselmann | return self._TestSerializer(serializer.Dump, serializer.Load) |
59 | 501c95a2 | Michael Hanselmann | |
60 | 501c95a2 | Michael Hanselmann | def testJson(self): |
61 | 501c95a2 | Michael Hanselmann | return self._TestSerializer(serializer.DumpJson, serializer.LoadJson) |
62 | 501c95a2 | Michael Hanselmann | |
63 | f4a2f532 | Guido Trotter | def testSignedMessage(self): |
64 | f4a2f532 | Guido Trotter | LoadSigned = serializer.LoadSigned |
65 | f4a2f532 | Guido Trotter | DumpSigned = serializer.DumpSigned |
66 | f4a2f532 | Guido Trotter | |
67 | f4a2f532 | Guido Trotter | for data in self._TESTDATA: |
68 | 4e9dac14 | Guido Trotter | self.assertEqual(LoadSigned(DumpSigned(data, "mykey"), "mykey"), |
69 | 4e9dac14 | Guido Trotter | (data, ''))
|
70 | f4a2f532 | Guido Trotter | self.assertEqual(LoadSigned(
|
71 | f4a2f532 | Guido Trotter | DumpSigned(data, "myprivatekey", "mysalt"), |
72 | 4e9dac14 | Guido Trotter | "myprivatekey"), (data, "mysalt")) |
73 | f4a2f532 | Guido Trotter | self.assertRaises(errors.SignatureError, serializer.LoadSigned,
|
74 | f4a2f532 | Guido Trotter | serializer.DumpSigned("test", "myprivatekey"), |
75 | f4a2f532 | Guido Trotter | "myotherkey")
|
76 | f4a2f532 | Guido Trotter | |
77 | 501c95a2 | Michael Hanselmann | def _TestSerializer(self, dump_fn, load_fn): |
78 | 501c95a2 | Michael Hanselmann | for data in self._TESTDATA: |
79 | 501c95a2 | Michael Hanselmann | self.failUnless(dump_fn(data).endswith("\n")) |
80 | 501c95a2 | Michael Hanselmann | self.failUnlessEqual(load_fn(dump_fn(data)), data)
|
81 | 501c95a2 | Michael Hanselmann | |
82 | 501c95a2 | Michael Hanselmann | |
83 | 501c95a2 | Michael Hanselmann | if __name__ == '__main__': |
84 | 501c95a2 | Michael Hanselmann | unittest.main() |