Skip to content

Commit

Permalink
fix: Support binding with the gateway on Android 11+ (#154)
Browse files Browse the repository at this point in the history
Related with: relaycorp/awala-ping-android#158

Changes made to Package Visibility and security with Android 11.
https://developer.android.com/training/package-visibility/declaring#package-name

In this PR you can also see some changes to compile to Android 12, and Mac M1
  • Loading branch information
FilipeA authored Oct 20, 2021
1 parent cdbb3a4 commit 6dcc3f8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ plugins {
apply from: 'jacoco.gradle'

android {
compileSdkVersion 30
compileSdkVersion 31
buildToolsVersion "30.0.3"

defaultConfig {
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0.0"

Expand Down
3 changes: 3 additions & 0 deletions lib/jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# configuration file for building snapshots and releases with jitpack.io
jdk:
- openjdk11
13 changes: 12 additions & 1 deletion lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
>
<receiver
android:name=".background.NotificationBroadcastReceiver"
tools:ignore="ExportedReceiver">
tools:ignore="ExportedReceiver"
android:exported="true">
<intent-filter>
<action android:name="tech.relaycorp.endpoint.NOTIFY" />
</intent-filter>
</receiver>
</application>

<queries>
<intent>
<action android:name="tech.relaycorp.gateway.SYNC"/>
</intent>
<intent>
<action android:name="tech.relaycorp.gateway.ENDPOINT_PRE_REGISTRATION"/>
</intent>
</queries>

</manifest>
7 changes: 6 additions & 1 deletion lib/src/main/java/tech/relaycorp/awaladroid/Awala.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import tech.relaycorp.awaladroid.storage.persistence.EncryptedDiskPersistence

public object Awala {
internal const val POWEB_PORT = 13276
internal const val GATEWAY_PACKAGE = "tech.relaycorp.gateway"
internal const val GATEWAY_PACKAGE =
"tech.relaycorp.gateway"
internal const val GATEWAY_PRE_REGISTER_ACTION =
"tech.relaycorp.gateway.ENDPOINT_PRE_REGISTRATION"
internal const val GATEWAY_PRE_REGISTER_COMPONENT =
"tech.relaycorp.gateway.background.endpoint.EndpointPreRegistrationService"
internal const val GATEWAY_SYNC_COMPONENT =
"tech.relaycorp.gateway.background.endpoint.GatewaySyncService"
internal const val GATEWAY_SYNC_ACTION =
"tech.relaycorp.gateway.SYNC"

/**
* Set up the endpoint library.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal constructor(
gwServiceInteractor = serviceInteractorBuilder().apply {
try {
bind(
Awala.GATEWAY_SYNC_ACTION,
Awala.GATEWAY_PACKAGE,
Awala.GATEWAY_SYNC_COMPONENT
)
Expand Down Expand Up @@ -122,6 +123,7 @@ internal constructor(
private suspend fun preRegister(): ByteArray {
val interactor = serviceInteractorBuilder().apply {
bind(
Awala.GATEWAY_PRE_REGISTER_ACTION,
Awala.GATEWAY_PACKAGE,
Awala.GATEWAY_PRE_REGISTER_COMPONENT
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class ServiceInteractor(
private var binder: IBinder? = null

@Throws(BindFailedException::class)
suspend fun bind(packageName: String, componentName: String) =
suspend fun bind(action: String, packageName: String, componentName: String) =
suspendCoroutine<Unit> { cont ->
var isResumed = false

Expand Down Expand Up @@ -63,8 +63,15 @@ internal class ServiceInteractor(
}
}

val intent = Intent(action).apply {
component = ComponentName(
packageName,
componentName
)
}

val bindWasSuccessful = context.bindService(
Intent().setComponent(ComponentName(packageName, componentName)),
intent,
serviceConnection,
Context.BIND_AUTO_CREATE
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ internal class GatewayClientImplTest {
fun bind_successful() = coroutineScope.runBlockingTest {
subject.bind()

verify(serviceInteractor).bind(Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
verify(serviceInteractor).bind(
Awala.GATEWAY_SYNC_ACTION,
Awala.GATEWAY_PACKAGE,
Awala.GATEWAY_SYNC_COMPONENT
)
}

@Test
Expand All @@ -66,7 +70,7 @@ internal class GatewayClientImplTest {
subject.bind()

verify(serviceInteractor, times(1))
.bind(Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
.bind(Awala.GATEWAY_SYNC_ACTION, Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
}

@Test
Expand All @@ -76,12 +80,12 @@ internal class GatewayClientImplTest {
subject.bind()

verify(serviceInteractor, times(2))
.bind(Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
.bind(Awala.GATEWAY_SYNC_ACTION, Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
}

@Test(expected = GatewayBindingException::class)
fun bind_unsuccessful() = coroutineScope.runBlockingTest {
whenever(serviceInteractor.bind(any(), any()))
whenever(serviceInteractor.bind(any(), any(), any()))
.thenThrow(ServiceInteractor.BindFailedException(""))

subject.bind()
Expand All @@ -102,9 +106,13 @@ internal class GatewayClientImplTest {
val result = subject.registerEndpoint(KeyPairSet.PRIVATE_ENDPOINT)

verify(serviceInteractor)
.bind(Awala.GATEWAY_PACKAGE, Awala.GATEWAY_PRE_REGISTER_COMPONENT)
.bind(
Awala.GATEWAY_PRE_REGISTER_ACTION,
Awala.GATEWAY_PACKAGE,
Awala.GATEWAY_PRE_REGISTER_COMPONENT
)
verify(serviceInteractor)
.bind(Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)
.bind(Awala.GATEWAY_SYNC_ACTION, Awala.GATEWAY_PACKAGE, Awala.GATEWAY_SYNC_COMPONENT)

assertEquals(PDACertPath.PRIVATE_ENDPOINT, result.privateNodeCertificate)
assertEquals(PDACertPath.PRIVATE_GW, result.gatewayCertificate)
Expand Down Expand Up @@ -217,7 +225,11 @@ internal class GatewayClientImplTest {
subject.checkForNewMessages()

verify(serviceInteractor)
.bind(eq(Awala.GATEWAY_PACKAGE), eq(Awala.GATEWAY_SYNC_COMPONENT))
.bind(
eq(Awala.GATEWAY_SYNC_ACTION),
eq(Awala.GATEWAY_PACKAGE),
eq(Awala.GATEWAY_SYNC_COMPONENT)
)
verify(serviceInteractor)
.unbind()
}
Expand All @@ -229,7 +241,7 @@ internal class GatewayClientImplTest {
subject.bind()
subject.checkForNewMessages()

verify(serviceInteractor, times(1)).bind(any(), any())
verify(serviceInteractor, times(1)).bind(any(), any(), any())
}

@Test
Expand Down

0 comments on commit 6dcc3f8

Please sign in to comment.