Author: elecharny
Date: Sat Mar 19 07:25:32 2005
New Revision: 158245
URL: http://svn.apache.org/viewcvs?view=rev&rev=158245
Log:
Created a LdapDN class with its test.
Corrected DNUtils & added some functions in it
Modified directory (added a codec subdir)
This LdapDN class purpose is to test that a LDAPDN respect
the BNF given in RFC2253 & RFC 1779.
Added:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java
(with props)
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/
- copied from r158215,
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/utils/
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/LdapDNTest.java
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/DNUtils.java
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/DNUtilsTest.java
Added:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java?view=auto&rev=158245
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java
(added)
+++
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java
Sat Mar 19 07:25:32 2005
@@ -0,0 +1,226 @@
+package org.apache.asn1.codec;
+/*
+ * Apache License
+ * Version 2.0, January 2004
+ * http://www.apache.org/licenses/
+ *
+ * TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+ *
+ * 1. Definitions.
+ *
+ * "License" shall mean the terms and conditions for use, reproduction,
+ * and distribution as defined by Sections 1 through 9 of this document.
+ *
+ * "Licensor" shall mean the copyright owner or entity authorized by
+ * the copyright owner that is granting the License.
+ *
+ * "Legal Entity" shall mean the union of the acting entity and all
+ * other entities that control, are controlled by, or are under common
+ * control with that entity. For the purposes of this definition,
+ * "control" means (i) the power, direct or indirect, to cause the
+ * direction or management of such entity, whether by contract or
+ * otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ * outstanding shares, or (iii) beneficial ownership of such entity.
+ *
+ * "You" (or "Your") shall mean an individual or Legal Entity
+ * exercising permissions granted by this License.
+ *
+ * "Source" form shall mean the preferred form for making modifications,
+ * including but not limited to software source code, documentation
+ * source, and configuration files.
+ *
+ * "Object" form shall mean any form resulting from mechanical
+ * transformation or translation of a Source form, including but
+ * not limited to compiled object code, generated documentation,
+ * and conversions to other media types.
+ *
+ * "Work" shall mean the work of authorship, whether in Source or
+ * Object form, made available under the License, as indicated by a
+ * copyright notice that is included in or attached to the work
+ * (an example is provided in the Appendix below).
+ *
+ * "Derivative Works" shall mean any work, whether in Source or Object
+ * form, that is based on (or derived from) the Work and for which the
+ * editorial revisions, annotations, elaborations, or other modifications
+ * represent, as a whole, an original work of authorship. For the purposes
+ * of this License, Derivative Works shall not include works that remain
+ * separable from, or merely link (or bind by name) to the interfaces of,
+ * the Work and Derivative Works thereof.
+ *
+ * "Contribution" shall mean any work of authorship, including
+ * the original version of the Work and any modifications or additions
+ * to that Work or Derivative Works thereof, that is intentionally
+ * submitted to Licensor for inclusion in the Work by the copyright owner
+ * or by an individual or Legal Entity authorized to submit on behalf of
+ * the copyright owner. For the purposes of this definition, "submitted"
+ * means any form of electronic, verbal, or written communication sent
+ * to the Licensor or its representatives, including but not limited to
+ * communication on electronic mailing lists, source code control systems,
+ * and issue tracking systems that are managed by, or on behalf of, the
+ * Licensor for the purpose of discussing and improving the Work, but
+ * excluding communication that is conspicuously marked or otherwise
+ * designated in writing by the copyright owner as "Not a Contribution."
+ *
+ * "Contributor" shall mean Licensor and any individual or Legal Entity
+ * on behalf of whom a Contribution has been received by Licensor and
+ * subsequently incorporated within the Work.
+ *
+ * 2. Grant of Copyright License. Subject to the terms and conditions of
+ * this License, each Contributor hereby grants to You a perpetual,
+ * worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ * copyright license to reproduce, prepare Derivative Works of,
+ * publicly display, publicly perform, sublicense, and distribute the
+ * Work and such Derivative Works in Source or Object form.
+ *
+ * 3. Grant of Patent License. Subject to the terms and conditions of
+ * this License, each Contributor hereby grants to You a perpetual,
+ * worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ * (except as stated in this section) patent license to make, have made,
+ * use, offer to sell, sell, import, and otherwise transfer the Work,
+ * where such license applies only to those patent claims licensable
+ * by such Contributor that are necessarily infringed by their
+ * Contribution(s) alone or by combination of their Contribution(s)
+ * with the Work to which such Contribution(s) was submitted. If You
+ * institute patent litigation against any entity (including a
+ * cross-claim or counterclaim in a lawsuit) alleging that the Work
+ * or a Contribution incorporated within the Work constitutes direct
+ * or contributory patent infringement, then any patent licenses
+ * granted to You under this License for that Work shall terminate
+ * as of the date such litigation is filed.
+ *
+ * 4. Redistribution. You may reproduce and distribute copies of the
+ * Work or Derivative Works thereof in any medium, with or without
+ * modifications, and in Source or Object form, provided that You
+ * meet the following conditions:
+ *
+ * (a) You must give any other recipients of the Work or
+ * Derivative Works a copy of this License; and
+ *
+ * (b) You must cause any modified files to carry prominent notices
+ * stating that You changed the files; and
+ *
+ * (c) You must retain, in the Source form of any Derivative Works
+ * that You distribute, all copyright, patent, trademark, and
+ * attribution notices from the Source form of the Work,
+ * excluding those notices that do not pertain to any part of
+ * the Derivative Works; and
+ *
+ * (d) If the Work includes a "NOTICE" text file as part of its
+ * distribution, then any Derivative Works that You distribute must
+ * include a readable copy of the attribution notices contained
+ * within such NOTICE file, excluding those notices that do not
+ * pertain to any part of the Derivative Works, in at least one
+ * of the following places: within a NOTICE text file distributed
+ * as part of the Derivative Works; within the Source form or
+ * documentation, if provided along with the Derivative Works; or,
+ * within a display generated by the Derivative Works, if and
+ * wherever such third-party notices normally appear. The contents
+ * of the NOTICE file are for informational purposes only and
+ * do not modify the License. You may add Your own attribution
+ * notices within Derivative Works that You distribute, alongside
+ * or as an addendum to the NOTICE text from the Work, provided
+ * that such additional attribution notices cannot be construed
+ * as modifying the License.
+ *
+ * You may add Your own copyright statement to Your modifications and
+ * may provide additional or different license terms and conditions
+ * for use, reproduction, or distribution of Your modifications, or
+ * for any such Derivative Works as a whole, provided Your use,
+ * reproduction, and distribution of the Work otherwise complies with
+ * the conditions stated in this License.
+ *
+ * 5. Submission of Contributions. Unless You explicitly state otherwise,
+ * any Contribution intentionally submitted for inclusion in the Work
+ * by You to the Licensor shall be under the terms and conditions of
+ * this License, without any additional terms or conditions.
+ * Notwithstanding the above, nothing herein shall supersede or modify
+ * the terms of any separate license agreement you may have executed
+ * with Licensor regarding such Contributions.
+ *
+ * 6. Trademarks. This License does not grant permission to use the trade
+ * names, trademarks, service marks, or product names of the Licensor,
+ * except as required for reasonable and customary use in describing the
+ * origin of the Work and reproducing the content of the NOTICE file.
+ *
+ * 7. Disclaimer of Warranty. Unless required by applicable law or
+ * agreed to in writing, Licensor provides the Work (and each
+ * Contributor provides its Contributions) on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied, including, without limitation, any warranties or conditions
+ * of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ * PARTICULAR PURPOSE. You are solely responsible for determining the
+ * appropriateness of using or redistributing the Work and assume any
+ * risks associated with Your exercise of permissions under this License.
+ *
+ * 8. Limitation of Liability. In no event and under no legal theory,
+ * whether in tort (including negligence), contract, or otherwise,
+ * unless required by applicable law (such as deliberate and grossly
+ * negligent acts) or agreed to in writing, shall any Contributor be
+ * liable to You for damages, including any direct, indirect, special,
+ * incidental, or consequential damages of any character arising as a
+ * result of this License or out of the use or inability to use the
+ * Work (including but not limited to damages for loss of goodwill,
+ * work stoppage, computer failure or malfunction, or any and all
+ * other commercial damages or losses), even if such Contributor
+ * has been advised of the possibility of such damages.
+ *
+ * 9. Accepting Warranty or Additional Liability. While redistributing
+ * the Work or Derivative Works thereof, You may choose to offer,
+ * and charge a fee for, acceptance of support, warranty, indemnity,
+ * or other liability obligations and/or rights consistent with this
+ * License. However, in accepting such obligations, You may act only
+ * on Your own behalf and on Your sole responsibility, not on behalf
+ * of any other Contributor, and only if You agree to indemnify,
+ * defend, and hold each Contributor harmless for any liability
+ * incurred by, or claims asserted against, such Contributor by reason
+ * of your accepting any such warranty or additional liability.
+ *
+ * END OF TERMS AND CONDITIONS
+ *
+ * APPENDIX: How to apply the Apache License to your work.
+ *
+ * To apply the Apache License to your work, attach the following
+ * boilerplate notice, with the fields enclosed by brackets "[]"
+ * replaced with your own identifying information. (Don't include
+ * the brackets!) The text should be enclosed in the appropriate
+ * comment syntax for the file format. We also recommend that a
+ * file or class name and description of purpose be included on the
+ * same "printed page" as the copyright notice for easier
+ * identification within third-party archives.
+ *
+ * Copyright [yyyy] [name of copyright owner]
+ *
+ * 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.
+ *
+ */
+/**
+ * Thrown when a Decoder has encountered a failure condition during a decode.
+ *
+ * @author Apache Software Foundation
+ * @version $Id: DecoderException.java,v 1.11 2004/09/09 16:00:13 ggregory Exp
$
+ */
+public class DecoderException extends Exception
+{
+ //~ Constructors
-------------------------------------------------------------------------------
+
+ /**
+ * Creates a DecoderException
+ *
+ * @param pMessage A message with meaning to a human
+ */
+ public DecoderException( String pMessage )
+ {
+ super( pMessage );
+ }
+
+}
Propchange:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/codec/DecoderException.java
------------------------------------------------------------------------------
svn:executable = *
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/DNUtils.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/DNUtils.java?view=diff&r1=158215&r2=158245
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/DNUtils.java
(original)
+++
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/DNUtils.java
Sat Mar 19 07:25:32 2005
@@ -14,10 +14,10 @@
* limitations under the License.
*
*/
-package org.apache.asn1.ldap.utils;
+package org.apache.asn1.ldap.codec.utils;
/**
- * Utility class used by the DN Parser.
+ * Utility class used by the LdapDN Parser.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
*/
@@ -130,26 +130,26 @@
true, true, true, false, false, false, false, false
};
- /** '\' | '"' | '#' | '+' | ',' | [0-9] | ';' | '<' | '=' | '>' |
[A-Z] | [a-z]
- * 0x1C | 0x22 | 0x23 | 0x2B | 0x2C | [0x30-0x39] | 0x3B | 0x3C | 0x3D |
0x3E | [0x41-0x5A] | [0x61-0x7A] */
+ /** '"' | '#' | '+' | ',' | [0-9] | ';' | '<' | '=' | '>' | [A-F]
| '\' | [a-f]
+ * 0x22 | 0x23 | 0x2B | 0x2C | [0x30-0x39] | 0x3B | 0x3C | 0x3D | 0x3E |
[0x41-0x46] | 0x5C | [0x61-0x66] */
private static final boolean[] PAIR_CHAR =
{
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
- false, false, false, false, true, false, false, false,
+ false, false, false, false, false, false, false, false,
false, false, true, true, false, false, false, false,
false, false, false, true, true, false, false, false,
true, true, true, true, true, true, true, true,
true, true, false, true, true, true, true, false,
- false, true, true, true, true, true, true, true,
- true, true, true, true, true, true, true, true,
- true, true, true, true, true, true, true, true,
- true, true, true, false, false, false, false, false,
- false, true, true, true, true, true, true, true,
- true, true, true, true, true, true, true, true,
- true, true, true, true, true, true, true, true,
- true, true, true, false, false, false, false, false
+ false, true, true, true, true, true, true, false,
+ false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false,
+ false, false, false, false, true, false, false, false,
+ false, true, true, true, true, true, true, false,
+ false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false,
+ false, false, false, false, false, false, false, false
};
/** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
@@ -270,6 +270,42 @@
}
/**
+ * Test if the current character is an Alpha character :
+ * <alpha> ::= [0x41-0x5A] | [0x61-0x7A]
+ *
+ * @param byteArray The buffer which contains the data
+ * @param index Current position in the buffer
+ *
+ * @return <code>true</code> if the current character is an Alpha character
+ */
+ public static boolean isAlpha( byte[] byteArray, int index )
+ {
+
+ if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0
) ||
+ ( index >= byteArray.length ) )
+ {
+
+ return false;
+ }
+ else
+ {
+
+ byte c = byteArray[index++];
+
+ if ( ( c < 0 ) || ( ALPHA[c] == false ) )
+ {
+
+ return false;
+ }
+ else
+ {
+
+ return true;
+ }
+ }
+ }
+
+ /**
* Walk the buffer while characters are Safe String characters :
* <safe-string> ::= <safe-init-char> <safe-chars>
* <safe-init-char> ::= [0x01-0x09] | 0x0B | 0x0C | [0x0E-0x1F] |
[0x21-0x39] | 0x3B | [0x3D-0x7F]
@@ -377,7 +413,7 @@
else
{
- byte c = byteArray[index++];
+ byte c = byteArray[index];
if ( ( c < 0 ) || ( HEX[c] == false ) )
{
@@ -395,7 +431,7 @@
/**
* Check if the current character is a Pair Char
* <pairchar> ::= ',' | '=' | '+' | '<' | '>' | '#' | ';' | '\' | '"'
| [0-9a-fA-F] [0-9a-fA-F]
- *
+ *
* @param byteArray The buffer which contains the data
* @param index Current position in the buffer
*
@@ -413,7 +449,7 @@
else
{
- byte c = byteArray[index++];
+ byte c = byteArray[index];
if ( ( c < 0 ) || ( PAIR_CHAR[c] == false ) )
{
@@ -423,7 +459,14 @@
else
{
- return true;
+ if (isHex(byteArray, index++))
+ {
+ return isHex(byteArray, index);
+ }
+ else
+ {
+ return true;
+ }
}
}
}
@@ -515,15 +558,22 @@
* @param byteArray The buffer which contains the data
* @param index Current position in the buffer
*
- * @return The new position
+ * @return The new position, -1 if the buffer does not contain an HexPair,
-2 if the
+ * buffer contains an hex byte but not two.
*/
public static int parseHexPair( byte[] byteArray, int index )
{
- if ( isHex( byteArray, index ) && isHex( byteArray, index + 1 ) )
+ if ( isHex( byteArray, index ))
{
-
- return index + 2;
+ if (isHex( byteArray, index + 1 ))
+ {
+ return index + 2;
+ }
+ else
+ {
+ return -2;
+ }
}
else
{
@@ -540,27 +590,29 @@
* @param byteArray The buffer which contains the data
* @param index Current position in the buffer
*
- * @return Return the first position which is not an hex pair
+ * @return Return the first position which is not an hex pair, or -1 if
there is no
+ * hexpair at the beginning or if an hexpair is invalid (if we have only
one hex instead of 2)
*/
public static int parseHexString( byte[] byteArray, int index )
{
- if ( parseHexPair( byteArray, index ) == -1 )
- {
-
- return -1;
- }
- else
- {
- index += 2;
- }
+ int result = parseHexPair( byteArray, index );
+
+ if (result < 0)
+ {
+ return -1;
+ }
+ else
+ {
+ index += 2;
+ }
- while ( parseHexPair( byteArray, index ) != -1 )
+ while ((result = parseHexPair( byteArray, index )) >= 0 )
{
index += 2;
}
- return index;
+ return (result == -2 ? -1 : index);
}
/**
Added:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java?view=auto&rev=158245
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
(added)
+++
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/utils/LdapDN.java
Sat Mar 19 07:25:32 2005
@@ -0,0 +1,511 @@
+/*
+ * Copyright 2005 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.asn1.ldap.codec.utils;
+
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * This class parses a DN.
+ *
+ * The DN MUST respect this BNF grammar (as of RFC2253, par. 3, and RFC1779,
fig. 1) <br>
+ *
+ * <p>
+ *- <distinguishedName> ::= <name> | e <br>
+ *- <name> ::= <name-component>
<name-components> <br>
+ *- <name-components> ::= <spaces> <separator>
<spaces> <name-component> <name-components> | e <br>
+ *- <name-component> ::= <attributeType> <spaces>
'=' <spaces> <attributeValue> <attributeTypeAndValues> <br>
+ *- <attributeTypeAndValues> ::= <spaces> '+' <spaces>
<attributeType> <spaces> '=' <spaces> <attributeValue>
<attributeTypeAndValues> | e <br>
+ *- <attributeType> ::= [a-zA-Z] <keychars> |
<oidPrefix> [0-9] <digits> <oids> | [0-9] <digits>
<oids> <br>
+ *- <keychars> ::= [a-zA-Z] <keychars> | [0-9]
<keychars> | '-' <keychars> | e <br>
+ *- <oidPrefix> ::= 'OID.' | 'oid.' | e <br>
+ *- <oids> ::= '.' [0-9] <digits>
<oids> | e <br>
+ *- <attributeValue> ::= <pairs-or-strings> | '#'
<hexstring> |'"' <quotechar-or-pairs> '"' <br>
+ *- <pairs-or-strings> ::= '\' <pairchar>
<pairs-or-strings> | <stringchar> <pairs-or-strings> | e <br>
+ *- <quotechar-or-pairs> ::= <quotechar>
<quotechar-or-pairs> | '\' <pairchar> <quotechar-or-pairs> |
e <br>
+ *- <pairchar> ::= ',' | '=' | '+' | '<' | '>' |
'#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br>
+ *- <hexstring> ::= [0-9a-fA-F] [0-9a-fA-F]
<hexpairs> <br>
+ *- <hexpairs> ::= [0-9a-fA-F] [0-9a-fA-F]
<hexpairs> | e <br>
+ *- <digits> ::= [0-9] <digits> | e <br>
+ *- <stringchar> ::= [0x00-0xFF] - [,=+<>#;\"\n\r]
<br>
+ *- <quotechar> ::= [0x00-0xFF] - [\"] <br>
+ *- <separator> ::= ',' | ';' <br>
+ *- <spaces> ::= ' ' <spaces> | e <br>
+ * </p>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
+ */
+public class LdapDN
+{
+ //~ Static fields/initializers
-----------------------------------------------------------------
+
+ /** "oid." static */
+ private static final byte[] OID_LOWER = new byte[] { 'o', 'i', 'd', '.' };
+
+ /** "OID." static */
+ private static final byte[] OID_UPPER = new byte[] { 'O', 'I', 'D', '.' };
+
+ //~ Methods
------------------------------------------------------------------------------------
+
+ /**
+ * Walk the buffer while the current char is a Space char
+ * <p>
+ * <spaces> ::= ' ' <spaces> | e
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer
+ */
+ private static int parseSpaces( byte[] bytes, int pos )
+ {
+
+ while ( DNUtils.isChar( bytes, pos, ' ' ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <attributeValue> ::= <pairs-or-strings> | '#'
<hexstring> |'"' <quotechar-or-pairs> '"' <br>
+ * <pairs-or-strings> ::= '\' <pairchar>
<pairs-or-strings> | <stringchar> <pairs-or-strings> | | e
<br>
+ * <quotechar-or-pairs> ::= <quotechar>
<quotechar-or-pairs> | '\' <pairchar> <quotechar-or-pairs> |
e <br>
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseAttributeValue( byte[] bytes, int pos )
+ {
+
+ if ( DNUtils.isChar( bytes, pos, '#' ) )
+ {
+ pos++;
+
+ // <attributeValue> ::= '#' <hexstring>
+ if ( ( pos = DNUtils.parseHexString( bytes, pos ) ) == -1 )
+ {
+
+ return -1;
+ }
+
+ return parseSpaces( bytes, pos );
+ }
+ else if ( DNUtils.isChar( bytes, pos, '"' ) )
+ {
+ pos++;
+
+ // <attributeValue> ::= '"' <quotechar-or-pair> '"'
+ // <quotechar-or-pairs> ::= <quotechar> <quotechar-or-pairs> |
'\' <pairchar> <quotechar-or-pairs> | e
+ while ( true )
+ {
+
+ if ( DNUtils.isChar( bytes, pos, '\\' ) )
+ {
+ pos++;
+
+ if ( DNUtils.isPairChar( bytes, pos ) )
+ {
+ pos++;
+ }
+ else
+ {
+
+ return -1;
+ }
+ }
+ else if ( DNUtils.isQuoteChar( bytes, pos ) )
+ {
+ pos++;
+ }
+ else
+ {
+
+ break;
+ }
+ }
+
+ if ( DNUtils.isChar( bytes, pos, '"' ) )
+ {
+ pos++;
+
+ return parseSpaces( bytes, pos );
+ }
+ else
+ {
+
+ return -1;
+ }
+ }
+ else
+ {
+
+ while ( true )
+ {
+
+ if ( DNUtils.isChar( bytes, pos, '\\' ) )
+ {
+
+ // '\' <pairchar> <pairs-or-strings>
+ pos++;
+
+ if ( DNUtils.isPairChar( bytes, pos ) == false )
+ {
+
+ return -1;
+ }
+ else
+ {
+ pos++;
+ }
+ }
+ else
+ {
+
+ // <stringchar> <pairs-or-strings>
+ if ( DNUtils.isStringChar( bytes, pos ) )
+ {
+
+ // A special case : if we have some spaces before the
'+' character,
+ // we MUST skip them.
+ int initPos = pos;
+
+ if ( DNUtils.isChar( bytes, pos, ' ' ) )
+ {
+ pos = parseSpaces( bytes, pos );
+
+ if ( ( DNUtils.isStringChar( bytes, pos ) == false
) &&
+ ( DNUtils.isChar( bytes, pos, '\\' ) ==
false ) )
+ {
+
+ // Ok, we are done with the stringchar.
+ return pos;
+ }
+ }
+ else
+ {
+ pos++;
+ }
+ }
+ else
+ {
+
+ return pos;
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <oidPrefix> ::= 'OID.' | 'oid.' | e
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseOidPrefix( byte[] bytes, int pos )
+ {
+
+ if ( ( DNUtils.areEquals( bytes, pos, OID_LOWER ) == -1 ) &&
+ ( DNUtils.areEquals( bytes, pos, OID_UPPER ) == -1 ) )
+ {
+
+ return -1;
+ }
+ else
+ {
+ pos += 4;
+
+ return pos;
+ }
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <oidValue> ::= [0-9] <digits> <oids>
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseOidValue(byte[] bytes, int pos)
+ {
+ // <attributType> ::= [0-9] <digits> <oids>
+ if ( DNUtils.isDigit( bytes, pos ) == false )
+ {
+
+ // Nope... An error
+ return -1;
+ }
+ else
+ {
+
+ // Let's process an oid
+ pos++;
+
+ while ( DNUtils.isDigit( bytes, pos ) )
+ {
+ pos++;
+ }
+
+ // <oids> ::= '.' [0-9] <digits> <oids> | e
+ if ( DNUtils.isChar( bytes, pos, '.' ) == false )
+ {
+
+ return pos;
+ }
+ else
+ {
+
+ do
+ {
+ pos++;
+
+ if ( DNUtils.isDigit( bytes, pos ) == false )
+ {
+
+ return -1;
+ }
+ else
+ {
+ pos++;
+
+ while ( DNUtils.isDigit( bytes, pos ) )
+ {
+ pos++;
+ }
+ }
+ }
+ while ( DNUtils.isChar( bytes, pos, '.' ) );
+
+ return pos;
+ }
+ }
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <attributType> ::= [a-zA-Z] <keychars> |
+ *
<oidPrefix> [0-9] <digits> <oids> | [0-9] <digits>
<oids>
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseAttributeType( byte[] bytes, int pos )
+ {
+
+ // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] <digits>
<oids> | [0-9] <digits> <oids>
+
+ if ( DNUtils.isAlpha( bytes, pos ))
+ {
+ // <attributType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
<digits> <oids>
+
+ // We have got an Alpha char, it may be the begining of an OID ?
+ int oldPos = pos;
+
+ if ( ( pos = parseOidPrefix( bytes, oldPos ) ) != -1 )
+ {
+ return parseOidValue(bytes, pos);
+ }
+ else
+ {
+ // It's not an oid, it's a String
+ // <attributType> ::= [a-zA-Z] <keychars>
+ // <keychars> ::= [a-zA-Z] <keychar> | [0-9] <keychar> |
'-' <keychar> | e
+ pos = oldPos + 1;
+
+ while ( ( DNUtils.parseChar( bytes, pos ) != -1 ) )
+ {
+ pos++;
+ }
+
+ return pos;
+ }
+ }
+ else
+ {
+
+ // An oid
+ // <attributType> ::= [0-9] <digits> <oids>
+ return parseOidValue(bytes, pos);
+ }
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <attributeTypeAndValues> ::= <spaces> '+' <spaces>
<attributeType> <spaces> '=' <spaces> <attributeValue>
<attributeTypeAndValues> | e
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the byte buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseAttributeTypeAndValues( byte[] bytes, int pos )
+ {
+
+ while ( true )
+ {
+ pos = parseSpaces( bytes, pos );
+
+ if ( DNUtils.isChar( bytes, pos, '+' ) )
+ {
+ pos++;
+ }
+ else
+ {
+
+ // <attributeTypeAndValues> ::= e
+ return pos;
+ }
+
+ pos = parseSpaces( bytes, pos );
+
+ if ( ( pos = parseAttributeType( bytes, pos ) ) == -1 )
+ {
+
+ return -1;
+ }
+
+ pos = parseSpaces( bytes, pos );
+
+ if ( DNUtils.isChar( bytes, pos, '=' ) )
+ {
+ pos++;
+ }
+ else
+ {
+
+ return -1;
+ }
+
+ pos = parseSpaces( bytes, pos );
+
+ return parseAttributeValue( bytes, pos );
+ }
+ }
+
+ /**
+ * Parse this rule : <br>
+ * <p>
+ * <name-component> ::= <attributeType> <spaces> '='
<spaces> <attributeValue> <attributeTypeAndValues>
+ * </p>
+ *
+ * @param bytes The buffer to parse
+ * @param pos The current position in the buffer
+ * @return The new position in the byte buffer, or -1 if the rule does not
apply to the byte buffer
+ */
+ private static int parseNameComponent( byte[] bytes, int pos )
+ {
+
+ if ( ( pos = parseAttributeType( bytes, pos ) ) == -1 )
+ {
+
+ return -1;
+ }
+
+ pos = parseSpaces( bytes, pos );
+
+ if ( DNUtils.isChar( bytes, pos, '=' ) == false )
+ {
+
+ return -1;
+ }
+ else
+ {
+ pos++;
+ }
+
+ pos = parseSpaces( bytes, pos );
+
+ if ( ( pos = parseAttributeValue( bytes, pos ) ) == -1 )
+ {
+
+ return -1;
+ }
+
+ return parseAttributeTypeAndValues( bytes, pos );
+ }
+
+ /**
+ * Parse a buffer and checks that it is a valid DN <br>
+ * <p>
+ * <distinguishedName> ::= <name> | e <br>
+ * <name> ::= <name-component>
<name-components> <br>
+ * <name-components> ::= <spaces> <separator>
<spaces> <name-component> <name-components> | e <br>
+ * </p>
+ *
+ * @param bytes The byte buffer that contains the DN
+ * @return A String containing the DN.
+ * @exception A DecoderException is thrown if the buffer does not contains
a valid DN.
+ */
+ public static String parseDN( byte[] bytes ) throws DecoderException
+ {
+
+ // <distinguishedName> ::= e
+ if ( ( bytes == null ) || ( bytes.length == 0 ) )
+ {
+
+ return "";
+ }
+
+ int pos = 0;
+
+ // <name> ::= <name-component> <name-components>
+ // <name-components> ::= <spaces> <separator> <spaces>
<name-component> <name-components> | e
+ if ( ( pos = parseNameComponent( bytes, pos ) ) != -1 )
+ {
+
+ do
+ {
+
+ if ( ( DNUtils.isChar( bytes, pos, ',' ) == false ) &&
+ ( DNUtils.isChar( bytes, pos, ';' ) == false ) )
+ {
+
+ break;
+ }
+
+ bytes[pos] = ',';
+ pos++;
+
+ pos = parseSpaces( bytes, pos );
+ }
+ while ( ( pos = parseNameComponent( bytes, pos ) ) != -1 );
+ }
+ else
+ {
+ throw new DecoderException( "Bad DN : " + new String( bytes) );
+ }
+
+ return new String( bytes );
+ }
+}
Modified:
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/DNUtilsTest.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/DNUtilsTest.java?view=diff&r1=158244&r2=158245
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/DNUtilsTest.java
(original)
+++
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/DNUtilsTest.java
Sat Mar 19 07:25:32 2005
@@ -16,6 +16,8 @@
*/
package org.apache.asn1.ldap.utils;
+import org.apache.asn1.ldap.codec.utils.DNUtils;
+
import junit.framework.Assert;
import junit.framework.TestCase;
@@ -30,18 +32,62 @@
/**
* Test the DNUtils AreEquals method
*/
- public void testAreEquals()
+ public void testAreEqualsFull()
{
-
// Full compare
Assert.assertEquals( 6, DNUtils.areEquals( "azerty".getBytes(), 0,
"azerty" ) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsDiff()
+ {
// First character is !=
Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"Azerty" ) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsEmpty()
+ {
// Compare to an empty string
Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0, ""
) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsFirstCharDiff()
+ {
+ // First character is !=
+ Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"Azerty" ) );
+ }
+
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsMiddleCharDiff()
+ {
+ // First character is !=
+ Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"azeRty" ) );
+ }
+
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsLastCharDiff()
+ {
+ // First character is !=
+ Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"azertY" ) );
+ }
+
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsCharByChar()
+ {
// Index must be incremented after each comparison
Assert.assertEquals( 1, DNUtils.areEquals( "azerty".getBytes(), 0, "a"
) );
Assert.assertEquals( 2, DNUtils.areEquals( "azerty".getBytes(), 1, "z"
) );
@@ -49,13 +95,40 @@
Assert.assertEquals( 4, DNUtils.areEquals( "azerty".getBytes(), 3, "r"
) );
Assert.assertEquals( 5, DNUtils.areEquals( "azerty".getBytes(), 4, "t"
) );
Assert.assertEquals( 6, DNUtils.areEquals( "azerty".getBytes(), 5, "y"
) );
+ }
+
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsTooShort()
+ {
+ // length too short
+ Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"azertyiop" ) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsTooShortMiddle()
+ {
// length too short
- Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 5,
"azerty" ) );
+ Assert.assertEquals( -1, DNUtils.areEquals( "azerty".getBytes(), 0,
"ertyiop" ) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsLastChar()
+ {
// last character
Assert.assertEquals( 6, DNUtils.areEquals( "azerty".getBytes(), 5, "y"
) );
+ }
+ /**
+ * Test the DNUtils AreEquals method
+ */
+ public void testAreEqualsMiddle()
+ {
// In the middle
Assert.assertEquals( 4, DNUtils.areEquals( "azerty".getBytes(), 2,
"er" ) );
}
Added:
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/LdapDNTest.java
URL:
http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/LdapDNTest.java?view=auto&rev=158245
==============================================================================
---
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/LdapDNTest.java
(added)
+++
directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/ldap/utils/LdapDNTest.java
Sat Mar 19 07:25:32 2005
@@ -0,0 +1,159 @@
+/*
+ * Copyright 2005 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.asn1.ldap.utils;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1.ldap.codec.utils.LdapDN;
+
+
+/**
+ * Test the class DNUtils
+ */
+public class LdapDNTest extends TestCase
+{
+ /**
+ * Test a null DN
+ */
+ public void testLdapDNNull() throws DecoderException
+ {
+ Assert.assertEquals("", LdapDN.parseDN(null) );
+ }
+
+ /**
+ * test an empty DN
+ */
+ public void testLdapDNEmpty() throws DecoderException
+ {
+ Assert.assertEquals("", LdapDN.parseDN("".getBytes()) );
+ }
+
+ /**
+ * test an empty DN
+ */
+ public void testLdapDN() throws DecoderException
+ {
+ Assert.assertEquals("", LdapDN.parseDN("".getBytes()) );
+ }
+
+ /**
+ * test a simple DN : a = b
+ */
+ public void testLdapDNSimple() throws DecoderException
+ {
+ Assert.assertEquals("a = b", LdapDN.parseDN("a = b".getBytes()) );
+ }
+
+ /**
+ * test a composite DN : a = b, d = e
+ */
+ public void testLdapDNComposite() throws DecoderException
+ {
+ Assert.assertEquals("a = b, c = d", LdapDN.parseDN("a = b, c =
d".getBytes()) );
+ }
+
+ /**
+ * test a composite DN with or without spaces: a=b, a =b, a= b, a = b, a
= b
+ */
+ public void testLdapDNCompositeWithSpace() throws DecoderException
+ {
+ Assert.assertEquals("a=b, a =b, a= b, a = b, a = b",
LdapDN.parseDN("a=b, a =b, a= b, a = b, a = b".getBytes()) );
+ }
+
+ /**
+ * test a composite DN with differents separators : a=b;c=d,e=f
+ * It should return a=b,c=d,e=f (the ';' is replaced by a ',')
+ */
+ public void testLdapDNCompositeSepators() throws DecoderException
+ {
+ Assert.assertEquals("a=b,c=d,e=f",
LdapDN.parseDN("a=b;c=d,e=f".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with differents separators : a = b + c = d
+ */
+ public void testLdapDNSimpleMultivaluedAttribute() throws DecoderException
+ {
+ Assert.assertEquals("a = b + c = d", LdapDN.parseDN("a = b + c =
d".getBytes()) );
+ }
+
+ /**
+ * test a composite DN with differents separators : a=b+c=d, e=f + g=h +
i=j
+ */
+ public void testLdapDNCompositeMultivaluedAttribute() throws
DecoderException
+ {
+ Assert.assertEquals("a=b+c=d, e=f + g=h + i=j",
LdapDN.parseDN("a=b+c=d, e=f + g=h + i=j".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with an oid prefix (uppercase) : OID.12.34.56 = azerty
+ */
+ public void testLdapDNOidUpper() throws DecoderException
+ {
+ Assert.assertEquals("OID.12.34.56 = azerty",
LdapDN.parseDN("OID.12.34.56 = azerty".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with an oid prefix (lowercase) : oid.12.34.56 = azerty
+ */
+ public void testLdapDNOidLower() throws DecoderException
+ {
+ Assert.assertEquals("oid.12.34.56 = azerty",
LdapDN.parseDN("oid.12.34.56 = azerty".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with an oid attribut wiithout oid prefix : 12.34.56 =
azerty
+ */
+ public void testLdapDNOidWithoutPrefix() throws DecoderException
+ {
+ Assert.assertEquals("12.34.56 = azerty", LdapDN.parseDN("12.34.56 =
azerty".getBytes()) );
+ }
+
+ /**
+ * test a composite DN with an oid attribut wiithout oid prefix : 12.34.56
= azerty; 7.8 = test
+ */
+ public void testLdapDNCompositeOidWithoutPrefix() throws DecoderException
+ {
+ Assert.assertEquals("12.34.56 = azerty, 7.8 = test",
LdapDN.parseDN("12.34.56 = azerty; 7.8 = test".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with pair char attribute value : a =
\,\=\+\<\>\#\;\\\"\A0\00"
+ */
+ public void testLdapDNPairCharAttributeValue() throws DecoderException
+ {
+ Assert.assertEquals("a = \\,\\=\\+\\<\\>\\#\\;\\\\\\\"\\A0\\00",
LdapDN.parseDN("a = \\,\\=\\+\\<\\>\\#\\;\\\\\\\"\\A0\\00".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with hexString attribute value : a = #0010A0AAFF
+ */
+ public void testLdapDNHexStringAttributeValue() throws DecoderException
+ {
+ Assert.assertEquals("a = #0010A0AAFF", LdapDN.parseDN("a =
#0010A0AAFF".getBytes()) );
+ }
+
+ /**
+ * test a simple DN with quoted attribute value : a = "quoted \"value"
+ */
+ public void testLdapDNQuotedAttributeValue() throws DecoderException
+ {
+ Assert.assertEquals("a = quoted \\\"value", LdapDN.parseDN("a = quoted
\\\"value".getBytes()) );
+ }
+}