From e7575a3dec0c906fada777fe340c9ed767d3e173 Mon Sep 17 00:00:00 2001
From: Yannick Lecaillez <yannick.lecaillez@forgerock.com>
Date: Mon, 05 Sep 2016 09:30:52 +0000
Subject: [PATCH] Fix: isChild() mistakenly report parent-child relationship.

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/DnKeyFormat.java     |   12 +++++++-----
 opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/DnKeyFormat.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/DnKeyFormat.java
index 87403d2..5d4981e 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/DnKeyFormat.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/DnKeyFormat.java
@@ -118,23 +118,25 @@
    */
   static boolean isChild(ByteSequence parent, ByteSequence child)
   {
-    if (!child.startsWith(parent))
+    if (child.length() <= parent.length()
+        || child.byteAt(parent.length()) != NORMALIZED_RDN_SEPARATOR
+        || !child.startsWith(parent))
     {
       return false;
     }
     // Immediate children should only have one RDN separator past the parent length
-    int nbSeparator = 0;
+    boolean childSeparatorDetected = false;
     for (int i = parent.length() ; i < child.length(); i++)
     {
       if (child.byteAt(i) == NORMALIZED_RDN_SEPARATOR)
       {
-        nbSeparator++;
-        if (nbSeparator > 1)
+        if (childSeparatorDetected)
         {
           return false;
         }
+        childSeparatorDetected = true;
       }
     }
-    return nbSeparator == 1;
+    return childSeparatorDetected;
   }
 }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java b/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
index cd51450..b91a4c1 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/backends/pluggable/TestDnKeyFormat.java
@@ -497,6 +497,46 @@
     assertThat(DnKeyFormat.findDNKeyParent(dnKey)).isEqualTo(expectedLength);
   }
 
+  @DataProvider
+  private Object[][] testIsChildData()
+  {
+    return new Object[][]
+    {
+      {           "dc=example,dc=com\\,org", // parentDn
+        "ou=people,dc=example,dc=com\\,org", // childDn
+        true },                              // Is childDn a child of parentDn ?
+
+      { "dc=example,dc=com",
+                   "dc=com",
+        false },
+
+      {  "ou=people,dc=example,dc=com",
+        "ou=people1,dc=example,dc=com",
+        false },
+
+      {                      "dc=example,dc=com",
+        "uid=user.0,ou=people,dc=example,dc=com",
+        false },
+
+      {           "dc=example,dc=com",
+        "ou=people,dc=elpmaxe,dc=com",
+        false },
+
+      { "dc=example,dc=com",
+        "dc=example,dc=com",
+        false },
+    };
+  }
+
+  @Test(dataProvider="testIsChildData")
+  public void testIsChild(String parentDn, String childDn, boolean expected) {
+    assertThat(
+      DnKeyFormat.isChild(
+          DnKeyFormat.dnToDNKey(DN.valueOf(parentDn), 0),
+          DnKeyFormat.dnToDNKey(DN.valueOf(childDn), 0))
+      ).isEqualTo(expected);
+  }
+
   private void ensureServerIsUpAndRunning() throws Exception
   {
     TestCaseUtils.startServer();

--
Gitblit v1.10.0