From 43718b3055068721a34bd39ef60ff1d02ee0ff1c Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Sat, 07 Jul 2007 22:42:36 +0000
Subject: [PATCH] Update the LDIF reader so that it has the ability to perform syntax validation as well as schema checking.  It will honor the ds-cfg-invalid-attribute-syntax-behavior configuration.

---
 opends/src/server/org/opends/server/messages/UtilityMessages.java                         |   17 ++++++++
 opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java |   28 +++++++-------
 opends/src/server/org/opends/server/util/LDIFReader.java                                  |   26 +++++++++++++
 3 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/opends/src/server/org/opends/server/messages/UtilityMessages.java b/opends/src/server/org/opends/server/messages/UtilityMessages.java
index c5554c6..20f612e 100644
--- a/opends/src/server/org/opends/server/messages/UtilityMessages.java
+++ b/opends/src/server/org/opends/server/messages/UtilityMessages.java
@@ -1714,6 +1714,19 @@
 
 
   /**
+   * The message ID for the warning message that will be used if an entry is
+   * read from an LDIF file that contains an attribute value that violates the
+   * associated syntax.  This takes five arguments, which are the DN of the
+   * entry, the starting line number for the entry, the invalid value, the name
+   * of the attribute, and a message explaining the reason that the value is not
+   * acceptable.
+   */
+  public static final int MSGID_LDIF_VALUE_VIOLATES_SYNTAX =
+       CATEGORY_MASK_UTIL | SEVERITY_MASK_MILD_WARNING | 163;
+
+
+
+  /**
    * Associates a set of generic messages with the message IDs defined in this
    * class.
    */
@@ -1780,6 +1793,10 @@
                     "Entry %s read from LDIF starting at line %d includes a " +
                     "duplicate objectclass value %s.  The second occurrence " +
                     "of that objectclass has been skipped");
+    registerMessage(MSGID_LDIF_VALUE_VIOLATES_SYNTAX,
+                    "Entry %s read from LDIF starting at line %d includes " +
+                    "value '%s' for attribute %s that is invalid according " +
+                    "to the associated syntax:  %s");
     registerMessage(MSGID_LDIF_DUPLICATE_ATTR,
                     "Entry %s read from LDIF starting at line %d includes a " +
                     "duplicate attribute %s with value %s.  The second " +
diff --git a/opends/src/server/org/opends/server/util/LDIFReader.java b/opends/src/server/org/opends/server/util/LDIFReader.java
index 71ec8f0..75c5b72 100644
--- a/opends/src/server/org/opends/server/util/LDIFReader.java
+++ b/opends/src/server/org/opends/server/util/LDIFReader.java
@@ -54,6 +54,7 @@
 import org.opends.server.protocols.asn1.ASN1OctetString;
 import org.opends.server.protocols.ldap.LDAPAttribute;
 import org.opends.server.protocols.ldap.LDAPModification;
+import org.opends.server.types.AcceptRejectWarn;
 import org.opends.server.types.Attribute;
 import org.opends.server.types.AttributeType;
 import org.opends.server.types.AttributeValue;
@@ -893,6 +894,31 @@
         return;
       }
 
