Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2012 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.security;
     18 
     19 import android.test.AndroidTestCase;
     20 
     21 import java.math.BigInteger;
     22 import java.util.Date;
     23 
     24 import javax.security.auth.x500.X500Principal;
     25 
     26 public class KeyPairGeneratorSpecTest extends AndroidTestCase {
     27     private static final String TEST_ALIAS_1 = "test1";
     28 
     29     private static final X500Principal TEST_DN_1 = new X500Principal("CN=test1");
     30 
     31     private static final long NOW_MILLIS = System.currentTimeMillis();
     32 
     33     private static final BigInteger SERIAL_1 = BigInteger.ONE;
     34 
     35     /* We have to round this off because X509v3 doesn't store milliseconds. */
     36     private static final Date NOW = new Date(NOW_MILLIS - (NOW_MILLIS % 1000L));
     37 
     38     @SuppressWarnings("deprecation")
     39     private static final Date NOW_PLUS_10_YEARS = new Date(NOW.getYear() + 10, 0, 1);
     40 
     41     public void testConstructor_Success() throws Exception {
     42         KeyPairGeneratorSpec spec =
     43                 new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1,
     44                         NOW, NOW_PLUS_10_YEARS, 0);
     45 
     46         assertEquals("Context should be the one specified", getContext(), spec.getContext());
     47 
     48         assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias());
     49 
     50         assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());
     51 
     52         assertEquals("startDate should be the one specified", NOW, spec.getStartDate());
     53 
     54         assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());
     55     }
     56 
     57     public void testBuilder_Success() throws Exception {
     58         KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext())
     59                 .setAlias(TEST_ALIAS_1)
     60                 .setSubject(TEST_DN_1)
     61                 .setSerialNumber(SERIAL_1)
     62                 .setStartDate(NOW)
     63                 .setEndDate(NOW_PLUS_10_YEARS)
     64                 .setEncryptionRequired()
     65                 .build();
     66 
     67         assertEquals("Context should be the one specified", getContext(), spec.getContext());
     68 
     69         assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias());
     70 
     71         assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());
     72 
     73         assertEquals("startDate should be the one specified", NOW, spec.getStartDate());
     74 
     75         assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());
     76 
     77         assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags());
     78     }
     79 
     80     public void testConstructor_NullContext_Failure() throws Exception {
     81         try {
     82             new KeyPairGeneratorSpec(null, TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW,
     83                     NOW_PLUS_10_YEARS, 0);
     84             fail("Should throw IllegalArgumentException when context is null");
     85         } catch (IllegalArgumentException success) {
     86         }
     87     }
     88 
     89     public void testConstructor_NullKeystoreAlias_Failure() throws Exception {
     90         try {
     91             new KeyPairGeneratorSpec(getContext(), null, TEST_DN_1, SERIAL_1, NOW,
     92                     NOW_PLUS_10_YEARS, 0);
     93             fail("Should throw IllegalArgumentException when keystoreAlias is null");
     94         } catch (IllegalArgumentException success) {
     95         }
     96     }
     97 
     98     public void testConstructor_NullSubjectDN_Failure() throws Exception {
     99         try {
    100             new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, null, SERIAL_1, NOW,
    101                     NOW_PLUS_10_YEARS, 0);
    102             fail("Should throw IllegalArgumentException when subjectDN is null");
    103         } catch (IllegalArgumentException success) {
    104         }
    105     }
    106 
    107     public void testConstructor_NullSerial_Failure() throws Exception {
    108         try {
    109             new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, null, NOW,
    110                     NOW_PLUS_10_YEARS, 0);
    111             fail("Should throw IllegalArgumentException when startDate is null");
    112         } catch (IllegalArgumentException success) {
    113         }
    114     }
    115 
    116     public void testConstructor_NullStartDate_Failure() throws Exception {
    117         try {
    118             new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, null,
    119                     NOW_PLUS_10_YEARS, 0);
    120             fail("Should throw IllegalArgumentException when startDate is null");
    121         } catch (IllegalArgumentException success) {
    122         }
    123     }
    124 
    125     public void testConstructor_NullEndDate_Failure() throws Exception {
    126         try {
    127             new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW,
    128                     null, 0);
    129             fail("Should throw IllegalArgumentException when keystoreAlias is null");
    130         } catch (IllegalArgumentException success) {
    131         }
    132     }
    133 
    134     public void testConstructor_EndBeforeStart_Failure() throws Exception {
    135         try {
    136             new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1,
    137                     NOW_PLUS_10_YEARS, NOW, 0);
    138             fail("Should throw IllegalArgumentException when end is before start");
    139         } catch (IllegalArgumentException success) {
    140         }
    141     }
    142 }
    143