Android strongbox backed keys cannot be used for RSA encryption - android

I've been playing with the new Android Strongbox backed keys on a Pixel 3a, and I've encountered an issue where keys that are backed by Android Strongbox cannot be used with RSA/ECB/OAEPPadding where SHA256 is used as the digest.
All the primitives in question should work with Strongbox, so I'm a bit stumped.
/**
* Generates an 2048 bit RSA keypair
*/
private fun generateRsaKeypair(alias: String, isStrongboxBacked: Boolean): KeyPair {
val keyGenParameterSpec = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_DECRYPT
or KeyProperties.PURPOSE_ENCRYPT)
.run {
setIsStrongBoxBacked(isStrongboxBacked)
setDigests(KeyProperties.DIGEST_SHA256)
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
setBlockModes(KeyProperties.BLOCK_MODE_ECB)
setUserAuthenticationRequired(false)
build()
}
val keypair = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore").let {
it.initialize(keyGenParameterSpec)
it.genKeyPair()
}
return keypair;
}
/**
* Tests that a short array of 32 bytes can be encrypted and recovered
*/
#Test
fun canRsaRoundtrip() {
val encAlgo = "RSA/ECB/OAEPPadding"
val decAlgo = encAlgo
val wrappingKey = generateRsaKeypair("roundtrip", isStrongboxBacked = true)
val input = ByteArray(32)
val spec = OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)
val encCipher = Cipher.getInstance(encAlgo)
encCipher.init(Cipher.ENCRYPT_MODE, wrappingKey.public, spec)
val ciphertext = encCipher.doFinal(input)
val decCipher = Cipher.getInstance(decAlgo)
decCipher.init(Cipher.DECRYPT_MODE, wrappingKey.private, spec)
// Throws android.security.KeyStoreException: Unknown error
val cleartext = decCipher.doFinal(ciphertext)
}
The exception thrown is:
javax.crypto.IllegalBlockSizeException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:519)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
at com.minimumreproducible.KeyWrapperTest.canRsaRoundtrip(KeyWrapperTest.kt:204)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2196)
Caused by: android.security.KeyStoreException: Unknown error
at android.security.KeyStore.getKeyStoreException(KeyStore.java:1303)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
at com.minimumreproducible.KeyWrapperTest.canRsaRoundtrip(KeyWrapperTest.kt:204)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2196)
If I change isStrongboxBacked to false then the test passes.
If I change OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT) to OAEPParameterSpec("SHA-1", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT) then it passes regardless of isStrongboxBacked.
Can anyone advise?

Related

RecyclerView Adapter instrumentation test with ViewBinding

I have a RecyclerView Adapter with multiple ViewHolders. Each ViewHolder uses a ViewBinding Class to inflate the view. I am trying to write some test for the adapter class. Here is my test class.
#RunWith(AndroidJUnit4ClassRunner::class)
class HomeTest {
private lateinit var adapter: Adapterr
#Before
fun setup() {
adapter = Adapterr()
}
#Test
fun createViewHolder() {
adapter.updateAdapter(getDummyAdapterItems())
//Error happens on this line.
val p = RecyclerView(InstrumentationRegistry.getInstrumentation().context)
p.adapter = adapter
val viewHolder = adapter.onCreateViewHolder(p, 0)
assertThat(viewHolder, notNullValue())
}
I get the following error.
java.lang.NoSuchMethodException: android.view.View.retrieveExplicitStyle [class android.content.res.Resources$Theme, interface android.util.AttributeSet]
at java.lang.Class.getMethod(Class.java:2072)
at java.lang.Class.getDeclaredMethod(Class.java:2050)
at io.mockk.proxy.android.MethodDescriptor.getMethod(MethodDescriptor.kt:22)
at io.mockk.proxy.android.advice.Advice.getOrigin(Advice.kt:37)
at java.lang.reflect.Method.invoke(Native Method)
at io.mockk.proxy.android.AndroidMockKDispatcher.getOrigin(AndroidMockKDispatcher.java:117)
at android.view.View.retrieveExplicitStyle(Unknown Source:14)
at android.view.View.<init>(View.java:5474)
at android.view.ViewGroup.<init>(ViewGroup.java:697)
at android.view.ViewGroup.<init>(ViewGroup.java:693)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:654)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:650)
at androidx.recyclerview.widget.RecyclerView.<init>(RecyclerView.java:646)
at com.example.home.HomeTest.createViewHolder(HomeTest.kt:57)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2205)
I even tried running it with a MockViewGroup Object, but I get the same error.

JUnit assert that an Activity is implementation of an interface

