From e321713f3ff8547011f4d022d843303c72bae077 Mon Sep 17 00:00:00 2001 From: citronneur Date: Mon, 14 Apr 2014 22:07:54 +0200 Subject: [PATCH] add unit test for const and layer module --- rdpy/dotest.py | 14 +++++++++++--- rdpy/test/network/const.py | 34 ++++++++++++++++++++++++++++++++++ rdpy/test/network/layer.py | 38 ++++++++++++++++++++++++++++++++++++++ rdpy/test/network/type.py | 21 +++++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 rdpy/test/network/const.py create mode 100644 rdpy/test/network/layer.py diff --git a/rdpy/dotest.py b/rdpy/dotest.py index 49cc586..db56580 100644 --- a/rdpy/dotest.py +++ b/rdpy/dotest.py @@ -4,15 +4,23 @@ import sys, os # Change path so we find rdpy sys.path.insert(1, os.path.join(sys.path[0], '..')) -import unittest, rdpy.test.network.type +import unittest, rdpy.test.network.type, rdpy.test.network.const, rdpy.test.network.layer def headerTest(name): - print "-"*40 + print "*"*70 print name - print "-"*40 + print "*"*70 if __name__ == '__main__': headerTest("Test case rdpy.test.network.type.TypeCase") suite = unittest.TestLoader().loadTestsFromTestCase(rdpy.test.network.type.TypeCase) unittest.TextTestRunner(verbosity=2).run(suite) + + headerTest("Test case rdpy.test.network.const.ConstCase") + suite = unittest.TestLoader().loadTestsFromTestCase(rdpy.test.network.const.ConstCase) + unittest.TextTestRunner(verbosity=2).run(suite) + + headerTest("Test case rdpy.test.network.type.layer.LayerCase") + suite = unittest.TestLoader().loadTestsFromTestCase(rdpy.test.network.layer.LayerCase) + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/rdpy/test/network/const.py b/rdpy/test/network/const.py new file mode 100644 index 0000000..e5d99ae --- /dev/null +++ b/rdpy/test/network/const.py @@ -0,0 +1,34 @@ +''' +@author: sylvain +''' +import unittest +import rdpy.network.const +import rdpy.network.type + +class ConstCase(unittest.TestCase): + ''' + represent test case for all classes and function + present in rdpy.network.const + ''' + def test_type_attributes(self): + ''' + test if type attributes decorator works + ''' + @rdpy.network.const.TypeAttributes(rdpy.network.type.UInt16Le) + class Test: + MEMBER_1 = 1 + MEMBER_2 = 2 + + self.assertIsInstance(Test.MEMBER_1, rdpy.network.type.UInt16Le, "MEMBER_1 is not in correct type") + self.assertIsInstance(Test.MEMBER_2, rdpy.network.type.UInt16Le, "MEMBER_2 is not in correct type") + + def test_const(self): + ''' + test if get on const class member generate new object each + ''' + @rdpy.network.const.ConstAttributes + class Test: + MEMBER_1 = 1 + MEMBER_2 = 2 + + self.assertEquals(Test.MEMBER_1, Test.MEMBER_1, "handle same type of object") \ No newline at end of file diff --git a/rdpy/test/network/layer.py b/rdpy/test/network/layer.py new file mode 100644 index 0000000..cad5904 --- /dev/null +++ b/rdpy/test/network/layer.py @@ -0,0 +1,38 @@ +''' +@author: sylvain +''' +import unittest +import rdpy.network.layer + +class LayerCase(unittest.TestCase): + ''' + represent test case for all classes and function + present in rdpy.network.layer + ''' + + class LayerCaseException(Exception): + ''' + exception use for event base test + ''' + pass + + def test_layer_connect_event(self): + ''' + test if connect event is send from transport to presentation + ''' + class TestConnect(rdpy.network.layer.Layer): + def connect(self): + raise LayerCase.LayerCaseException() + + self.assertRaises(LayerCase.LayerCaseException, rdpy.network.layer.Layer(presentation = TestConnect()).connect) + + def test_layer_receive_event(self): + ''' + test if recv event is send from transport to presentation + ''' + class TestConnect(rdpy.network.layer.Layer): + def recv(self, s): + if s == "message": + raise LayerCase.LayerCaseException() + + self.assertRaises(LayerCase.LayerCaseException, rdpy.network.layer.Layer(presentation = TestConnect()).recv, "message") \ No newline at end of file diff --git a/rdpy/test/network/type.py b/rdpy/test/network/type.py index c640f9e..a5a109c 100644 --- a/rdpy/test/network/type.py +++ b/rdpy/test/network/type.py @@ -240,7 +240,28 @@ class TypeCase(unittest.TestCase): ''' #unsigned int case t = rdpy.network.type.SimpleType("I", 4, False, 0, optional = True) + #empty stream s1 = rdpy.network.type.Stream() s1.readType(t) self.assertEqual(t.value, 0, "invalid stream read optional value") + + def test_stream_read_conditional_singletype_false(self): + ''' + test conditional option in case of simple type reading and when condition is false (not read) + ''' + #unsigned int case + t = rdpy.network.type.SimpleType("I", 4, False, 0, conditional = lambda:False) + s1 = rdpy.network.type.Stream("\x01\x00\x00\x00") + s1.readType(t) + self.assertEqual(t.value, 0, "invalid stream read conditional value") + + def test_stream_read_conditional_singletype_true(self): + ''' + test conditional option in case of simple type reading and when condition is true (must be read) + ''' + #unsigned int case + t = rdpy.network.type.SimpleType("I", 4, False, 0, conditional = lambda:True) + s1 = rdpy.network.type.Stream("\x01\x00\x00\x00") + s1.readType(t) + self.assertEqual(t.value, 1, "invalid stream read conditional value") \ No newline at end of file