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
18package tests.security.spec;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import junit.framework.TestCase;
26
27import java.math.BigInteger;
28import java.security.spec.ECField;
29import java.security.spec.ECFieldF2m;
30import java.security.spec.ECFieldFp;
31import java.security.spec.EllipticCurve;
32import java.util.Arrays;
33
34/**
35 * Tests for <code>EllipticCurve</code> class fields and methods.
36 *
37 */
38@TestTargetClass(EllipticCurve.class)
39public class EllipticCurveTest extends TestCase {
40
41    /**
42     * Test #1 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
43     * constructor<br>
44     * Assertion: creates instance of EllipticCurve<br>
45     * Test preconditions: valid parameters passed<br>
46     * Expected: must pass without any exceptions
47     */
48    @TestTargetNew(
49        level = TestLevel.PARTIAL_COMPLETE,
50        notes = "Verifies positive cases.",
51        method = "EllipticCurve",
52        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class}
53    )
54    public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray01() {
55        // test case 1 parameters set
56        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
57        BigInteger a = BigInteger.ONE;
58        BigInteger b = BigInteger.valueOf(19L);
59        byte[] seed = new byte[24];
60        // perform test case 1
61        new EllipticCurve(f, a, b, seed);
62
63        // test case 2 parameters set
64        ECFieldF2m f1 = new ECFieldF2m(5);
65        a = BigInteger.ZERO;
66        b = BigInteger.valueOf(23L);
67        // perform test case 2
68        new EllipticCurve(f1, a, b, seed);
69
70        // test case 3 parameters set,
71        // the seed parameter may be null
72        f = new ECFieldFp(BigInteger.valueOf(23L));
73        a = BigInteger.ONE;
74        b = BigInteger.valueOf(19L);
75        seed = null;
76        // perform test case 3
77        new EllipticCurve(f, a, b, seed);
78    }
79
80    /**
81     * Test #2 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
82     * constructor<br>
83     * Assertion: throws <code>NullPointerException</code> if <code>field</code>,
84     * <code>a</code> or <code>b</code> is <code>null</code><br>
85     * Test preconditions: pass <code>null</code> as mentioned parameters<br>
86     * Expected: must throw <code>NullPointerException</code>
87     */
88    @TestTargetNew(
89        level = TestLevel.PARTIAL_COMPLETE,
90        notes = "Verifies NullPointerException.",
91        method = "EllipticCurve",
92        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class}
93    )
94    public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray02() {
95        // test case 1 parameters set
96        ECFieldFp f = null;
97        BigInteger a = BigInteger.ONE;
98        BigInteger b = BigInteger.valueOf(19L);
99        byte[] seed = new byte[24];
100
101        // perform test case 1
102        try {
103            new EllipticCurve(f, a, b, seed);
104            fail("#1: Expected NPE not thrown");
105        } catch (NullPointerException ok) {}
106
107        // test case 2 parameters set,
108        f = new ECFieldFp(BigInteger.valueOf(23L));
109        a = null;
110        b = BigInteger.valueOf(19L);
111        seed = new byte[24];
112        // perform test case 2
113        try {
114            new EllipticCurve(f, a, b, seed);
115            fail("#2: Expected NPE not thrown");
116        } catch (NullPointerException ok) {}
117
118        // test case 3 parameters set,
119        f = new ECFieldFp(BigInteger.valueOf(23L));
120        a = BigInteger.ONE;
121        b = null;
122        seed = new byte[24];
123        // perform test case 2
124        try {
125            new EllipticCurve(f, a, b, seed);
126            fail("#3: Expected NPE not thrown");
127        } catch (NullPointerException ok) {}
128    }
129
130    /**
131     * Test #3 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
132     * constructor<br>
133     * Assertion: throws <code>IllegalArgumentException</code> if
134     * <code>a</code> or <code>b</code> is not <code>null</code> and not in
135     * the <code>field</code><br>
136     * Test preconditions: pass <code>a</code>, <code>b</code> which are
137     * not in the <code>field</code> of type <code>ECFieldFp</code><br>
138     * Expected: must throw <code>IllegalArgumentException</code>
139     */
140    @TestTargetNew(
141        level = TestLevel.PARTIAL_COMPLETE,
142        notes = "Verifies IllegalArgumentException.",
143        method = "EllipticCurve",
144        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class}
145    )
146    public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray03() {
147        // test case 1 parameters set,
148        // a is not in field
149        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
150        BigInteger a = BigInteger.valueOf(24L);
151        BigInteger b = BigInteger.valueOf(19L);
152        byte[] seed = new byte[24];
153
154        // perform test case 1
155        try {
156            new EllipticCurve(f, a, b, seed);
157            fail("#1: Expected IAE not thrown");
158        } catch (IllegalArgumentException ok) {}
159
160        // test case 1.1 parameters set,
161        // b is not in field
162        f = new ECFieldFp(BigInteger.valueOf(23L));
163        a = BigInteger.valueOf(1L);
164        b = BigInteger.valueOf(23L);
165        seed = new byte[24];
166        // perform test case 1.1
167        try {
168            new EllipticCurve(f, a, b, seed);
169            fail("#1.1: Expected IAE not thrown");
170        } catch (IllegalArgumentException ok) {}
171
172        // test case 2 parameters set,
173        // b is not in field
174        f = new ECFieldFp(BigInteger.valueOf(23L));
175        a = BigInteger.valueOf(19L);
176        b = BigInteger.valueOf(24L);
177        seed = new byte[24];
178        // perform test case 2
179        try {
180            new EllipticCurve(f, a, b, seed);
181            fail("#2: Expected IAE not thrown");
182        } catch (IllegalArgumentException ok) {}
183
184        // test case 3 parameters set,
185        // both a and b are not in field
186        f = new ECFieldFp(BigInteger.valueOf(23L));
187        a = BigInteger.valueOf(25L);
188        b = BigInteger.valueOf(240L);
189        seed = new byte[24];
190        // perform test case 3
191        try {
192            new EllipticCurve(f, a, b, seed);
193            fail("#3: Expected IAE not thrown");
194        } catch (IllegalArgumentException ok) {}
195    }
196
197    /**
198     * Test #4 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
199     * constructor<br>
200     * Assertion: throws <code>IllegalArgumentException</code> if
201     * <code>a</code> or <code>b</code> is not <code>null</code> and not in
202     * the <code>field</code><br>
203     * Test preconditions: pass <code>a</code>, <code>b</code> which are
204     * not in the <code>field</code> of type <code>ECFieldF2m</code><br>
205     * Expected: must throw <code>IllegalArgumentException</code>
206     */
207    @TestTargetNew(
208        level = TestLevel.PARTIAL_COMPLETE,
209        notes = "Verifies IllegalArgumentException.",
210        method = "EllipticCurve",
211        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class}
212    )
213    public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray04() {
214        // test case 1 parameters set,
215        // a is not in field
216        ECFieldF2m f = new ECFieldF2m(5);
217        BigInteger a = BigInteger.valueOf(32L);
218        BigInteger b = BigInteger.valueOf(19L);
219        byte[] seed = new byte[24];
220
221        // perform test case 1
222        try {
223            new EllipticCurve(f, a, b, seed);
224            fail("#1: Expected IAE not thrown");
225        } catch (IllegalArgumentException ok) {}
226
227        // test case 2 parameters set,
228        // b is not in field
229        f = new ECFieldF2m(5);
230        a = BigInteger.valueOf(19L);
231        b = BigInteger.valueOf(32L);
232        seed = new byte[24];
233        // perform test case 2
234        try {
235            new EllipticCurve(f, a, b, seed);
236            fail("#2: Expected IAE not thrown");
237        } catch (IllegalArgumentException ok) {}
238
239        // test case 3 parameters set,
240        // both a and b are not in field
241        f = new ECFieldF2m(5);
242        a = BigInteger.valueOf(32L);
243        b = BigInteger.valueOf(43L);
244        seed = new byte[24];
245        // perform test case 3
246        try {
247            new EllipticCurve(f, a, b, seed);
248            fail("#3: Expected IAE not thrown");
249        } catch (IllegalArgumentException ok) {}
250    }
251
252    /**
253     * Test #5 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
254     * constructor<br>
255     * Assertion: array <code>seed</code> is copied to prevent subsequent modification<br>
256     * Test preconditions: pass <code>seed</code> to the ctor then modify it<br>
257     * Expected: getSeed() must return unmodified array
258     */
259    @TestTargetNew(
260        level = TestLevel.PARTIAL_COMPLETE,
261        notes = "Verifies that byte array of EllipticCurve can't be modified",
262        method = "EllipticCurve",
263        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class, byte[].class}
264    )
265    public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray05() {
266        ECFieldF2m f = new ECFieldF2m(5);
267        BigInteger a = BigInteger.valueOf(0L);
268        BigInteger b = BigInteger.valueOf(19L);
269        byte[] seed = new byte[24];
270        byte[] seedCopy = seed.clone();
271        EllipticCurve c = new EllipticCurve(f, a, b, seedCopy);
272        // modify array passed
273        seedCopy[0] = (byte) 1;
274        // check that above modification did not changed
275        // internal state of test object
276        assertTrue(Arrays.equals(seed, c.getSeed()));
277    }
278
279    /**
280     * Test #1 for <code>EllipticCurve(ECField, BigInteger, BigInteger)</code>
281     * constructor<br>
282     * Assertion: creates instance of EllipticCurve<br>
283     * Test preconditions: valid parameters passed, field type is ECFieldFp<br>
284     * Expected: must pass without any exceptions
285     */
286    @TestTargetNew(
287        level = TestLevel.PARTIAL_COMPLETE,
288        notes = "Doesn't verify exceptions.",
289        method = "EllipticCurve",
290        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class}
291    )
292    public final void testEllipticCurveECFieldBigIntegerBigInteger01() {
293        // test case 1 parameters set
294        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
295        BigInteger a = BigInteger.ONE;
296        BigInteger b = BigInteger.valueOf(19L);
297        // perform test case 1
298        new EllipticCurve(f, a, b);
299
300        // test case 2 parameters set
301        ECFieldF2m f1 = new ECFieldF2m(5);
302        a = BigInteger.ZERO;
303        b = BigInteger.valueOf(23L);
304        // perform test case 2
305        new EllipticCurve(f1, a, b);
306
307        // test case 3 parameters set,
308        // the seed parameter may be null
309        f = new ECFieldFp(BigInteger.valueOf(23L));
310        a = BigInteger.ONE;
311        b = BigInteger.valueOf(19L);
312        // perform test case 3
313        new EllipticCurve(f, a, b);
314    }
315
316    /**
317     * Test #2 for <code>EllipticCurve(ECField, BigInteger, BigInteger)</code>
318     * constructor<br>
319     * Assertion: throws <code>NullPointerException</code> if <code>field</code>,
320     * <code>a</code> or <code>b</code> is <code>null</code><br>
321     * Test preconditions: pass <code>null</code> as mentioned parameters<br>
322     * Expected: must throw <code>NullPointerException</code>
323     */
324    @TestTargetNew(
325        level = TestLevel.PARTIAL_COMPLETE,
326        notes = "Verifies NullPointerException.",
327        method = "EllipticCurve",
328        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class}
329    )
330    public final void testEllipticCurveECFieldBigIntegerBigInteger02() {
331        // test case 1 parameters set
332        ECFieldFp f = null;
333        BigInteger a = BigInteger.ONE;
334        BigInteger b = BigInteger.valueOf(19L);
335
336        // perform test case 1
337        try {
338            new EllipticCurve(f, a, b);
339            fail("#1: Expected NPE not thrown");
340        } catch (NullPointerException ok) {}
341
342        // test case 2 parameters set,
343        f = new ECFieldFp(BigInteger.valueOf(23L));
344        a = null;
345        b = BigInteger.valueOf(19L);
346        // perform test case 2
347        try {
348            new EllipticCurve(f, a, b);
349            fail("#2: Expected NPE not thrown");
350        } catch (NullPointerException ok) {}
351
352        // test case 3 parameters set,
353        f = new ECFieldFp(BigInteger.valueOf(23L));
354        a = BigInteger.ONE;
355        b = null;
356        // perform test case 3
357        try {
358            new EllipticCurve(f, a, b);
359            fail("#3: Expected NPE not thrown");
360        } catch (NullPointerException ok) {}
361    }
362
363    /**
364     * Test #3 for <code>EllipticCurve(ECField, BigInteger, BigInteger)</code>
365     * constructor<br>
366     * Assertion: throws <code>IllegalArgumentException</code> if
367     * <code>a</code> or <code>b</code> is not <code>null</code> and not in
368     * the <code>field</code><br>
369     * Test preconditions: pass <code>a</code>, <code>b</code> which are
370     * not in the <code>field</code> of type <code>ECFieldFp</code><br>
371     * Expected: must throw <code>IllegalArgumentException</code>
372     */
373    @TestTargetNew(
374        level = TestLevel.PARTIAL_COMPLETE,
375        notes = "Verifies IllegalArgumentException.",
376        method = "EllipticCurve",
377        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class}
378    )
379    public final void testEllipticCurveECFieldBigIntegerBigInteger03() {
380        // test case 1 parameters set,
381        // a is not in field
382        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
383        BigInteger a = BigInteger.valueOf(24L);
384        BigInteger b = BigInteger.valueOf(19L);
385
386        // perform test case 1
387        try {
388            new EllipticCurve(f, a, b);
389            fail("#1: Expected IAE not thrown");
390        } catch (IllegalArgumentException ok) {}
391
392        // test case 1.1 parameters set,
393        // a is not in field
394        f = new ECFieldFp(BigInteger.valueOf(23L));
395        a = BigInteger.valueOf(23L);
396        b = BigInteger.valueOf(19L);
397        // perform test case 1.1
398        try {
399            new EllipticCurve(f, a, b);
400            fail("#1.1: Expected IAE not thrown");
401        } catch (IllegalArgumentException ok) {}
402
403        // test case 2 parameters set,
404        // b is not in field
405        f = new ECFieldFp(BigInteger.valueOf(23L));
406        a = BigInteger.valueOf(19L);
407        b = BigInteger.valueOf(24L);
408        // perform test case 2
409        try {
410            new EllipticCurve(f, a, b);
411            fail("#2: Expected IAE not thrown");
412        } catch (IllegalArgumentException ok) {}
413
414        // test case 3 parameters set,
415        // both a and b are not in field
416        f = new ECFieldFp(BigInteger.valueOf(23L));
417        a = BigInteger.valueOf(25L);
418        b = BigInteger.valueOf(240L);
419        // perform test case 3
420        try {
421            new EllipticCurve(f, a, b);
422            fail("#3: Expected IAE not thrown");
423        } catch (IllegalArgumentException ok) {}
424    }
425
426    /**
427     * Test #4 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
428     * constructor<br>
429     * Assertion: throws <code>IllegalArgumentException</code> if
430     * <code>a</code> or <code>b</code> is not <code>null</code> and not in
431     * the <code>field</code><br>
432     * Test preconditions: pass <code>a</code>, <code>b</code> which are
433     * not in the <code>field</code> of type <code>ECFieldF2m</code><br>
434     * Expected: must throw <code>IllegalArgumentException</code>
435     */
436    @TestTargetNew(
437        level = TestLevel.PARTIAL_COMPLETE,
438        notes = "Verifies IllegalArgumentException.",
439        method = "EllipticCurve",
440        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class}
441    )
442    public final void testEllipticCurveECFieldBigIntegerBigInteger04() {
443        // test case 1 parameters set,
444        // a is not in field
445        ECFieldF2m f = new ECFieldF2m(5);
446        BigInteger a = BigInteger.valueOf(32L);
447        BigInteger b = BigInteger.valueOf(19L);
448        // perform test case 1
449        try {
450            new EllipticCurve(f, a, b);
451            fail("#1: Expected IAE not thrown");
452        } catch (IllegalArgumentException ok) {}
453
454        // test case 2 parameters set,
455        // b is not in field
456        f = new ECFieldF2m(5);
457        a = BigInteger.valueOf(19L);
458        b = BigInteger.valueOf(32L);
459        // perform test case 2
460        try {
461            new EllipticCurve(f, a, b);
462            fail("#2: Expected IAE not thrown");
463        } catch (IllegalArgumentException ok) {}
464
465        // test case 3 parameters set,
466        // both a and b are not in field
467        f = new ECFieldF2m(5);
468        a = BigInteger.valueOf(32L);
469        b = BigInteger.valueOf(43L);
470        // perform test case 3
471        try {
472            new EllipticCurve(f, a, b);
473            fail("#3: Expected IAE not thrown");
474        } catch (IllegalArgumentException ok) {}
475    }
476
477    /**
478     * Test for <code>getA()</code> method<br>
479     * Assertion: returns coefficient <code>a</code><br>
480     * Test preconditions: <code>ECFieldF2m</code> instance
481     * created using valid parameters<br>
482     * Expected: must return coefficient <code>a</code> which is equal
483     * to the one passed to the constructor; (both must refer
484     * the same object)
485     */
486    @TestTargetNew(
487        level = TestLevel.COMPLETE,
488        notes = "",
489        method = "getA",
490        args = {}
491    )
492    public final void testGetA() {
493        ECFieldF2m f = new ECFieldF2m(5);
494        BigInteger a = BigInteger.valueOf(5L);
495        BigInteger b = BigInteger.valueOf(19L);
496        EllipticCurve c = new EllipticCurve(f, a, b);
497        assertEquals(a, c.getA());
498        assertSame(a, c.getA());
499    }
500
501    /**
502     * @tests java/security/spec/EllipticCurve#EllipticCurve(EcField,BigInteger,BigInteger)
503     */
504    @TestTargetNew(
505        level = TestLevel.PARTIAL_COMPLETE,
506        notes = "Regression test.",
507        method = "EllipticCurve",
508        args = {java.security.spec.ECField.class, java.math.BigInteger.class, java.math.BigInteger.class}
509    )
510    public final void testEllipticCurveECFieldBigIntegerBigInteger05() {
511        // Regression for Harmony-731
512        EllipticCurve ec = new EllipticCurve(new testECField(), BigInteger
513                .valueOf(4L), BigInteger.ONE);
514        assertEquals("incorrect a", ec.getA(), BigInteger.valueOf(4L));
515        assertEquals("incorrect b", ec.getB(), BigInteger.ONE);
516        assertEquals("incorrect size", ec.getField().getFieldSize(), 2);
517    }
518
519    /**
520     * Test for <code>getB()</code> method<br>
521     * Assertion: returns coefficient <code>b</code><br>
522     * Test preconditions: <code>ECFieldF2m</code> instance
523     * created using valid parameters<br>
524     * Expected: must return coefficient <code>b</code> which is equal
525     * to the one passed to the constructor; (both must refer
526     * the same object)
527     */
528    @TestTargetNew(
529        level = TestLevel.COMPLETE,
530        notes = "",
531        method = "getB",
532        args = {}
533    )
534    public final void testGetB() {
535        ECFieldF2m f = new ECFieldF2m(5);
536        BigInteger a = BigInteger.valueOf(5L);
537        BigInteger b = BigInteger.valueOf(19L);
538        EllipticCurve c = new EllipticCurve(f, a, b);
539        assertEquals(b, c.getB());
540        assertSame(b, c.getB());
541    }
542
543    /**
544     * Test for <code>getField()</code> method<br>
545     * Assertion: returns <code>field</code><br>
546     * Test preconditions: <code>ECFieldF2m</code> instance
547     * created using valid parameters<br>
548     * Expected: must return <code>field</code> which is equal
549     * to the one passed to the constructor; (both must refer
550     * the same object)
551     */
552    @TestTargetNew(
553        level = TestLevel.COMPLETE,
554        notes = "",
555        method = "getField",
556        args = {}
557    )
558    public final void testGetField() {
559        ECFieldF2m f = new ECFieldF2m(5);
560        BigInteger a = BigInteger.valueOf(5L);
561        BigInteger b = BigInteger.valueOf(19L);
562        EllipticCurve c = new EllipticCurve(f, a, b);
563        assertEquals(f, c.getField());
564        assertSame(f, c.getField());
565    }
566
567    /**
568     * Test #1 for <code>getSeed()</code> method<br>
569     * Assertion: returns <code>seed</code><br>
570     * Test preconditions: <code>ECFieldF2m</code> instance
571     * created using valid parameters<br>
572     * Expected: must return <code>seed</code> which is equal
573     * to the one passed to the constructor
574     */
575    @TestTargetNew(
576        level = TestLevel.PARTIAL_COMPLETE,
577        notes = "Verifies positive case.",
578        method = "getSeed",
579        args = {}
580    )
581    public final void testGetSeed01() {
582        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
583        BigInteger a = BigInteger.ONE;
584        BigInteger b = BigInteger.valueOf(19L);
585        byte[] seed = new byte[24];
586        EllipticCurve c = new EllipticCurve(f, a, b, seed);
587        byte[] seedRet = c.getSeed();
588        assertNotNull(seedRet);
589        assertTrue(Arrays.equals(seed, seedRet));
590    }
591
592    /**
593     * Test #2 for <code>getSeed()</code> method<br>
594     * Assertion: returned array is copied to prevent subsequent modification<br>
595     * Test preconditions: <code>ECFieldF2m</code> instance
596     * created using valid parameters; <code>getSeed()</code>
597     * called and then returned array modified<br>
598     * Expected: internal state must not be affected by the modification
599     */
600    @TestTargetNew(
601        level = TestLevel.PARTIAL_COMPLETE,
602        notes = "Verifies that modification of byte array  doesn't change internal state of test object.",
603        method = "getSeed",
604        args = {}
605    )
606    public final void testGetSeed02() {
607        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
608        BigInteger a = BigInteger.ONE;
609        BigInteger b = BigInteger.valueOf(19L);
610        byte[] seed = new byte[24];
611        EllipticCurve c = new EllipticCurve(f, a, b, seed.clone());
612        byte[] seedRet = c.getSeed();
613        // modify returned array
614        seedRet[0] = (byte) 1;
615        // check that above modification did not changed
616        // internal state of test object
617        assertTrue(Arrays.equals(seed, c.getSeed()));
618    }
619
620    /**
621     * Test #3 for <code>getSeed()</code> method<br>
622     * Assertion: returned array is copied to prevent subsequent modification<br>
623     * Test preconditions: <code>ECFieldF2m</code> instance
624     * created using valid parameters<br>
625     * Expected: repeated method calls must return different refs
626     */
627    @TestTargetNew(
628        level = TestLevel.PARTIAL_COMPLETE,
629        notes = "Verifies that repeated calls of getSeed method must return different refs.",
630        method = "getSeed",
631        args = {}
632    )
633    public final void testGetSeed03() {
634        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
635        BigInteger a = BigInteger.ONE;
636        BigInteger b = BigInteger.valueOf(19L);
637        byte[] seed = new byte[24];
638        EllipticCurve c = new EllipticCurve(f, a, b, seed);
639        c.getSeed();
640        assertNotSame(c.getSeed(), c.getSeed());
641    }
642
643    /**
644     * @tests java.security.spec.EllipticCurve#getSeed()
645     * Assertion: null if not specified
646     */
647    @TestTargetNew(
648        level = TestLevel.PARTIAL_COMPLETE,
649        notes = "Regression test.",
650        method = "getSeed",
651        args = {}
652    )
653    public final void testGetSeed04() {
654        //Regression for HARMONY-732
655        ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
656        BigInteger a = BigInteger.ONE;
657        assertNull(new EllipticCurve(f, a, a).getSeed());
658    }
659
660    /**
661     * Test #1 for <code>equals(Object other)</code> method<br>
662     * Assertion: return true if this and other objects are equal<br>
663     * Test preconditions: see test comments<br>
664     * Expected: all objects in this test must be equal
665     */
666    @TestTargetNew(
667        level = TestLevel.COMPLETE,
668        notes = "",
669        method = "equals",
670        args = {java.lang.Object.class}
671    )
672    public final void testEqualsObject01() {
673        // test case 1: must be equal to itself
674        EllipticCurve c2 = null, c1 = new EllipticCurve(new ECFieldFp(
675                BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger
676                .valueOf(19L));
677        assertTrue(c1.equals(c1));
678
679        // test case 2: equal objects
680        c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
681                BigInteger.ONE, BigInteger.valueOf(19L));
682        c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
683                BigInteger.valueOf(1L), BigInteger.valueOf(19L));
684        assertTrue(c1.equals(c2) && c2.equals(c1));
685
686        // test case 3: equal objects with seed not null
687        c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
688                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
689        c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
690                BigInteger.valueOf(1L), BigInteger.valueOf(19L), new byte[24]);
691        assertTrue(c1.equals(c2) && c2.equals(c1));
692
693        // test case 4: equal object and subclass object
694        c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
695                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
696        MyEllipticCurve c3 = new MyEllipticCurve(new ECFieldFp(BigInteger
697                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
698                new byte[24]);
699        assertTrue(c1.equals(c3) && c3.equals(c1));
700
701        // test case 5: equal objects
702        c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
703                BigInteger.ONE, BigInteger.valueOf(19L));
704        c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
705                BigInteger.valueOf(1L), BigInteger.valueOf(19L), null);
706        assertTrue(c1.equals(c2) && c2.equals(c1));
707    }
708
709    /**
710     * Test #1 for <code>hashCode()</code> method.<br>
711     *
712     * Assertion: must return the same value if invoked
713     * repeatedly on the same object.
714     */
715    @TestTargetNew(
716        level = TestLevel.PARTIAL_COMPLETE,
717        notes = "Verifies that several calls of hashCode method for the same objects return the same values.",
718        method = "hashCode",
719        args = {}
720    )
721    public final void testHashCode01() {
722        int hc = 0;
723        EllipticCurve f = new EllipticCurve(new ECFieldFp(BigInteger
724                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
725                new byte[24]);
726        hc = f.hashCode();
727        assertTrue(hc == f.hashCode() && hc == f.hashCode()
728                && hc == f.hashCode() && hc == f.hashCode()
729                && hc == f.hashCode() && hc == f.hashCode()
730                && hc == f.hashCode() && hc == f.hashCode());
731    }
732
733    /**
734     * Test #2 for <code>hashCode()</code> method.<br>
735     *
736     * Assertion: must return the same value if invoked
737     * on equal (according to the <code>equals(Object)</code> method) objects.
738     */
739    @TestTargetNew(
740        level = TestLevel.PARTIAL_COMPLETE,
741        notes = "",
742        method = "hashCode",
743        args = {}
744    )
745    public final void testHashCode02() {
746        assertEquals(new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
747                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24])
748                .hashCode(), new EllipticCurve(new ECFieldFp(BigInteger
749                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
750                new byte[24]).hashCode());
751    }
752
753    //
754    // Private stuff
755    //
756
757    class testECField implements ECField {
758
759        public int getFieldSize() {
760            return 2;
761        }
762    }
763
764    /**
765     * EllipticCurve subclass for testing purposes
766     *
767     */
768    private static class MyEllipticCurve extends EllipticCurve {
769
770        MyEllipticCurve(ECField f, BigInteger a, BigInteger b, byte[] seed) {
771            super(f, a, b, seed);
772        }
773    }
774}
775