ECPointTest.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.ECPoint;
26
27import junit.framework.TestCase;
28
29/**
30 * Tests for <code>ECPoint</code> class fields and methods.
31 *
32 */
33public class ECPointTest extends TestCase {
34
35    /**
36     * Constructor for ECPointTest.
37     * @param name
38     */
39    public ECPointTest(String name) {
40        super(name);
41    }
42
43    //
44    // Tests
45    //
46
47    /**
48     * Test #1 for <code>ECPoint(BigInteger, BigInteger)</code> constructor<br>
49     * Assertion: creates <code>ECPoint</code> instance<br>
50     * Test preconditions: valid parameters passed<br>
51     * Expected: must pass without any exceptions
52     */
53    public final void testECPoint01() {
54        new ECPoint(BigInteger.ZERO, BigInteger.ZERO);
55        new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(-23456L));
56        new ECPoint(BigInteger.valueOf(123456L), BigInteger.valueOf(123456L));
57        new ECPoint(BigInteger.valueOf(-56L), BigInteger.valueOf(234L));
58        new ECPoint(BigInteger.valueOf(3456L), BigInteger.valueOf(-2344L));
59    }
60
61    /**
62     * Test #2 for <code>ECPoint(BigInteger x, BigInteger y)</code> constructor<br>
63     * Assertion: throws <code>NullPointerException</code> if <code>x</code>or
64     * <code>y</code> is <code>null</code><br>
65     * Test preconditions: pass <code>null</code> as mentioned parameters<br>
66     * Expected: must throw <code>NullPointerException</code>
67     */
68    public final void testECPoint02() {
69        // test case 1: x is null
70        try {
71            new ECPoint(null, BigInteger.ZERO);
72            fail("#1: Expected NPE not thrown");
73        } catch (NullPointerException ok) {
74        }
75
76
77        // test case 2: y is null
78        try {
79            new ECPoint(BigInteger.ZERO, null);
80            fail("#2: Expected NPE not thrown");
81        } catch (NullPointerException ok) {
82        }
83
84
85        // test case 3: both : x and y are null
86        try {
87            new ECPoint(null, null);
88            fail("#3: Expected NPE not thrown");
89        } catch (NullPointerException ok) {
90        }
91    }
92
93    /**
94     * Test #1 for <code>getAffineX()</code> method<br>
95     * Assertion: returns affine <code>x</code> coordinate<br>
96     * Test preconditions: <code>ECPoint</code> instance
97     * created using valid parameters<br>
98     * Expected: must return affine <code>x</code> coordinate
99     * which is equal to the one passed to the constructor;
100     * (both must refer the same object)
101     */
102    public final void testGetAffineX01() {
103        BigInteger x = BigInteger.valueOf(-23456L);
104        ECPoint p = new ECPoint(x, BigInteger.valueOf(23456L));
105        BigInteger xRet = p.getAffineX();
106        assertEquals(x, xRet);
107        assertSame(x, xRet);
108    }
109
110    /**
111     * Test #2 for <code>getAffineX()</code> method<br>
112     * Assertion: returns <code>null</code> for <code>ECPoint.POINT_INFINITY</code><br>
113     * Test preconditions: none<br>
114     * Expected: must return <code>null</code> for
115     * <code>ECPoint.POINT_INFINITY</code>
116     */
117    public final void testGetAffineX02() {
118        assertNull(ECPoint.POINT_INFINITY.getAffineX());
119    }
120
121    /**
122     * Test #1 for <code>getAffineY()</code> method<br>
123     * Assertion: returns affine <code>y</code> coordinate<br>
124     * Test preconditions: <code>ECPoint</code> instance
125     * created using valid parameters<br>
126     * Expected: must return affine <code>y</code> coordinate
127     * which is equal to the one passed to the constructor;
128     * (both must refer the same object)
129     */
130    public final void testGetAffineY01() {
131        BigInteger y =  BigInteger.valueOf(23456L);
132        ECPoint p = new ECPoint(BigInteger.valueOf(-23456L), y);
133        BigInteger yRet = p.getAffineY();
134        assertEquals(y, yRet);
135        assertSame(y, yRet);
136    }
137
138    /**
139     * Test #2 for <code>getAffineX()</code> method<br>
140     * Assertion: returns <code>null</code> for <code>ECPoint.POINT_INFINITY</code><br>
141     * Test preconditions: none<br>
142     * Expected: must return <code>null</code> for
143     * <code>ECPoint.POINT_INFINITY</code>
144     */
145    public final void testGetAffineY02() {
146        assertNull(ECPoint.POINT_INFINITY.getAffineY());
147    }
148
149    /**
150     * Test #1 for <code>equals(Object other)</code> method<br>
151     * Assertion: return true if this and other objects are equal<br>
152     * Test preconditions: see test comments<br>
153     * Expected: all objects in this test must be equal
154     */
155    public final void testEqualsObject01() {
156        // test case 1: must be equal to itself
157        ECPoint p2=null, p1 =
158            new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
159        assertTrue(p1.equals(p1));
160
161        // test case 2: equal objects
162        p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
163        p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L));
164        assertTrue(p1.equals(p2) && p2.equals(p1));
165
166        // test case 3: equal POINT_INFINITY object(s)
167        p1 = ECPoint.POINT_INFINITY;
168        p2 = ECPoint.POINT_INFINITY;
169        assertTrue(p1.equals(p2) && p2.equals(p1));
170    }
171
172    /**
173     * Test #2 for <code>equals(Object other)</code> method<br>
174     * Assertion: return false if this and other objects are not equal<br>
175     * Test preconditions: see test comments<br>
176     * Expected: all objects in this test must be not equal
177     */
178    public final void testEqualsObject02() {
179        // test case 1: must be not equal to null
180        ECPoint p2=null, p1 =
181            new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
182        assertFalse(p1.equals(p2));
183
184        // test case 2: not equal objects - x
185        p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
186        p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L));
187        assertFalse(p1.equals(p2) || p2.equals(p1));
188
189        // test case 3: not equal objects - y
190        p1 = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
191        p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
192        assertFalse(p1.equals(p2) || p2.equals(p1));
193
194        // test case 4: not equal - some point and POINT_INFINITY
195        p1 = ECPoint.POINT_INFINITY;
196        p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ZERO);
197        assertFalse(p1.equals(p2) || p2.equals(p1));
198    }
199
200    /**
201     * Test #1 for <code>hashCode()</code> method.<br>
202     *
203     * Assertion: must return the same value if invoked
204     * repeatedly on the same object.
205     */
206    public final void testHashCode01() {
207        ECPoint f = new ECPoint(BigInteger.valueOf(-23457L), BigInteger.ONE);
208        int hc = f.hashCode();
209        assertTrue(hc == f.hashCode() &&
210                   hc == f.hashCode() &&
211                   hc == f.hashCode() &&
212                   hc == f.hashCode() &&
213                   hc == f.hashCode() &&
214                   hc == f.hashCode() &&
215                   hc == f.hashCode() &&
216                   hc == f.hashCode());
217
218
219        // the same for POINT_INFINITY
220        hc = ECPoint.POINT_INFINITY.hashCode();
221        assertTrue(hc == ECPoint.POINT_INFINITY.hashCode() &&
222                   hc == ECPoint.POINT_INFINITY.hashCode() &&
223                   hc == ECPoint.POINT_INFINITY.hashCode() &&
224                   hc == ECPoint.POINT_INFINITY.hashCode() &&
225                   hc == ECPoint.POINT_INFINITY.hashCode() &&
226                   hc == ECPoint.POINT_INFINITY.hashCode() &&
227                   hc == ECPoint.POINT_INFINITY.hashCode() &&
228                   hc == ECPoint.POINT_INFINITY.hashCode());
229    }
230
231    /**
232     * Test #2 for <code>hashCode()</code> method.<br>
233     *
234     * Assertion: must return the same value if invoked
235     * on equal (according to the <code>equals(Object)</code> method) objects.
236     */
237    public final void testHashCode02() {
238        ECPoint p1 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.ONE);
239        ECPoint p2 = new ECPoint(BigInteger.valueOf(-23456L), BigInteger.valueOf(1L));
240        assertEquals(p1.hashCode(), p2.hashCode());
241    }
242
243}
244