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 dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargets;
27import dalvik.annotation.TestLevel;
28import dalvik.annotation.TestTargetNew;
29
30import java.util.Arrays;
31
32import javax.crypto.spec.PBEParameterSpec;
33
34import junit.framework.Test;
35import junit.framework.TestCase;
36import junit.framework.TestSuite;
37
38@TestTargetClass(PBEParameterSpec.class)
39/**
40 */
41
42public class PBEParameterSpecTest extends TestCase {
43
44    /**
45     * PBEParameterSpec(byte[] salt, int iterationCount) method testing.
46     * Tests the behavior of the method in the case of null input array
47     * and tests that input array is copied during the object initialization.
48     */
49    @TestTargetNew(
50        level = TestLevel.COMPLETE,
51        notes = "",
52        method = "PBEParameterSpec",
53        args = {byte[].class, int.class}
54    )
55    public void testPBEParameterSpec() {
56        byte[] salt = {1, 2, 3, 4, 5};
57        int iterationCount = 10;
58
59        try {
60            new PBEParameterSpec(null, iterationCount);
61            fail("A NullPointerException should be was thrown "
62                    + "in the case of null salt.");
63        } catch (NullPointerException e) {
64        }
65
66        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
67        salt[0] ++;
68        assertFalse("The change of salt specified in the constructor "
69                    + "should not cause the change of internal array.",
70                    salt[0] == pbeps.getSalt()[0]);
71   }
72
73    /**
74     * getSalt() method testing. Tests that returned salt is equal
75     * to the salt specified in the constructor and that the change of
76     * returned array does not cause the change of internal array.
77     */
78    @TestTargetNew(
79        level = TestLevel.COMPLETE,
80        notes = "",
81        method = "getSalt",
82        args = {}
83    )
84    public void testGetSalt() {
85        byte[] salt = new byte[] {1, 2, 3, 4, 5};
86        int iterationCount = 10;
87        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
88        byte[] result = pbeps.getSalt();
89        if (! Arrays.equals(salt, result)) {
90            fail("The returned salt is not equal to the specified "
91                    + "in the constructor.");
92        }
93        result[0] ++;
94        assertFalse("The change of returned by getSalt() method salt"
95                    + "should not cause the change of internal array.",
96                    result[0] == pbeps.getSalt()[0]);
97    }
98
99    /**
100     * getIterationCount() method testing. Tests that returned value is equal
101     * to the value specified in the constructor.
102     */
103    @TestTargetNew(
104        level = TestLevel.COMPLETE,
105        notes = "",
106        method = "getIterationCount",
107        args = {}
108    )
109    public void testGetIterationCount() {
110        byte[] salt = new byte[] {1, 2, 3, 4, 5};
111        int iterationCount = 10;
112        PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
113        assertTrue("The returned iterationCount is not equal to the specified "
114                + "in the constructor.",
115                pbeps.getIterationCount() == iterationCount);
116    }
117
118    public static Test suite() {
119        return new TestSuite(PBEParameterSpecTest.class);
120    }
121
122    public static void main(String[] args) {
123        junit.textui.TestRunner.run(suite());
124    }
125}
126
127