From 5af30c2b6e071cc0c857d122f9cdb6821bb365ff Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 16 Jun 2011 10:43:16 +0000
Subject: [PATCH] Partial fix for OPENDJ-198: RFC 4512 compliance for ldap-toolkit

---
 opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptionsTest.java |  143 +++++++++++++++++++++++++++++++++++
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptions.java     |   62 ++++++++++++++-
 2 files changed, 201 insertions(+), 4 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptions.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptions.java
index b70e2a5..43804d5 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptions.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptions.java
@@ -67,6 +67,8 @@
 
   private boolean allowZeroLengthDirectoryStrings = false;
 
+  private boolean allowMalformedNamesAndOptions = false;
+
 
 
   // Prevent direct instantiation.
@@ -78,6 +80,58 @@
 
 
   /**
+   * Returns {@code true} if the schema should allow certain illegal characters
+   * in OIDs and attribute options. When this compatibility option is set to
+   * {@code true} the following illegal characters will be permitted:
+   *
+   * <pre>
+   * USCORE  = %x5F ; underscore ("_")
+   * DOT     = %x2E ; period (".")
+   * </pre>
+   *
+   * By default this compatibility option is set to {@code false}.
+   *
+   * @return {@code true} if the schema should allow certain illegal characters
+   *         in OIDs and attribute options.
+   * @see <a href="http://tools.ietf.org/html/rfc4512">RFC 4512 - Lightweight
+   *      Directory Access Protocol (LDAP): Directory Information Models </a>
+   */
+  public boolean allowMalformedNamesAndOptions()
+  {
+    return allowMalformedNamesAndOptions;
+  }
+
+
+
+  /**
+   * Specifies whether or not the schema should allow certain illegal characters
+   * in OIDs and attribute options. When this compatibility option is set to
+   * {@code true} the following illegal characters will be permitted:
+   *
+   * <pre>
+   * USCORE  = %x5F ; underscore ("_")
+   * DOT     = %x2E ; period (".")
+   * </pre>
+   *
+   * By default this compatibility option is set to {@code false}.
+   *
+   * @param allowMalformedNamesAndOptions
+   *          {@code true} if the schema should allow certain illegal characters
+   *          in OIDs and attribute options.
+   * @return A reference to this {@code SchemaCompatOptions}.
+   * @see <a href="http://tools.ietf.org/html/rfc4512">RFC 4512 - Lightweight
+   *      Directory Access Protocol (LDAP): Directory Information Models </a>
+   */
+  public SchemaCompatOptions allowMalformedNamesAndOptions(
+      final boolean allowMalformedNamesAndOptions)
+  {
+    this.allowMalformedNamesAndOptions = allowMalformedNamesAndOptions;
+    return this;
+  }
+
+
+
+  /**
    * Returns {@code true} if the Telephone Number syntax should allow values
    * which do not conform to the E.123 international telephone number format.
    * <p>
@@ -160,10 +214,10 @@
   // Assigns the provided options to this set of options.
   SchemaCompatOptions assign(final SchemaCompatOptions options)
   {
-    return allowNonStandardTelephoneNumbers(
-        options.allowNonStandardTelephoneNumbers)
-        .allowZeroLengthDirectoryStrings(
-            options.allowNonStandardTelephoneNumbers);
+    this.allowMalformedNamesAndOptions = options.allowMalformedNamesAndOptions;
+    this.allowNonStandardTelephoneNumbers =options.allowNonStandardTelephoneNumbers;
+    this.allowZeroLengthDirectoryStrings = options.allowZeroLengthDirectoryStrings;
+    return this;
   }
 
 }
diff --git a/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptionsTest.java b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptionsTest.java
new file mode 100644
index 0000000..6414403
--- /dev/null
+++ b/opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/schema/SchemaCompatOptionsTest.java
@@ -0,0 +1,143 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opendj3/legal-notices/CDDLv1_0.txt
+ * or http://forgerock.org/license/CDDLv1.0.html.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2011 ForgeRock AS.
+ */
+package org.forgerock.opendj.ldap.schema;
+
+
+
+import org.forgerock.i18n.LocalizedIllegalArgumentException;
+import org.forgerock.opendj.ldap.AttributeDescription;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+
+
+/**
+ * Test SchemaCompatOptions.
+ */
+public class SchemaCompatOptionsTest extends SchemaTestCase
+{
+  /**
+   * Returns test data for valid attribute descriptions.
+   *
+   * @return The test data.
+   */
+  @DataProvider
+  public Object[][] validAttributeDescriptions()
+  {
+    // @formatter:off
+    return new Object[][] {
+        // No options.
+        { "cn", false },
+        { "cn-xxx", false },
+        { "cn", true },
+        { "cn-xxx", true },
+        { "cn_xxx", true },
+        { "cn.xxx", true },
+        // With options.
+        { "cn;xxx", false },
+        { "cn;xxx-yyy", false },
+        { "cn;xxx", true },
+        { "cn;xxx-yyy", true },
+        { "cn;xxx_yyy", true },
+        { "cn;xxx.yyy", true },
+    };
+    // @formatter:on
+  }
+
+
+
+  /**
+   * Tests valid attribute description parsing behavior depends on compat
+   * options.
+   *
+   * @param atd
+   *          The attribute description to be parsed.
+   * @param allowIllegalCharacters
+   *          {@code true} if the attribute description requires the
+   *          compatibility option to be set.
+   */
+  @Test(enabled = false, dataProvider = "validAttributeDescriptions")
+  public void testValidAttributeDescriptions(String atd,
+      boolean allowIllegalCharacters)
+  {
+    SchemaBuilder builder = new SchemaBuilder(Schema.getCoreSchema())
+        .setSchemaCompatOptions(SchemaCompatOptions.defaultOptions()
+            .allowMalformedNamesAndOptions(allowIllegalCharacters));
+    Schema schema = builder.toSchema().nonStrict();
+    AttributeDescription.valueOf(atd, schema);
+  }
+
+
+
+  /**
+   * Returns test data for invalid attribute descriptions.
+   *
+   * @return The test data.
+   */
+  @DataProvider
+  public Object[][] invalidAttributeDescriptions()
+  {
+    // @formatter:off
+    return new Object[][] {
+        // No options.
+        { "cn+xxx", false }, // always invalid
+        { "cn_xxx", false },
+        { "cn.xxx", false },
+        { "cn+xxx", true }, // always invalid
+        // With options.
+        { "cn;xxx+yyy", false }, // always invalid
+        { "cn;xxx_yyy", false },
+        { "cn;xxx.yyy", false },
+        { "cn;xxx+yyy", true }, // always invalid
+    };
+    // @formatter:on
+  }
+
+
+
+  /**
+   * Tests invalid attribute description parsing behavior depends on compat
+   * options.
+   *
+   * @param atd
+   *          The attribute description to be parsed.
+   * @param allowIllegalCharacters
+   *          {@code true} if the attribute description requires the
+   *          compatibility option to be set.
+   */
+  @Test(enabled = false, dataProvider = "invalidAttributeDescriptions",
+      expectedExceptions = LocalizedIllegalArgumentException.class)
+  public void testinvalidAttributeDescriptions(String atd,
+      boolean allowIllegalCharacters)
+  {
+    SchemaBuilder builder = new SchemaBuilder(Schema.getCoreSchema())
+        .setSchemaCompatOptions(SchemaCompatOptions.defaultOptions()
+            .allowMalformedNamesAndOptions(allowIllegalCharacters));
+    Schema schema = builder.toSchema().nonStrict();
+    AttributeDescription.valueOf(atd, schema);
+  }
+}

--
Gitblit v1.10.0