001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.satsa.utils;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import com.sun.satsa.pki.RFC2253Name;
032: import com.sun.satsa.util.TLV;
033: import com.sun.satsa.util.TLVException;
034: import com.sun.satsa.util.Utils;
035: import java.io.PrintStream;
036: import java.io.UnsupportedEncodingException;
037:
038: /**
039: * This test case tests DomainComponent encoding
040: * made by RFC2253 class.
041: */
042: public class TestIA5 extends TestCase {
043: static byte[] testOneResult = { 0x30, 0x55, 0x31, 0x13, 0x30, 0x11,
044: 0x06, 0x0a, 0x09, (byte) 0x92, 0x26, (byte) 0x89,
045: (byte) 0x93, (byte) 0xf2, 0x2c, 0x64, 0x01, 0x19, 0x16,
046: 0x03, 0x63, 0x6f, 0x6d, 0x31, 0x13, 0x30, 0x11, 0x06, 0x0a,
047: 0x09, (byte) 0x92, 0x26, (byte) 0x89, (byte) 0x93,
048: (byte) 0xf2, 0x2c, 0x64, 0x01, 0x19, 0x16, 0x03, 0x73,
049: 0x75, 0x6e, 0x31, 0x13, 0x30, 0x11, 0x06, 0x0a, 0x09,
050: (byte) 0x92, 0x26, (byte) 0x89, (byte) 0x93, (byte) 0xf2,
051: 0x2c, 0x64, 0x01, 0x19, 0x16, 0x03, 0x77, 0x77, 0x77, 0x31,
052: 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0b,
053: 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74,
054: 0x65 };
055: static byte[] testTwoResult = { 0x30, 0x29, 0x31, 0x10, 0x30, 0x0e,
056: 0x06, 0x0a, 0x09, (byte) 0x92, 0x26, (byte) 0x89,
057: (byte) 0x93, (byte) 0xf2, 0x2c, 0x64, 0x01, 0x19, 0x16,
058: 0x00, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x03,
059: 0x0c, 0x0c, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
060: 0x61, 0x74, 0x65, 0x31 };
061:
062: /**
063: * Tests DER encoding of DomainComponent.
064: */
065: private void testOne() {
066: String nameInfo = "cn=Certificate, dc=www, Dc=sun, dC=com";
067: TLV name;
068: boolean ok = true;
069:
070: try {
071: name = new TLV(RFC2253Name.toDER(nameInfo), 0);
072: } catch (TLVException e) {
073: name = null;
074: ok = false;
075: }
076: assertTrue("Invalid name", ok);
077: assertTrue("Bad DER result", name != null
078: && equal(name.getDERData(), testOneResult));
079: }
080:
081: private void testTwo() {
082: String nameInfo = "cn=Certificate1, dc=";
083: TLV name;
084: boolean ok = true;
085:
086: try {
087: name = new TLV(RFC2253Name.toDER(nameInfo), 0);
088: } catch (TLVException e) {
089: name = null;
090: ok = false;
091: }
092: assertTrue("Invalid name", ok);
093: assertTrue("Bad DER result", name != null
094: && equal(name.getDERData(), testTwoResult));
095: }
096:
097: private void testThree() {
098: String nameInfo = "cn=Certificate, OID.0.9.2342.19200300.100.1.25=www, dc=sun, dc=com";
099: TLV name;
100: boolean ok = true;
101:
102: try {
103: name = new TLV(RFC2253Name.toDER(nameInfo), 0);
104: } catch (TLVException e) {
105: name = null;
106: ok = false;
107: }
108: assertTrue("Invalid name", ok);
109: assertTrue("Bad DER result", name != null
110: && equal(name.getDERData(), testOneResult));
111: }
112:
113: /**
114: * Run tests.
115: */
116: public void runTests() {
117: try {
118: declare("testOne");
119: testOne();
120:
121: declare("testTwo");
122: testTwo();
123:
124: declare("testThree");
125: testThree();
126: } catch (Throwable t) {
127: fail("" + t);
128: }
129: }
130:
131: /**
132: * Compare two byte arrays.
133: * @param one the first array
134: * @param two the second array
135: * @return true if arrays are equal, false otherwise
136: */
137: private boolean equal(byte[] one, byte[] two) {
138: if (one.length != two.length) {
139: return false;
140: }
141: for (int i = 0; i < one.length; i++) {
142: if (one[i] != two[i]) {
143: return false;
144: }
145: }
146: return true;
147: }
148:
149: }
|