I have an Android Activity that implements an delegate interface which is called IMeetingRoomDelegate
interface IMeetingRoomDelegate {
fun onMeetingRoomFragmentClicked(homeFragment: MeetingRoomHomeFragment, meetingRoom: ParcelableMeetingRoomData)
}
I want to always assure that my Activity is always an implementation of IMeetingRoomDelegate.
How would I check this in Kotlin? I so far have this:
#RunWith(AndroidJUnit4::class)
class GivenTheMainActivityIsLoaded {
#get:Rule
val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var mainActivity: MainActivity
#Before
fun setup() {
this.mainActivity = activityRule.activity
}
#Test
fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
assertTrue(implementsInterface(this.mainActivity::class.java))
}
private fun implementsInterface(interf: Class<*>): Boolean {
return interf is IMeetingRoomDelegate
}
}
I've tried seeing what the issue is myself but the test runner is barely giving anything substantial away.
The error
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at activities.GivenTheMainActivityIsLoaded.thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent(MainActivitySpec.kt:54)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Tests ran to completion.
You could use the reified generics instead of asserting the reflections:
#Test
fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
assertTrue(this.mainActivity.implementsInterface())
}
private inline fun <reified T> T.implementsInterface(): Boolean {
return this is IMeetingRoomDelegate
}

java.lang.RuntimeException: Could not launch activity

In my test I need to start activity as singleton. So I use flag FLAG_ACTIVITY_SINGLE_TOP
Here my Espresso's test:
#RunWith(AndroidJUnit4::class)
class TradersActivityTest {
private var intent = Intent()
#Rule
#JvmField
var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)
#Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
#Test
fun network_clientError_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(400))
tradersIntentTestRule.launchActivity(intent) // error here
onView(withText(R.string.client_error)).inRoot(ToastMatcher()).check(matches(isDisplayed()))
}
But when I start test network_clientError_showToast I get error:
java.lang.RuntimeException: Could not launch activity
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:473)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at com.myproject.activity.radersActivityTest.network_clientError_showToast(TradersActivityTest.kt:112)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:672)
at android.app.ContextImpl.startActivity(ContextImpl.java:659)
at android.app.Instrumentation.startActivitySync(Instrumentation.java:405)
at androidx.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:99)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:449)
at androidx.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:446)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
You're starting your activity outside of an activity context, so you have to use FLAG_ACTIVITY_NEW_TASK along with your other flags:
#Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK)
}

Espresso tests with intended(hasComponent(...)) are failing on API 28

Our tests are passing on API 27
However on API 28 we're getting this error on all our Espresso tests that have intended(hasComponent(MyActivity.class.getName()));
I saw this answer but we're already using buildToolsVersion '28.0.3', I also migrated to AndroidX and no chage.
Stacktrace:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: has component: has component with: class name: is "com.mydomain.myapp.activity.MyActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String
Matched intents:[]
Recorded intents:
-Intent { flg=0x14000000 cmp=com.mydomain.myapp.qa/com.mydomain.myapp.activity.ActivityB } handling packages:[[com.mydomain.myapp.qa]])
-Intent { cmp=com.mydomain.myapp.qa/com.mydomain.myapp.activity.ActivityC (has extras) } handling packages:[[com.mydomain.myapp.qa]], extras:[Bundle[{utm.content=null, goto.signup=false}]])
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1538)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:88)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:297)
at android.support.test.espresso.intent.Intents.intended(Intents.java:188)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at com.mydomain.myapp.activity.CCFailureTest.activeLowRiskUserCanCloseUpdateCCPopupTest(CCFailureTest.java:92)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:129)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

GrantPermissionRule leads to PermissionException

I've tried today the new android testing support lib. I need the write external storage permission for my tests so I've created the following test with the new GrantPermissionRule.
ExternalDirTest.java
#Rule
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);
#Test
public void test() throws IOException {
Application.getApplication().createDir();
}
Application.java
public void createDir() throws IOException {
new File(Environment.getExternalStorageDirectory() + "/test").createNewFile();
}
Officially I get the permission (visible in the app settings). Nevertheless I get the following exception:
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
at java.io.File.createNewFile(File.java:948)
at test.app.directory.Application.createDir(EarApplication.java:253)
at test.app.directory.ExternalDirTest.test(ExternalDirTest.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:109)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:369)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
If I test the code manual it works perfectly.
Has someone facing the same problem or is it maybe a bug in the library itself?
Should now be fixed in version 1.0.2-alpha2
See https://issuetracker.google.com/issues/64389280

Categories

Resources