tests/replica_sync_rodc: Test conflict handling on an RODC
[metze/samba/wip.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_inbound_repl(self.dnsname_dc1)
47         self._disable_all_repl(self.dnsname_dc1)
48         self.ou1 = None
49         self.ou2 = None
50
51     def tearDown(self):
52         # re-enable replication
53         self._enable_inbound_repl(self.dnsname_dc1)
54         self._enable_all_repl(self.dnsname_dc1)
55
56         super(DrsReplicaSyncTestCase, self).tearDown()
57
58     def _create_ou(self, samdb, name):
59         ldif = """
60 dn: %s,%s
61 objectClass: organizationalUnit
62 """ % (name, self.domain_dn)
63         samdb.add_ldif(ldif)
64         res = samdb.search(base="%s,%s" % (name, self.domain_dn),
65                            scope=SCOPE_BASE, attrs=["objectGUID"])
66         return self._GUID_string(res[0]["objectGUID"][0])
67
68     def _check_deleted(self, sam_ldb, guid):
69         # search the user by guid as it may be deleted
70         res = sam_ldb.search(base='<GUID=%s>' % guid,
71                              controls=["show_deleted:1"],
72                              attrs=["isDeleted", "objectCategory", "ou"])
73         self.assertEquals(len(res), 1)
74         ou_cur = res[0]
75         # Deleted Object base DN
76         dodn = self._deleted_objects_dn(sam_ldb)
77         # now check properties of the user
78         name_cur  = ou_cur["ou"][0]
79         self.assertEquals(ou_cur["isDeleted"][0],"TRUE")
80         self.assertTrue(not("objectCategory" in ou_cur))
81         self.assertTrue(dodn in str(ou_cur["dn"]),
82                         "OU %s is deleted but it is not located under %s!" % (name_cur, dodn))
83
84
85     def test_ReplConflictsRODC(self):
86         """Tests that objects created in conflict become conflict DNs"""
87         # Replicate all objects to RODC beforehand
88         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
89
90         # Create conflicting objects on DC1 and DC2, with DC1 object created first
91         name = "OU=Test RODC Conflict"
92         self.ou1 = self._create_ou(self.ldb_dc1, name)
93
94         # Replicate single object
95         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
96                                 nc_dn="%s,%s" % (name, self.domain_dn),
97                                 local=True, single=True, forced=True)
98
99         # Delete the object, so another can be added
100         self.ldb_dc1.delete('<GUID=%s>' % self.ou1)
101
102         # Create a conflicting DN as it would appear to the RODC
103         self.ou2 = self._create_ou(self.ldb_dc1, name)
104
105         try:
106             self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
107                                     nc_dn="%s,%s" % (name, self.domain_dn),
108                                     local=True, single=True, forced=True)
109         except:
110             # Cleanup the object
111             self.ldb_dc1.delete('<GUID=%s>' % self.ou2)
112             return
113
114         # Replicate cannot succeed, HWM would be updated incorrectly.
115         self.fail("DRS replicate should have failed.")
116
117     def test_ReplConflictsRODCRename(self):
118         """Tests that objects created in conflict become conflict DNs"""
119         # Replicate all objects to RODC beforehand
120         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
121
122         # Create conflicting objects on DC1 and DC2, with DC1 object created first
123         name = "OU=Test RODC Rename Conflict"
124         self.ou1 = self._create_ou(self.ldb_dc1, name)
125
126         # Replicate single object
127         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
128                                 nc_dn="%s,%s" % (name, self.domain_dn),
129                                 local=True, single=True, forced=True)
130
131         # Create a non-conflicting DN to rename as conflicting
132         free_name = "OU=Test RODC Rename No Conflict"
133         self.ou2 = self._create_ou(self.ldb_dc1, free_name)
134
135         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
136                                 nc_dn="%s,%s" % (free_name, self.domain_dn),
137                                 local=True, single=True, forced=True)
138
139         # Delete the object, so we can rename freely
140         # DO NOT REPLICATE TO THE RODC
141         self.ldb_dc1.delete('<GUID=%s>' % self.ou1)
142
143         # Collide the name from the RODC perspective
144         self.ldb_dc1.rename("<GUID=%s>" % self.ou2, "%s,%s" % (name, self.domain_dn))
145
146         try:
147             self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1,
148                                     nc_dn="%s,%s" % (name, self.domain_dn),
149                                     local=True, single=True, forced=True)
150         except:
151             # Cleanup the object
152             self.ldb_dc1.delete('<GUID=%s>' % self.ou2)
153             return
154
155         # Replicate cannot succeed, HWM would be updated incorrectly.
156         self.fail("DRS replicate should have failed.")