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