blob: bc8dd13e99f3eb63e9a0f8b75ef60f005d09fedd [file] [log] [blame]
Kenny Rootdb026712012-08-20 10:48:46 -07001/*
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
Kenny Root1c219f62013-04-18 17:57:03 -070026public class KeyPairGeneratorSpecTest extends AndroidTestCase {
Kenny Rootdb026712012-08-20 10:48:46 -070027 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 {
Kenny Root1c219f62013-04-18 17:57:03 -070042 KeyPairGeneratorSpec spec =
Kenny Rootf64386f2013-08-16 14:03:29 -070043 new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1,
44 SERIAL_1, NOW, NOW_PLUS_10_YEARS, 0);
Kenny Rootdb026712012-08-20 10:48:46 -070045
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
Kenny Rootf64386f2013-08-16 14:03:29 -070050 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
Kenny Rootdb026712012-08-20 10:48:46 -070054 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
Kenny Rootacb0b5b2013-03-28 15:05:03 -070061 public void testBuilder_Success() throws Exception {
Kenny Root1c219f62013-04-18 17:57:03 -070062 KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext())
Kenny Rootacb0b5b2013-03-28 15:05:03 -070063 .setAlias(TEST_ALIAS_1)
Kenny Rootf64386f2013-08-16 14:03:29 -070064 .setKeyType("RSA")
65 .setKeySize(1024)
Kenny Rootacb0b5b2013-03-28 15:05:03 -070066 .setSubject(TEST_DN_1)
67 .setSerialNumber(SERIAL_1)
68 .setStartDate(NOW)
69 .setEndDate(NOW_PLUS_10_YEARS)
Kenny Root2eeda722013-04-10 11:30:58 -070070 .setEncryptionRequired()
Kenny Rootacb0b5b2013-03-28 15:05:03 -070071 .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
Kenny Rootf64386f2013-08-16 14:03:29 -070077 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
Kenny Rootacb0b5b2013-03-28 15:05:03 -070081 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());
Kenny Root2eeda722013-04-10 11:30:58 -070086
87 assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags());
Kenny Rootacb0b5b2013-03-28 15:05:03 -070088 }
89
Kenny Rootdb026712012-08-20 10:48:46 -070090 public void testConstructor_NullContext_Failure() throws Exception {
91 try {
Kenny Rootf64386f2013-08-16 14:03:29 -070092 new KeyPairGeneratorSpec(null, TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
Kenny Root2eeda722013-04-10 11:30:58 -070093 NOW_PLUS_10_YEARS, 0);
Kenny Rootdb026712012-08-20 10:48:46 -070094 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 {
Kenny Rootf64386f2013-08-16 14:03:29 -0700101 new KeyPairGeneratorSpec(getContext(), null, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
Kenny Root2eeda722013-04-10 11:30:58 -0700102 NOW_PLUS_10_YEARS, 0);
Kenny Rootdb026712012-08-20 10:48:46 -0700103 fail("Should throw IllegalArgumentException when keystoreAlias is null");
104 } catch (IllegalArgumentException success) {
105 }
106 }
107
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700108 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 }
Kenny Rootdb026712012-08-20 10:48:46 -0700115 }
116
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700117 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 }
Kenny Rootdb026712012-08-20 10:48:46 -0700124 }
125
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700126 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 }
Kenny Rootdb026712012-08-20 10:48:46 -0700133 }
134
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700135 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 }
Kenny Rootdb026712012-08-20 10:48:46 -0700142 }
143
144 public void testConstructor_EndBeforeStart_Failure() throws Exception {
145 try {
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700146 new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
147 NOW_PLUS_10_YEARS, NOW, 0);
Kenny Rootdb026712012-08-20 10:48:46 -0700148 fail("Should throw IllegalArgumentException when end is before start");
149 } catch (IllegalArgumentException success) {
150 }
151 }
152}