2006-12-01 12:59:47 -04:00
|
|
|
from test import test_support
|
|
|
|
import unittest
|
|
|
|
|
2005-02-24 16:22:10 -04:00
|
|
|
import xdrlib
|
|
|
|
|
2006-12-01 12:59:47 -04:00
|
|
|
class XDRTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_xdr(self):
|
|
|
|
p = xdrlib.Packer()
|
|
|
|
|
|
|
|
s = 'hello world'
|
|
|
|
a = ['what', 'is', 'hapnin', 'doctor']
|
|
|
|
|
|
|
|
p.pack_int(42)
|
2011-03-27 12:15:24 -03:00
|
|
|
p.pack_int(-17)
|
2006-12-01 12:59:47 -04:00
|
|
|
p.pack_uint(9)
|
|
|
|
p.pack_bool(True)
|
|
|
|
p.pack_bool(False)
|
|
|
|
p.pack_uhyper(45L)
|
|
|
|
p.pack_float(1.9)
|
|
|
|
p.pack_double(1.9)
|
|
|
|
p.pack_string(s)
|
|
|
|
p.pack_list(range(5), p.pack_uint)
|
|
|
|
p.pack_array(a, p.pack_string)
|
|
|
|
|
|
|
|
# now verify
|
|
|
|
data = p.get_buffer()
|
|
|
|
up = xdrlib.Unpacker(data)
|
|
|
|
|
|
|
|
self.assertEqual(up.get_position(), 0)
|
|
|
|
|
|
|
|
self.assertEqual(up.unpack_int(), 42)
|
2011-03-27 12:15:24 -03:00
|
|
|
self.assertEqual(up.unpack_int(), -17)
|
2006-12-01 12:59:47 -04:00
|
|
|
self.assertEqual(up.unpack_uint(), 9)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(up.unpack_bool() is True)
|
2006-12-01 12:59:47 -04:00
|
|
|
|
|
|
|
# remember position
|
|
|
|
pos = up.get_position()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(up.unpack_bool() is False)
|
2006-12-01 12:59:47 -04:00
|
|
|
|
|
|
|
# rewind and unpack again
|
|
|
|
up.set_position(pos)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(up.unpack_bool() is False)
|
2006-12-01 12:59:47 -04:00
|
|
|
|
|
|
|
self.assertEqual(up.unpack_uhyper(), 45L)
|
|
|
|
self.assertAlmostEqual(up.unpack_float(), 1.9)
|
|
|
|
self.assertAlmostEqual(up.unpack_double(), 1.9)
|
|
|
|
self.assertEqual(up.unpack_string(), s)
|
|
|
|
self.assertEqual(up.unpack_list(up.unpack_uint), range(5))
|
|
|
|
self.assertEqual(up.unpack_array(up.unpack_string), a)
|
|
|
|
up.done()
|
|
|
|
self.assertRaises(EOFError, up.unpack_uint)
|
|
|
|
|
2014-10-10 15:11:34 -03:00
|
|
|
class ConversionErrorTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.packer = xdrlib.Packer()
|
|
|
|
|
|
|
|
def assertRaisesConversion(self, *args):
|
|
|
|
self.assertRaises(xdrlib.ConversionError, *args)
|
|
|
|
|
|
|
|
def test_pack_int(self):
|
|
|
|
self.assertRaisesConversion(self.packer.pack_int, 'string')
|
|
|
|
|
|
|
|
def test_pack_uint(self):
|
|
|
|
self.assertRaisesConversion(self.packer.pack_uint, 'string')
|
|
|
|
|
|
|
|
def test_float(self):
|
|
|
|
self.assertRaisesConversion(self.packer.pack_float, 'string')
|
|
|
|
|
|
|
|
def test_double(self):
|
|
|
|
self.assertRaisesConversion(self.packer.pack_double, 'string')
|
|
|
|
|
|
|
|
def test_uhyper(self):
|
|
|
|
self.assertRaisesConversion(self.packer.pack_uhyper, 'string')
|
|
|
|
|
2006-12-01 12:59:47 -04:00
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(XDRTest)
|
2014-10-10 15:11:34 -03:00
|
|
|
test_support.run_unittest(ConversionErrorTest)
|
2006-12-01 12:59:47 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|