f36232e0a53eedbaeafc699f354c67b8cf7aa273
[mat/samba.git] / source4 / torture / drs / python / delete_object.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Unix SMB/CIFS implementation.
5 # Copyright (C) Kamen Mazdrashki <kamenim@samba.org> 2010
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 #
22 # Usage:
23 #  export DC1=dc1_dns_name
24 #  export DC2=dc2_dns_name
25 #  export SUBUNITRUN=$samba4srcdir/scripting/bin/subunitrun
26 #  PYTHONPATH="$PYTHONPATH:$samba4srcdir/torture/drs/python" $SUBUNITRUN delete_object -U"$DOMAIN/$DC_USERNAME"%"$DC_PASSWORD"
27 #
28
29 import time
30
31
32 from ldb import (
33     SCOPE_SUBTREE
34     )
35
36 import drs_base
37
38
39 class DrsDeleteObjectTestCase(drs_base.DrsBaseTestCase):
40
41     def setUp(self):
42         super(DrsDeleteObjectTestCase, self).setUp()
43         # make sure DCs are synchronized before the test
44         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
45         self._net_drs_replicate(DC=self.dnsname_dc1, fromDC=self.dnsname_dc2, forced=True)
46         # disable automatic replication temporary
47         self._disable_inbound_repl(self.dnsname_dc1)
48         self._disable_inbound_repl(self.dnsname_dc2)
49
50     def tearDown(self):
51         self._enable_inbound_repl(self.dnsname_dc1)
52         self._enable_inbound_repl(self.dnsname_dc2)
53         super(DrsDeleteObjectTestCase, self).tearDown()
54
55     def _make_username(self):
56         return "DrsDelObjUser_" + time.strftime("%s", time.gmtime())
57
58     def _check_user(self, sam_ldb, user_orig, is_deleted):
59         # search the user by guid as it may be deleted
60         guid_str = self._GUID_string(user_orig["objectGUID"][0])
61         expression = "(objectGUID=%s)" % guid_str
62         res = sam_ldb.search(base=self.domain_dn,
63                              expression=expression,
64                              controls=["show_deleted:1"])
65         self.assertEquals(len(res), 1)
66         user_cur = res[0]
67         # Deleted Object base DN
68         dodn = self._deleted_objects_dn(sam_ldb)
69         # now check properties of the user
70         name_orig = user_orig["cn"][0]
71         name_cur  = user_cur["cn"][0]
72         if is_deleted:
73             self.assertEquals(user_cur["isDeleted"][0],"TRUE")
74             self.assertTrue(not("objectCategory" in user_cur))
75             self.assertTrue(not("sAMAccountType" in user_cur))
76             self.assertTrue(dodn in str(user_cur["dn"]),
77                             "User %s is deleted but it is not located under %s (found at %s)!" % (name_orig, dodn, user_cur["dn"]))
78             self.assertEquals(name_cur, name_orig + "\nDEL:" + guid_str)
79         else:
80             self.assertTrue(not("isDeleted" in user_cur))
81             self.assertEquals(name_cur, name_orig)
82             self.assertEquals(user_orig["dn"], user_cur["dn"])
83             self.assertTrue(dodn not in str(user_cur["dn"]))
84
85     def test_ReplicateDeteleteObject(self):
86         """Verifies how a deleted-object is replicated between two DCs.
87            This test should verify that:
88             - deleted-object is replicated properly
89            TODO: We should verify that after replication,
90                  object's state to conform to a deleted-object state
91                  or tombstone -object, depending on DC's features
92                  It will also be great if check replPropertyMetaData."""
93         # work-out unique username to test with
94         username = self._make_username()
95
96         # create user on DC1
97         self.ldb_dc1.newuser(username=username, password="P@sswOrd!")
98         ldb_res = self.ldb_dc1.search(base=self.domain_dn,
99                                       scope=SCOPE_SUBTREE,
100                                       expression="(samAccountName=%s)" % username)
101         self.assertEquals(len(ldb_res), 1)
102         user_orig = ldb_res[0]
103         user_dn   = ldb_res[0]["dn"]
104
105         # check user info on DC1
106         print "Testing for %s with GUID %s" % (username, self._GUID_string(user_orig["objectGUID"][0]))
107         self._check_user(sam_ldb=self.ldb_dc1, user_orig=user_orig, is_deleted=False)
108
109         # trigger replication from DC1 to DC2
110         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
111
112         # delete user on DC1
113         self.ldb_dc1.delete(user_dn)
114         # check user info on DC1 - should be deleted
115         self._check_user(sam_ldb=self.ldb_dc1, user_orig=user_orig, is_deleted=True)
116         # check user info on DC2 - should be valid user
117         self._check_user(sam_ldb=self.ldb_dc2, user_orig=user_orig, is_deleted=False)
118
119         # trigger replication from DC2 to DC1
120         # to check if deleted object gets restored
121         self._net_drs_replicate(DC=self.dnsname_dc1, fromDC=self.dnsname_dc2, forced=True)
122         # check user info on DC1 - should be deleted
123         self._check_user(sam_ldb=self.ldb_dc1, user_orig=user_orig, is_deleted=True)
124         # check user info on DC2 - should be valid user
125         self._check_user(sam_ldb=self.ldb_dc2, user_orig=user_orig, is_deleted=False)
126
127         # trigger replication from DC1 to DC2
128         # to check if deleted object is replicated
129         self._net_drs_replicate(DC=self.dnsname_dc2, fromDC=self.dnsname_dc1, forced=True)
130         # check user info on DC1 - should be deleted
131         self._check_user(sam_ldb=self.ldb_dc1, user_orig=user_orig, is_deleted=True)
132         # check user info on DC2 - should be deleted
133         self._check_user(sam_ldb=self.ldb_dc2, user_orig=user_orig, is_deleted=True)
134