directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r432490 - in /directory/branches/shared/0.9.5/ldap/src: main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Date Fri, 18 Aug 2006 05:26:51 GMT
Author: akarasulu
Date: Thu Aug 17 22:26:48 2006
New Revision: 432490

URL: http://svn.apache.org/viewvc?rev=432490&view=rev
Log:
Applying PAMs patches for DIRSERVER-612

Added:
    directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java

Added: directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?rev=432490&view=auto
==============================================================================
--- directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
(added)
+++ directory/branches/shared/0.9.5/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
Thu Aug 17 22:26:48 2006
@@ -0,0 +1,105 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.ldif;
+
+
+/**
+ * Some LDIF useful methods
+ *
+ */
+public class LdifUtils
+{
+
+	/** The array that will be used to match the first char.*/
+    private static boolean[] LDIF_SAFE_STARTING_CHAR_ALPHABET = new boolean[128];
+    
+    /** The array that will be used to match the other chars.*/
+    private static boolean[] LDIF_SAFE_OTHER_CHARS_ALPHABET = new boolean[128];
+    
+    static
+    {
+    	// Initialization of the array that will be used to match the first char.
+    	for (int i = 0; i < 128; i++) {
+    		LDIF_SAFE_STARTING_CHAR_ALPHABET[i] = true;
+		}
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[0] = false; // 0 (NUL)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[10] = false; // 10 (LF)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[13] = false; // 13 (CR)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[32] = false; // 32 (SPACE)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[58] = false; // 58 (:)
+    	LDIF_SAFE_STARTING_CHAR_ALPHABET[60] = false; // 60 (>)
+    	
+    	// Initialization of the array that will be used to match the other chars.
+    	for (int i = 0; i < 128; i++) {
+    		LDIF_SAFE_OTHER_CHARS_ALPHABET[i] = true;
+		}
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[0] = false; // 0 (NUL)
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[10] = false; // 10 (LF)
+    	LDIF_SAFE_OTHER_CHARS_ALPHABET[13] = false; // 13 (CR)
+    }
+
+    /**
+     * Checks if the input String contains only safe values, that is, the data
+     * does not need to be encoded for use with LDIF. The rules for checking safety
+     * are based on the rules for LDIF (LDAP Data Interchange Format) per RFC 2849.
+     * The data does not need to be encoded if all the following are true:
+     * 
+     * The data cannot start with the following char values:
+     * 		00 (NUL)
+     * 		10 (LF)
+     * 		13 (CR)
+     * 		32 (SPACE)
+     * 		58 (:)
+     * 		60 (<)
+     * 		Any character with value greater than 127
+     * 
+     * The data cannot contain any of the following char values:
+     * 		00 (NUL)
+     * 		10 (LF)
+     * 		13 (CR)
+     * 		Any character with value greater than 127
+     * 
+     * The data cannot end with a space.
+     * 
+     * @param str the String to be checked
+     * @return true if encoding not required for LDIF
+     */
+    public static boolean isLDIFSafe( String str )
+    {
+    	// Checking the first char
+    	char currentChar = str.charAt(0);
+    	if ( currentChar > 127 || !LDIF_SAFE_STARTING_CHAR_ALPHABET[currentChar] )
+    	{
+    		return false;
+    	}
+    	
+    	// Checking the other chars
+    	for (int i = 1; i < str.length(); i++)
+    	{
+        	currentChar = str.charAt(i);
+        	
+        	if ( currentChar > 127 || !LDIF_SAFE_OTHER_CHARS_ALPHABET[currentChar] )
+        	{
+        		return false;
+        	}
+		}
+    	
+    	// The String cannot end with a space
+    	return ( currentChar != ' ' );
+    }
+}
+

Added: directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java?rev=432490&view=auto
==============================================================================
--- directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
(added)
+++ directory/branches/shared/0.9.5/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
Thu Aug 17 22:26:48 2006
@@ -0,0 +1,186 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.ldif;
+
+import junit.framework.TestCase;
+
+public class LdifUtilsTest extends TestCase
+{
+	private String testString = "this is a test";
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char NUL (ASCII code 0)
+	 */
+	public void testIsLdifSafeStartingWithNUL()
+    {
+		char c = ( char ) 0;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char LF (ASCII code 10)
+	 */
+	public void testIsLdifSafeStartingWithLF()
+    {
+		char c = ( char ) 10;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char CR (ASCII code 13)
+	 */
+	public void testIsLdifSafeStartingWithCR()
+    {
+		char c = ( char ) 13;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char SPACE (ASCII code 32)
+	 */
+	public void testIsLdifSafeStartingWithSpace()
+    {
+		char c = ( char ) 32;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char COLON (:) (ASCII code 58)
+	 */
+	public void testIsLdifSafeStartingWithColon()
+    {
+		char c = ( char ) 58;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char LESS_THAN (<) (ASCII code 60)
+	 */
+	public void testIsLdifSafeStartingWithLessThan()
+    {
+		char c = ( char ) 60;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char with ASCII code 127
+	 */
+	public void testIsLdifSafeStartingWithCharGreaterThan127()
+    {
+		char c = ( char ) 127;
+		
+		assertTrue( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String starting with the
+	 * char with ASCII code greater than 127
+	 */
+	public void testIsLdifSafeStartingWithCharGreaterThan127Bis()
+    {
+		char c = ( char ) 222;
+		
+		assertFalse( LdifUtils.isLDIFSafe( c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char NUL (ASCII code 0)
+	 */
+	public void testIsLdifSafeContainsNUL()
+    {
+		char c = ( char ) 0;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char LF (ASCII code 10)
+	 */
+	public void testIsLdifSafeContainsLF()
+    {
+		char c = ( char ) 10;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char CR (ASCII code 13)
+	 */
+	public void testIsLdifSafeContainsCR()
+    {
+		char c = ( char ) 13;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing the
+	 * char with ASCII code 127
+	 */
+	public void testIsLdifSafeContainsCharGreaterThan127()
+    {
+		char c = ( char ) 127;
+		
+		assertTrue( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String containing a
+	 * char with ASCII code greater than 127
+	 */
+	public void testIsLdifSafeContainsCharGreaterThan127Bis()
+    {
+		char c = ( char ) 328;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString + c + testString ) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a String ending with the
+	 * char SPACE (ASCII code 32)
+	 */
+	public void testIsLdifSafeEndingWithSpace()
+    {
+		char c = ( char ) 32;
+		
+		assertFalse( LdifUtils.isLDIFSafe( testString  + c) );
+    }
+	
+	/**
+	 * Tests the method IsLdifSafe with a correct String
+	 */
+	public void testIsLdifSafeCorrectString()
+    {		
+		assertTrue( LdifUtils.isLDIFSafe( testString ) );
+    }
+}



Mime
View raw message