PSourceTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 javax.crypto.spec;
23
24import java.util.Arrays;
25import javax.crypto.spec.PSource;
26
27import junit.framework.Test;
28import junit.framework.TestCase;
29import junit.framework.TestSuite;
30
31/**
32 */
33
34public class PSourceTest extends TestCase {
35
36    /**
37     * PSpecified(byte[] p) method testing. Tests that NullPointerException
38     * is thrown in the case of null p array. Also it checks the value of
39     * DEFAULT field, and that input p array is copied to protect against
40     * subsequent modification.
41     */
42    public void testPSpecified() {
43        try {
44            new PSource.PSpecified(null);
45            fail("NullPointerException should be thrown in the case of "
46                    + "null p array.");
47        } catch (NullPointerException e) {
48        }
49
50        assertEquals("The PSource.PSpecified DEFAULT value should be byte[0]",
51                            0, PSource.PSpecified.DEFAULT.getValue().length);
52
53        byte[] p = new byte[] {1, 2, 3, 4, 5};
54        PSource.PSpecified ps = new PSource.PSpecified(p);
55        p[0] ++;
56        assertFalse("The change of p specified in the constructor "
57                    + "should not cause the change of internal array.",
58                    p[0] == ps.getValue()[0]);
59    }
60
61    /**
62     * getValue() method testing. Tests that returned array is equal to the
63     * array specified in the constructor. Checks that modification
64     * of returned array does not affect the internal array.
65     */
66    public void testGetValue() {
67        byte[] p = new byte[] {1, 2, 3, 4, 5};
68
69        PSource.PSpecified ps = new PSource.PSpecified(p);
70        byte[] result = ps.getValue();
71        if (! Arrays.equals(p, result)) {
72            fail("The returned array does not equal to the specified "
73                    + "in the constructor.");
74        }
75        result[0] ++;
76        assertFalse("The change of returned by getValue() array "
77                    + "should not cause the change of internal array.",
78                    result[0] == ps.getValue()[0]);
79    }
80
81    /**
82     * PSource(String pSrcName) method testing. Tests that returned value is
83     * equal to the value specified in the constructor.
84     */
85    public void testPSource() {
86        try {
87            new PSource(null);
88            fail("NullPointerException should be thrown in the case of "
89                    + "null pSrcName.");
90        } catch (NullPointerException e) {
91        }
92    }
93
94    /**
95     * getAlgorithm() method testing. Tests that returned value is
96     * equal to the value specified in the constructor.
97     */
98    public void testGetAlgorithm() {
99        String pSrcName = "pSrcName";
100        PSource ps = new PSource(pSrcName);
101        assertTrue("The returned value is not equal to the value specified "
102                + "in constructor", pSrcName.equals(ps.getAlgorithm()));
103    }
104
105    public static Test suite() {
106        return new TestSuite(PSourceTest.class);
107    }
108
109    public static void main(String[] args) {
110        junit.textui.TestRunner.run(suite());
111    }
112}
113
114