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
25 def open_bytes(filename):
26 if sys.version_info[0] == 3:
27 return open(filename, errors='ignore')
29 return open(filename, 'rb')
31 class DCKeytabTests(tests.TestCase):
33 super(DCKeytabTests, self).setUp()
35 self.lp.load_default()
36 self.creds = self.insta_creds(template=self.get_credentials())
37 self.ktfile = os.path.join(self.lp.get('private dir'), 'test.keytab')
38 self.principal = self.creds.get_principal()
41 super(DCKeytabTests, self).tearDown()
42 os.remove(self.ktfile)
44 def test_export_keytab(self):
45 net = Net(None, self.lp)
46 net.export_keytab(keytab=self.ktfile, principal=self.principal)
47 assert os.path.exists(self.ktfile), 'keytab was not created'
48 with open_bytes(self.ktfile) as bytes_kt:
50 for c in bytes_kt.read():
51 if c in string.printable:
53 principal_parts = self.principal.split('@')
54 assert principal_parts[0] in result and \
55 principal_parts[1] in result, \
56 'Principal not found in generated keytab'