tests/dcerpc/misc.GUID: improve tests
authorJoe Guo <joeg@catalyst.net.nz>
Tue, 20 Mar 2018 23:13:56 +0000 (12:13 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 23 Mar 2018 06:28:25 +0000 (07:28 +0100)
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 <joeg@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/samba/tests/dcerpc/misc.py

index cf3dcbf3bb411d3ed240e2f5fe8608d64a857786..d2ba1843a0ec596a133b1609da0cb9a8a2f7e205 100644 (file)
@@ -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):