Merge changes If663677b,I387d90ea am: ea4a93a946

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1706425

Change-Id: I59eb38410465eed276c5744d5373b4ef84656f37
This commit is contained in:
Treehugger Robot
2021-05-13 14:36:47 +00:00
committed by Automerger Merge Worker
5 changed files with 76 additions and 10 deletions

View File

@ -41,6 +41,8 @@ import android.system.keystore2.KeyMetadata;
import android.system.keystore2.ResponseCode;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -974,7 +976,6 @@ public class AndroidKeyStoreSpi extends KeyStoreSpi {
}
private Set<String> getUniqueAliases() {
try {
final KeyDescriptor[] keys = mKeyStore.list(
getTargetDomain(),
@ -987,7 +988,7 @@ public class AndroidKeyStoreSpi extends KeyStoreSpi {
return aliases;
} catch (android.security.KeyStoreException e) {
Log.e(TAG, "Failed to list keystore entries.", e);
return null;
return new HashSet<>();
}
}
@ -1099,6 +1100,17 @@ public class AndroidKeyStoreSpi extends KeyStoreSpi {
return caAlias;
}
/**
* Used by Tests to initialize with a fake KeyStore2.
* @hide
* @param keystore
*/
@VisibleForTesting
public void initForTesting(KeyStore2 keystore) {
mKeyStore = keystore;
mNamespace = KeyProperties.NAMESPACE_APPLICATION;
}
@Override
public void engineStore(OutputStream stream, char[] password) throws IOException,
NoSuchAlgorithmException, CertificateException {

View File

@ -28,6 +28,7 @@ android_test {
static_libs: [
"androidx.test.rules",
"hamcrest-library",
"mockito-target-minus-junit4",
],
platform_apis: true,
libs: ["android.test.runner"],

View File

@ -43,7 +43,6 @@ public final class ParcelableKeyGenParameterSpecTest {
static final String ALIAS = "keystore-alias";
static final String ANOTHER_ALIAS = "another-keystore-alias";
static final int KEY_PURPOSES = KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY;
static final int UID = 1230;
static final int KEYSIZE = 2048;
static final X500Principal SUBJECT = new X500Principal("CN=subject");
static final BigInteger SERIAL = new BigInteger("1234567890");
@ -61,7 +60,7 @@ public final class ParcelableKeyGenParameterSpecTest {
public static KeyGenParameterSpec configureDefaultSpec() {
return new KeyGenParameterSpec.Builder(ALIAS, KEY_PURPOSES)
.setUid(UID)
.setNamespace(KeyProperties.NAMESPACE_WIFI)
.setKeySize(KEYSIZE)
.setCertificateSubject(SUBJECT)
.setCertificateSerialNumber(SERIAL)
@ -88,10 +87,11 @@ public final class ParcelableKeyGenParameterSpecTest {
.build();
}
public static void validateSpecValues(KeyGenParameterSpec spec, int uid, String alias) {
public static void validateSpecValues(KeyGenParameterSpec spec,
@KeyProperties.Namespace int namespace, String alias) {
assertThat(spec.getKeystoreAlias(), is(alias));
assertThat(spec.getPurposes(), is(KEY_PURPOSES));
assertThat(spec.getUid(), is(uid));
assertThat(spec.getNamespace(), is(namespace));
assertThat(spec.getKeySize(), is(KEYSIZE));
assertThat(spec.getCertificateSubject(), is(SUBJECT));
assertThat(spec.getCertificateSerialNumber(), is(SERIAL));
@ -134,7 +134,7 @@ public final class ParcelableKeyGenParameterSpecTest {
Parcel parcel = parcelForReading(spec);
ParcelableKeyGenParameterSpec fromParcel =
ParcelableKeyGenParameterSpec.CREATOR.createFromParcel(parcel);
validateSpecValues(fromParcel.getSpec(), UID, ALIAS);
validateSpecValues(fromParcel.getSpec(), KeyProperties.NAMESPACE_WIFI, ALIAS);
assertThat(parcel.dataAvail(), is(0));
}

View File

@ -21,8 +21,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import android.security.ParcelableKeyGenParameterSpecTest;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import androidx.test.runner.AndroidJUnit4;
@ -41,7 +39,7 @@ public final class KeyGenParameterSpecTest {
KeyGenParameterSpec copiedSpec =
new KeyGenParameterSpec.Builder(spec).build();
ParcelableKeyGenParameterSpecTest.validateSpecValues(
copiedSpec, spec.getUid(), spec.getKeystoreAlias());
copiedSpec, spec.getNamespace(), spec.getKeystoreAlias());
}
@Test

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2021 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.keystore2;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.security.KeyStore2;
import android.security.KeyStoreException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class AndroidKeyStoreSpiTest {
@Mock
private KeyStore2 mKeystore2;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testEngineAliasesReturnsEmptySetOnKeyStoreError() throws Exception {
when(mKeystore2.list(anyInt(), anyLong()))
.thenThrow(new KeyStoreException(6, "Some Error"));
AndroidKeyStoreSpi spi = new AndroidKeyStoreSpi();
spi.initForTesting(mKeystore2);
assertThat("Empty collection expected", !spi.engineAliases().hasMoreElements());
verify(mKeystore2).list(anyInt(), anyLong());
}
}