1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19 * @author Alexander Y. Kleymenov
20 */
21
22package org.apache.harmony.security.tests.x509;
23
24import java.io.IOException;
25
26import junit.framework.TestCase;
27
28import org.apache.harmony.security.x501.Name;
29import org.apache.harmony.security.x509.EDIPartyName;
30import org.apache.harmony.security.x509.GeneralName;
31import org.apache.harmony.security.x509.GeneralNames;
32import org.apache.harmony.security.x509.ORAddress;
33import org.apache.harmony.security.x509.OtherName;
34
35
36/**
37 * GeneralNameTest
38 */
39public class GeneralNameTest extends TestCase {
40
41    public void testGeneralName() {
42        try {
43            GeneralName san0 =
44                    new GeneralName(new OtherName("1.2.3.4.5", new byte[] { 1, 2, 0, 1 }));
45            GeneralName san1 = new GeneralName(1, "rfc@822.Name");
46            GeneralName san2 = new GeneralName(2, "dNSName");
47            GeneralName san3 = new GeneralName(new ORAddress());
48            GeneralName san4 = new GeneralName(new Name("O=Organization"));
49            GeneralName san5 =
50                    new GeneralName(new EDIPartyName("assigner", "party"));
51            GeneralName san6 = new GeneralName(6, "http://uniform.Resource.Id");
52            GeneralName san7 = new GeneralName(7, "1.1.1.1");
53            GeneralName san8 = new GeneralName(8, "1.2.3.4444.55555");
54
55            GeneralNames sans_1 = new GeneralNames();
56            sans_1.addName(san0);
57            sans_1.addName(san1);
58            sans_1.addName(san2);
59            sans_1.addName(san3);
60            sans_1.addName(san4);
61            sans_1.addName(san5);
62            sans_1.addName(san6);
63            sans_1.addName(san7);
64            sans_1.addName(san8);
65
66            byte[] encoding = GeneralNames.ASN1.encode(sans_1);
67            GeneralNames.ASN1.decode(encoding);
68        } catch (Exception e) {
69            // should not be thrown:
70            // provided string representations are correct
71            e.printStackTrace();
72        }
73    }
74
75    public void testGeneralName1() throws Exception {
76        OtherName on =
77                new OtherName("1.2.3.4.5", new byte[] { 1, 2, 0, 1 });
78        byte[] encoding = OtherName.ASN1.encode(on);
79        new GeneralName(0, encoding);
80        OtherName.ASN1.decode(encoding);
81        GeneralName gn = new GeneralName(on);
82        new GeneralName(0, gn.getEncodedName());
83        assertEquals(gn, new GeneralName(0, gn.getEncodedName()));
84    }
85
86    /**
87     * ipStrToBytes method testing.
88     */
89    public void testIpStrToBytes() throws Exception {
90        // Regression for HARMONY-727
91        Object[][] positives = {
92                { "010a:020b:3337:1000:FFFA:ABCD:9999:0000",
93                        new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x33, 0x37, 0x10, 0x00, 0xFF,
94                                0xFA, 0xAB, 0xCD, 0x99, 0x99, 0x00, 0x00 } },
95                { "010a:020b:3337:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
96                        new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x33, 0x37, 0x10, 0x00, 0xFF,
97                                0xFA, 0xAB, 0xCD, 0x99, 0x99, 0x00, 0x00, 0x01, 0x02, 0x03,
98                                0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0b, 0x0c, 0x0D,
99                                0x0e, 0x0f, 0x10 } },
100                { "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
101                        new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x11, 0x33, 0x10, 0x00, 0xFF,
102                                0xFA, 0xAB, 0xCD, 0x99, 0x99, 0x00, 0x00, 0x01, 0x02, 0x03,
103                                0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0b, 0x0c, 0x0D,
104                                0x0e, 0x0f, 0x10 } },
105                { "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
106                        new int[] { 0x01, 0x0a, 0x02, 0x0b, 0x11, 0x33, 0x10, 0x00, 0xFF,
107                                0xFA, 0xAB, 0xCD, 0x99, 0x99, 0x00, 0x00, 0x01, 0x02, 0x03,
108                                0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0b, 0x0c, 0x0D,
109                                0x0e, 0x0f, 0x10 } },
110                { "100.2.35.244",
111                        new int[] { 100, 2, 35, 244 } },
112                { "100.2.35.244/51.6.79.118",
113                        new int[] { 100, 2, 35, 244, 51, 6, 79, 118 } },
114        };
115        String[] negatives = {
116                "010a:0000:3333:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0",
117                "010a:020b:3:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
118                "010a:020b:33:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
119                "010a:020b:333:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
120                "010a:020b:1133:10V0:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
121                "010a:020b:1133:1000-FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
122                "010a:020b:1133:1000:FFFA:ABCD:9999",
123                "010a:020b:1133:1000:FFFA:ABCD:9999/0000:0102:0304:0506:0708:090A:0b0c:0D0e:0f10",
124                "010a:020b:1133:1000:FFFA:ABCD:9999:0000:0102/0304:0506:0708:090A:0b0c:0D0e:0f10",
125                "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e:0f10:1234",
126                "100.2.35.244/51.6.79.118.119",
127                "100.2.35.244.115/79.118.119",
128                "100.2.35.244/79.118.119.1167",
129                "100.2.35.244/79.118.119.116.7",
130                "100.2.35.244.79/118.119.116.7",
131                "100.2.35/79/118.119.116.7",
132                "100.2.35.79/118/119.116.7",
133                "100.2..35.79/118/119.116.7",
134                "100.2.a.35.79/118/119.116.7",
135                "100.2.35.79/119.116.7-1",
136                "100.2.35.244.111",
137                "100.2.35.244/111",
138                "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e0f10",
139                "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102:0304:0506:0708:090A:0b0c:0D0e0f:10",
140                "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102/0304:0506:0708:090A:0b0c:0D0e0f:10",
141                "010a:020b:1133:1000:FFFA:ABCD:9999:0000/0102030405060708090A0b0c:0D0e:0f10:ffff",
142                "010a:020b:1133:1000:FFFA:ABCD:9999:00000102030405060708090A/0b0c:0D0e:0f10:ffff",
143        };
144        for (int i = 0; i < positives.length; i++) {
145            byte[] res = GeneralName.ipStrToBytes((String) positives[i][0]);
146            int[] ref = (int[]) positives[i][1];
147            assertEquals("Length differs for " + positives[i][0], ref.length, res.length);
148            for (int j = 0; j < res.length; j++) {
149                assertEquals("Element differs for " + positives[i][0], (byte) ref[j], res[j]);
150            }
151        }
152        for (int n = 0; n < negatives.length; n++) {
153            String ip = negatives[n];
154            try {
155                byte[] bts = GeneralName.ipStrToBytes(ip);
156                for (int i = 0; i < bts.length; i++) {
157                    System.out.print((bts[i] & 0xFF) + " ");
158                }
159                System.out.println("");
160                System.out.println(ip);
161                fail("No expected IOException was thrown for " + n);
162            } catch (IOException e) {
163                // expected
164            }
165        }
166    }
167
168    /**
169     * oidStrToInts method testing
170     */
171    public void testOidStrToInts() throws Exception {
172        // Regression for HARMONY-727
173        Object[][] positives = {
174                { "1.2", new int[] { 1, 2 } },
175                { "1.2.3.4.5", new int[] { 1, 2, 3, 4, 5 } },
176                { "123.456.7890.1234567890",
177                        new int[] { 123, 456, 7890, 1234567890 } }, };
178        String[] negatives = { ".1.2", "1.2.", "11-22.44.22", "111..222" };
179        for (int i = 0; i < positives.length; i++) {
180            int[] res = GeneralName.oidStrToInts((String) positives[i][0]);
181            int[] ref = (int[]) positives[i][1];
182            assertEquals("Length differs for " + positives[i][0], ref.length,
183                    res.length);
184            for (int j = 0; j < res.length; j++) {
185                if (res[j] != ref[j]) {
186                    assertEquals("Element differs for " + positives[i][0],
187                            (byte) ref[j], res[j]);
188                }
189            }
190        }
191        for (int i = 0; i < negatives.length; i++) {
192            try {
193                GeneralName.oidStrToInts(negatives[i]);
194                fail("Expected IOException was not thrown for " + negatives[i]);
195            } catch (IOException e) {
196                // expected
197            }
198        }
199    }
200}
201