Home | History | Annotate | Download | only in keychain
      1 /*
      2  * Copyright (C) 2017 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 com.android.keychain;
     18 
     19 import static org.mockito.Mockito.mock;
     20 import static org.mockito.Mockito.when;
     21 
     22 import android.security.Credentials;
     23 import android.security.KeyStore;
     24 import com.android.keychain.internal.KeyInfoProvider;
     25 import java.util.concurrent.CancellationException;
     26 import java.util.concurrent.ExecutionException;
     27 import java.util.concurrent.TimeUnit;
     28 import java.util.concurrent.TimeoutException;
     29 import org.junit.Assert;
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.robolectric.RobolectricTestRunner;
     34 import org.robolectric.RuntimeEnvironment;
     35 import org.robolectric.annotation.Config;
     36 import org.robolectric.shadows.ShadowApplication;
     37 
     38 @RunWith(RobolectricTestRunner.class)
     39 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     40 public final class AliasLoaderTest {
     41     private KeyInfoProvider mDummyInfoProvider;
     42 
     43     @Before
     44     public void setUp() {
     45         mDummyInfoProvider =
     46                 new KeyInfoProvider() {
     47                     public boolean isUserSelectable(String alias) {
     48                         return true;
     49                     }
     50                 };
     51     }
     52 
     53     @Test
     54     public void testAliasLoader_loadsAllAliases()
     55             throws InterruptedException, ExecutionException, CancellationException,
     56                     TimeoutException {
     57         KeyStore keyStore = mock(KeyStore.class);
     58         when(keyStore.list(Credentials.USER_PRIVATE_KEY)).thenReturn(new String[] {"b", "c", "a"});
     59 
     60         KeyChainActivity.AliasLoader loader =
     61                 new KeyChainActivity.AliasLoader(
     62                         keyStore, RuntimeEnvironment.application, mDummyInfoProvider);
     63         loader.execute();
     64 
     65         ShadowApplication.runBackgroundTasks();
     66         KeyChainActivity.CertificateAdapter result = loader.get(5, TimeUnit.SECONDS);
     67         Assert.assertNotNull(result);
     68         Assert.assertEquals(3, result.getCount());
     69         Assert.assertEquals("a", result.getItem(0));
     70         Assert.assertEquals("b", result.getItem(1));
     71         Assert.assertEquals("c", result.getItem(2));
     72     }
     73 
     74     @Test
     75     public void testAliasLoader_copesWithNoAliases()
     76             throws InterruptedException, ExecutionException, CancellationException,
     77                     TimeoutException {
     78         KeyStore keyStore = mock(KeyStore.class);
     79         when(keyStore.list(Credentials.USER_PRIVATE_KEY)).thenReturn(null);
     80 
     81         KeyChainActivity.AliasLoader loader =
     82                 new KeyChainActivity.AliasLoader(
     83                         keyStore, RuntimeEnvironment.application, mDummyInfoProvider);
     84         loader.execute();
     85 
     86         ShadowApplication.runBackgroundTasks();
     87         KeyChainActivity.CertificateAdapter result = loader.get(5, TimeUnit.SECONDS);
     88         Assert.assertNotNull(result);
     89         Assert.assertEquals(0, result.getCount());
     90     }
     91 
     92     @Test
     93     public void testAliasLoader_filtersNonUserSelectableAliases()
     94             throws InterruptedException, ExecutionException, CancellationException,
     95                     TimeoutException {
     96         KeyStore keyStore = mock(KeyStore.class);
     97         when(keyStore.list(Credentials.USER_PRIVATE_KEY)).thenReturn(new String[] {"a", "b", "c"});
     98         KeyInfoProvider infoProvider = mock(KeyInfoProvider.class);
     99         when(infoProvider.isUserSelectable("a")).thenReturn(false);
    100         when(infoProvider.isUserSelectable("b")).thenReturn(true);
    101         when(infoProvider.isUserSelectable("c")).thenReturn(false);
    102 
    103         KeyChainActivity.AliasLoader loader =
    104                 new KeyChainActivity.AliasLoader(
    105                         keyStore, RuntimeEnvironment.application, infoProvider);
    106         loader.execute();
    107 
    108         ShadowApplication.runBackgroundTasks();
    109         KeyChainActivity.CertificateAdapter result = loader.get(5, TimeUnit.SECONDS);
    110         Assert.assertNotNull(result);
    111         Assert.assertEquals(1, result.getCount());
    112         Assert.assertEquals("b", result.getItem(0));
    113     }
    114 }
    115