001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.jndi.provider.ldap.asn1;
018:
019: import java.lang.reflect.Field;
020: import java.util.Collection;
021:
022: import junit.framework.Assert;
023:
024: import org.apache.harmony.jndi.provider.ldap.DeleteOp;
025: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
026: import org.apache.harmony.security.asn1.ASN1Boolean;
027: import org.apache.harmony.security.asn1.ASN1Enumerated;
028: import org.apache.harmony.security.asn1.ASN1Implicit;
029: import org.apache.harmony.security.asn1.ASN1Integer;
030: import org.apache.harmony.security.asn1.ASN1OctetString;
031: import org.apache.harmony.security.asn1.ASN1SequenceOf;
032: import org.apache.harmony.security.asn1.ASN1SetOf;
033: import org.apache.harmony.security.asn1.ASN1Type;
034: import org.apache.harmony.security.asn1.ASN1ValueCollection;
035:
036: public class ASN1TestUtils {
037: private static final String ASN1ENCODABLE_NAME = ASN1Encodable.class
038: .getCanonicalName();
039: private static final String CHOSENVALUE_NAME = ChosenValue.class
040: .getCanonicalName();
041:
042: public static void checkEncode(Object value, ASN1Type type) {
043: if (type instanceof ASN1Implicit) {
044: type = getWrappedType((ASN1Implicit) type);
045: }
046:
047: if (type instanceof ASN1Integer
048: || type instanceof ASN1Enumerated) {
049: Assert.assertTrue(value instanceof byte[]);
050:
051: Assert.assertTrue(
052: "value should not be zero-length byte array",
053: ((byte[]) value).length != 0);
054: } else if (type instanceof ASN1Boolean) {
055: Assert.assertTrue(value instanceof Boolean);
056: } else if (type instanceof ASN1OctetString) {
057: if (value instanceof DeleteOp) {
058: Object[] objs = new Object[1];
059: ((DeleteOp) value).encodeValues(objs);
060: value = objs[0];
061: }
062: Assert.assertTrue(value instanceof byte[]);
063: } else if (type instanceof ASN1SequenceWrap) {
064: checkEncodeSequence(value, (ASN1SequenceWrap) type);
065: } else if (type instanceof ASN1SequenceOf
066: || type instanceof ASN1SetOf) {
067: Assert.assertTrue(value instanceof Collection);
068: Collection collection = (Collection) value;
069: for (Object object : collection) {
070: checkEncode(object, ((ASN1ValueCollection) type).type);
071: }
072: } else if (type instanceof ASN1ChoiceWrap) {
073: checkEncodeChoice(value, (ASN1ChoiceWrap) type);
074: } else if (type instanceof ASN1LdapFilter) {
075: checkEncodeFilter(value, (ASN1LdapFilter) type);
076: } else {
077: Assert.fail("Not supported ASN.1 type");
078: }
079: }
080:
081: private static void checkEncodeFilter(Object value,
082: ASN1LdapFilter filter) {
083: checkEncode(value, LdapASN1Constant.Filter);
084: }
085:
086: private static void checkEncodeChoice(Object value,
087: ASN1ChoiceWrap type) {
088: if (value instanceof Object[]) {
089: Object[] objs = (Object[]) value;
090: Assert.assertEquals(2, objs.length);
091: Assert.assertTrue(objs[0] instanceof Integer);
092:
093: int index = ((Integer) objs[0]).intValue();
094: checkEncode(objs[1], type.type[index]);
095: } else if (value instanceof ChosenValue) {
096: ChosenValue chosen = (ChosenValue) value;
097: checkEncode(chosen.getValue(), type.type[chosen.getIndex()]);
098: } else if (value instanceof ASN1Encodable) {
099: Object[] objs = new Object[1];
100: ((ASN1Encodable) value).encodeValues(objs);
101: checkEncode(objs[0], type);
102: } else {
103:
104: Assert
105: .fail("Value for ASN1ChoiceWrap should be Object[2], "
106: + CHOSENVALUE_NAME
107: + " or "
108: + ASN1ENCODABLE_NAME);
109: }
110: }
111:
112: private static void checkEncodeSequence(Object value,
113: ASN1SequenceWrap type) {
114: if (value instanceof Object[]) {
115: Object[] objs = (Object[]) value;
116: Assert.assertEquals(type.type.length, objs.length);
117: for (int i = 0; i < objs.length; i++) {
118: if (objs[i] == null && type.OPTIONAL[i]) {
119: continue;
120: }
121: checkEncode(objs[i], type.type[i]);
122: }
123: } else if (value instanceof ASN1Encodable) {
124: Object[] objs = new Object[type.type.length];
125: ((ASN1Encodable) value).encodeValues(objs);
126: checkEncodeSequence(objs, type);
127: } else {
128: Assert
129: .fail("Value for ASN1SequenceWrap should be Object[], or "
130: + ASN1ENCODABLE_NAME);
131: }
132: }
133:
134: private static ASN1Type getWrappedType(ASN1Implicit type) {
135: try {
136: Field field = ASN1Implicit.class.getDeclaredField("type");
137: field.setAccessible(true);
138: return (ASN1Type) field.get(type);
139: } catch (Exception e) {
140: // can't reach, unless implement changed
141: throw new RuntimeException("Can't get wrapped type.", e);
142: }
143: }
144: }
|