Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binder: Avoid an ISE from asAndroidAppUri() #9169

Merged
merged 2 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -121,9 +121,17 @@ public Intent asBindIntent() {

/**
* Returns this address as an "android-app://" uri.
*
* <p>See {@link Intent#URI_ANDROID_APP_SCHEME} for details.
*/
public String asAndroidAppUri() {
return bindIntent.toUri(URI_ANDROID_APP_SCHEME);
Intent intentForUri = bindIntent;
if (intentForUri.getPackage() == null) {
// URI_ANDROID_APP_SCHEME requires an "explicit package name" which isn't set by any of our
// factory methods. Oddly, our explicit ComponentName is not enough.
intentForUri = intentForUri.cloneFilter().setPackage(getComponent().getPackageName());
}
return intentForUri.toUri(URI_ANDROID_APP_SCHEME);
}

@Override
Expand Down
Expand Up @@ -16,6 +16,7 @@

package io.grpc.binder;

import static android.content.Intent.URI_ANDROID_APP_SCHEME;
import static com.google.common.truth.Truth.assertThat;

import android.content.ComponentName;
Expand All @@ -24,6 +25,7 @@
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import com.google.common.testing.EqualsTester;
import java.net.URISyntaxException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -61,6 +63,30 @@ public void testAsBindIntent() {
assertThat(addr.asBindIntent().filterEquals(bindIntent)).isTrue();
}

@Test
@Config(sdk = 30)
public void testAsAndroidAppUriSdk30() throws URISyntaxException {
AndroidComponentAddress addr =
AndroidComponentAddress.forRemoteComponent("com.foo", "com.foo.Service");
AndroidComponentAddress addrClone =
AndroidComponentAddress.forBindIntent(
Intent.parseUri(addr.asAndroidAppUri(), URI_ANDROID_APP_SCHEME));
assertThat(addr).isEqualTo(addrClone);
}

@Test
@Config(sdk = 29)
public void testAsAndroidAppUriSdk29() throws URISyntaxException {
AndroidComponentAddress addr =
AndroidComponentAddress.forRemoteComponent("com.foo", "com.foo.Service");
AndroidComponentAddress addrClone =
AndroidComponentAddress.forBindIntent(
Intent.parseUri(addr.asAndroidAppUri(), URI_ANDROID_APP_SCHEME));
// Can't test for equality because URI_ANDROID_APP_SCHEME adds a (redundant) package filter.
assertThat(addr.getComponent()).isEqualTo(addrClone.getComponent());
assertThat(addr.getAuthority()).isEqualTo(addrClone.getAuthority());
}

@Test
public void testEquality() {
new EqualsTester()
Expand Down