Using the Android KeyStore in Robolectric tests

家住魔仙堡 提交于 2020-01-03 08:48:10

问题


I'm attempting to write a few testcases that work against the Android Keystore. However, when I write the following test case:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class FancyPantsUnitTest {
   @Test
   public void buildKey() {
        keyPairGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
        keyPairGenerator.initialize(4096);
        final KeyPair keyPair = keyPairGenerator.generateKeyPair();
   }
}

This fails with the following exception:

org.junit.ComparisonFailure: expected:<null> but was:<java.security.KeyStoreException: AndroidKeyStore not found>

I'm targeting API Level 23 if that helps.


回答1:


There is already a discussion on this at https://github.com/robolectric/robolectric/issues/1518 .

In short:

From java.security.Security javadoc:

The default values of security properties are read from an implementation-specific location, which is typically the properties file lib/security/java.security in the Java installation directory.

… which we probably don't want to encourage people to monkey with.

Looks like this will need to be a method intercept rule...

The same happens when trying PowerMockito.



来源:https://stackoverflow.com/questions/38213748/using-the-android-keystore-in-robolectric-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!