+      if (checkSchema &&
+          (DirectoryServer.getSyntaxEnforcementPolicy() !=
+               AcceptRejectWarn.ACCEPT))
+      {
+        StringBuilder invalidReason = new StringBuilder(0);
+        if (! attrType.getSyntax().valueIsAcceptable(value, invalidReason))
+        {
+          int    msgID   = MSGID_LDIF_VALUE_VIOLATES_SYNTAX;
+          String message = getMessage(msgID, String.valueOf(entryDN),
+                                      lastEntryLineNumber, value.stringValue(),
+                                      attrName, invalidReason.toString());
+          if (DirectoryServer.getSyntaxEnforcementPolicy() ==
+                   AcceptRejectWarn.WARN)
+          {
+            logError(ErrorLogCategory.SCHEMA, ErrorLogSeverity.MILD_WARNING,
+                     message, msgID);
+          }
+          else
+          {
+            logToRejectWriter(lines, message);
+            throw new LDIFException(msgID, message, lastEntryLineNumber,
+                                    true);
+          }
+        }
+      }
 
       AttributeValue attributeValue = new AttributeValue(attrType, value);
       List<Attribute> attrList;
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java
index b0b2e08..37fb368 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/ImportLDIFTestCase.java
@@ -62,7 +62,7 @@
   {
     TestCaseUtils.startServer();
     beID = "userRoot";
-	  configFilePath = DirectoryServer.getConfigFile();
+    configFilePath = DirectoryServer.getConfigFile();
     TaskUtils.disableBackend(beID);
 
     String entry =
@@ -82,10 +82,10 @@
           "uid: user.0\n" +
           "mail: user.0@example.com\n" +
           "userPassword: password\n" +
-          "telephoneNumber: 380-535-2354\n" +
+          "telephoneNumber: +1 380-535-2354\n" +
           "description: This is the description for Aaccf Amar\n" +
-          "creatorsName: Import\n" +
-          "modifiersName: Import\n";
+          "creatorsName: cn=Import\n" +
+          "modifiersName: cn=Import\n";
 
     tempDir = TestCaseUtils.createTemporaryDirectory("importLDIFtest");
     homeDirName = tempDir.getAbsolutePath();
@@ -152,8 +152,8 @@
 
     Attribute[]  opAttr =
     {
-      new Attribute ("creatorsname", "Import") ,
-      new Attribute("modifiersname","Import")
+      new Attribute ("creatorsname", "cn=Import") ,
+      new Attribute("modifiersname","cn=Import")
      }    ;
     //operational attributes shouldn't be present.
     assertEntry(opAttr,false);
@@ -190,8 +190,8 @@
       new Attribute ("description",
           "This is the description for Aaccf Amar"),
       new Attribute("mail","user.0@example.com"),
-      new Attribute ("creatorsname", "Import") ,
-      new Attribute("modifiersname","Import")
+      new Attribute ("creatorsname", "cn=Import") ,
+      new Attribute("modifiersname","cn=Import")
     }    ;
     assertEntry(attr,true);
   }
@@ -280,8 +280,8 @@
     assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
     assertRejectedFile(reject,true);
     Attribute[] attrs = {
-       new Attribute ("creatorsname", "Import") ,
-       new Attribute("modifiersname","Import")
+       new Attribute ("creatorsname", "cn=Import") ,
+       new Attribute("modifiersname","cn=Import")
     };
     assertEntry(attrs,false);
   }
@@ -312,7 +312,7 @@
     assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
     assertRejectedFile(reject,true);
     Attribute[] attrs = {
-       new Attribute ("creatorsname", "Import")
+       new Attribute ("creatorsname", "cn=Import")
     };
     assertEntry(attrs,true);
   }
@@ -346,7 +346,7 @@
     assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
     assertRejectedFile(reject,true);
     Attribute[] attrsPr = {
-       new Attribute ("creatorsname", "Import")
+       new Attribute ("creatorsname", "cn=Import")
     };
     assertEntry(attrsPr,true);
     Attribute[] attrsAb = {
@@ -382,12 +382,12 @@
     assertEquals(ImportLDIF.mainImportLDIF(args,false), 0);
     assertRejectedFile(reject,true);
     Attribute[] attrsPr = {
-       new Attribute ("modifiersname", "Import"),
+       new Attribute ("modifiersname", "cn=Import"),
        new Attribute("employeenumber","0")
     };
     assertEntry(attrsPr,true);
     Attribute[] attrsAb = {
-       new Attribute ("creatorsname", "Import"),
+       new Attribute ("creatorsname", "cn=Import"),
        new Attribute("givenname","Aaccf")
     };
     assertEntry(attrsAb,false);

--
Gitblit v1.10.0