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