在 Robolectric 测试中使用 Android KeyStore [英] Using the Android KeyStore in Robolectric tests
问题描述
我正在尝试编写一些针对 Android Keystore.但是,当我编写以下测试用例时:
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>
如果有帮助,我的目标是 API 级别 23.
I'm targeting API Level 23 if that helps.
推荐答案
https://github.com/robolectric/robolectric/issues/1518 .
简而言之:
来自 java.security.Security javadoc:
From java.security.Security javadoc:
安全属性的默认值是从一个特定于实现的位置,通常是属性Java 安装目录中的文件 lib/security/java.security.
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...
尝试 PowerMockito 时也会发生同样的情况.
The same happens when trying PowerMockito.
这篇关于在 Robolectric 测试中使用 Android KeyStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!