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.ECPrivateKeySpec;
     26 import java.security.spec.EllipticCurve;
     27 
     28 public class ECPrivateKeySpecTest extends TestCase {
     29 
     30     BigInteger s;
     31 
     32     ECParameterSpec ecparams;
     33 
     34     ECPrivateKeySpec ecpks;
     35 
     36     protected void setUp() throws Exception {
     37         super.setUp();
     38 
     39         ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger
     40                 .valueOf(1));
     41         EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger
     42                 .valueOf(1), BigInteger.valueOf(1));
     43 
     44         s = BigInteger.valueOf(1);
     45         ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
     46         ecpks = new ECPrivateKeySpec(s, ecparams);
     47     }
     48 
     49     protected void tearDown() throws Exception {
     50         s = null;
     51         ecparams = null;
     52         ecpks = null;
     53         super.tearDown();
     54     }
     55 
     56     /**
     57      * test for constructor ECPrivateKeySpec(BigInteger, ECParameterSpec)
     58      * test covers following usecases:
     59      * case 1: creating object with valid parameters
     60      * case 2: catch NullPointerException - if s is null.
     61      * case 3: catch NullPointerException - if params is null.
     62      */
     63     public void test_constructorLjava_math_BigIntegerLjava_security_spec_ECParameterSpec() {
     64 
     65         // case 1: creating object with valid parameters
     66         assertEquals("wrong private value", s, ecpks.getS());
     67         assertEquals("wrong parameters", ecparams, ecpks.getParams());
     68 
     69         // case 2: catch NullPointerException - if s is null.
     70         try {
     71             new ECPrivateKeySpec(null, ecparams);
     72             fail("NullPointerException has not been thrown");
     73         } catch (NullPointerException e) {
     74             // expected
     75         }
     76 
     77         // case 3: catch NullPointerException - if params is null.
     78         try {
     79             new ECPrivateKeySpec(s, null);
     80             fail("NullPointerException has not been thrown");
     81         } catch (NullPointerException e) {
     82             // expected
     83         }
     84     }
     85 
     86     /**
     87      * test for getS() method
     88      */
     89     public void test_GetS() {
     90         assertEquals("wrong private value", s, ecpks.getS());
     91     }
     92 
     93     /**
     94      * test for getParams() method
     95      */
     96     public void test_GetParams() {
     97         assertEquals("wrong parameters", ecparams, ecpks.getParams());
     98     }
     99 }
    100