1 # Tests for source4/libnet/py_net_dckeytab.c
3 # Copyright (C) David Mulder <dmulder@suse.com> 2018
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import os, sys, string
20 from samba.net import Net
22 from samba import tests
23 from samba.param import LoadParm
26 def open_bytes(filename):
27 if sys.version_info[0] == 3:
28 return open(filename, errors='ignore')
30 return open(filename, 'rb')
33 class DCKeytabTests(tests.TestCase):
35 super(DCKeytabTests, self).setUp()
37 self.lp.load_default()
38 self.creds = self.insta_creds(template=self.get_credentials())
39 self.ktfile = os.path.join(self.lp.get('private dir'), 'test.keytab')
40 self.principal = self.creds.get_principal()
43 super(DCKeytabTests, self).tearDown()
44 os.remove(self.ktfile)
46 def test_export_keytab(self):
47 net = Net(None, self.lp)
48 net.export_keytab(keytab=self.ktfile, principal=self.principal)
49 assert os.path.exists(self.ktfile), 'keytab was not created'
50 with open_bytes(self.ktfile) as bytes_kt:
52 for c in bytes_kt.read():
53 if c in string.printable:
55 principal_parts = self.principal.split('@')
56 assert principal_parts[0] in result and \
57 principal_parts[1] in result, \
58 'Principal not found in generated keytab'