From 3a43d0841a820eb53b7337704585337768ef60b8 Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Thu, 27 May 2010 14:59:40 +0000
Subject: [PATCH] Fix issue #4468. Now values in filters and indexes are normalized according to the MR used and not the equality MR.

---
 opendj-sdk/opends/src/server/org/opends/server/backends/jeb/SubstringIndexer.java                          |   15 ++++-
 opendj-sdk/opends/src/server/org/opends/server/replication/plugin/HistoricalCsnOrderingMatchingRule.java   |   11 ---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/AttributeTypeSyntaxTest.java |   73 ++++++++++++++++++++++++
 opendj-sdk/opends/src/server/org/opends/server/types/AttributeBuilder.java                                 |   16 +++--
 opendj-sdk/opends/src/server/org/opends/server/controls/MatchedValuesFilter.java                           |   13 ++-
 5 files changed, 104 insertions(+), 24 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/SubstringIndexer.java b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/SubstringIndexer.java
index bcc55e0..f223a00 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/SubstringIndexer.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/backends/jeb/SubstringIndexer.java
@@ -31,6 +31,7 @@
 import org.opends.server.types.*;
 
 import java.util.*;
+import org.opends.server.api.SubstringMatchingRule;
 
 /**
  * An implementation of an Indexer for attribute substrings.
@@ -161,18 +162,21 @@
                               Set<byte[]> keys)
   {
     if (attrList == null) return;
-
     for (Attribute attr : attrList)
     {
       if (attr.isVirtual())
       {
         continue;
       }
+      //Get the substring matching rule.
+      SubstringMatchingRule rule =
+              attr.getAttributeType().getSubstringMatchingRule();
       for (AttributeValue value : attr)
       {
         try
         {
-          byte[] normalizedBytes = value.getNormalizedValue().toByteArray();
+          byte[] normalizedBytes = rule.normalizeValue(value.getValue()).
+                  toByteArray();
 
           substringKeys(normalizedBytes, keys);
         }
@@ -249,11 +253,16 @@
       {
         continue;
       }
+            //Get the substring matching rule.
+      SubstringMatchingRule rule =
+              attr.getAttributeType().getSubstringMatchingRule();
+
       for (AttributeValue value : attr)
       {
         try
         {
-          byte[] normalizedBytes = value.getNormalizedValue().toByteArray();
+          byte[] normalizedBytes = rule.normalizeValue(value.getValue())
+                  .toByteArray();
 
           substringKeys(normalizedBytes, modifiedKeys, insert);
         }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/controls/MatchedValuesFilter.java b/opendj-sdk/opends/src/server/org/opends/server/controls/MatchedValuesFilter.java
index 2895360..0297566 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/controls/MatchedValuesFilter.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/controls/MatchedValuesFilter.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.controls;
 import org.opends.messages.Message;
@@ -1357,7 +1357,8 @@
                  new ArrayList<ByteSequence>(normalizedSubAny);
 
             return substringMatchingRule.valueMatchesSubstring(
-                 value.getNormalizedValue(), normalizedSubInitial,
+                 substringMatchingRule.normalizeValue(value.getValue()),
+                 normalizedSubInitial,
                  normalizedSubAnyBS, normalizedSubFinal);
           }
           catch (Exception e)
@@ -1385,7 +1386,8 @@
           {
             return (orderingMatchingRule.compareValues(
                          assertionValue.getNormalizedValue(),
-                         value.getNormalizedValue()) >= 0);
+                         orderingMatchingRule.normalizeValue(
+                         value.getValue())) >= 0);
           }
           catch (Exception e)
           {
@@ -1412,7 +1414,8 @@
           {
             return (orderingMatchingRule.compareValues(
                          assertionValue.getNormalizedValue(),
-                         value.getNormalizedValue()) <= 0);
+                         orderingMatchingRule.normalizeValue(
+                         value.getValue())) <= 0);
           }
           catch (Exception e)
           {
@@ -1445,7 +1448,7 @@
             ByteString nv1 =  approximateMatchingRule.normalizeValue(
                     assertionValue.getNormalizedValue());
             ByteString nv2 =  approximateMatchingRule.normalizeValue(
-                    value.getNormalizedValue());
+                    approximateMatchingRule.normalizeValue(value.getValue()));
 
             return approximateMatchingRule.approximatelyMatch(nv1, nv2);
           }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/HistoricalCsnOrderingMatchingRule.java b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/HistoricalCsnOrderingMatchingRule.java
index c3aead8..5ec8b1a 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/HistoricalCsnOrderingMatchingRule.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/HistoricalCsnOrderingMatchingRule.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.replication.plugin;
 
@@ -67,13 +67,7 @@
    */
   public int compareValues(ByteSequence value1, ByteSequence value2)
   {
-    String[] token1 = value1.toString().split(":", 3);
-    String[] token2 = value2.toString().split(":", 3);
-
-    if ((token1[1] == null) || (token2[1] == null))
-      return -1;
-
-    return token1[1].compareTo(token2[1]);
+    return value1.compareTo(value2);
   }
 
 
