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.EllipticCurve;
29
30import junit.framework.TestCase;
31
32/**
33 * Tests for <code>ECParameterSpec</code> class fields and methods.
34 *
35 */
36public class ECParameterSpec_ImplTest extends TestCase {
37
38    //
39    // Tests
40    //
41    // NOTE: the following tests use EC domain parameters
42    // which are invalid for real EC cryptography application
43    // but must be acceptable by the class under test according
44    // to the API specification
45    //
46
47    /**
48     * Test #1 for <code>ECParameterSpec(EllipticCurve, ECPoint, BigInteger, int)</code> constructor<br>
49     * Assertion: creates <code>ECParameterSpec</code> instance<br>
50     * Test preconditions: valid parameters passed<br>
51     * Expected: must pass without any exceptions
52     */
53    public final void testECParameterSpec01() {
54        // Valid (see note below) parameters set
55        EllipticCurve c =
56            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
57                              BigInteger.ZERO,
58                              BigInteger.valueOf(4L));
59        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
60        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);
61    }
62
63    /**
64     * Test #2 for <code>ECParameterSpec(EllipticCurve, ECPoint, BigInteger, int)</code> constructor<br>
65     * Assertion: throws <code>NullPointerException</code> if
66     * <code>curve</code>, <code>generator</code> or <code>order</code> is <code>null</code><br>
67     * Test preconditions: pass <code>null</code> as mentioned parameters<br>
68     * Expected: must throw <code>NullPointerException</code>
69     */
70    public final void testECParameterSpec02() {
71        // Valid (see note below) parameters set
72        EllipticCurve curve =
73            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
74                              BigInteger.ZERO,
75                              BigInteger.valueOf(4L));
76        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
77        BigInteger order = BigInteger.valueOf(5L);
78
79        // Test case 1: curve is null
80        try {
81            new ECParameterSpec(null, generator, order, 10);
82            fail("#1: Expected NPE not thrown");
83        } catch (NullPointerException ok) {
84        }
85
86
87        // Test case 2: generator is null
88        try {
89            new ECParameterSpec(curve, null, order, 10);
90            fail("#2: Expected NPE not thrown");
91        } catch (NullPointerException ok) {
92        }
93
94
95        // Test case 3: order is null
96        try {
97            new ECParameterSpec(curve, generator, null, 10);
98            fail("#3: Expected NPE not thrown");
99        } catch (NullPointerException ok) {
100        }
101
102
103        // Test case 4: all above are null
104        try {
105            new ECParameterSpec(null, null, null, 10);
106            fail("#4: Expected NPE not thrown");
107        } catch (NullPointerException ok) {
108        }
109    }
110
111    /**
112     * Test #3 for <code>ECParameterSpec(EllipticCurve, ECPoint, BigInteger, int)</code> constructor<br>
113     * Assertion: throws <code>IllegalArgumentException</code> if
114     * <code>order</code> or <code>cofactor</code> is not positive<br>
115     * Test preconditions: pass not positive as mentioned parameters<br>
116     * Expected: must throw <code>IllegalArgumentException</code>
117     */
118    public final void testECParameterSpec03() {
119        // Valid (see note below) parameters set
120        EllipticCurve curve =
121            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
122                              BigInteger.ZERO,
123                              BigInteger.valueOf(4L));
124        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
125
126
127        // Test case 1: order is negative
128        try {
129            new ECParameterSpec(curve, generator, BigInteger.valueOf(-5L), 10);
130            fail("#1: Expected IAE not thrown");
131        } catch (IllegalArgumentException ok) {
132        }
133
134
135        // Test case 2: order == 0
136        try {
137            new ECParameterSpec(curve, generator, BigInteger.ZERO, 10);
138            fail("#2: Expected IAE not thrown");
139        } catch (IllegalArgumentException ok) {
140        }
141
142
143        // Test case 3: cofactor is negative
144        try {
145            new ECParameterSpec(curve, generator, BigInteger.valueOf(5L), -10);
146            fail("#3: Expected IAE not thrown");
147        } catch (IllegalArgumentException ok) {
148        }
149
150
151        // Test case 4: cofactor == 0
152        try {
153            new ECParameterSpec(curve, generator, BigInteger.valueOf(5L), 0);
154            fail("#4: Expected IAE not thrown");
155        } catch (IllegalArgumentException ok) {
156        }
157
158
159        // Test case 5: both order and cofactor are not positive
160        try {
161            new ECParameterSpec(curve, generator, BigInteger.valueOf(-5L), 0);
162            fail("#5: Expected IAE not thrown");
163        } catch (IllegalArgumentException ok) {
164        }
165    }
166
167    /**
168     * Test for <code>getCofactor()</code> method<br>
169     * Assertion: returns cofactor<br>
170     * Test preconditions: <code>ECParameterSpec</code> instance
171     * created using valid parameters<br>
172     * Expected: must return cofactor value which is equal
173     * to the one passed to the constructor
174     */
175    public final void testGetCofactor() {
176        // Valid (see note below) parameters set
177        EllipticCurve curve =
178            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
179                              BigInteger.ZERO,
180                              BigInteger.valueOf(4L));
181        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
182        BigInteger order = BigInteger.valueOf(5L);
183        int cofactor = 10;
184        ECParameterSpec ps =
185            new ECParameterSpec(curve, generator, order, cofactor);
186        assertEquals(cofactor, ps.getCofactor());
187    }
188
189    /**
190     * Test for <code>getCurve()</code> method<br>
191     * Assertion: returns curve<br>
192     * Test preconditions: <code>ECParameterSpec</code> instance
193     * created using valid parameters<br>
194     * Expected: must return ref to the <code>EllipticCurve</code> object
195     * which is equal to the one passed to the constructor; (both must refer
196     * the same object)
197     */
198    public final void testGetCurve() {
199        // Valid (see note below) parameters set
200        EllipticCurve curve =
201            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
202                              BigInteger.ZERO,
203                              BigInteger.valueOf(4L));
204        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
205        BigInteger order = BigInteger.valueOf(5L);
206        int cofactor = 10;
207        ECParameterSpec ps =
208            new ECParameterSpec(curve, generator, order, cofactor);
209        EllipticCurve curveRet = ps.getCurve();
210        assertEquals(curve, curveRet);
211        assertSame(curve, curveRet);
212    }
213
214    /**
215     * Test for <code>getGenerator()</code> method<br>
216     * Assertion: returns generator<br>
217     * Test preconditions: <code>ECParameterSpec</code> instance
218     * created using valid parameters<br>
219     * Expected: must return ref to the <code>ECPoint</code> object
220     * which is equal to the one passed to the constructor; (both must refer
221     * the same object)
222     */
223    public final void testGetGenerator() {
224        // Valid (see note below) parameters set
225        EllipticCurve curve =
226            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
227                              BigInteger.ZERO,
228                              BigInteger.valueOf(4L));
229        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
230        BigInteger order = BigInteger.valueOf(5L);
231        int cofactor = 10;
232        ECParameterSpec ps =
233            new ECParameterSpec(curve, generator, order, cofactor);
234        ECPoint generatorRet = ps.getGenerator();
235        assertEquals(generator, generatorRet);
236        assertSame(generator, generatorRet);
237    }
238
239    /**
240     * Test for <code>getOrder()</code> method<br>
241     * Assertion: returns order<br>
242     * Test preconditions: <code>ECParameterSpec</code> instance
243     * created using valid parameters<br>
244     * Expected: must return ref to the <code>BigInteger</code> object
245     * which is equal to the one passed to the constructor; (both must refer
246     * the same object)
247     */
248    public final void testGetOrder() {
249        // Valid (see note below) parameters set
250        EllipticCurve curve =
251            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
252                              BigInteger.ZERO,
253                              BigInteger.valueOf(4L));
254        ECPoint generator = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
255        BigInteger order = BigInteger.valueOf(5L);
256        int cofactor = 10;
257        ECParameterSpec ps =
258            new ECParameterSpec(curve, generator, order, cofactor);
259        BigInteger orderRet = ps.getOrder();
260        assertEquals(order, orderRet);
261        assertSame(order, orderRet);
262    }
263
264}
265