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 * @author Vladimir N. Molotkov
19 */
20
21package org.apache.harmony.security.tests.java.security.cert;
22
23import java.io.IOException;
24import java.security.cert.PolicyQualifierInfo;
25import java.util.Arrays;
26
27import junit.framework.TestCase;
28
29/**
30 * PolicyQualifierInfo test
31 */
32public class PolicyQualifierInfoTest extends TestCase {
33
34    /**
35     * Constructor for PolicyQualifierInfoTest.
36     *
37     * @param name
38     */
39    public PolicyQualifierInfoTest(String name) {
40        super(name);
41    }
42
43    /**
44     * Test #1 for <code>PolicyQualifierInfo</code> constructor<br>
45     * Assertion: throws <code>IOException</code> if byte array
46     * parameter does not represent a valid and parsable policy
47     * qualifier info
48     */
49    public final void test_Ctor() throws IOException {
50        try {
51            // pass null
52            new PolicyQualifierInfo(null);
53            fail("No expected NullPointerException");
54        } catch (NullPointerException e) {
55        }
56
57        try {
58            // pass empty array
59            new PolicyQualifierInfo(new byte[0]);
60            fail("IOE expected");
61        } catch (IOException e) {
62        }
63
64
65        try {
66            // pass invalid array
67            new PolicyQualifierInfo(
68                    new byte[] { (byte) 0x06, (byte) 0x03,
69                            (byte) 0x81, (byte) 0x34, (byte) 0x03 });
70            fail("IOE expected");
71        } catch (IOException e) {
72        }
73    }
74
75    /**
76     * Test #2 for <code>PolicyQualifierInfo</code> constructor<br>
77     * Assertion: throws <code>IOException</code> if byte array
78     * parameter does not represent a valid and parsable policy
79     * qualifier info
80     */
81    public final void testPolicyQualifierInfo02() {
82        // get valid encoding
83        byte[] encoding = getDerEncoding();
84        // corrupt root seq length
85        encoding[1] = (byte) 0x27;
86
87        try {
88            // pass invalid array
89            new PolicyQualifierInfo(encoding);
90            fail("IOE expected");
91        } catch (IOException e) {
92        }
93
94
95        // get valid encoding
96        encoding = getDerEncoding();
97        // corrupt policy qualifier ID:
98        //  - change OID to the Relative OID
99        encoding[2] = (byte) 13;
100        try {
101            // pass invalid array
102            new PolicyQualifierInfo(encoding);
103            fail("IOE expected");
104        } catch (IOException e) {
105        }
106    }
107
108    /**
109     * Test #3 for <code>PolicyQualifierInfo</code> constructor<br>
110     * Assertion: Creates an instance of <code>PolicyQualifierInfo</code>
111     * from the encoded bytes
112     *
113     * @throws IOException
114     */
115    public final void testPolicyQualifierInfo03() throws IOException {
116        // get valid encoding
117        byte[] encoding = getDerEncoding();
118        // pass valid array
119        new PolicyQualifierInfo(encoding);
120    }
121
122    /**
123     * Test #4 for <code>PolicyQualifierInfo</code> constructor<br>
124     * Assertion: The encoded byte array is copied on construction
125     *
126     * @throws IOException
127     */
128    public final void testPolicyQualifierInfo04() throws IOException {
129        // get valid encoding
130        byte[] encoding = getDerEncoding();
131        byte[] encodingCopy = encoding.clone();
132        // pass valid array
133        PolicyQualifierInfo i = new PolicyQualifierInfo(encodingCopy);
134        // get encoding
135        byte[] encodingRet = i.getEncoded();
136        // check returned array
137        assertTrue(Arrays.equals(encoding, encodingRet));
138        // modify input
139        encodingCopy[0] = (byte) 0;
140        // get encoding again
141        byte[] encodingRet1 = i.getEncoded();
142        // check that above modification did not change
143        // internal state of the PolicyQualifierInfo instance
144        assertTrue(Arrays.equals(encoding, encodingRet1));
145    }
146
147    /**
148     * Test #1 for <code>getEncoded()</code> method
149     * Assertion: Returns the ASN.1 DER encoded form of
150     * this <code>PolicyQualifierInfo</code>
151     *
152     * @throws IOException
153     */
154    public final void testGetEncoded01() throws IOException {
155        // get valid encoding
156        byte[] encoding = getDerEncoding();
157        // pass valid array
158        PolicyQualifierInfo i = new PolicyQualifierInfo(encoding);
159        // get encoding
160        byte[] encodingRet = i.getEncoded();
161        // check returned array
162        assertTrue(Arrays.equals(encoding, encodingRet));
163    }
164
165    /**
166     * Test #2 for <code>getEncoded()</code> method
167     * Assertion: a copy is returned each time
168     *
169     * @throws IOException
170     */
171    public final void testGetEncoded02() throws IOException {
172        // get valid encoding
173        byte[] encoding = getDerEncoding();
174        byte[] encodingCopy = encoding.clone();
175        // pass valid array
176        PolicyQualifierInfo i = new PolicyQualifierInfo(encodingCopy);
177        // get encoding
178        byte[] encodingRet = i.getEncoded();
179        // modify returned array
180        encodingRet[0] = (byte) 0;
181        // get encoding again
182        byte[] encodingRet1 = i.getEncoded();
183        // check that above modification did not change
184        // internal state of the PolicyQualifierInfo instance
185        assertTrue(Arrays.equals(encoding, encodingRet1));
186    }
187
188    /**
189     * Test #1 for <code>getPolicyQualifier()</code> method
190     * Assertion: Returns the ASN.1 DER encoded form of
191     * this <code>PolicyQualifierInfo</code>
192     *
193     * @throws IOException
194     */
195    public final void testGetPolicyQualifier01() throws IOException {
196        // get valid encoding
197        byte[] encoding = getDerEncoding();
198        // get policy qualifier encoding
199        byte[] pqEncoding = new byte[28];
200        System.arraycopy(encoding, 12, pqEncoding, 0, pqEncoding.length);
201        // pass valid array
202        PolicyQualifierInfo i = new PolicyQualifierInfo(encoding);
203        // get encoding
204        byte[] pqEncodingRet = i.getPolicyQualifier();
205        // check returned array
206        assertTrue(Arrays.equals(pqEncoding, pqEncodingRet));
207    }
208
209    /**
210     * Test #2 for <code>getPolicyQualifier()</code> method
211     * Assertion: a copy is returned each time
212     *
213     * @throws IOException
214     */
215    public final void testGetPolicyQualifier02() throws IOException {
216        // get valid encoding
217        byte[] encoding = getDerEncoding();
218        // get policy qualifier encoding
219        byte[] pqEncoding = new byte[28];
220        System.arraycopy(encoding, 12, pqEncoding, 0, pqEncoding.length);
221        // pass valid array
222        PolicyQualifierInfo i = new PolicyQualifierInfo(encoding);
223        // get encoding
224        byte[] pqEncodingRet = i.getPolicyQualifier();
225        // modify returned array
226        pqEncodingRet[0] = (byte) 0;
227        // get encoding again
228        byte[] pqEncodingRet1 = i.getPolicyQualifier();
229        //
230        assertNotSame(pqEncodingRet, pqEncodingRet1);
231        // check that above modification did not change
232        // internal state of the PolicyQualifierInfo instance
233        assertTrue(Arrays.equals(pqEncoding, pqEncodingRet1));
234    }
235
236    /**
237     * Test for <code>getPolicyQualifierId()</code> method
238     * Assertion: Returns the <code>policyQualifierId</code>
239     * field of this <code>PolicyQualifierInfo</code>.
240     * The <code>policyQualifierId</code> is an Object Identifier (OID)
241     * represented by a set of nonnegative integers separated by periods
242     *
243     * @throws IOException
244     */
245    public final void testGetPolicyQualifierId() throws IOException {
246        // get valid encoding
247        byte[] encoding = getDerEncoding();
248        // pass valid array
249        PolicyQualifierInfo i = new PolicyQualifierInfo(encoding);
250        // get OID as String and check it
251        assertEquals("1.3.6.1.5.5.7.2.1", i.getPolicyQualifierId());
252
253        // get valid encoding
254        encoding = getDerEncoding();
255        // change OID to 1.3.98437.82818.1
256        encoding[5] = (byte) 0x86;
257        encoding[6] = (byte) 0x81;
258        encoding[8] = (byte) 0x85;
259        encoding[9] = (byte) 0x87;
260        i = new PolicyQualifierInfo(encoding);
261        // get OID as String and check it
262        assertEquals("1.3.98437.82818.1", i.getPolicyQualifierId());
263    }
264
265    /**
266     * Test for <code>toString()</code> method
267     * Assertion: returns description of the contents of this
268     * <code>PolicyQualifierInfo</code> as printable <code>String</code>
269     *
270     * @throws IOException
271     * @throws IOException
272     */
273    public final void testToString() throws IOException {
274        // get valid encoding
275        byte[] encoding = getDerEncoding();
276        // pass valid array
277        PolicyQualifierInfo i = new PolicyQualifierInfo(encoding);
278
279        assertNotNull(i.toString());
280    }
281
282    //
283    // Private stuff
284    //
285
286    /**
287     * Returns valid DER encoding for the following ASN.1 definition
288     * (as specified in RFC 3280 -
289     * Internet X.509 Public Key Infrastructure.
290     * Certificate and Certificate Revocation List (CRL) Profile.
291     * http://www.ietf.org/rfc/rfc3280.txt):
292     * <p/>
293     * PolicyQualifierInfo ::= SEQUENCE {
294     * policyQualifierId       PolicyQualifierId,
295     * qualifier               ANY DEFINED BY policyQualifierId
296     * }
297     * <p/>
298     * where policyQualifierId (OID) is
299     * 1.3.6.1.5.5.7.2.1
300     * and qualifier (IA5String) is
301     * "http://www.qq.com/stmt.txt"
302     * <p/>
303     * (data generated by own encoder during test development)
304     */
305    private static final byte[] getDerEncoding() {
306        // DO NOT MODIFY!
307        return new byte[] {
308                (byte) 0x30, (byte) 0x26, // tag Seq, length
309                (byte) 0x06, (byte) 0x08, // tag OID, length
310                (byte) 0x2b, (byte) 0x06, (byte) 0x01, (byte) 0x05, // oid value
311                (byte) 0x05, (byte) 0x07, (byte) 0x02, (byte) 0x01, // oid value
312                (byte) 0x16, (byte) 0x1a, // tag IA5String, length
313                (byte) 0x68, (byte) 0x74, (byte) 0x74, (byte) 0x70,  // IA5String value
314                (byte) 0x3a, (byte) 0x2f, (byte) 0x2f, (byte) 0x77,  // IA5String value
315                (byte) 0x77, (byte) 0x77, (byte) 0x2e, (byte) 0x71,  // IA5String value
316                (byte) 0x71, (byte) 0x2e, (byte) 0x63, (byte) 0x6f,  // IA5String value
317                (byte) 0x6d, (byte) 0x2f, (byte) 0x73, (byte) 0x74,  // IA5String value
318                (byte) 0x6d, (byte) 0x74, (byte) 0x2e, (byte) 0x74,  // IA5String value
319                (byte) 0x78, (byte) 0x74   // IA5String value
320        };
321    }
322}
323