From fce07e7d0dba9f77cd7f24b2213bed81a999f4e5 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Tue, 06 Aug 2024 12:41:06 +0000
Subject: [PATCH] [#250] Add Overlapping Backend TestSuite (#373)

---
 opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/Issue84TestSuite.java            |    2 
 opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/OverlappingBackendTestSuite.java |  129 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/Issue84TestSuite.java b/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/Issue84TestSuite.java
index 10af17e..5b0c7fb 100644
--- a/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/Issue84TestSuite.java
+++ b/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/Issue84TestSuite.java
@@ -77,6 +77,7 @@
         return connection;
     }
 
+    @Test
     public void test_user() throws LdapException {
         try(Connection connection=getConnection("uid=user.1,ou=People,o=test","password")){
             final SearchRequest request =Requests.newSearchRequest("ou=Services,o=test", SearchScope.WHOLE_SUBTREE,"(&)");
@@ -88,6 +89,7 @@
         }
     }
 
+    @Test
     public void test_service() throws LdapException, SearchResultReferenceIOException {
         try(Connection connection=getConnection("uid=service.1,ou=Services,o=test","password")){
             final SearchRequest request =Requests.newSearchRequest("ou=Services,o=test", SearchScope.WHOLE_SUBTREE,"(&)");
diff --git a/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/OverlappingBackendTestSuite.java b/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/OverlappingBackendTestSuite.java
new file mode 100644
index 0000000..4294802
--- /dev/null
+++ b/opendj-server-legacy/src/test/java/org/openidentityplatform/opendj/OverlappingBackendTestSuite.java
@@ -0,0 +1,129 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development and
+ * Distribution License (the License). You may not use this file except in compliance with the
+ * License.
+ *
+ * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
+ * specific language governing permission and limitations under the License.
+ *
+ * When distributing Covered Software, include this CDDL Header Notice in each file and include
+ * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
+ * Header, with the fields enclosed by brackets [] replaced by your own identifying
+ * information: "Portions Copyright [year] [name of copyright owner]".
+ *
+ * Copyright 2024 3A Systems, LLC.
+ */
+package org.openidentityplatform.opendj;
+import org.forgerock.opendj.ldap.*;
+import org.forgerock.opendj.ldap.requests.Requests;
+import org.forgerock.opendj.ldap.requests.SearchRequest;
+import org.forgerock.opendj.ldap.responses.SearchResultEntry;
+import org.forgerock.opendj.ldif.ConnectionEntryReader;
+import org.opends.server.DirectoryServerTestCase;
+import org.opends.server.TestCaseUtils;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Test(sequential = true)
+public class OverlappingBackendTestSuite extends DirectoryServerTestCase {
+
+    @BeforeClass
+    public void startServer() throws Exception {
+        TestCaseUtils.startServer();
+
+        TestCaseUtils.initializeTestBackend(true);
+
+        TestCaseUtils.addEntries(
+                "dn: ou=es,o=test",
+                "objectClass: organizationalUnit",
+                "objectClass: top",
+                "ou: es",
+                "",
+                "dn: o=test,ou=es,o=test",
+                "objectClass: organization",
+                "objectClass: top",
+                "o: test",
+                "",
+                "dn: uid=user.1,o=test,ou=es,o=test",
+                "objectClass: person",
+                "objectClass: organizationalPerson",
+                "objectClass: inetOrgPerson",
+                "objectClass: top",
+                "cn: Aaren Atp",
+                "sn: Atp",
+                "uid: user.1",
+                "userPassword: password",
+                ""
+        );
+    }
+
+    Set<String> search(final String base) throws LdapException, SearchResultReferenceIOException {
+        final LDAPConnectionFactory factory =new LDAPConnectionFactory("localhost", TestCaseUtils.getServerLdapPort());
+        final Connection connection = factory.getConnection();
+        connection.bind("cn=Directory Manager", "password".toCharArray());
+        assertThat(connection.isValid()).isTrue();
+
+        Set<String> res=new HashSet<>();
+        final SearchRequest request =Requests.newSearchRequest(base, SearchScope.WHOLE_SUBTREE,"(&)");
+        System.out.println("---------------------------------------------------------------------------------------");
+        System.out.println(request);
+        final ConnectionEntryReader reader = connection.search(request);
+        while(reader.hasNext()) {
+            final SearchResultEntry entry=reader.readEntry();
+            System.out.println(entry);
+            assertThat(entry).isNotNull();
+            res.add(entry.getName().toString());
+        }
+        connection.close();
+        factory.close();
+        return res;
+    }
+
+    void hasUserRoot(Set<String> res) {
+        assertThat(res.contains("o=test")).isTrue();
+        assertThat(res.contains("ou=es,o=test")).isTrue();
+        assertThat(res.contains("o=test,ou=es,o=test")).isTrue();
+        assertThat(res.contains("uid=user.1,o=test,ou=es,o=test")).isTrue();
+    }
+
+    void hasUserRoot2base(Set<String> res) {
+        assertThat(res.contains("ou=eus,o=test")).isTrue();
+    }
+
+    void hasUserRoot2(Set<String> res) {
+        hasUserRoot2base(res);
+        assertThat(res.contains("o=test,ou=eus,o=test")).isTrue();
+        assertThat(res.contains("uid=user.2,o=test,ou=eus,o=test")).isTrue();
+    }
+    @Test
+    public void test_userRoot2() throws Exception {
+        hasUserRoot(search("o=test"));
+        TestCaseUtils.initializeMemoryBackend("userRoot2","ou=eus,o=test",true);
+        hasUserRoot(search("o=test"));
+        hasUserRoot2base(search("o=test"));
+        TestCaseUtils.addEntries(
+              "dn: o=test,ou=eus,o=test",
+              "objectClass: organization",
+              "objectClass: top",
+              "o: test",
+              "",
+              "dn: uid=user.2,o=test,ou=eus,o=test",
+              "objectClass: top",
+              "objectClass: account",
+              "objectClass: simpleSecurityObject",
+              "uid: user.2",
+              "userPassword: password",
+              ""
+        );
+        hasUserRoot(search("o=test"));
+        hasUserRoot2(search("o=test"));
+        hasUserRoot2(search("ou=eus,o=test"));
+      }
+}

--
Gitblit v1.10.0