|
16 | 16 |
|
17 | 17 | package io.grpc.binder.internal; |
18 | 18 |
|
| 19 | +import static android.os.Process.myUid; |
19 | 20 | import static com.google.common.truth.Truth.assertThat; |
| 21 | +import static io.grpc.binder.internal.BinderTransport.REMOTE_UID; |
| 22 | +import static io.grpc.binder.internal.BinderTransport.SETUP_TRANSPORT; |
| 23 | +import static io.grpc.binder.internal.BinderTransport.WIRE_FORMAT_VERSION; |
20 | 24 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 | 25 | import static org.mockito.Mockito.never; |
22 | 26 | import static org.mockito.Mockito.timeout; |
|
28 | 32 | import android.content.pm.ApplicationInfo; |
29 | 33 | import android.content.pm.PackageInfo; |
30 | 34 | import android.content.pm.ServiceInfo; |
| 35 | +import android.os.Binder; |
| 36 | +import android.os.Parcel; |
31 | 37 | import androidx.test.core.app.ApplicationProvider; |
32 | 38 | import androidx.test.core.content.pm.ApplicationInfoBuilder; |
33 | 39 | import androidx.test.core.content.pm.PackageInfoBuilder; |
|
38 | 44 | import io.grpc.binder.AndroidComponentAddress; |
39 | 45 | import io.grpc.binder.ApiConstants; |
40 | 46 | import io.grpc.binder.AsyncSecurityPolicy; |
| 47 | +import io.grpc.binder.SecurityPolicies; |
41 | 48 | import io.grpc.binder.internal.SettableAsyncSecurityPolicy.AuthRequest; |
42 | 49 | import io.grpc.internal.AbstractTransportTest; |
43 | 50 | import io.grpc.internal.ClientTransportFactory.ClientTransportOptions; |
| 51 | +import io.grpc.internal.ConnectionClientTransport; |
44 | 52 | import io.grpc.internal.GrpcUtil; |
45 | 53 | import io.grpc.internal.InternalServer; |
46 | 54 | import io.grpc.internal.ManagedClientTransport; |
@@ -109,7 +117,7 @@ public static ImmutableList<Boolean> data() { |
109 | 117 | public void setUp() { |
110 | 118 | serverAppInfo = |
111 | 119 | ApplicationInfoBuilder.newBuilder().setPackageName("the.server.package").build(); |
112 | | - serverAppInfo.uid = android.os.Process.myUid(); |
| 120 | + serverAppInfo.uid = myUid(); |
113 | 121 | serverPkgInfo = |
114 | 122 | PackageInfoBuilder.newBuilder() |
115 | 123 | .setPackageName(serverAppInfo.packageName) |
@@ -264,6 +272,38 @@ public void eagAttributeCanOverrideChannelPreAuthServerSetting() throws Exceptio |
264 | 272 | verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportReady(); |
265 | 273 | } |
266 | 274 |
|
| 275 | + @Test |
| 276 | + public void clientIgnoresDuplicateSetupTransaction() throws Exception { |
| 277 | + server.start(serverListener); |
| 278 | + client = |
| 279 | + newClientTransportBuilder() |
| 280 | + .setFactory( |
| 281 | + newClientTransportFactoryBuilder() |
| 282 | + .setSecurityPolicy(SecurityPolicies.internalOnly()) |
| 283 | + .buildClientTransportFactory()) |
| 284 | + .build(); |
| 285 | + runIfNotNull(client.start(mockClientTransportListener)); |
| 286 | + verify(mockClientTransportListener, timeout(TIMEOUT_MS)).transportReady(); |
| 287 | + |
| 288 | + assertThat(((ConnectionClientTransport) client).getAttributes().get(REMOTE_UID)) |
| 289 | + .isEqualTo(myUid()); |
| 290 | + |
| 291 | + Parcel setupParcel = Parcel.obtain(); |
| 292 | + try { |
| 293 | + setupParcel.writeInt(WIRE_FORMAT_VERSION); |
| 294 | + setupParcel.writeStrongBinder(new Binder()); |
| 295 | + setupParcel.setDataPosition(0); |
| 296 | + ShadowBinder.setCallingUid(1 + myUid()); |
| 297 | + ((BinderClientTransport) client).handleTransaction(SETUP_TRANSPORT, setupParcel); |
| 298 | + } finally { |
| 299 | + ShadowBinder.setCallingUid(myUid()); |
| 300 | + setupParcel.recycle(); |
| 301 | + } |
| 302 | + |
| 303 | + assertThat(((ConnectionClientTransport) client).getAttributes().get(REMOTE_UID)) |
| 304 | + .isEqualTo(myUid()); |
| 305 | + } |
| 306 | + |
267 | 307 | @Test |
268 | 308 | @Ignore("See BinderTransportTest#socketStats.") |
269 | 309 | @Override |
|
0 commit comments