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 Vladimir N. Molotkov
20 * @version $Revision$
21 */
22
23package tests.security.spec;
24
25import junit.framework.TestCase;
26
27import org.apache.harmony.security.tests.support.spec.MyEncodedKeySpec;
28
29import java.security.spec.EncodedKeySpec;
30import java.util.Arrays;
31
32/**
33 * Tests for <code>EncodedKeySpec</code> class fields and methods.
34 *
35 */
36public class EncodedKeySpecTest extends TestCase {
37
38    /**
39     * Tests for constructor <code>EncodedKeySpec(byte[])</code><br>
40     */
41    public final void testEncodedKeySpec() {
42        byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
43        EncodedKeySpec eks = new MyEncodedKeySpec(encodedKey);
44
45        assertTrue("wrong encoded key was returned", Arrays.equals(encodedKey,
46                eks.getEncoded()));
47        assertEquals("wrong name of encoding format", "My", eks.getFormat());
48
49        encodedKey = null;
50        try {
51            eks = new MyEncodedKeySpec(encodedKey);
52            fail("expected NullPointerException");
53        } catch (NullPointerException e) {
54            //
55        }
56    }
57
58    /**
59     * Tests that <code>getEncoded()</code> method returns valid byte array
60     */
61    public final void testGetEncoded() {
62
63        byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
64        EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
65
66        /* Get encoded key */
67        byte[] ek = meks.getEncoded();
68
69        /* Check returned array */
70        boolean result = true;
71        for (int i = 0; i < encodedKey.length; i++) {
72            if (encodedKey[i] != ek[i]) {
73                /* indicate failure */
74                result = false;
75            }
76        }
77        /* passed */
78        assertTrue(result);
79    }
80
81    /**
82     * Tests that internal state of the object can not be modified by modifying
83     * initial array value
84     */
85    public final void testIsStatePreserved1() {
86        /* Create initial byte array */
87        byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
88
89        EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
90
91        /* Modify initial array's value */
92        encodedKey[3] = (byte) 5;
93
94        /* Get encoded key */
95        byte[] ek = meks.getEncoded();
96
97        /* Check that byte value has not been changed */
98        assertTrue(ek[3] == (byte) 4);
99    }
100
101    /**
102     * Tests that internal state of the object can not be modified using
103     * returned value of <code>getEncoded()</code> method
104     */
105    public final void testIsStatePreserved2() {
106
107        byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
108        EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
109
110        /* Get encoded key */
111        byte[] ek = meks.getEncoded();
112        /* Modify returned value */
113        ek[3] = (byte) 5;
114        /* Get encoded key again */
115        byte[] ek1 = meks.getEncoded();
116
117        /* Check that byte value has not been changed */
118        assertTrue(ek1[3] == (byte) 4);
119    }
120
121}
122