Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright 2013 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 android.keystore.cts;
     18 
     19 import android.content.pm.PackageManager;
     20 import android.os.Handler;
     21 import android.security.KeyChain;
     22 import android.security.KeyChainException;
     23 import android.test.AndroidTestCase;
     24 
     25 import java.util.concurrent.CountDownLatch;
     26 
     27 public class KeyChainTest extends AndroidTestCase {
     28     public void testIsKeyAlgorithmSupported_RequiredAlgorithmsSupported() throws Exception {
     29         assertFalse("DSA must not be supported", KeyChain.isKeyAlgorithmSupported("DSA"));
     30         assertTrue("EC must be supported", KeyChain.isKeyAlgorithmSupported("EC"));
     31         assertTrue("RSA must be supported", KeyChain.isKeyAlgorithmSupported("RSA"));
     32     }
     33 
     34     public void testNullPrivateKeyArgumentsFail()
     35             throws KeyChainException, InterruptedException {
     36         try {
     37             KeyChain.getPrivateKey(null, null);
     38             fail("NullPointerException was expected for null arguments to "
     39                     + "KeyChain.getPrivateKey(Context, String)");
     40         } catch (NullPointerException expected) {
     41         }
     42     }
     43 
     44     public void testNullPrivateKeyAliasArgumentFails()
     45             throws KeyChainException, InterruptedException {
     46         try {
     47             KeyChain.getPrivateKey(getContext(), null);
     48             fail("NullPointerException was expected with null String argument to "
     49                         + "KeyChain.getPrivateKey(Context, String).");
     50         } catch (NullPointerException expected) {
     51         }
     52     }
     53 
     54     public void testNullPrivateKeyContextArgumentFails()
     55             throws KeyChainException, InterruptedException {
     56         try {
     57             KeyChain.getPrivateKey(null, "");
     58             fail("NullPointerException was expected with null Context argument to "
     59                     + "KeyChain.getPrivateKey(Context, String).");
     60         } catch (NullPointerException expected) {
     61         }
     62     }
     63 
     64     /**
     65      * Tests whether the required algorithms are backed by a Keymaster HAL that
     66      * binds the key material to the specific device it was created or imported
     67      * to. For more information on the Keymaster HAL, look at the header file at
     68      * hardware/libhardware/include/hardware/keymaster.h and the associated
     69      * tests in hardware/libhardware/tests/keymaster/
     70      */
     71     public void testIsBoundKeyAlgorithm_RequiredAlgorithmsSupported() throws Exception {
     72         if (isLeanbackOnly()) {
     73             KeyChain.isBoundKeyAlgorithm("RSA");
     74         }
     75         else {
     76             assertTrue("RSA must be hardware-backed by a hardware-specific Keymaster HAL",
     77                        KeyChain.isBoundKeyAlgorithm("RSA"));
     78         }
     79 
     80         // These are not required, but must not throw an exception
     81         KeyChain.isBoundKeyAlgorithm("DSA");
     82         KeyChain.isBoundKeyAlgorithm("EC");
     83     }
     84 
     85     private boolean isLeanbackOnly() {
     86         PackageManager pm = getContext().getPackageManager();
     87         return (pm != null && pm.hasSystemFeature("android.software.leanback_only"));
     88     }
     89 }
     90