ECPublicKeySpec_ImplTest.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;
26import java.security.spec.ECParameterSpec;
27import java.security.spec.ECPoint;
28import java.security.spec.ECPublicKeySpec;
29import java.security.spec.EllipticCurve;
30
31import junit.framework.TestCase;
32
33/**
34 * Tests for <code>ECPublicKeySpec</code> class fields and methods.
35 *
36 */
37public class ECPublicKeySpec_ImplTest extends TestCase {
38
39    //
40    // Tests
41    //
42    // NOTE: the following tests use EC domain parameters
43    // which are invalid for real EC cryptography application
44    // but must be acceptable by the class under test according
45    // to the API specification
46    //
47
48    /**
49     * Test #1 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br>
50     * Assertion: creates <code>ECPublicKeySpec</code> instance<br>
51     * Test preconditions: valid parameters passed<br>
52     * Expected: must pass without any exceptions
53     */
54    public final void testECPublicKeySpec01() {
55        // Valid (see note below) parameters set
56        EllipticCurve c =
57            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
58                              BigInteger.ZERO,
59                              BigInteger.valueOf(4L));
60        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
61        new ECPublicKeySpec(g,
62                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
63
64    }
65
66   /**
67     * Test #2 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br>
68     * Assertion: throws <code>NullPointerException</code> if
69     * <code>w</code> or <code>params</code> is <code>null</code><br>
70     * Test preconditions: pass <code>null</code> as mentioned parameters<br>
71     * Expected: must throw <code>NullPointerException</code>
72     */
73    public final void testECPublicKeySpec02() {
74        // Valid (see note below) parameters set
75        EllipticCurve c =
76            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
77                              BigInteger.ZERO,
78                              BigInteger.valueOf(4L));
79        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
80
81        // Test case 1: w is null
82        try {
83            new ECPublicKeySpec(null,
84                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
85            fail("#1: Expected NPE not thrown");
86        } catch (NullPointerException ok) {
87        }
88
89
90        // Test case 2: params is null
91        try {
92            new ECPublicKeySpec(g, null);
93            fail("#2: Expected NPE not thrown");
94        } catch (NullPointerException ok) {
95        }
96
97
98        // Test case 3: both w and params are null
99        try {
100            new ECPublicKeySpec(null, null);
101            fail("#3: Expected NPE not thrown");
102        } catch (NullPointerException ok) {
103        }
104    }
105
106    /**
107      * Test #3 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br>
108      * Assertion: throws <code>IllegalArgumentException</code> if
109      * <code>w</code> is point at infinity<br>
110      * Test preconditions: pass <code>ECPoint.POINT_INFINITY</code>
111      * as mentioned parameter value<br>
112      * Expected: must throw <code>IllegalArgumentException</code>
113      */
114     public final void testECPublicKeySpec03() {
115         // Valid (see note below) parameters set
116         EllipticCurve c =
117             new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
118                               BigInteger.ZERO,
119                               BigInteger.valueOf(4L));
120         ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
121
122         try {
123             new ECPublicKeySpec(ECPoint.POINT_INFINITY,
124                     new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
125            fail("Expected IAE not thrown");
126         } catch (IllegalArgumentException ok) {
127         }
128     }
129
130     /**
131     * Test for <code>getParams()</code> method<br>
132     * Assertion: returns associated EC parameters<br>
133     * Test preconditions: <code>ECPublicKeySpec</code> instance
134     * created using valid parameters<br>
135     * Expected: must return params value which is equal
136     * to the one passed to the constructor; (both must refer
137     * the same object)
138     */
139    public final void testGetParams() {
140        // Valid (see note below) parameters set
141        EllipticCurve c =
142            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
143                              BigInteger.ZERO,
144                              BigInteger.valueOf(4L));
145        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
146        ECParameterSpec params =
147            new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);
148
149        ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
150        ECParameterSpec paramsRet = ks.getParams();
151
152        assertEquals(params, paramsRet);
153        assertSame(params, paramsRet);
154    }
155
156    /**
157     * Test for <code>getW()</code> method<br>
158     * Assertion: returns associated public point<br>
159     * Test preconditions: <code>ECPublicKeySpec</code> instance
160     * created using valid parameters<br>
161     * Expected: must return w value which is equal
162     * to the one passed to the constructor; (both must refer
163     * the same object)
164     */
165    public final void testGetW() {
166        // Valid (see note below) parameters set
167        EllipticCurve c =
168            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
169                              BigInteger.ZERO,
170                              BigInteger.valueOf(4L));
171        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
172        ECParameterSpec params =
173            new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);
174
175        ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
176        ECPoint wRet = ks.getW();
177
178        assertEquals(g, wRet);
179        assertSame(g, wRet);
180    }
181}
182