@@ -136,7 +130,6 @@
   @Override
   public ByteString normalizeValue(ByteSequence value)
   {
-
     String[] token = value.toString().split(":", 3);
 
     /* Change the format of the value to index and start
diff --git a/opendj-sdk/opends/src/server/org/opends/server/types/AttributeBuilder.java b/opendj-sdk/opends/src/server/org/opends/server/types/AttributeBuilder.java
index 692e060..1c7d9b7 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/types/AttributeBuilder.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/types/AttributeBuilder.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.types;
 
@@ -275,7 +275,8 @@
       ByteString normalizedValue;
       try
       {
-        normalizedValue = value.getNormalizedValue();
+        normalizedValue =
+                matchingRule.normalizeValue(value.getValue());
       }
       catch (Exception e)
       {
@@ -294,7 +295,8 @@
       {
         try
         {
-          ByteString nv = v.getNormalizedValue();
+          ByteString nv =
+                  matchingRule.normalizeValue(v.getValue());
           int comparisonResult = matchingRule
               .compareValues(nv, normalizedValue);
           if (comparisonResult >= 0)
@@ -357,7 +359,8 @@
       ByteString normalizedValue;
       try
       {
-        normalizedValue = value.getNormalizedValue();
+        normalizedValue =
+                matchingRule.normalizeValue(value.getValue());
       }
       catch (Exception e)
       {
@@ -376,7 +379,7 @@
       {
         try
         {
-          ByteString nv = v.getNormalizedValue();
+          ByteString nv = matchingRule.normalizeValue(v.getValue());
           int comparisonResult = matchingRule
               .compareValues(nv, normalizedValue);
           if (comparisonResult <= 0)
@@ -502,7 +505,8 @@
         try
         {
           if (matchingRule.valueMatchesSubstring(
-              value.getNormalizedValue(),
+              attributeType.getSubstringMatchingRule().
+                    normalizeValue(value.getValue()),
               normalizedSubInitial,
               normalizedSubAny,
               normalizedSubFinal))
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/AttributeTypeSyntaxTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/AttributeTypeSyntaxTest.java
index 425b3c8..d6cee59 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/AttributeTypeSyntaxTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/schema/AttributeTypeSyntaxTest.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.schema;
 
@@ -34,6 +34,15 @@
 import org.opends.server.types.AttributeType;
 import org.opends.server.types.ByteString;
 import org.opends.messages.MessageBuilder;
+import java.util.ArrayList;
+import org.opends.server.TestCaseUtils;
+import org.opends.server.protocols.internal.InternalClientConnection;
+import org.opends.server.protocols.internal.InternalSearchOperation;
+import org.opends.server.protocols.ldap.LDAPFilter;
+import org.opends.server.types.Control;
+import org.opends.server.types.DereferencePolicy;
+import org.opends.server.types.ResultCode;
+import org.opends.server.types.SearchScope;
 
 import static org.testng.Assert.*;
 
@@ -149,5 +158,67 @@
     assertNotNull(attrType.getApproximateMatchingRule());
     assertEquals(attrType.getApproximateMatchingRule(), testApproxRule);
   }
+
+
+
+  /**
+   * Tests a situation when two radically different equality and substring
+   * matching rules (such as Case Ignore and Case Exact) are used to define an
+   * attribute description. For more information, look at the issue# 4468 in
+   * Issue Tracker.
+   *
+   * @throws Exception In case of an error.
+   */
+  @Test()
+  public void testMixedEqualityAndSubstringMatchingRules() throws Exception
+  {
+    //Add an attribute with directory string syntax.
+    int  resultCode = TestCaseUtils.applyModifications(true,
+      "dn: cn=schema",
+      "changetype: modify",
+      "add: attributetypes",
+      "attributeTypes: ( gvRights-oid NAME 'gvRights' DESC 'x attr' EQUALITY " +
+      "caseIgnoreMatch ORDERING caseIgnoreOrderingMatch SUBSTR " +
+      "caseExactSubstringsMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 " +
+      "USAGE userApplications )",
+      "-",
+      "add: objectclasses",
+      "objectClasses: ( gvRightsTest-oid NAME 'gvRightsTest' DESC 'Test' SUP top AUXILIARY " +
+      "MUST ( objectClass $ gvRights ) )");
+    assertTrue(resultCode == 0);
+    TestCaseUtils.initializeTestBackend(true);
+    //add the test entry.
+    TestCaseUtils.addEntry(
+    "dn: cn=gvrightstest,o=test",
+    "objectclass: person",
+    "objectclass: gvRightsTest",
+    "cn: gvrightstest",
+    "sn: test",
+    "gvRights: gvApplId=test2,ou=Applications,dc=bla$test2-T");
+
+    //Search for the entry using substring matching rule filter.
+    InternalClientConnection conn =
+       InternalClientConnection.getRootConnection();
+
+    InternalSearchOperation searchOperation =
+       new InternalSearchOperation(
+            conn,
+            InternalClientConnection.nextOperationID(),
+            InternalClientConnection.nextMessageID(),
+            new ArrayList<Control>(),
+            ByteString.valueOf("cn=gvrightstest,o=test"),
+            SearchScope.WHOLE_SUBTREE,
+            DereferencePolicy.NEVER_DEREF_ALIASES,
+            Integer.MAX_VALUE,
+            Integer.MAX_VALUE,
+            false,
+            LDAPFilter.decode("(&(gvrights=*ApplId=test2,ou=*)" +
+            "(gvrights=*test2,ou=A*))"),
+            null, null);
+
+    searchOperation.run();
+    assertEquals(searchOperation.getResultCode(), ResultCode.SUCCESS);
+    assertEquals(searchOperation.getEntriesSent(), 1);
+  }
 }
 

--
Gitblit v1.10.0