From 1c219f619291ba818bc2542390a2988539d94ed0 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Thu, 18 Apr 2013 17:57:03 -0700 Subject: Rename API AndroidKey* -> Key* Bug: 8657552 Change-Id: Id9102b7c2c2f6d27fba7645f0629750cfe1eb510 --- .../security/AndroidKeyPairGeneratorSpecTest.java | 142 --------------------- .../security/AndroidKeyPairGeneratorTest.java | 18 +-- .../src/android/security/AndroidKeyStoreTest.java | 14 +- .../android/security/KeyPairGeneratorSpecTest.java | 142 +++++++++++++++++++++ 4 files changed, 159 insertions(+), 157 deletions(-) delete mode 100644 keystore/tests/src/android/security/AndroidKeyPairGeneratorSpecTest.java create mode 100644 keystore/tests/src/android/security/KeyPairGeneratorSpecTest.java (limited to 'keystore/tests') diff --git a/keystore/tests/src/android/security/AndroidKeyPairGeneratorSpecTest.java b/keystore/tests/src/android/security/AndroidKeyPairGeneratorSpecTest.java deleted file mode 100644 index 5d4ab9c..0000000 --- a/keystore/tests/src/android/security/AndroidKeyPairGeneratorSpecTest.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.security; - -import android.test.AndroidTestCase; - -import java.math.BigInteger; -import java.util.Date; - -import javax.security.auth.x500.X500Principal; - -public class AndroidKeyPairGeneratorSpecTest extends AndroidTestCase { - private static final String TEST_ALIAS_1 = "test1"; - - private static final X500Principal TEST_DN_1 = new X500Principal("CN=test1"); - - private static final long NOW_MILLIS = System.currentTimeMillis(); - - private static final BigInteger SERIAL_1 = BigInteger.ONE; - - /* We have to round this off because X509v3 doesn't store milliseconds. */ - private static final Date NOW = new Date(NOW_MILLIS - (NOW_MILLIS % 1000L)); - - @SuppressWarnings("deprecation") - private static final Date NOW_PLUS_10_YEARS = new Date(NOW.getYear() + 10, 0, 1); - - public void testConstructor_Success() throws Exception { - AndroidKeyPairGeneratorSpec spec = - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, - NOW, NOW_PLUS_10_YEARS, 0); - - assertEquals("Context should be the one specified", getContext(), spec.getContext()); - - assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias()); - - assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN()); - - assertEquals("startDate should be the one specified", NOW, spec.getStartDate()); - - assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate()); - } - - public void testBuilder_Success() throws Exception { - AndroidKeyPairGeneratorSpec spec = new AndroidKeyPairGeneratorSpec.Builder(getContext()) - .setAlias(TEST_ALIAS_1) - .setSubject(TEST_DN_1) - .setSerialNumber(SERIAL_1) - .setStartDate(NOW) - .setEndDate(NOW_PLUS_10_YEARS) - .setEncryptionRequired() - .build(); - - assertEquals("Context should be the one specified", getContext(), spec.getContext()); - - assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias()); - - assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN()); - - assertEquals("startDate should be the one specified", NOW, spec.getStartDate()); - - assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate()); - - assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags()); - } - - public void testConstructor_NullContext_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(null, TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW, - NOW_PLUS_10_YEARS, 0); - fail("Should throw IllegalArgumentException when context is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_NullKeystoreAlias_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), null, TEST_DN_1, SERIAL_1, NOW, - NOW_PLUS_10_YEARS, 0); - fail("Should throw IllegalArgumentException when keystoreAlias is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_NullSubjectDN_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, null, SERIAL_1, NOW, - NOW_PLUS_10_YEARS, 0); - fail("Should throw IllegalArgumentException when subjectDN is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_NullSerial_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, null, NOW, - NOW_PLUS_10_YEARS, 0); - fail("Should throw IllegalArgumentException when startDate is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_NullStartDate_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, null, - NOW_PLUS_10_YEARS, 0); - fail("Should throw IllegalArgumentException when startDate is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_NullEndDate_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW, - null, 0); - fail("Should throw IllegalArgumentException when keystoreAlias is null"); - } catch (IllegalArgumentException success) { - } - } - - public void testConstructor_EndBeforeStart_Failure() throws Exception { - try { - new AndroidKeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, - NOW_PLUS_10_YEARS, NOW, 0); - fail("Should throw IllegalArgumentException when end is before start"); - } catch (IllegalArgumentException success) { - } - } -} diff --git a/keystore/tests/src/android/security/AndroidKeyPairGeneratorTest.java b/keystore/tests/src/android/security/AndroidKeyPairGeneratorTest.java index c5cf514..1582f74 100644 --- a/keystore/tests/src/android/security/AndroidKeyPairGeneratorTest.java +++ b/keystore/tests/src/android/security/AndroidKeyPairGeneratorTest.java @@ -65,7 +65,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { assertFalse(mAndroidKeyStore.isUnlocked()); - mGenerator = java.security.KeyPairGenerator.getInstance("AndroidKeyStore"); + mGenerator = java.security.KeyPairGenerator.getInstance("RSA", "AndroidKeyStore"); } private void setupPassword() { @@ -80,7 +80,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { public void testKeyPairGenerator_Initialize_Params_Encrypted_Success() throws Exception { setupPassword(); - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -116,7 +116,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { setupPassword(); mGenerator.initialize( - new AndroidKeyPairGeneratorSpec.Builder(getContext()) + new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -130,7 +130,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { public void testKeyPairGenerator_GenerateKeyPair_Encrypted_Success() throws Exception { setupPassword(); - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -146,7 +146,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { } public void testKeyPairGenerator_GenerateKeyPair_Unencrypted_Success() throws Exception { - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -163,7 +163,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { public void testKeyPairGenerator_GenerateKeyPair_Replaced_Success() throws Exception { // Generate the first key { - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -178,7 +178,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { // Replace the original key { - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_2) .setSubject(TEST_DN_2) .setSerialNumber(TEST_SERIAL_2) @@ -196,7 +196,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { throws Exception { // Generate the first key { - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_1) .setSerialNumber(TEST_SERIAL_1) @@ -211,7 +211,7 @@ public class AndroidKeyPairGeneratorTest extends AndroidTestCase { // Attempt to replace previous key { - mGenerator.initialize(new AndroidKeyPairGeneratorSpec.Builder(getContext()) + mGenerator.initialize(new KeyPairGeneratorSpec.Builder(getContext()) .setAlias(TEST_ALIAS_1) .setSubject(TEST_DN_2) .setSerialNumber(TEST_SERIAL_2) diff --git a/keystore/tests/src/android/security/AndroidKeyStoreTest.java b/keystore/tests/src/android/security/AndroidKeyStoreTest.java index 507d41c..8798fb5 100644 --- a/keystore/tests/src/android/security/AndroidKeyStoreTest.java +++ b/keystore/tests/src/android/security/AndroidKeyStoreTest.java @@ -1232,8 +1232,8 @@ public class AndroidKeyStoreTest extends AndroidTestCase { try { mKeyStore.setEntry(TEST_ALIAS_1, entry, - new AndroidKeyStoreParameter.Builder(getContext()) - .setEncryptionRequired() + new KeyStoreParameter.Builder(getContext()) + .setEncryptionRequired(true) .build()); fail("Shouldn't be able to insert encrypted entry when KeyStore uninitialized"); } catch (KeyStoreException expected) { @@ -1752,8 +1752,10 @@ public class AndroidKeyStoreTest extends AndroidTestCase { Entry entry = mKeyStore.getEntry(TEST_ALIAS_1, null); try { - mKeyStore.setEntry(TEST_ALIAS_1, entry, new AndroidKeyStoreParameter.Builder( - getContext()).setEncryptionRequired().build()); + mKeyStore.setEntry(TEST_ALIAS_1, entry, + new KeyStoreParameter.Builder(getContext()) + .setEncryptionRequired(true) + .build()); fail("Should not allow setting of Entry without unlocked keystore"); } catch (KeyStoreException success) { } @@ -1762,8 +1764,8 @@ public class AndroidKeyStoreTest extends AndroidTestCase { assertTrue(mAndroidKeyStore.isUnlocked()); mKeyStore.setEntry(TEST_ALIAS_1, entry, - new AndroidKeyStoreParameter.Builder(getContext()) - .setEncryptionRequired() + new KeyStoreParameter.Builder(getContext()) + .setEncryptionRequired(true) .build()); } } diff --git a/keystore/tests/src/android/security/KeyPairGeneratorSpecTest.java b/keystore/tests/src/android/security/KeyPairGeneratorSpecTest.java new file mode 100644 index 0000000..113d730 --- /dev/null +++ b/keystore/tests/src/android/security/KeyPairGeneratorSpecTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.security; + +import android.test.AndroidTestCase; + +import java.math.BigInteger; +import java.util.Date; + +import javax.security.auth.x500.X500Principal; + +public class KeyPairGeneratorSpecTest extends AndroidTestCase { + private static final String TEST_ALIAS_1 = "test1"; + + private static final X500Principal TEST_DN_1 = new X500Principal("CN=test1"); + + private static final long NOW_MILLIS = System.currentTimeMillis(); + + private static final BigInteger SERIAL_1 = BigInteger.ONE; + + /* We have to round this off because X509v3 doesn't store milliseconds. */ + private static final Date NOW = new Date(NOW_MILLIS - (NOW_MILLIS % 1000L)); + + @SuppressWarnings("deprecation") + private static final Date NOW_PLUS_10_YEARS = new Date(NOW.getYear() + 10, 0, 1); + + public void testConstructor_Success() throws Exception { + KeyPairGeneratorSpec spec = + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, + NOW, NOW_PLUS_10_YEARS, 0); + + assertEquals("Context should be the one specified", getContext(), spec.getContext()); + + assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias()); + + assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN()); + + assertEquals("startDate should be the one specified", NOW, spec.getStartDate()); + + assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate()); + } + + public void testBuilder_Success() throws Exception { + KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext()) + .setAlias(TEST_ALIAS_1) + .setSubject(TEST_DN_1) + .setSerialNumber(SERIAL_1) + .setStartDate(NOW) + .setEndDate(NOW_PLUS_10_YEARS) + .setEncryptionRequired() + .build(); + + assertEquals("Context should be the one specified", getContext(), spec.getContext()); + + assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias()); + + assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN()); + + assertEquals("startDate should be the one specified", NOW, spec.getStartDate()); + + assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate()); + + assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags()); + } + + public void testConstructor_NullContext_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(null, TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW, + NOW_PLUS_10_YEARS, 0); + fail("Should throw IllegalArgumentException when context is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_NullKeystoreAlias_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), null, TEST_DN_1, SERIAL_1, NOW, + NOW_PLUS_10_YEARS, 0); + fail("Should throw IllegalArgumentException when keystoreAlias is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_NullSubjectDN_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, null, SERIAL_1, NOW, + NOW_PLUS_10_YEARS, 0); + fail("Should throw IllegalArgumentException when subjectDN is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_NullSerial_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, null, NOW, + NOW_PLUS_10_YEARS, 0); + fail("Should throw IllegalArgumentException when startDate is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_NullStartDate_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, null, + NOW_PLUS_10_YEARS, 0); + fail("Should throw IllegalArgumentException when startDate is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_NullEndDate_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, NOW, + null, 0); + fail("Should throw IllegalArgumentException when keystoreAlias is null"); + } catch (IllegalArgumentException success) { + } + } + + public void testConstructor_EndBeforeStart_Failure() throws Exception { + try { + new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, TEST_DN_1, SERIAL_1, + NOW_PLUS_10_YEARS, NOW, 0); + fail("Should throw IllegalArgumentException when end is before start"); + } catch (IllegalArgumentException success) { + } + } +} -- cgit v1.1