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
17package android.security;
18
19import android.test.AndroidTestCase;
20
21import java.math.BigInteger;
22import java.util.Date;
23
24import javax.security.auth.x500.X500Principal;
25
26public 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, "RSA", 1024, null, TEST_DN_1,
44                        SERIAL_1, 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("Key algorithm should be the one specified", "RSA", spec.getKeyType());
51
52        assertEquals("Key size should be the one specified", 1024, spec.getKeySize());
53
54        assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());
55
56        assertEquals("startDate should be the one specified", NOW, spec.getStartDate());
57
58        assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());
59    }
60
61    public void testBuilder_Success() throws Exception {
62        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext())
63                .setAlias(TEST_ALIAS_1)
64                .setKeyType("RSA")
65                .setKeySize(1024)
66                .setSubject(TEST_DN_1)
67                .setSerialNumber(SERIAL_1)
68                .setStartDate(NOW)
69                .setEndDate(NOW_PLUS_10_YEARS)
70                .setEncryptionRequired()
71                .build();
72
73        assertEquals("Context should be the one specified", getContext(), spec.getContext());
74
75        assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias());
76
77        assertEquals("Key algorithm should be the one specified", "RSA", spec.getKeyType());
78
79        assertEquals("Key size should be the one specified", 1024, spec.getKeySize());
80
81        assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());
82
83        assertEquals("startDate should be the one specified", NOW, spec.getStartDate());
84
85        assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());
86
87        assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags());
88    }
89
90    public void testConstructor_NullContext_Failure() throws Exception {
91        try {
92            new KeyPairGeneratorSpec(null, TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
93                    NOW_PLUS_10_YEARS, 0);
94            fail("Should throw IllegalArgumentException when context is null");
95        } catch (IllegalArgumentException success) {
96        }
97    }
98
99    public void testConstructor_NullKeystoreAlias_Failure() throws Exception {
100        try {
101            new KeyPairGeneratorSpec(getContext(), null, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
102                    NOW_PLUS_10_YEARS, 0);
103            fail("Should throw IllegalArgumentException when keystoreAlias is null");
104        } catch (IllegalArgumentException success) {
105        }
106    }
107
108    public void testConstructor_NullSubjectDN_Failure() throws Exception {
109        try {
110            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, null, SERIAL_1, NOW,
111                    NOW_PLUS_10_YEARS, 0);
112            fail("Should throw IllegalArgumentException when subjectDN is null");
113        } catch (IllegalArgumentException success) {
114        }
115    }
116
117    public void testConstructor_NullSerial_Failure() throws Exception {
118        try {
119            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, null, NOW,
120                    NOW_PLUS_10_YEARS, 0);
121            fail("Should throw IllegalArgumentException when startDate is null");
122        } catch (IllegalArgumentException success) {
123        }
124    }
125
126    public void testConstructor_NullStartDate_Failure() throws Exception {
127        try {
128            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
129                    null, NOW_PLUS_10_YEARS, 0);
130            fail("Should throw IllegalArgumentException when startDate is null");
131        } catch (IllegalArgumentException success) {
132        }
133    }
134
135    public void testConstructor_NullEndDate_Failure() throws Exception {
136        try {
137            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
138                    NOW, null, 0);
139            fail("Should throw IllegalArgumentException when keystoreAlias is null");
140        } catch (IllegalArgumentException success) {
141        }
142    }
143
144    public void testConstructor_EndBeforeStart_Failure() throws Exception {
145        try {
146            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
147                    NOW_PLUS_10_YEARS, NOW, 0);
148            fail("Should throw IllegalArgumentException when end is before start");
149        } catch (IllegalArgumentException success) {
150        }
151    }
152}
153