PEP8: fix E127: continuation line over-indented for visual indent
[samba.git] / python / samba / tests / dckeytab.py
1 # Tests for source4/libnet/py_net_dckeytab.c
2 #
3 # Copyright (C) David Mulder <dmulder@suse.com> 2018
4 #
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.
9 #
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.
14 #
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/>.
17 #
18
19 import os, sys, string
20 from samba.net import Net
21 import samba.dckeytab
22 from samba import tests
23 from samba.param import LoadParm
24
25 def open_bytes(filename):
26     if sys.version_info[0] == 3:
27         return open(filename, errors='ignore')
28     else:
29         return open(filename, 'rb')
30
31 class DCKeytabTests(tests.TestCase):
32     def setUp(self):
33         super(DCKeytabTests, self).setUp()
34         self.lp = LoadParm()
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()
39
40     def tearDown(self):
41         super(DCKeytabTests, self).tearDown()
42         os.remove(self.ktfile)
43
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:
49             result = ''
50             for c in bytes_kt.read():
51                 if c in string.printable:
52                     result += c
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'