root / test / ganeti.serializer_unittest.py @ 8342c325
History | View | Annotate | Download (3.5 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 | d357f531 | Michael Hanselmann | import testutils |
31 | 501c95a2 | Michael Hanselmann | |
32 | 501c95a2 | Michael Hanselmann | |
33 | d357f531 | Michael Hanselmann | class TestSerializer(testutils.GanetiTestCase): |
34 | 501c95a2 | Michael Hanselmann | """Serializer tests"""
|
35 | 501c95a2 | Michael Hanselmann | |
36 | 501c95a2 | Michael Hanselmann | _TESTDATA = [ |
37 | 501c95a2 | Michael Hanselmann | "test",
|
38 | 501c95a2 | Michael Hanselmann | 255,
|
39 | 501c95a2 | Michael Hanselmann | [1, 2, 3], |
40 | 501c95a2 | Michael Hanselmann | (1, 2, 3), |
41 | d357f531 | Michael Hanselmann | { "1": 2, "foo": "bar", }, |
42 | d357f531 | Michael Hanselmann | ["abc", 1, 2, 3, 999, |
43 | d357f531 | Michael Hanselmann | { |
44 | d357f531 | Michael Hanselmann | "a1": ("Hello", "World"), |
45 | d357f531 | Michael Hanselmann | "a2": "This is only a test", |
46 | d357f531 | Michael Hanselmann | "a3": None, |
47 | d357f531 | Michael Hanselmann | }, |
48 | d357f531 | Michael Hanselmann | { |
49 | d357f531 | Michael Hanselmann | "foo": "bar", |
50 | d357f531 | Michael Hanselmann | }, |
51 | d357f531 | Michael Hanselmann | ] |
52 | 501c95a2 | Michael Hanselmann | ] |
53 | 501c95a2 | Michael Hanselmann | |
54 | d357f531 | Michael Hanselmann | def _TestSerializer(self, dump_fn, load_fn): |
55 | 02141fb1 | Michael Hanselmann | for indent in [True, False]: |
56 | 02141fb1 | Michael Hanselmann | for data in self._TESTDATA: |
57 | 02141fb1 | Michael Hanselmann | self.failUnless(dump_fn(data, indent=indent).endswith("\n")) |
58 | 02141fb1 | Michael Hanselmann | self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
|
59 | 501c95a2 | Michael Hanselmann | |
60 | 501c95a2 | Michael Hanselmann | def testGeneric(self): |
61 | 02141fb1 | Michael Hanselmann | self._TestSerializer(serializer.Dump, serializer.Load)
|
62 | 501c95a2 | Michael Hanselmann | |
63 | dd999b06 | Michael Hanselmann | def testSignedGeneric(self): |
64 | dd999b06 | Michael Hanselmann | self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
|
65 | dd999b06 | Michael Hanselmann | |
66 | 501c95a2 | Michael Hanselmann | def testJson(self): |
67 | 02141fb1 | Michael Hanselmann | self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
|
68 | 02141fb1 | Michael Hanselmann | |
69 | dd999b06 | Michael Hanselmann | def testSignedJson(self): |
70 | dd999b06 | Michael Hanselmann | self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
|
71 | f4a2f532 | Guido Trotter | |
72 | dd999b06 | Michael Hanselmann | def _TestSigned(self, dump_fn, load_fn): |
73 | f4a2f532 | Guido Trotter | for data in self._TESTDATA: |
74 | dd999b06 | Michael Hanselmann | self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"), |
75 | d357f531 | Michael Hanselmann | (data, ''))
|
76 | dd999b06 | Michael Hanselmann | self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey", |
77 | dd999b06 | Michael Hanselmann | salt="mysalt"),
|
78 | dd999b06 | Michael Hanselmann | "myprivatekey"),
|
79 | d357f531 | Michael Hanselmann | (data, "mysalt"))
|
80 | d357f531 | Michael Hanselmann | |
81 | dd999b06 | Michael Hanselmann | keydict = { |
82 | dd999b06 | Michael Hanselmann | "mykey_id": "myprivatekey", |
83 | dd999b06 | Michael Hanselmann | } |
84 | dd999b06 | Michael Hanselmann | self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey", |
85 | dd999b06 | Michael Hanselmann | salt="mysalt",
|
86 | dd999b06 | Michael Hanselmann | key_selector="mykey_id"),
|
87 | dd999b06 | Michael Hanselmann | keydict.get), |
88 | 72ca1dcb | Balazs Lecz | (data, "mysalt"))
|
89 | dd999b06 | Michael Hanselmann | self.assertRaises(errors.SignatureError, load_fn,
|
90 | dd999b06 | Michael Hanselmann | dump_fn(data, "myprivatekey",
|
91 | dd999b06 | Michael Hanselmann | salt="mysalt",
|
92 | dd999b06 | Michael Hanselmann | key_selector="mykey_id"),
|
93 | dd999b06 | Michael Hanselmann | {}.get) |
94 | dd999b06 | Michael Hanselmann | |
95 | dd999b06 | Michael Hanselmann | self.assertRaises(errors.SignatureError, load_fn,
|
96 | dd999b06 | Michael Hanselmann | dump_fn("test", "myprivatekey"), |
97 | f4a2f532 | Guido Trotter | "myotherkey")
|
98 | f4a2f532 | Guido Trotter | |
99 | dd999b06 | Michael Hanselmann | self.assertRaises(errors.SignatureError, load_fn,
|
100 | dd999b06 | Michael Hanselmann | serializer.DumpJson("This is a test"), "mykey") |
101 | dd999b06 | Michael Hanselmann | self.assertRaises(errors.SignatureError, load_fn,
|
102 | dd999b06 | Michael Hanselmann | serializer.DumpJson({}), "mykey")
|
103 | dd999b06 | Michael Hanselmann | |
104 | dd999b06 | Michael Hanselmann | # Message missing salt and HMAC
|
105 | dd999b06 | Michael Hanselmann | tdata = { "msg": "Foo", } |
106 | dd999b06 | Michael Hanselmann | self.assertRaises(errors.SignatureError, load_fn,
|
107 | dd999b06 | Michael Hanselmann | serializer.DumpJson(tdata), "mykey")
|
108 | dd999b06 | Michael Hanselmann | |
109 | 501c95a2 | Michael Hanselmann | |
110 | 501c95a2 | Michael Hanselmann | if __name__ == '__main__': |
111 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |