41220280e621251a7797e5daa865c921f99ec92e
[nivanova/samba-autobuild/.git] / python / samba / tests / net_join.py
1 # Unix SMB/CIFS implementation.
2 #
3 # Copyright (C) Catalyst.Net Ltd. 2017
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 """
20 Confirm that net.join_member works
21 """
22
23 import samba.tests
24 import os
25 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
26 from samba.credentials import DONT_USE_KERBEROS
27 from samba import NTSTATUSError, ntstatus
28 import ctypes
29
30 class NetJoinTests(samba.tests.TestCaseInTempDir):
31
32     def setUp(self):
33         super(NetJoinTests, self).setUp()
34         self.domain = os.environ["DOMAIN"]
35         self.server = os.environ["SERVER"]
36         self.lp = self.get_loadparm()
37         self.lp.set("private dir", self.tempdir)
38         self.lp.set("lock dir", self.tempdir)
39         self.lp.set("state directory", self.tempdir)
40
41     def tearDown(self):
42         super(NetJoinTests, self).tearDown()
43
44     def test_net_join(self):
45         netbios_name = "NetJoinTest"
46         machinepass  = "abcdefghij"
47         creds = self.insta_creds(template=self.get_credentials(),
48                                  kerberos_state=DONT_USE_KERBEROS)
49
50         net = Net(creds, self.lp, server=self.server)
51
52         # NOTE WELL: We must not run more than one successful
53         # net.join_member per file (process), as the shared
54         # secrets.ldb handle will be kept between runs.
55         try:
56             (join_password, sid, domain_name) = net.join_member(
57                 self.domain, netbios_name, LIBNET_JOIN_AUTOMATIC,
58                 machinepass=machinepass)
59         except NTSTATUSError as e:
60             code = ctypes.c_uint32(e.args[0]).value
61             if code == ntstatus.NT_STATUS_CONNECTION_DISCONNECTED:
62                 self.fail("Connection failure")
63             raise
64         os.unlink(os.path.join(self.tempdir, "secrets.ldb"))
65         pass