python:tests: Improve keytab comparison of dckeytab
authorAndreas Schneider <asn@samba.org>
Fri, 5 Apr 2024 12:33:04 +0000 (14:33 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 8 Apr 2024 03:00:39 +0000 (03:00 +0000)
This will give better output on failure as it compares strings instead
of bytes.

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/dckeytab.py

index 978e3753cc78f4162b1b6e1701e7f85732e4e628..090f53332c86459d75fc6dfed0c04c2a21009de2 100644 (file)
 #
 
 import os
-import sys
-import string
+import subprocess
 from samba.net import Net
 from samba import enable_net_export_keytab
 
-from samba import credentials, dsdb, ntstatus, NTSTATUSError, tests
+from samba import credentials, dsdb, ntstatus, NTSTATUSError
 from samba.dcerpc import krb5ccache, security
 from samba.dsdb import UF_WORKSTATION_TRUST_ACCOUNT
 from samba.ndr import ndr_unpack, ndr_pack
@@ -153,10 +152,28 @@ class DCKeytabTests(TestCaseInTempDir):
         net.export_keytab(keytab=self.ktfile, principal=new_principal)
         self.assertTrue(os.path.exists(self.ktfile), 'keytab was not created')
 
+        cmd = ['klist', '-K', '-C', '-t', '-k', self.ktfile]
+        keytab_orig_content = subprocess.Popen(
+            cmd,
+            shell=False,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT,
+        ).communicate()[0]
+
         with open(self.ktfile, 'rb') as bytes_kt:
             keytab_orig_bytes = bytes_kt.read()
 
         net.export_keytab(keytab=self.ktfile, principal=new_principal)
+        self.assertTrue(os.path.exists(self.ktfile), 'keytab was not created')
+
+        keytab_content = subprocess.Popen(
+            cmd,
+            shell=False,
+            stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT,
+        ).communicate()[0]
+
+        self.assertEqual(keytab_orig_content, keytab_content)
 
         # Parse the first entry in the keytab
         with open(self.ktfile, 'rb') as bytes_kt: