Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Android KeyStore in Robolectric tests

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.

like image 757
jackyalcine Avatar asked Jul 05 '16 22:07

jackyalcine


1 Answers

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.

like image 121
marius bardan Avatar answered Oct 19 '22 08:10

marius bardan