1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-24 02:04:21 +03:00

python/tests: Convert dckeytab test to use new NDR keytab parser

This is much nicer than reading strings out of the binary file.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Jo Sutton <josutton@catalyst.net.nz>
This commit is contained in:
Andrew Bartlett 2024-02-21 14:14:24 +13:00
parent 2e230f728e
commit a1d7af2485

View File

@ -23,19 +23,13 @@ from samba.net import Net
from samba import enable_net_export_keytab from samba import enable_net_export_keytab
from samba import tests from samba import tests
from samba.dcerpc import krb5ccache
from samba.ndr import ndr_unpack
from samba.param import LoadParm from samba.param import LoadParm
enable_net_export_keytab() enable_net_export_keytab()
def open_bytes(filename):
if sys.version_info[0] == 3:
return open(filename, errors='ignore')
else:
return open(filename, 'rb')
class DCKeytabTests(tests.TestCase): class DCKeytabTests(tests.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
@ -52,13 +46,18 @@ class DCKeytabTests(tests.TestCase):
def test_export_keytab(self): def test_export_keytab(self):
net = Net(None, self.lp) net = Net(None, self.lp)
net.export_keytab(keytab=self.ktfile, principal=self.principal) net.export_keytab(keytab=self.ktfile, principal=self.principal)
assert os.path.exists(self.ktfile), 'keytab was not created' self.assertTrue(os.path.exists(self.ktfile), 'keytab was not created')
with open_bytes(self.ktfile) as bytes_kt:
result = '' # Parse the first entry in the keytab
for c in bytes_kt.read(): with open(self.ktfile, 'rb') as bytes_kt:
if c in string.printable: keytab_bytes = bytes_kt.read()
result += c
principal_parts = self.principal.split('@') keytab = ndr_unpack(krb5ccache.KEYTAB, keytab_bytes)
assert principal_parts[0] in result and \
principal_parts[1] in result, \ # Confirm that the principal is as expected
'Principal not found in generated keytab'
principal_parts = self.principal.split('@')
self.assertEqual(keytab.entry.principal.component_count, 1)
self.assertEqual(keytab.entry.principal.realm, principal_parts[1])
self.assertEqual(keytab.entry.principal.components[0], principal_parts[0])