From 45336c8745f2183dee07bbbb8ba8f41a57219da9 Mon Sep 17 00:00:00 2001
From: Sankar Ramalingam <sramling@redhat.com>
Date: Fri, 9 Jun 2017 20:07:03 +0530
Subject: [PATCH] Ticket #48133 - Non tombstone entry which dn starting with "nsuniqueid=...," cannot be deleted

Description: Checking if dn entries starting with "nsuniqueid=...," can be deleted

https://pagure.io/389-ds-base/issue/48133

Reviewed by: ?
---
 dirsrvtests/tests/tickets/ticket48133_test.py | 100 ++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 dirsrvtests/tests/tickets/ticket48133_test.py

diff --git a/dirsrvtests/tests/tickets/ticket48133_test.py b/dirsrvtests/tests/tickets/ticket48133_test.py
new file mode 100644
index 0000000..7d6b571
--- /dev/null
+++ b/dirsrvtests/tests/tickets/ticket48133_test.py
@@ -0,0 +1,100 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2017 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+#
+import pytest
+from lib389.tasks import *
+from lib389.utils import *
+from ldif import LDIFParser, LDIFWriter
+from lib389.topologies import topology_m2 as topo
+
+DEBUGGING = os.getenv("DEBUGGING", default=False)
+if DEBUGGING:
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
+else:
+    logging.getLogger(__name__).setLevel(logging.INFO)
+log = logging.getLogger(__name__)
+
+
+class MyLDIF(LDIFParser):
+    def __init__(self, input, output):
+        LDIFParser.__init__(self, input)
+        self.writer = LDIFWriter(output)
+
+    def handle(self, dn, entry):
+        if 'tuser' in dn:
+            dn = dn.replace(",cn=tuser", "")
+            global dn_to_delete
+            dn_to_delete = dn
+            del entry['nsParentUniqueId']
+            for k, v in entry.items():
+                if v == ["nsTombstone"]:
+                    del entry[k]
+        self.writer.unparse(dn, entry)
+
+
+def test_delete_dn_with_nsuniqueid(topo):
+    """Non tombstone entry which dn starting with nsuniqueid=..., cannot be deleted
+
+    :ID: 7c711afa-5eb7-4aa5-a068-ef6f7f30b729
+    :feature: Replication
+    :setup: Two master replication setup
+    :steps: 1. Create user and delete it afterwards to create tombstone entry
+            2. Run db2ldif to emport tombstone entries to LDIF file
+            3. Edit LDIF file and remove oc tombstone and parentuniqueid from the entry
+            4. Run ldif2db to import entries to Server
+            5. Check if entries starting with nsuniqueid= can be deleted
+    :expectedresults: Dn entries starting with nsuniqueid= should be deleted successfully
+    """
+
+    if DEBUGGING:
+        topo.ms['master1'].config.set('nsslapd-auditlog-logging-enabled', 'on')
+        topo.ms['master1'].config.set('nsslapd-errorlog-level', '8192')
+
+    USER_DN = 'cn=tuser,ou=People,{}'.format(DEFAULT_SUFFIX)
+    try:
+        topo.ms['master1'].add_s(Entry((USER_DN, {
+            'objectclass': 'inetorgperson extensibleObject'.split(),
+            'cn': 'tuser',
+            'sn': 'tuser',
+        })))
+    except ldap.LDAPError as e:
+        log.error('Failed to add user {}: error {}'.format(USER_DN, e.message['desc']))
+        raise e
+
+    log.info('Create tombstone entry by deleting an entry in Master1')
+    topo.ms['master1'].delete_s(USER_DN)
+
+    log.info('Stop master and export entries to LDIF file')
+    topo.ms['master1'].stop()
+    topo.ms['master1'].db2ldif(bename=DEFAULT_BENAME, suffixes=[DEFAULT_SUFFIX], excludeSuffixes=[], encrypt=False,
+                               repl_data=True, outputfile='/tmp/tmp.ldif')
+
+    log.info('Parse LDIF file')
+    with open("/tmp/tmp.ldif", 'rb') as in_ldif, open("/tmp/out.ldif", 'wb') as out_ldif:
+        parser = MyLDIF(in_ldif, out_ldif)
+        parser.parse()
+
+    log.info('Import LDIF file to server')
+    topo.ms['master1'].ldif2db(DEFAULT_BENAME, None, None, None, '/tmp/out.ldif')
+
+    log.info('Start the server')
+    topo.ms['master1'].start()
+
+    log.info('Delete entry starting with nsuniqueid')
+    try:
+        topo.ms['master1'].delete_s(dn_to_delete)
+    except ldap.OPERATIONS_ERROR as e:
+        log.error('Failed to delete an entry starting with nsuniqueid: ' + e.message['desc'])
+        assert False
+
+
+if __name__ == '__main__':
+    # Run isolated
+    # -s for DEBUG mode
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s {}".format(CURRENT_FILE))
-- 
2.7.4

