239d3113dad4dea4d92fc8b34112c936415219ab
[garming/samba-autobuild/.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
26 def open_bytes(filename):
27     if sys.version_info[0] == 3:
28         return open(filename, errors='ignore')
29     else:
30         return open(filename, 'rb')
31
32
33 class DCKeytabTests(tests.TestCase):
34     def setUp(self):
35         super(DCKeytabTests, self).setUp()
36         self.lp = LoadParm()
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()
41
42     def tearDown(self):
43         super(DCKeytabTests, self).tearDown()
44         os.remove(self.ktfile)
45
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:
51             result = ''
52             for c in bytes_kt.read():
53                 if c in string.printable:
54                     result += c
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'