Home | History | Annotate | Download | only in interfaces
      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.interfaces;
     18 import dalvik.annotation.TestLevel;
     19 import dalvik.annotation.TestTargetNew;
     20 import dalvik.annotation.TestTargetClass;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import java.security.KeyPairGenerator;
     25 import java.security.interfaces.DSAKey;
     26 import java.security.interfaces.DSAParams;
     27 import java.security.spec.DSAParameterSpec;
     28 
     29 @TestTargetClass(DSAKey.class)
     30 public class DSAKeyTest extends TestCase {
     31 
     32     /**
     33      * @tests java.security.interfaces.DSAKey
     34      * #getParams()
     35      * test covers following use cases
     36      *   Case 1: check private key
     37      *   Case 2: check public key
     38      */
     39     @TestTargetNew(
     40         level = TestLevel.COMPLETE,
     41         notes = "",
     42         method = "getParams",
     43         args = {}
     44     )
     45     public void test_getParams() throws Exception {
     46         DSAParams param = new DSAParameterSpec(Util.P, Util.Q, Util.G);
     47 
     48         KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
     49         gen.initialize((DSAParameterSpec) param);
     50         DSAKey key = null;
     51 
     52         // Case 1: check private key
     53         key = (DSAKey) gen.generateKeyPair().getPrivate();
     54         assertDSAParamsEquals(param, key.getParams());
     55 
     56         // Case 2: check public key
     57         key = (DSAKey) gen.generateKeyPair().getPublic();
     58         assertDSAParamsEquals(param, key.getParams());
     59     }
     60 
     61     private void assertDSAParamsEquals(DSAParams expected, DSAParams actual) {
     62         assertEquals("P differ", expected.getP(), actual.getP());
     63         assertEquals("Q differ", expected.getQ(), actual.getQ());
     64         assertEquals("G differ", expected.getG(), actual.getG());
     65     }
     66 }
     67