From 23b867c70bd995ca6eedcf0b72a24c043e8e7fb0 Mon Sep 17 00:00:00 2001
From: Andrew Bartlett <abartlet@samba.org>
Date: Mon, 7 Aug 2023 13:15:40 +1200
Subject: [PATCH 20/30] CVE-2023-4154 dsdb/tests: Use self.addCleanup() and
 delete_force()

Thie helps ensure this test is reliable even in spite of errors while
running.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15424

Signed-off-by: Andrew Bartlett <abartlet@samba.org>

Conflict: NA
Reference: https://download.samba.org/pub/samba/patches/security/samba-4.17.12-security-2023-10-10.patch
[PATCH 20/30] CVE-2023-4154 dsdb/tests: Use self.addCleanup() and
 delete_force()
---
 source4/dsdb/tests/python/confidential_attr.py | 6 ++----
 source4/dsdb/tests/python/dirsync.py           | 6 +-----
 2 files changed, 3 insertions(+), 9 deletions(-)

diff --git a/source4/dsdb/tests/python/confidential_attr.py b/source4/dsdb/tests/python/confidential_attr.py
index 8ca56bd1023..3997848f8f9 100755
--- a/source4/dsdb/tests/python/confidential_attr.py
+++ b/source4/dsdb/tests/python/confidential_attr.py
@@ -98,7 +98,9 @@ class ConfidentialAttrCommon(samba.tests.TestCase):
 
         userou = "OU=conf-attr-test"
         self.ou = "{0},{1}".format(userou, self.base_dn)
+        samba.tests.delete_force(self.ldb_admin, self.ou, controls=['tree_delete:1'])
         self.ldb_admin.create_ou(self.ou)
+        self.addCleanup(samba.tests.delete_force, self.ldb_admin, self.ou, controls=['tree_delete:1'])
 
         # use a common username prefix, so we can use sAMAccountName=CATC-* as
         # a search filter to only return the users we're interested in
@@ -139,10 +141,6 @@ class ConfidentialAttrCommon(samba.tests.TestCase):
                          "{0} searchFlags already {1}".format(self.conf_attr,
                                                               search_flags))
 
-    def tearDown(self):
-        super(ConfidentialAttrCommon, self).tearDown()
-        self.ldb_admin.delete(self.ou, ["tree_delete:1"])
-
     def add_attr(self, dn, attr, value):
         m = Message()
         m.dn = Dn(self.ldb_admin, dn)
diff --git a/source4/dsdb/tests/python/dirsync.py b/source4/dsdb/tests/python/dirsync.py
index ad136b7efee..e06b85bc749 100755
--- a/source4/dsdb/tests/python/dirsync.py
+++ b/source4/dsdb/tests/python/dirsync.py
@@ -137,10 +137,6 @@ class SimpleDirsyncTests(DirsyncBaseTests):
         if self.ouname:
             delete_force(self.ldb_admin, self.ouname)
         self.sd_utils.modify_sd_on_dn(self.base_dn, self.desc_sddl)
-        try:
-            self.ldb_admin.deletegroup("testgroup")
-        except Exception:
-            pass
 
     # def test_dirsync_errors(self):
 
@@ -499,6 +495,7 @@ class SimpleDirsyncTests(DirsyncBaseTests):
         self.assertEqual(len(res[0].get("member")), size)
 
         self.ldb_admin.newgroup("testgroup")
+        self.addCleanup(self.ldb_admin.deletegroup, "testgroup")
         self.ldb_admin.add_remove_group_members("testgroup", [self.simple_user],
                                                 add_members_operation=True)
 
@@ -537,7 +534,6 @@ class SimpleDirsyncTests(DirsyncBaseTests):
                                     attrs=["member"],
                                     controls=[control1])
 
-        self.ldb_admin.deletegroup("testgroup")
         self.assertEqual(len(res[0].get("member")), 0)
 
     def test_dirsync_deleted_items(self):
-- 
2.34.1
