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.io.IOException;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.harmony.security.x501.Name;
028: import org.apache.harmony.security.x509.EDIPartyName;
029: import org.apache.harmony.security.x509.GeneralName;
030: import org.apache.harmony.security.x509.GeneralNames;
031: import org.apache.harmony.security.x509.ORAddress;
032: import org.apache.harmony.security.x509.OtherName;
033:
034: /**
035: * GeneralNameTest
036: */
037: public class GeneralNameTest extends TestCase {
038:
039: public void testGeneralName() {
040: try {
041: GeneralName san0 = new GeneralName(new OtherName(
042: "1.2.3.4.5", new byte[] { 1, 2, 0, 1 }));
043: GeneralName san1 = new GeneralName(1, "rfc@822.Name");
044: GeneralName san2 = new GeneralName(2, "dNSName");
045: GeneralName san3 = new GeneralName(new ORAddress());
046: GeneralName san4 = new GeneralName(new Name(
047: "O=Organization"));
048: GeneralName san5 = new GeneralName(new EDIPartyName(
049: "assigner", "party"));
050: GeneralName san6 = new GeneralName(6,
051: "http://uniform.Resource.Id");
052: GeneralName san7 = new GeneralName(7, "1.1.1.1");
053: GeneralName san8 = new GeneralName(8, "1.2.3.4444.55555");
054:
055: GeneralNames sans_1 = new GeneralNames();
056: sans_1.addName(san0);
057: sans_1.addName(san1);
058: sans_1.addName(san2);
059: sans_1.addName(san3);
060: sans_1.addName(san4);
061: sans_1.addName(san5);
062: sans_1.addName(san6);
063: sans_1.addName(san7);
064: sans_1.addName(san8);
065:
066: byte[] encoding = GeneralNames.ASN1.encode(sans_1);
067: GeneralNames.ASN1.decode(encoding);
068: } catch (Exception e) {
069: // should not be thrown:
070: // provided string representations are correct
071: e.printStackTrace();
072: }
073: }
074:
075: public void testGeneralName1() throws Exception {
076: OtherName on = new OtherName("1.2.3.4.5", new byte[] { 1, 2, 0,
077: 1 });
078: byte[] encoding = OtherName.ASN1.encode(on);
079: new GeneralName(0, encoding);
080: OtherName.ASN1.decode(encoding);
081: GeneralName gn = new GeneralName(on);
082: new GeneralName(0, gn.getEncodedName());
083: assertEquals(gn, new GeneralName(0, gn.getEncodedName()));
084: }
085:
086: /**
087: * ipStrToBytes method testing.
088: */
089: public void testIpStrToBytes() throws Exception {
090: // Regression for HARMONY-727
091: Object[][] positives = {
092: {
093: "010a:020b:3337:1000:FFFA:ABCD:9999:0000",
094: new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x33, 0x37,
095: 0x10, 0x00, 0xFF, 0xFA, 0xAB, 0xCD,
096: 0x99, 0x99, 0x00, 0x00 } },
097: {
098: "010a:020b:3337:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
099: new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x33, 0x37,
100: 0x10, 0x00, 0xFF, 0xFA, 0xAB, 0xCD,
101: 0x99, 0x99, 0x00, 0x00, 0x01, 0x02,
102: 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
103: 0x09, 0x0A, 0x0b, 0x0c, 0x0D, 0x0e,
104: 0x0f, 0x10 } },
105: {
106: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
107: new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x11, 0x33,
108: 0x10, 0x00, 0xFF, 0xFA, 0xAB, 0xCD,
109: 0x99, 0x99, 0x00, 0x00, 0x01, 0x02,
110: 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
111: 0x09, 0x0A, 0x0b, 0x0c, 0x0D, 0x0e,
112: 0x0f, 0x10 } },
113: {
114: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
115: new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x11, 0x33,
116: 0x10, 0x00, 0xFF, 0xFA, 0xAB, 0xCD,
117: 0x99, 0x99, 0x00, 0x00, 0x01, 0x02,
118: 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
119: 0x09, 0x0A, 0x0b, 0x0c, 0x0D, 0x0e,
120: 0x0f, 0x10 } },
121: { "100.2.35.244", new int[] { 100, 2, 35, 244 } },
122: { "100.2.35.244/51.6.79.118",
123: new int[] { 100, 2, 35, 244, 51, 6, 79, 118 } }, };
124: String[] negatives = {
125: "010a:0000:3333:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0",
126: "010a:020b:3:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
127: "010a:020b:33:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
128: "010a:020b:333:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
129: "010a:020b:1133:10V0:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
130: "010a:020b:1133:1000-FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
131: "010a:020b:1133:1000:FFFA:ABCD:9999",
132: "010a:020b:1133:1000:FFFA:ABCD:9999/0000:0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
133: "010a:020b:1133:1000:FFFA:ABCD:9999:0000:0102/0304:0506:0708:090A:0b0c:0D0e:0f10",
134: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10:1234",
135: "100.2.35.244/51.6.79.118.119",
136: "100.2.35.244.115/79.118.119",
137: "100.2.35.244/79.118.119.1167",
138: "100.2.35.244/79.118.119.116.7",
139: "100.2.35.244.79/118.119.116.7",
140: "100.2.35/79/118.119.116.7",
141: "100.2.35.79/118/119.116.7",
142: "100.2..35.79/118/119.116.7",
143: "100.2.a.35.79/118/119.116.7",
144: "100.2.35.79/119.116.7-1",
145: "100.2.35.244.111",
146: "100.2.35.244/111",
147: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e0f10",
148: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e0f:10",
149: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102/0304:0506:0708:090A:0b0c:0D0e0f:10",
150: "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102030405060708090A0b0c:0D0e:0f10:ffff",
151: "010a:020b:1133:1000:FFFA:ABCD:9999:00000102030405060708090A/0b0c:0D0e:0f10:ffff", };
152: for (int i = 0; i < positives.length; i++) {
153: byte[] res = GeneralName
154: .ipStrToBytes((String) positives[i][0]);
155: int[] ref = (int[]) positives[i][1];
156: assertEquals("Length differs for " + positives[i][0],
157: ref.length, res.length);
158: for (int j = 0; j < res.length; j++) {
159: assertEquals("Element differs for " + positives[i][0],
160: (byte) ref[j], res[j]);
161: }
162: }
163: for (int n = 0; n < negatives.length; n++) {
164: String ip = negatives[n];
165: try {
166: byte[] bts = GeneralName.ipStrToBytes(ip);
167: for (int i = 0; i < bts.length; i++) {
168: System.out.print((bts[i] & 0xFF) + " ");
169: }
170: System.out.println("");
171: System.out.println(ip);
172: fail("No expected IOException was thrown for " + n);
173: } catch (IOException e) {
174: // expected
175: }
176: }
177: }
178:
179: /**
180: * oidStrToInts method testing
181: */
182: public void testOidStrToInts() throws Exception {
183: // Regression for HARMONY-727
184: Object[][] positives = {
185: { "1.2", new int[] { 1, 2 } },
186: { "1.2.3.4.5", new int[] { 1, 2, 3, 4, 5 } },
187: { "123.456.7890.1234567890",
188: new int[] { 123, 456, 7890, 1234567890 } }, };
189: String[] negatives = { ".1.2", "1.2.", "11-22.44.22",
190: "111..222" };
191: for (int i = 0; i < positives.length; i++) {
192: int[] res = GeneralName
193: .oidStrToInts((String) positives[i][0]);
194: int[] ref = (int[]) positives[i][1];
195: assertEquals("Length differs for " + positives[i][0],
196: ref.length, res.length);
197: for (int j = 0; j < res.length; j++) {
198: if (res[j] != ref[j]) {
199: assertEquals("Element differs for "
200: + positives[i][0], (byte) ref[j], res[j]);
201: }
202: }
203: }
204: for (int i = 0; i < negatives.length; i++) {
205: try {
206: GeneralName.oidStrToInts(negatives[i]);
207: fail("Expected IOException was not thrown for "
208: + negatives[i]);
209: } catch (IOException e) {
210: // expected
211: }
212: }
213: }
214: }
|