From bb88292cee5289742b72ff0249f01d527920301f Mon Sep 17 00:00:00 2001 From: Joe Guo Date: Wed, 21 Mar 2018 12:13:56 +1300 Subject: [PATCH] tests/dcerpc/misc.GUID: improve tests 1. Merge tests for different formats into a for loop, make it easy to read and extend. 2. Add test for invalid formats. Signed-off-by: Joe Guo Reviewed-by: Andrew Bartlett Reviewed-by: Douglas Bagnall --- python/samba/tests/dcerpc/misc.py | 53 ++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/python/samba/tests/dcerpc/misc.py b/python/samba/tests/dcerpc/misc.py index cf3dcbf3bb41..d2ba1843a0ec 100644 --- a/python/samba/tests/dcerpc/misc.py +++ b/python/samba/tests/dcerpc/misc.py @@ -23,10 +23,7 @@ from samba.compat import PY3 text1 = "76f53846-a7c2-476a-ae2c-20e2b80d7b34" text2 = "344edffa-330a-4b39-b96e-2c34da52e8b1" -text3_s = "00112233-4455-6677-8899-aabbccddeeff" -text3_b = b"00112233-4455-6677-8899-aabbccddeeff" -text3_b16 = b"\x33\x22\x11\x00\x55\x44\x77\x66\x88\x99\xaa\xbb\xcc\xdd\xee\xff" -text3_h = "33221100554477668899aabbccddeeff" +text3 = "00112233-4455-6677-8899-aabbccddeeff" if PY3: @@ -59,21 +56,39 @@ class GUIDTests(samba.tests.TestCase): self.assertEquals(guid1, guid2) self.assertEquals(0, cmp(guid1, guid2)) - def test_bytes_equals_string(self): - guid = misc.GUID(text3_b) - self.assertEquals(text3_s, str(guid)) - - def test_binary_format(self): - guid = misc.GUID(text3_b16) - self.assertEquals(text3_s, str(guid)) - - def test_strhex_format(self): - guid = misc.GUID(text3_h) - self.assertEquals(text3_s, str(guid)) - - def test_bracketed_format(self): - guid = misc.GUID('{'+ text3_s + '}') - self.assertEquals(text3_s, str(guid)) + def test_valid_formats(self): + fmts = [ + "00112233-4455-6677-8899-aabbccddeeff", # 36 + b"00112233-4455-6677-8899-aabbccddeeff", # 36 as bytes + "{00112233-4455-6677-8899-aabbccddeeff}", # 38 + + "33221100554477668899aabbccddeeff", # 32 + b"33221100554477668899aabbccddeeff", # 32 as bytes + + # 16 as hex bytes + b"\x33\x22\x11\x00\x55\x44\x77\x66\x88\x99\xaa\xbb\xcc\xdd\xee\xff" + ] + for fmt in fmts: + guid = misc.GUID(fmt) + self.assertEquals(text3, str(guid)) + + def test_invalid_formats(self): + fmts = [ + "00112233-4455-6677-8899-aabbccddee", # 34 + "{33221100554477668899aabbccddeeff}", + "33221100554477668899aabbccddee", # 30 + "\\x33\\x22\\x11\\x00\\x55\\x44\\x77\\x66\\x88\\x99\\xaa\\xbb\\xcc\\xdd\\xee\\xff", + r"\x33\x22\x11\x00\x55\x44\x77\x66\x88\x99\xaa\xbb\xcc\xdd\xee\xff", + ] + for fmt in fmts: + try: + misc.GUID(fmt) + except samba.NTSTATUSError: + # invalid formats should get this error + continue + else: + # otherwise, test fail + self.fail() class PolicyHandleTests(samba.tests.TestCase): -- 2.34.1