root / test / ganeti.serializer_unittest.py @ fd0351ae
History | View | Annotate | Download (4.1 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 |
import warnings |
27 |
|
28 |
from ganeti import serializer |
29 |
from ganeti import errors |
30 |
from ganeti import compat |
31 |
|
32 |
import testutils |
33 |
|
34 |
|
35 |
class TestSerializer(testutils.GanetiTestCase): |
36 |
"""Serializer tests"""
|
37 |
|
38 |
_TESTDATA = [ |
39 |
"test",
|
40 |
255,
|
41 |
[1, 2, 3], |
42 |
(1, 2, 3), |
43 |
{ "1": 2, "foo": "bar", }, |
44 |
["abc", 1, 2, 3, 999, |
45 |
{ |
46 |
"a1": ("Hello", "World"), |
47 |
"a2": "This is only a test", |
48 |
"a3": None, |
49 |
}, |
50 |
{ |
51 |
"foo": "bar", |
52 |
}, |
53 |
] |
54 |
] |
55 |
|
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)
|
61 |
|
62 |
def testGeneric(self): |
63 |
self._TestSerializer(serializer.Dump, serializer.Load)
|
64 |
|
65 |
def testSignedGeneric(self): |
66 |
self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
|
67 |
|
68 |
def testJson(self): |
69 |
self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
|
70 |
|
71 |
def testSignedJson(self): |
72 |
self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
|
73 |
|
74 |
def _TestSigned(self, dump_fn, load_fn): |
75 |
for data in self._TESTDATA: |
76 |
self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"), |
77 |
(data, ''))
|
78 |
self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey", |
79 |
salt="mysalt"),
|
80 |
"myprivatekey"),
|
81 |
(data, "mysalt"))
|
82 |
|
83 |
keydict = { |
84 |
"mykey_id": "myprivatekey", |
85 |
} |
86 |
self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey", |
87 |
salt="mysalt",
|
88 |
key_selector="mykey_id"),
|
89 |
keydict.get), |
90 |
(data, "mysalt"))
|
91 |
self.assertRaises(errors.SignatureError, load_fn,
|
92 |
dump_fn(data, "myprivatekey",
|
93 |
salt="mysalt",
|
94 |
key_selector="mykey_id"),
|
95 |
{}.get) |
96 |
|
97 |
self.assertRaises(errors.SignatureError, load_fn,
|
98 |
dump_fn("test", "myprivatekey"), |
99 |
"myotherkey")
|
100 |
|
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")
|
105 |
|
106 |
# Message missing salt and HMAC
|
107 |
tdata = { "msg": "Foo", } |
108 |
self.assertRaises(errors.SignatureError, load_fn,
|
109 |
serializer.DumpJson(tdata), "mykey")
|
110 |
|
111 |
|
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"
|
117 |
" correctly")
|
118 |
self.assertTrue(serializer.DumpJson(data))
|
119 |
else:
|
120 |
self.assertRaises(ValueError, serializer.DumpJson, data) |
121 |
|
122 |
def test(self): |
123 |
for value in [123, 1.1, -1, -9492.1123, -3234e-4]: |
124 |
self._Test({value: ""}) |
125 |
|
126 |
def testAllowed(self): |
127 |
for value in ["", "Hello World", None, True, False]: |
128 |
self.assertTrue(serializer.DumpJson({value: ""})) |
129 |
|
130 |
|
131 |
if __name__ == '__main__': |
132 |
testutils.GanetiTestProgram() |