-
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.
chore: Parcel Sync instrumented tests (#148)
- Loading branch information
Showing
13 changed files
with
248 additions
and
33 deletions.
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
102 changes: 102 additions & 0 deletions
102
...java/tech/relaycorp/gateway/background/endpoint/GatewaySyncServiceParcelCollectionTest.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,102 @@ | ||
package tech.relaycorp.gateway.background.endpoint | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.test.core.app.ApplicationProvider.getApplicationContext | ||
import androidx.test.rule.ServiceTestRule | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import tech.relaycorp.gateway.data.disk.SensitiveStore | ||
import tech.relaycorp.gateway.data.model.RecipientLocation | ||
import tech.relaycorp.gateway.domain.StoreParcel | ||
import tech.relaycorp.gateway.pdc.local.PDCServer | ||
import tech.relaycorp.gateway.test.AppTestProvider | ||
import tech.relaycorp.gateway.test.factory.ParcelFactory | ||
import tech.relaycorp.poweb.PoWebClient | ||
import tech.relaycorp.poweb.ServerConnectionException | ||
import tech.relaycorp.relaynet.bindings.pdc.NonceSigner | ||
import tech.relaycorp.relaynet.bindings.pdc.StreamingMode | ||
import tech.relaycorp.relaynet.messages.Parcel | ||
import tech.relaycorp.relaynet.testing.CertificationPath | ||
import tech.relaycorp.relaynet.testing.KeyPairSet | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import javax.inject.Inject | ||
|
||
class GatewaySyncServiceParcelCollectionTest { | ||
|
||
@get:Rule | ||
val serviceRule = ServiceTestRule() | ||
|
||
@Inject | ||
lateinit var sensitiveStore: SensitiveStore | ||
|
||
@Inject | ||
lateinit var storeParcel: StoreParcel | ||
|
||
@Before | ||
fun setUp() { | ||
AppTestProvider.component.inject(this) | ||
serviceRule.bindService( | ||
Intent( | ||
getApplicationContext<Context>(), | ||
GatewaySyncService::class.java | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun parcelCollection_receiveParcel() = runBlocking { | ||
setGatewayCertificate(CertificationPath.PRIVATE_GW) | ||
val parcel = ParcelFactory.buildSerialized() | ||
val storeResult = storeParcel.store(parcel, RecipientLocation.LocalEndpoint) | ||
assertTrue(storeResult is StoreParcel.Result.Success) | ||
|
||
val parcelCollection = | ||
PoWebClient.initLocal(PDCServer.PORT) | ||
.collectParcels( | ||
arrayOf( | ||
NonceSigner( | ||
CertificationPath.PRIVATE_ENDPOINT, | ||
KeyPairSet.PRIVATE_ENDPOINT.private | ||
) | ||
), | ||
StreamingMode.CloseUponCompletion | ||
) | ||
.first() | ||
|
||
assertEquals( | ||
Parcel.deserialize(parcel).id, | ||
Parcel.deserialize(parcelCollection.parcelSerialized).id | ||
) | ||
} | ||
|
||
@Test(expected = ServerConnectionException::class) | ||
fun parcelCollection_invalidHandshake() = runBlocking { | ||
setGatewayCertificate(CertificationPath.PRIVATE_GW) | ||
val parcel = ParcelFactory.buildSerialized() | ||
val storeResult = storeParcel.store(parcel, RecipientLocation.LocalEndpoint) | ||
assertTrue(storeResult is StoreParcel.Result.Success) | ||
|
||
PoWebClient.initLocal(PDCServer.PORT) | ||
.collectParcels( | ||
arrayOf( | ||
NonceSigner( | ||
CertificationPath.PRIVATE_ENDPOINT, | ||
KeyPairSet.PUBLIC_GW.private // Invalid key to trigger invalid handshake | ||
) | ||
), | ||
StreamingMode.CloseUponCompletion | ||
) | ||
.collect() | ||
} | ||
|
||
private suspend fun setGatewayCertificate(cert: Certificate) { | ||
sensitiveStore.store("local_gateway.certificate", cert.serialize()) | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...t/java/tech/relaycorp/gateway/background/endpoint/GatewaySyncServiceParcelDeliveryTest.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,84 @@ | ||
package tech.relaycorp.gateway.background.endpoint | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.test.core.app.ApplicationProvider.getApplicationContext | ||
import androidx.test.rule.ServiceTestRule | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import tech.relaycorp.gateway.data.database.StoredParcelDao | ||
import tech.relaycorp.gateway.data.disk.SensitiveStore | ||
import tech.relaycorp.gateway.data.model.MessageAddress | ||
import tech.relaycorp.gateway.data.model.RecipientLocation | ||
import tech.relaycorp.gateway.pdc.local.PDCServer | ||
import tech.relaycorp.gateway.test.AppTestProvider | ||
import tech.relaycorp.poweb.PoWebClient | ||
import tech.relaycorp.poweb.RejectedParcelException | ||
import tech.relaycorp.relaynet.messages.Parcel | ||
import tech.relaycorp.relaynet.testing.CertificationPath | ||
import tech.relaycorp.relaynet.testing.KeyPairSet | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import javax.inject.Inject | ||
|
||
class GatewaySyncServiceParcelDeliveryTest { | ||
|
||
@get:Rule | ||
val serviceRule = ServiceTestRule() | ||
|
||
@Inject | ||
lateinit var sensitiveStore: SensitiveStore | ||
|
||
@Inject | ||
lateinit var storedParcelDao: StoredParcelDao | ||
|
||
@Before | ||
fun setUp() { | ||
AppTestProvider.component.inject(this) | ||
serviceRule.bindService( | ||
Intent( | ||
getApplicationContext<Context>(), | ||
GatewaySyncService::class.java | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun parcelDelivery_validParcel() = runBlocking { | ||
setGatewayCertificate(CertificationPath.PRIVATE_GW) | ||
val recipient = "https://example.org" | ||
|
||
val parcel = Parcel( | ||
recipient, | ||
ByteArray(0), | ||
CertificationPath.PRIVATE_ENDPOINT, | ||
senderCertificateChain = setOf(CertificationPath.PRIVATE_GW) | ||
).serialize(KeyPairSet.PRIVATE_ENDPOINT.private) | ||
|
||
PoWebClient.initLocal(PDCServer.PORT).deliverParcel(parcel) | ||
|
||
val storedParcels = storedParcelDao.listForRecipients( | ||
listOf(MessageAddress.of(recipient)), | ||
RecipientLocation.ExternalGateway | ||
).first() | ||
assertEquals(1, storedParcels.size) | ||
} | ||
|
||
@Test(expected = RejectedParcelException::class) | ||
fun parcelDelivery_invalidParcel() = runBlocking { | ||
val parcel = Parcel( | ||
"https://example.org", | ||
ByteArray(0), | ||
CertificationPath.PUBLIC_GW // Wrong certificate to make this parcel invalid | ||
).serialize(KeyPairSet.PUBLIC_GW.private) | ||
|
||
PoWebClient.initLocal(PDCServer.PORT).deliverParcel(parcel) | ||
} | ||
|
||
private suspend fun setGatewayCertificate(cert: Certificate) { | ||
sensitiveStore.store("local_gateway.certificate", cert.serialize()) | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<network-security-config> | ||
<!-- Allow local cleartext networking for instrumented tests of the PDC Server --> | ||
<domain-config cleartextTrafficPermitted="true"> | ||
<domain includeSubdomains="false">127.0.0.1</domain> | ||
</domain-config> | ||
</network-security-config> |
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
Oops, something went wrong.