Home | History | Annotate | Download | only in spec
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.security.spec;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.math.BigInteger;
     22 import java.security.spec.ECFieldF2m;
     23 import java.security.spec.ECParameterSpec;
     24 import java.security.spec.ECPoint;
     25 import java.security.spec.EllipticCurve;
     26 
     27 public class ECParameterSpecTest extends TestCase {
     28 
     29     EllipticCurve curve;
     30 
     31     ECPoint ecpoint;
     32 
     33     ECParameterSpec ecps;
     34 
     35     protected void setUp() throws Exception {
     36         super.setUp();
     37         curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1),
     38                 BigInteger.valueOf(1));
     39         ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
     40         ecps = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
     41     }
     42 
     43     protected void tearDown() throws Exception {
     44         curve = null;
     45         ecpoint = null;
     46         ecps = null;
     47         super.tearDown();
     48     }
     49 
     50     /**
     51      * test for ECParameterSpec(EllipticCurve, ECPoint, BigInteger, int) constructor
     52      * test covers following usecases:
     53      * case 1: creating object with valid parameters
     54      * case 2: NullPointerException - if curve is null
     55      * case 3: NullPointerException - if g is null
     56      * case 4: NullPointerException - if n is null
     57      * case 5: IllegalArgumentException - if n is not positive
     58      * case 6: IllegalArgumentException - if h is not positive
     59      */
     60     public void test_constructorLjava_security_spec_EllipticCurveLjava_security_spec_ECPointLjava_math_BigIntegerI() {
     61 
     62         // case 1: creating object with valid parameters
     63         assertEquals("wrong cofactor was returned", 1, ecps.getCofactor());
     64         assertEquals("wrong elliptic curve", curve, ecps.getCurve());
     65         assertEquals("wrong generator was returned", ecpoint, ecps
     66                 .getGenerator());
     67         assertEquals("wrong order was reteurned", BigInteger.valueOf(1), ecps
     68                 .getOrder());
     69 
     70         // case 2: NullPointerException - if curve is null.
     71         try {
     72             new ECParameterSpec(null, ecpoint, BigInteger.valueOf(1), 1);
     73             fail("NullPointerException exception has not been thrown");
     74         } catch (NullPointerException e) {
     75             // expected
     76         }
     77 
     78         // case 3: NullPointerException - if g is null.
     79         try {
     80             new ECParameterSpec(curve, null, BigInteger.valueOf(1), 1);
     81             fail("NullPointerException exception has not been thrown");
     82         } catch (NullPointerException e) {
     83             // expected
     84         }
     85 
     86         // case 4: NullPointerException - if n is null.
     87         try {
     88             new ECParameterSpec(curve, ecpoint, null, 1);
     89             fail("NullPointerException exception has not been thrown");
     90         } catch (NullPointerException e) {
     91             // expected
     92         }
     93 
     94         // case 5: IllegalArgumentException - if n is not positive.
     95         try {
     96             new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(-1), 1);
     97             fail("IllegalArgumentException exception has not been thrown");
     98         } catch (IllegalArgumentException e) {
     99             // expected
    100         }
    101 
    102         // case 6: IllegalArgumentException - if h is not positive.
    103         try {
    104             new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), -1);
    105             fail("IllegalArgumentException exception has not been thrown");
    106         } catch (IllegalArgumentException e) {
    107             // expected
    108         }
    109     }
    110 
    111     /**
    112      * test for getCurve() method
    113      */
    114     public void test_GetCurve() {
    115         assertEquals("wrong elliptic curve", curve, ecps.getCurve());
    116     }
    117 
    118     /**
    119      * test for getGenerator() method
    120      */
    121     public void test_GetGenerator() {
    122         assertEquals("wrong generator was returned", ecpoint, ecps
    123                 .getGenerator());
    124     }
    125 
    126     /**
    127      * test for getOrder() method
    128      */
    129     public void test_GetOrder() {
    130         assertEquals("wrong order was reteurned", BigInteger.valueOf(1), ecps
    131                 .getOrder());
    132     }
    133 
    134     /**
    135      * test for getCofactor() method
    136      */
    137     public void test_GetCofactor() {
    138         assertEquals("wrong cofactor was returned", 1, ecps.getCofactor());
    139     }
    140 }
    141