-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial setup the app needs.
- Loading branch information
Showing
9 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
app/src/main/java/tech/relaycorp/ping/awala/AwalaModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tech.relaycorp.ping.awala | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import tech.relaycorp.awaladroid.endpoint.FirstPartyEndpoint | ||
|
||
@Module | ||
class AwalaModule { | ||
|
||
@Provides | ||
fun firstPartyEndpointRegistration(): FirstPartyEndpointRegistration = | ||
object : FirstPartyEndpointRegistration { | ||
override suspend fun register(): FirstPartyEndpoint = | ||
FirstPartyEndpoint.register() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/tech/relaycorp/ping/awala/FirstPartyEndpointRegistration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package tech.relaycorp.ping.awala | ||
|
||
import tech.relaycorp.awaladroid.endpoint.FirstPartyEndpoint | ||
|
||
interface FirstPartyEndpointRegistration { | ||
suspend fun register(): FirstPartyEndpoint | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/src/main/java/tech/relaycorp/ping/domain/AddPublicPeer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tech.relaycorp.ping.domain | ||
|
||
import tech.relaycorp.awaladroid.endpoint.PublicThirdPartyEndpoint | ||
import tech.relaycorp.ping.data.database.dao.PublicPeerDao | ||
import tech.relaycorp.ping.data.database.entity.PublicPeerEntity | ||
import javax.inject.Inject | ||
|
||
class AddPublicPeer | ||
@Inject constructor( | ||
private val publicPeerDao: PublicPeerDao | ||
) { | ||
|
||
suspend fun add(address: String, identity: ByteArray): PublicThirdPartyEndpoint { | ||
val endpoint = PublicThirdPartyEndpoint.import(address, identity) | ||
publicPeerDao.save( | ||
PublicPeerEntity( | ||
privateAddress = endpoint.privateAddress, | ||
publicAddress = endpoint.publicAddress | ||
) | ||
) | ||
return endpoint | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/tech/relaycorp/ping/domain/BootstrapData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tech.relaycorp.ping.domain | ||
|
||
import android.content.res.Resources | ||
import kotlinx.coroutines.flow.first | ||
import tech.relaycorp.ping.R | ||
import tech.relaycorp.ping.awala.FirstPartyEndpointRegistration | ||
import tech.relaycorp.ping.data.preference.AppPreferences | ||
import javax.inject.Inject | ||
|
||
class BootstrapData | ||
@Inject constructor( | ||
private val resources: Resources, | ||
private val appPreferences: AppPreferences, | ||
private val addPublicPeer: AddPublicPeer, | ||
private val firstPartyEndpointRegistration: FirstPartyEndpointRegistration | ||
) { | ||
|
||
suspend fun bootstrapIfNeeded() { | ||
if (appPreferences.firstPartyEndpointAddress().first() != null) return | ||
|
||
importDefaultPublicPeer() | ||
val endpoint = firstPartyEndpointRegistration.register() | ||
appPreferences.setFirstPartyEndpointAddress(endpoint.privateAddress) | ||
} | ||
|
||
private suspend fun importDefaultPublicPeer() { | ||
addPublicPeer.add( | ||
"ping.awala.services", | ||
resources.openRawResource(R.raw.ping_awala_identity).use { it.readBytes() } | ||
) | ||
} | ||
} |
File renamed without changes.
48 changes: 48 additions & 0 deletions
48
app/src/test/java/tech/relaycorp/ping/domain/BootstrapDataTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tech.relaycorp.ping.domain | ||
|
||
import android.content.res.Resources | ||
import com.nhaarman.mockitokotlin2.* | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import org.junit.Test | ||
import tech.relaycorp.awaladroid.endpoint.FirstPartyEndpoint | ||
import tech.relaycorp.ping.awala.FirstPartyEndpointRegistration | ||
import tech.relaycorp.ping.data.preference.AppPreferences | ||
|
||
class BootstrapDataTest { | ||
|
||
private val resources = mock<Resources>() | ||
private val appPreferences = mock<AppPreferences>() | ||
private val addPublicPeer = mock<AddPublicPeer>() | ||
private val registerFirstPartyEndpoint = mock<FirstPartyEndpointRegistration>() | ||
|
||
private val subject = | ||
BootstrapData(resources, appPreferences, addPublicPeer, registerFirstPartyEndpoint) | ||
|
||
@Test | ||
fun bootstrap_ifNotNeeded() = runBlockingTest { | ||
whenever(appPreferences.firstPartyEndpointAddress()).thenReturn(flowOf("123456")) | ||
|
||
subject.bootstrapIfNeeded() | ||
|
||
verifyZeroInteractions(registerFirstPartyEndpoint) | ||
verifyZeroInteractions(addPublicPeer) | ||
verify(appPreferences, never()).setFirstPartyEndpointAddress(any()) | ||
} | ||
|
||
@Test | ||
fun bootstrap_ifNeeded() = runBlockingTest { | ||
whenever(appPreferences.firstPartyEndpointAddress()).thenReturn(flowOf(null)) | ||
val firstPartyEndpoint = mock<FirstPartyEndpoint>() | ||
val firstPartyAddress = "123456" | ||
whenever(firstPartyEndpoint.privateAddress).thenReturn(firstPartyAddress) | ||
whenever(registerFirstPartyEndpoint.register()).thenReturn(firstPartyEndpoint) | ||
whenever(resources.openRawResource(any())).thenReturn(ByteArray(0).inputStream()) | ||
|
||
subject.bootstrapIfNeeded() | ||
|
||
verify(addPublicPeer).add(eq("ping.awala.services"), any()) | ||
verify(registerFirstPartyEndpoint).register() | ||
verify(appPreferences).setFirstPartyEndpointAddress(eq(firstPartyAddress)) | ||
} | ||
} |