ECFieldFpTest.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 Vladimir N. Molotkov
20*/
21
22package org.apache.harmony.security.tests.java.security.spec;
23
24import java.math.BigInteger;
25import java.security.spec.ECFieldFp;
26
27import junit.framework.TestCase;
28
29/**
30 * Tests for <code>ECFieldFp</code> class fields and methods.
31 *
32 */
33public class ECFieldFpTest extends TestCase {
34
35    /**
36     * Constructor for ECFieldFpTest.
37     * @param name
38     */
39    public ECFieldFpTest(String name) {
40        super(name);
41    }
42
43    //
44    // Tests
45    //
46
47    /**
48     * Test #1 for <code>ECFieldFp</code> constructor
49     *
50     * Assertion: creates new object of <code>ECFieldFp</code> class
51     * using valid <code>p</code> (odd prime)
52     */
53    public final void testECFieldFp01() {
54        new ECFieldFp(BigInteger.valueOf(23L));
55    }
56
57    /**
58     * Test #2 for <code>ECFieldFp</code> constructor
59     *
60     * Assertion: creates new object of <code>ECFieldFp</code> class
61     * using valid <code>p</code> (odd but not prime)
62     */
63    public final void testECFieldFp02() {
64        new ECFieldFp(BigInteger.valueOf(21L));
65    }
66
67    /**
68     * Test #3 for <code>ECFieldFp</code> constructor
69     *
70     * Assertion: IllegalArgumentException if <code>p</code> is not positive
71     */
72    public final void testECFieldFp03() {
73        try {
74            new ECFieldFp(BigInteger.valueOf(-1L));
75            fail(getName() +
76                    " FAILED: expected exception has not been thrown");
77        } catch (IllegalArgumentException e) {
78        }
79    }
80
81    /**
82     * Test #4 for <code>ECFieldFp</code> constructor
83     *
84     * Assertion: IllegalArgumentException if <code>p</code> is not positive
85     */
86    public final void testECFieldFp04() {
87        try {
88            new ECFieldFp(BigInteger.valueOf(0L));
89            fail(getName() +
90                    " FAILED: expected exception has not been thrown");
91        } catch (IllegalArgumentException e) {
92        }
93    }
94
95    /**
96     * Test #4 for <code>ECFieldFp</code> constructor
97     *
98     * Assertion: NullPointerException if <code>p</code> is null
99     */
100    public final void testECFieldFp05() {
101        try {
102            new ECFieldFp(null);
103            fail(getName() +
104                    " FAILED: expected exception has not been thrown");
105        } catch (NullPointerException e) {
106        }
107    }
108
109    /**
110     * Test #1 for <code>hashCode()</code> method.<br>
111     *
112     * Assertion: must return the same value if invoked
113     * repeatedly on the same object.
114     */
115    public final void testHashCode01() {
116        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
117        int hc = f.hashCode();
118        assertTrue(hc == f.hashCode() &&
119                   hc == f.hashCode() &&
120                   hc == f.hashCode() &&
121                   hc == f.hashCode() &&
122                   hc == f.hashCode() &&
123                   hc == f.hashCode() &&
124                   hc == f.hashCode() &&
125                   hc == f.hashCode());
126    }
127
128    /**
129     * Test #2 for <code>hashCode()</code> method.<br>
130     *
131     * Assertion: must return the same value if invoked
132     * on equal (according to the <code>equals(Object)</code> method) objects.
133     */
134    public final void testHashCode02() {
135        assertTrue(new ECFieldFp(BigInteger.valueOf(23L)).hashCode() ==
136                   new ECFieldFp(BigInteger.valueOf(23L)).hashCode());
137    }
138
139    /**
140     * Test for <code>getFieldSize()()</code> method.<br>
141     *
142     * Assertion: returns field size in bits which is prime size
143     */
144    public final void testGetFieldSize() {
145        assertEquals(5, new ECFieldFp(BigInteger.valueOf(23L)).getFieldSize());
146    }
147
148    /**
149     * Test for <code>getP()</code> method.<br>
150     *
151     * Assertion: returns prime
152     */
153    public final void testGetP() {
154        BigInteger p = BigInteger.valueOf(23L);
155        assertTrue(p.equals(new ECFieldFp(p).getP()));
156    }
157
158    /**
159     * Test #1 for <code>equals()</code> method.<br>
160     *
161     * Assertion: object equals to itself.
162     */
163    public final void testEqualsObject01() {
164        ECFieldFp obj = new ECFieldFp(BigInteger.valueOf(23L));
165        assertTrue(obj.equals(obj));
166    }
167
168    /**
169     * Test #2 for <code>equals(Object obj)</code> method.<br>
170     *
171     * Assertion: returns false if <code>obj</code> is <code>null</code>
172     */
173    public final void testEqualsObject02() {
174        assertFalse(new ECFieldFp(BigInteger.valueOf(23L)).equals(null));
175    }
176
177    /**
178     * Test #3 for <code>equals(Object obj)</code> method.<br>
179     *
180     * Assertion: returns false if <code>obj</code>
181     * is not instance of <code>ECFieldFp</code>
182     */
183    public final void testEqualsObject03() {
184        assertFalse(new ECFieldFp(BigInteger.valueOf(23L)).equals(new Object()));
185    }
186
187    /**
188     * Test #4 for <code>equals()</code> method.<br>
189     *
190     * Assertion: true if prime values match.
191     */
192    public final void testEqualsObject04() {
193        assertTrue(new ECFieldFp(BigInteger.valueOf(23L)).equals(
194                   new ECFieldFp(BigInteger.valueOf(23L))));
195    }
196
197}
198