Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 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 | |
Kenny Root | 1c219f6 | 2013-04-18 17:57:03 -0700 | [diff] [blame] | 26 | public class KeyPairGeneratorSpecTest extends AndroidTestCase { |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 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 { |
Kenny Root | 1c219f6 | 2013-04-18 17:57:03 -0700 | [diff] [blame] | 42 | KeyPairGeneratorSpec spec = |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 43 | new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, |
| 44 | SERIAL_1, NOW, NOW_PLUS_10_YEARS, 0); |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 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 | |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 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 | |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 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 | |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 61 | public void testBuilder_Success() throws Exception { |
Kenny Root | 1c219f6 | 2013-04-18 17:57:03 -0700 | [diff] [blame] | 62 | KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext()) |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 63 | .setAlias(TEST_ALIAS_1) |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 64 | .setKeyType("RSA") |
| 65 | .setKeySize(1024) |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 66 | .setSubject(TEST_DN_1) |
| 67 | .setSerialNumber(SERIAL_1) |
| 68 | .setStartDate(NOW) |
| 69 | .setEndDate(NOW_PLUS_10_YEARS) |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 70 | .setEncryptionRequired() |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 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 | |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 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 | |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 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()); |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 86 | |
| 87 | assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags()); |
Kenny Root | acb0b5b | 2013-03-28 15:05:03 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 90 | public void testConstructor_NullContext_Failure() throws Exception { |
| 91 | try { |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 92 | new KeyPairGeneratorSpec(null, TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW, |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 93 | NOW_PLUS_10_YEARS, 0); |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 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 { |
Kenny Root | f64386f | 2013-08-16 14:03:29 -0700 | [diff] [blame] | 101 | new KeyPairGeneratorSpec(getContext(), null, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW, |
Kenny Root | 2eeda72 | 2013-04-10 11:30:58 -0700 | [diff] [blame] | 102 | NOW_PLUS_10_YEARS, 0); |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 103 | fail("Should throw IllegalArgumentException when keystoreAlias is null"); |
| 104 | } catch (IllegalArgumentException success) { |
| 105 | } |
| 106 | } |
| 107 | |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 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 | } |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 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 | } |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 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 | } |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 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 | } |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public void testConstructor_EndBeforeStart_Failure() throws Exception { |
| 145 | try { |
Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 146 | new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1, |
| 147 | NOW_PLUS_10_YEARS, NOW, 0); |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 148 | fail("Should throw IllegalArgumentException when end is before start"); |
| 149 | } catch (IllegalArgumentException success) { |
| 150 | } |
| 151 | } |
| 152 | } |