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* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto.spec;
24
25import java.util.Arrays;
26
27import javax.crypto.spec.PBEParameterSpec;
28
29import junit.framework.Test;
30import junit.framework.TestCase;
31import junit.framework.TestSuite;
32
33/**
34 */
35public class PBEParameterSpecTest extends TestCase {
36
37    /**
38     * PBEParameterSpec(byte[] salt, int iterationCount) method testing.
39     * Tests the behavior of the method in the case of null input array
40     * and tests that input array is copied during the object initialization.
41     */
42    public void testPBEParameterSpec() {
43        byte[] salt = {1, 2, 3, 4, 5};
44        int iterationCount = 10;
45
46        try {
47            new PBEParameterSpec(null, iterationCount);
48            fail("A NullPointerException should be was thrown "
49                    + "in the case of null salt.");
50        } catch (NullPointerException e) {
51        }
52
53        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
54        salt[0] ++;
55        assertFalse("The change of salt specified in the constructor "
56                    + "should not cause the change of internal array.",
57                    salt[0] == pbeps.getSalt()[0]);
58   }
59
60    /**
61     * getSalt() method testing. Tests that returned salt is equal
62     * to the salt specified in the constructor and that the change of
63     * returned array does not cause the change of internal array.
64     */
65    public void testGetSalt() {
66        byte[] salt = new byte[] {1, 2, 3, 4, 5};
67        int iterationCount = 10;
68        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
69        byte[] result = pbeps.getSalt();
70        if (! Arrays.equals(salt, result)) {
71            fail("The returned salt is not equal to the specified "
72                    + "in the constructor.");
73        }
74        result[0] ++;
75        assertFalse("The change of returned by getSalt() method salt"
76                    + "should not cause the change of internal array.",
77                    result[0] == pbeps.getSalt()[0]);
78    }
79
80    /**
81     * getIterationCount() method testing. Tests that returned value is equal
82     * to the value specified in the constructor.
83     */
84    public void testGetIterationCount() {
85        byte[] salt = new byte[] {1, 2, 3, 4, 5};
86        int iterationCount = 10;
87        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
88        assertTrue("The returned iterationCount is not equal to the specified "
89                + "in the constructor.",
90                pbeps.getIterationCount() == iterationCount);
91    }
92
93    public static Test suite() {
94        return new TestSuite(PBEParameterSpecTest.class);
95    }
96}
97
98