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/>.
22 from samba
.net
import Net
23 from samba
import enable_net_export_keytab
25 from samba
import tests
26 from samba
.param
import LoadParm
29 enable_net_export_keytab()
32 def open_bytes(filename
):
33 if sys
.version_info
[0] == 3:
34 return open(filename
, errors
='ignore')
36 return open(filename
, 'rb')
39 class DCKeytabTests(tests
.TestCase
):
41 super(DCKeytabTests
, self
).setUp()
43 self
.lp
.load_default()
44 self
.creds
= self
.insta_creds(template
=self
.get_credentials())
45 self
.ktfile
= os
.path
.join(self
.lp
.get('private dir'), 'test.keytab')
46 self
.principal
= self
.creds
.get_principal()
49 super(DCKeytabTests
, self
).tearDown()
50 os
.remove(self
.ktfile
)
52 def test_export_keytab(self
):
53 net
= Net(None, self
.lp
)
54 net
.export_keytab(keytab
=self
.ktfile
, principal
=self
.principal
)
55 assert os
.path
.exists(self
.ktfile
), 'keytab was not created'
56 with
open_bytes(self
.ktfile
) as bytes_kt
:
58 for c
in bytes_kt
.read():
59 if c
in string
.printable
:
61 principal_parts
= self
.principal
.split('@')
62 assert principal_parts
[0] in result
and \
63 principal_parts
[1] in result
, \
64 'Principal not found in generated keytab'