1 | n/a | import unittest |
---|
2 | n/a | |
---|
3 | n/a | import xdrlib |
---|
4 | n/a | |
---|
5 | n/a | class XDRTest(unittest.TestCase): |
---|
6 | n/a | |
---|
7 | n/a | def test_xdr(self): |
---|
8 | n/a | p = xdrlib.Packer() |
---|
9 | n/a | |
---|
10 | n/a | s = b'hello world' |
---|
11 | n/a | a = [b'what', b'is', b'hapnin', b'doctor'] |
---|
12 | n/a | |
---|
13 | n/a | p.pack_int(42) |
---|
14 | n/a | p.pack_int(-17) |
---|
15 | n/a | p.pack_uint(9) |
---|
16 | n/a | p.pack_bool(True) |
---|
17 | n/a | p.pack_bool(False) |
---|
18 | n/a | p.pack_uhyper(45) |
---|
19 | n/a | p.pack_float(1.9) |
---|
20 | n/a | p.pack_double(1.9) |
---|
21 | n/a | p.pack_string(s) |
---|
22 | n/a | p.pack_list(range(5), p.pack_uint) |
---|
23 | n/a | p.pack_array(a, p.pack_string) |
---|
24 | n/a | |
---|
25 | n/a | # now verify |
---|
26 | n/a | data = p.get_buffer() |
---|
27 | n/a | up = xdrlib.Unpacker(data) |
---|
28 | n/a | |
---|
29 | n/a | self.assertEqual(up.get_position(), 0) |
---|
30 | n/a | |
---|
31 | n/a | self.assertEqual(up.unpack_int(), 42) |
---|
32 | n/a | self.assertEqual(up.unpack_int(), -17) |
---|
33 | n/a | self.assertEqual(up.unpack_uint(), 9) |
---|
34 | n/a | self.assertTrue(up.unpack_bool() is True) |
---|
35 | n/a | |
---|
36 | n/a | # remember position |
---|
37 | n/a | pos = up.get_position() |
---|
38 | n/a | self.assertTrue(up.unpack_bool() is False) |
---|
39 | n/a | |
---|
40 | n/a | # rewind and unpack again |
---|
41 | n/a | up.set_position(pos) |
---|
42 | n/a | self.assertTrue(up.unpack_bool() is False) |
---|
43 | n/a | |
---|
44 | n/a | self.assertEqual(up.unpack_uhyper(), 45) |
---|
45 | n/a | self.assertAlmostEqual(up.unpack_float(), 1.9) |
---|
46 | n/a | self.assertAlmostEqual(up.unpack_double(), 1.9) |
---|
47 | n/a | self.assertEqual(up.unpack_string(), s) |
---|
48 | n/a | self.assertEqual(up.unpack_list(up.unpack_uint), list(range(5))) |
---|
49 | n/a | self.assertEqual(up.unpack_array(up.unpack_string), a) |
---|
50 | n/a | up.done() |
---|
51 | n/a | self.assertRaises(EOFError, up.unpack_uint) |
---|
52 | n/a | |
---|
53 | n/a | class ConversionErrorTest(unittest.TestCase): |
---|
54 | n/a | |
---|
55 | n/a | def setUp(self): |
---|
56 | n/a | self.packer = xdrlib.Packer() |
---|
57 | n/a | |
---|
58 | n/a | def assertRaisesConversion(self, *args): |
---|
59 | n/a | self.assertRaises(xdrlib.ConversionError, *args) |
---|
60 | n/a | |
---|
61 | n/a | def test_pack_int(self): |
---|
62 | n/a | self.assertRaisesConversion(self.packer.pack_int, 'string') |
---|
63 | n/a | |
---|
64 | n/a | def test_pack_uint(self): |
---|
65 | n/a | self.assertRaisesConversion(self.packer.pack_uint, 'string') |
---|
66 | n/a | |
---|
67 | n/a | def test_float(self): |
---|
68 | n/a | self.assertRaisesConversion(self.packer.pack_float, 'string') |
---|
69 | n/a | |
---|
70 | n/a | def test_double(self): |
---|
71 | n/a | self.assertRaisesConversion(self.packer.pack_double, 'string') |
---|
72 | n/a | |
---|
73 | n/a | def test_uhyper(self): |
---|
74 | n/a | self.assertRaisesConversion(self.packer.pack_uhyper, 'string') |
---|
75 | n/a | |
---|
76 | n/a | if __name__ == "__main__": |
---|
77 | n/a | unittest.main() |
---|