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:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests.x509;
022:
023: import java.util.Arrays;
024:
025: import org.apache.harmony.security.x509.EDIPartyName;
026: import org.apache.harmony.security.x509.GeneralName;
027: import org.apache.harmony.security.x509.GeneralNames;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032:
033: /**
034: */
035:
036: public class EDIPartyNameTest extends TestCase {
037:
038: public static void printAsHex(int perLine, String prefix,
039: String delimiter, byte[] data) {
040: for (int i = 0; i < data.length; i++) {
041: String tail = Integer.toHexString(0x000000ff & data[i]);
042: if (tail.length() == 1) {
043: tail = "0" + tail;
044: }
045: System.out.print(prefix + "0x" + tail + delimiter);
046:
047: if (((i + 1) % perLine) == 0) {
048: System.out.println();
049: }
050: }
051: System.out.println();
052: }
053:
054: /**
055: * EDIPartyName(String nameAssigner, String partyName) method testing.
056: */
057: public void _testEDIPartyName1() {
058: boolean pass = true;
059:
060: EDIPartyName ediPN = new EDIPartyName("nameAssigner",
061: "partyName");
062: byte[] encoded = ediPN.getEncoded();
063: // manually derived data:
064: byte[] _encoded = { (byte) 0x30, (byte) 0x1d, (byte) 0x80,
065: (byte) 0x0e, (byte) 0x13, (byte) 0x0c, (byte) 0x6e,
066: (byte) 0x61, (byte) 0x6d, (byte) 0x65, (byte) 0x41,
067: (byte) 0x73, (byte) 0x73, (byte) 0x69, (byte) 0x67,
068: (byte) 0x6e, (byte) 0x65, (byte) 0x72, (byte) 0x81,
069: (byte) 0x0b, (byte) 0x13, (byte) 0x09, (byte) 0x70,
070: (byte) 0x61, (byte) 0x72, (byte) 0x74, (byte) 0x79,
071: (byte) 0x4e, (byte) 0x61, (byte) 0x6d, (byte) 0x65 };
072: if (!Arrays.equals(encoded, _encoded)) {
073: System.out.println("Got encoded form of EDIPartyName is:");
074: printAsHex(16, "", " ", encoded);
075: System.out.println("But should be like this:");
076: printAsHex(16, "", " ", _encoded);
077: System.out.println("");
078: pass = false;
079: }
080:
081: GeneralName gName = new GeneralName(ediPN);
082: encoded = gName.getEncoded();
083: // manually derived data:
084: _encoded = new byte[] { (byte) 0xa5, (byte) 0x1d, (byte) 0x80,
085: (byte) 0x0e, (byte) 0x13, (byte) 0x0c, (byte) 0x6e,
086: (byte) 0x61, (byte) 0x6d, (byte) 0x65, (byte) 0x41,
087: (byte) 0x73, (byte) 0x73, (byte) 0x69, (byte) 0x67,
088: (byte) 0x6e, (byte) 0x65, (byte) 0x72, (byte) 0x81,
089: (byte) 0x0b, (byte) 0x13, (byte) 0x09, (byte) 0x70,
090: (byte) 0x61, (byte) 0x72, (byte) 0x74, (byte) 0x79,
091: (byte) 0x4e, (byte) 0x61, (byte) 0x6d, (byte) 0x65 };
092: if (!Arrays.equals(encoded, _encoded)) {
093: System.out.println("Got encoded form of GeneralName is:");
094: printAsHex(16, "", " ", encoded);
095: System.out.println("But should be like this:");
096: printAsHex(16, "", " ", _encoded);
097: System.out.println("");
098: pass = false;
099: }
100:
101: GeneralNames gNames = new GeneralNames();
102: gNames.addName(gName);
103: encoded = gNames.getEncoded();
104: // manually derived data:
105: _encoded = new byte[] { (byte) 0x30, (byte) 0x1f, (byte) 0xa5,
106: (byte) 0x1d, (byte) 0x80, (byte) 0x0e, (byte) 0x13,
107: (byte) 0x0c, (byte) 0x6e, (byte) 0x61, (byte) 0x6d,
108: (byte) 0x65, (byte) 0x41, (byte) 0x73, (byte) 0x73,
109: (byte) 0x69, (byte) 0x67, (byte) 0x6e, (byte) 0x65,
110: (byte) 0x72, (byte) 0x81, (byte) 0x0b, (byte) 0x13,
111: (byte) 0x09, (byte) 0x70, (byte) 0x61, (byte) 0x72,
112: (byte) 0x74, (byte) 0x79, (byte) 0x4e, (byte) 0x61,
113: (byte) 0x6d, (byte) 0x65 };
114: if (!Arrays.equals(encoded, _encoded)) {
115: System.out.println("Got encoded form of GeneralNames is:");
116: printAsHex(16, "", " ", encoded);
117: System.out.println("But should be like this:");
118: printAsHex(16, "", " ", _encoded);
119: System.out.println("");
120: pass = false;
121: }
122:
123: assertTrue("Some problems occured.", pass);
124: }
125:
126: /**
127: * EDIPartyName(String nameAssigner, String partyName, byte[] encoding)
128: * method testing.
129: */
130: public void testEDIPartyName2() throws Exception {
131: EDIPartyName ediName = new EDIPartyName("assigner", "party");
132: byte[] encoding = EDIPartyName.ASN1.encode(ediName);
133: EDIPartyName.ASN1.decode(encoding);
134: new GeneralName(5, encoding);
135:
136: GeneralName gn = new GeneralName(ediName);
137:
138: new GeneralName(5, gn.getEncodedName());
139: }
140:
141: /**
142: * getNameAssigner() method testing.
143: */
144: public void testGetNameAssigner() {
145: }
146:
147: /**
148: * getPartyName() method testing.
149: */
150: public void testGetPartyName() {
151: }
152:
153: /**
154: * getEncoded() method testing.
155: */
156: public void testGetEncoded() {
157: }
158:
159: /**
160: * getEncoder() method testing.
161: */
162: public void testGetEncoder() {
163: }
164:
165: /**
166: * DerEncoder(String nameAssigner, String partyName) method testing.
167: */
168: public void testDerEncoder() {
169: }
170:
171: /**
172: * getDirStrAlternatives() method testing.
173: */
174: public void testGetDirStrAlternatives() {
175: }
176:
177: /**
178: * DerDecoder() method testing.
179: */
180: public void testDerDecoder() {
181: }
182:
183: /**
184: * getValue(byte[] encoding) method testing.
185: */
186: public void testGetValue1() {
187: }
188:
189: /**
190: * getValue() method testing.
191: */
192: public void testGetValue2() {
193: }
194:
195: public static Test suite() {
196: return new TestSuite(EDIPartyNameTest.class);
197: }
198:
199: public static void main(String[] args) {
200: junit.textui.TestRunner.run(suite());
201: }
202: }
|