dbeb763e364fe36496fb1a5eb25262d0398b017c
[garming/samba-autobuild/.git] / python / samba / tests / samba_tool / rodc.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd. 2015
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 import os
19 import ldb
20 import samba
21 from samba.samdb import SamDB
22 from samba.tests import delete_force
23 from samba.tests.samba_tool.base import SambaToolCmdTest
24 from samba.credentials import Credentials
25 from samba.auth import system_session
26
27
28 class RodcCmdTestCase(SambaToolCmdTest):
29     def setUp(self):
30         super(RodcCmdTestCase, self).setUp()
31         self.lp = samba.param.LoadParm()
32         self.lp.load(os.environ["SMB_CONF_PATH"])
33         self.creds = Credentials()
34         self.creds.set_username(os.environ["DC_USERNAME"])
35         self.creds.set_password(os.environ["DC_PASSWORD"])
36         self.creds.guess(self.lp)
37         self.session = system_session()
38         self.ldb = SamDB("ldap://" + os.environ["DC_SERVER"],
39                          session_info=self.session, credentials=self.creds, lp=self.lp)
40
41         self.base_dn = self.ldb.domain_dn()
42
43         self.ldb.newuser("sambatool1", "1qazXSW@")
44         self.ldb.newuser("sambatool2", "2wsxCDE#")
45         self.ldb.newuser("sambatool3", "3edcVFR$")
46         self.ldb.newuser("sambatool4", "4rfvBGT%")
47         self.ldb.newuser("sambatool5", "5tjbNHY*")
48         self.ldb.newuser("sambatool6", "6yknMJU*")
49
50         self.ldb.add_remove_group_members("Allowed RODC Password Replication Group",
51                                           ["sambatool1", "sambatool2", "sambatool3",
52                                            "sambatool4", "sambatool5"],
53                                           add_members_operation=True)
54
55     def tearDown(self):
56         super(RodcCmdTestCase, self).tearDown()
57         self.ldb.deleteuser("sambatool1")
58         self.ldb.deleteuser("sambatool2")
59         self.ldb.deleteuser("sambatool3")
60         self.ldb.deleteuser("sambatool4")
61         self.ldb.deleteuser("sambatool5")
62         self.ldb.deleteuser("sambatool6")
63         (result, out, err) = self.runsubcmd("drs", "replicate", "--local", "unused",
64                                             os.environ["DC_SERVER"], self.base_dn)
65
66
67     def test_single_by_account_name(self):
68         (result, out, err) = self.runsubcmd("rodc", "preload", "sambatool1",
69                                             "--server", os.environ["DC_SERVER"])
70         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
71         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\n" % self.base_dn)
72         self.assertEqual(err, "")
73
74     def test_single_by_dn(self):
75         (result, out, err) = self.runsubcmd("rodc", "preload", "cn=sambatool2,cn=users,%s" % self.base_dn,
76                                             "--server", os.environ["DC_SERVER"])
77         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
78         self.assertEqual(out, "Replicating DN CN=sambatool2,CN=Users,%s\n" % self.base_dn)
79
80     def test_multi_by_account_name(self):
81         (result, out, err) = self.runsubcmd("rodc", "preload", "sambatool1", "sambatool2",
82                                             "--server", os.environ["DC_SERVER"])
83         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
84         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\nReplicating DN CN=sambatool2,CN=Users,%s\n" % (self.base_dn, self.base_dn))
85
86     def test_multi_by_dn(self):
87         (result, out, err) = self.runsubcmd("rodc", "preload", "cn=sambatool3,cn=users,%s" % self.base_dn, "cn=sambatool4,cn=users,%s" % self.base_dn,
88                                             "--server", os.environ["DC_SERVER"])
89         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
90         self.assertEqual(out, "Replicating DN CN=sambatool3,CN=Users,%s\nReplicating DN CN=sambatool4,CN=Users,%s\n" % (self.base_dn, self.base_dn))
91
92     def test_multi_in_file(self):
93         tempf = os.path.join(self.tempdir, "accountlist")
94         open(tempf, 'w').write("sambatool1\nsambatool2")
95         (result, out, err) = self.runsubcmd("rodc", "preload", "--file", tempf,
96                                             "--server", os.environ["DC_SERVER"])
97         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
98         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\nReplicating DN CN=sambatool2,CN=Users,%s\n" % (self.base_dn, self.base_dn))
99         os.unlink(tempf)
100
101     def test_multi_with_missing_name_success(self):
102         (result, out, err) = self.runsubcmd("rodc", "preload",
103                                             "nonexistentuser1", "sambatool5",
104                                             "nonexistentuser2",
105                                             "--server", os.environ["DC_SERVER"],
106                                             "--ignore-errors")
107         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
108         self.assertTrue(out.startswith("Replicating DN CN=sambatool5,CN=Users,%s\n"
109                                        % self.base_dn))
110
111     def test_multi_with_missing_name_failure(self):
112         (result, out, err) = self.runsubcmd("rodc", "preload",
113                                             "nonexistentuser1", "sambatool5",
114                                             "nonexistentuser2",
115                                             "--server", os.environ["DC_SERVER"])
116         self.assertCmdFail(result, "ensuring rodc prefetch quit on missing user")
117
118     def test_multi_without_group_success(self):
119         (result, out, err) = self.runsubcmd("rodc", "preload",
120                                             "sambatool6", "sambatool5",
121                                             "--server", os.environ["DC_SERVER"],
122                                             "--ignore-errors")
123         self.assertCmdSuccess(result, out, err, "ensuring rodc prefetch ran successfully")
124         self.assertTrue(out.startswith("Replicating DN CN=sambatool6,CN=Users,%s\n"
125                                        "Replicating DN CN=sambatool5,CN=Users,%s\n"
126                                        % (self.base_dn, self.base_dn)))
127
128     def test_multi_without_group_failure(self):
129         (result, out, err) = self.runsubcmd("rodc", "preload",
130                                             "sambatool6", "sambatool5",
131                                             "--server", os.environ["DC_SERVER"])
132         self.assertCmdFail(result, "ensuring rodc prefetch quit on non-replicated user")