ECPrivateKeySpec_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.ECPrivateKeySpec;
29import java.security.spec.EllipticCurve;
30
31import junit.framework.TestCase;
32
33/**
34 * Tests for <code>ECPrivateKeySpec</code> class fields and methods.
35 *
36 */
37public class ECPrivateKeySpec_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>ECPrivateKeySpec(BigInteger, ECParameterSpec)</code> constructor<br>
50     * Assertion: creates <code>ECPrivateKeySpec</code> instance<br>
51     * Test preconditions: valid parameters passed<br>
52     * Expected: must pass without any exceptions
53     */
54    public final void testECPrivateKeySpec01() {
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 ECPrivateKeySpec(BigInteger.ZERO,
62                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
63
64    }
65
66   /**
67     * Test #2 for <code>ECPrivateKeySpec(BigInteger, ECParameterSpec)</code> constructor<br>
68     * Assertion: throws <code>NullPointerException</code> if
69     * <code>s</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 testECPrivateKeySpec02() {
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: s is null
82        try {
83            new ECPrivateKeySpec(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 ECPrivateKeySpec(BigInteger.valueOf(0L), null);
93            fail("#2: Expected NPE not thrown");
94        } catch (NullPointerException ok) {
95        }
96
97
98        // Test case 3: both s and params are null
99        try {
100            new ECPrivateKeySpec(null, null);
101            fail("#3: Expected NPE not thrown");
102        } catch (NullPointerException ok) {
103        }
104    }
105
106    /**
107     * Test for <code>getParams()</code> method<br>
108     * Assertion: returns associated EC parameters<br>
109     * Test preconditions: <code>ECPrivateKeySpec</code> instance
110     * created using valid parameters<br>
111     * Expected: must return params value which is equal
112     * to the one passed to the constructor; (both must refer
113     * the same object)
114     */
115    public final void testGetParams() {
116        // Valid (see note below) parameters set
117        EllipticCurve c =
118            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
119                              BigInteger.ZERO,
120                              BigInteger.valueOf(4L));
121        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
122        ECParameterSpec params =
123            new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);
124
125        ECPrivateKeySpec ks = new ECPrivateKeySpec(BigInteger.ZERO, params);
126        ECParameterSpec paramsRet = ks.getParams();
127
128        assertEquals(params, paramsRet);
129        assertSame(params, paramsRet);
130    }
131
132    /**
133     * Test for <code>getS()</code> method<br>
134     * Assertion: returns associated private value<br>
135     * Test preconditions: <code>ECPrivateKeySpec</code> instance
136     * created using valid parameters<br>
137     * Expected: must return s value which is equal
138     * to the one passed to the constructor; (both must refer
139     * the same object)
140     */
141    public final void testGetS() {
142        // Valid (see note below) parameters set
143        EllipticCurve c =
144            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
145                              BigInteger.ZERO,
146                              BigInteger.valueOf(4L));
147        ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
148        ECParameterSpec params =
149            new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);
150        BigInteger s = BigInteger.valueOf(5L);
151
152        ECPrivateKeySpec ks = new ECPrivateKeySpec(s, params);
153        BigInteger sRet = ks.getS();
154
155        assertEquals(s, sRet);
156        assertSame(s, sRet);
157    }
158}
159