4930a3b5254a13795a8e0a2da2cbeb8e9c5420b7
[samba.git] / source4 / torture / drs / python / replica_sync_rodc.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Test conflict scenarios on the RODC
5 #
6 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2011
7 # Copyright (C) Catalyst.NET Ltd 2018
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23 #
24 # Usage:
25 #  export DC1=dc1_dns_name
26 #  export DC2=dc2_dns_name (RODC)
27 #  export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
28 #  PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN replica_sync_rodc -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
29 #
30
31 import drs_base
32 import samba.tests
33 import time
34 import ldb
35
36 from ldb import (
37     SCOPE_BASE, LdbError, ERR_NO_SUCH_OBJECT)
38
39 class DrsReplicaSyncTestCase(drs_base.DrsBaseTestCase):
40     """Intended as a black box test case for DsReplicaSync
41        implementation. It should test the behavior of this
42        case in cases when inbound replication is disabled"""
43
44     def setUp(self):
45         super(DrsReplicaSyncTestCase, self).setUp()
46         self._disable_all_repl(self.dnsname_dc1)
47         self.ou1 = None
48         self.ou2 = None
49
50     def tearDown(self):
51         # re-enable replication
52         self._enable_all_repl(self.dnsname_dc1)
53
54         super(DrsReplicaSyncTestCase, self).tearDown()
55
56     def _create_ou(self, samdb, name):
57         ldif = """
58 dn: %s,%s
59 objectClass: organizationalUnit
60 """ % (name, self.domain_dn)
61         samdb.add_ldif(ldif)
62         res = samdb.search(base="%s,%s" % (name, self.domain_dn),
63                            scope=SCOPE_BASE, attrs=["objectGUID"])
64         return self._GUID_string(res[0]["objectGUID"][0])
65
66     def _check_deleted(self, sam_ldb, guid):
67         # search the user by guid as it may be deleted
68         res = sam_ldb.search(base='<GUID=%s>' % guid,
69                              controls=["show_deleted:1"],
70                              attrs=["isDeleted", "objectCategory", "ou"])
71         self.assertEquals(len(res), 1)
72         ou_cur = res[0]
73         # Deleted Object base DN
74         dodn = self._deleted_objects_dn(sam_ldb)
75         # now check properties of the user
76         name_cur = ou_cur["ou"][0]
77         self.assertEquals(ou_cur["isDeleted"][0], "TRUE")
78         self.assertTrue(not("objectCategory" in ou_cur))
79         self.assertTrue(dodn in str(ou_cur["dn"]),
80                         "OU %s is deleted but it is not located under %s!" % (name_cur, dodn))
81
82
83     def test_ReplConflictsRODC(self):
84         """Tests that objects created in conflict become conflict DNs"""
85         # Replicate all objects to RODC beforehand
86         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
87
88         # Create conflicting objects on DC1 and DC2, with DC1 object created first
89         name = "OU=Test RODC Conflict"
90         self.ou1 = self._create_ou(self.ldb_dc1, name)
91
92         # Replicate single object
93         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
94                                 nc_dn="%s,%s" % (name, self.domain_dn),
95                                 local=True, single=True, forced=True)
96
97         # Delete the object, so another can be added
98         self.ldb_dc1.delete('<GUID=%s>' % self.ou1)
99
100         # Create a conflicting DN as it would appear to the RODC
101         self.ou2 = self._create_ou(self.ldb_dc1, name)
102
103         try:
104             self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
105                                     nc_dn="%s,%s" % (name, self.domain_dn),
106                                     local=True, single=True, forced=True)
107         except:
108             # Cleanup the object
109             self.ldb_dc1.delete('<GUID=%s>' % self.ou2)
110             return
111
112         # Replicate cannot succeed, HWM would be updated incorrectly.
113         self.fail("DRS replicate should have failed.")
114
115     def test_ReplConflictsRODCRename(self):
116         """Tests that objects created in conflict become conflict DNs"""
117         # Replicate all objects to RODC beforehand
118         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
119
120         # Create conflicting objects on DC1 and DC2, with DC1 object created first
121         name = "OU=Test RODC Rename Conflict"
122         self.ou1 = self._create_ou(self.ldb_dc1, name)
123
124         # Replicate single object
125         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
126                                 nc_dn="%s,%s" % (name, self.domain_dn),
127                                 local=True, single=True, forced=True)
128
129         # Create a non-conflicting DN to rename as conflicting
130         free_name = "OU=Test RODC Rename No Conflict"
131         self.ou2 = self._create_ou(self.ldb_dc1, free_name)
132
133         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
134                                 nc_dn="%s,%s" % (free_name, self.domain_dn),
135                                 local=True, single=True, forced=True)
136
137         # Delete the object, so we can rename freely
138         # DO NOT REPLICATE TO THE RODC
139         self.ldb_dc1.delete('<GUID=%s>' % self.ou1)
140
141         # Collide the name from the RODC perspective
142         self.ldb_dc1.rename("<GUID=%s>" % self.ou2, "%s,%s" % (name, self.domain_dn))
143
144         try:
145             self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
146                                     nc_dn="%s,%s" % (name, self.domain_dn),
147                                     local=True, single=True, forced=True)
148         except:
149             # Cleanup the object
150             self.ldb_dc1.delete('<GUID=%s>' % self.ou2)
151             return
152
153         # Replicate cannot succeed, HWM would be updated incorrectly.
154         self.fail("DRS replicate should have failed.")