# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-# 0.0510-1301, USA.
+# 02110-1301, USA.
"""Script for unittesting the serializer module"""
]
def _TestSerializer(self, dump_fn, load_fn):
- for indent in [True, False]:
- for data in self._TESTDATA:
- self.failUnless(dump_fn(data, indent=indent).endswith("\n"))
- self.assertEqualValues(load_fn(dump_fn(data, indent=indent)), data)
+ for data in self._TESTDATA:
+ self.failUnless(dump_fn(data).endswith("\n"))
+ self.assertEqualValues(load_fn(dump_fn(data)), data)
def testGeneric(self):
self._TestSerializer(serializer.Dump, serializer.Load)
+ def testSignedGeneric(self):
+ self._TestSigned(serializer.DumpSigned, serializer.LoadSigned)
+
def testJson(self):
self._TestSerializer(serializer.DumpJson, serializer.LoadJson)
- def testJsonIndent(self):
- data = {
- "k1": 1,
- "k2": 3,
- "k3": 4,
- }
- self.assert_(len(serializer.DumpJson(data, indent=True).splitlines()) > 3)
-
- def testSignedMessage(self):
- LoadSigned = serializer.LoadSigned
- DumpSigned = serializer.DumpSigned
+ def testSignedJson(self):
+ self._TestSigned(serializer.DumpSignedJson, serializer.LoadSignedJson)
+ def _TestSigned(self, dump_fn, load_fn):
for data in self._TESTDATA:
- self.assertEqualValues(LoadSigned(DumpSigned(data, "mykey"), "mykey"),
+ self.assertEqualValues(load_fn(dump_fn(data, "mykey"), "mykey"),
(data, ''))
- self.assertEqualValues(LoadSigned(DumpSigned(data, "myprivatekey",
- "mysalt"),
- "myprivatekey"),
+ self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
+ salt="mysalt"),
+ "myprivatekey"),
(data, "mysalt"))
- self.assertRaises(errors.SignatureError, serializer.LoadSigned,
- serializer.DumpSigned("test", "myprivatekey"),
+ keydict = {
+ "mykey_id": "myprivatekey",
+ }
+ self.assertEqualValues(load_fn(dump_fn(data, "myprivatekey",
+ salt="mysalt",
+ key_selector="mykey_id"),
+ keydict.get),
+ (data, "mysalt"))
+ self.assertRaises(errors.SignatureError, load_fn,
+ dump_fn(data, "myprivatekey",
+ salt="mysalt",
+ key_selector="mykey_id"),
+ {}.get)
+
+ self.assertRaises(errors.SignatureError, load_fn,
+ dump_fn("test", "myprivatekey"),
"myotherkey")
+ self.assertRaises(errors.SignatureError, load_fn,
+ serializer.DumpJson("This is a test"), "mykey")
+ self.assertRaises(errors.SignatureError, load_fn,
+ serializer.DumpJson({}), "mykey")
+
+ # Message missing salt and HMAC
+ tdata = { "msg": "Foo", }
+ self.assertRaises(errors.SignatureError, load_fn,
+ serializer.DumpJson(tdata), "mykey")
+
if __name__ == '__main__':
testutils.GanetiTestProgram()