Skip to content

Commit

Permalink
Merge branch 'master' into cargo-gen-cap-ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Jul 25, 2024
2 parents 8a893a2 + ca29375 commit 6e0415a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
ext {
ktorVersion = '2.3.7'
junitVersion = '5.8.2'
awalaTestingVersion = '1.5.10'
awalaTestingVersion = '1.5.31'
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ dependencies {

// Awala
implementation 'tech.relaycorp:awala:1.69.0'
implementation 'tech.relaycorp:awala-keystore-file:1.6.31'
implementation 'tech.relaycorp:awala-keystore-file:1.6.53'
implementation 'tech.relaycorp:cogrpc:1.1.32'
implementation 'tech.relaycorp:cogrpc-okhttp:1.1.28'
testImplementation "tech.relaycorp:awala-testing:$awalaTestingVersion"
Expand All @@ -162,7 +162,7 @@ dependencies {
implementation 'org.conscrypt:conscrypt-android:2.5.2'

// Local and Internet-based Parcel Delivery Connections (PDCs)
implementation 'tech.relaycorp:poweb:1.5.80'
implementation 'tech.relaycorp:poweb:1.5.87'
implementation "io.ktor:ktor-server-core:$ktorVersion"
implementation "io.ktor:ktor-server-netty:$ktorVersion"
implementation "io.ktor:ktor-server-websockets:$ktorVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class RegisterGateway
localConfig.setParcelDeliveryCertificate(registration.privateNodeCertificate)
publicKeyStore.save(
registration.gatewaySessionKey!!,
registration.privateNodeCertificate.subjectId,
registration.gatewayCertificate.subjectId,
)
internetGatewayPreferences.setRegistrationState(RegistrationState.Done)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import com.nhaarman.mockitokotlin2.whenever
import io.ktor.test.dispatcher.testSuspend
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import tech.relaycorp.gateway.data.doh.InternetAddressResolutionException
Expand All @@ -24,6 +26,7 @@ import tech.relaycorp.gateway.domain.StoreParcel
import tech.relaycorp.gateway.domain.endpoint.IncomingParcelNotifier
import tech.relaycorp.gateway.pdc.PoWebClientProvider
import tech.relaycorp.gateway.test.BaseDataTestCase
import tech.relaycorp.gateway.test.TestLogHandler
import tech.relaycorp.poweb.PoWebClient
import tech.relaycorp.relaynet.bindings.pdc.ClientBindingException
import tech.relaycorp.relaynet.bindings.pdc.NonceSignerException
Expand All @@ -33,6 +36,8 @@ import tech.relaycorp.relaynet.bindings.pdc.StreamingMode
import tech.relaycorp.relaynet.messages.Parcel
import tech.relaycorp.relaynet.messages.Recipient
import tech.relaycorp.relaynet.testing.pki.PDACertPath
import java.util.logging.Level
import java.util.logging.Logger

class CollectParcelsFromGatewayTest : BaseDataTestCase() {

Expand All @@ -54,6 +59,7 @@ class CollectParcelsFromGatewayTest : BaseDataTestCase() {
notifyEndpoints,
mockLocalConfig,
)
private val testLogHandler = TestLogHandler()

@BeforeEach
fun setUp() = testSuspend {
Expand All @@ -62,6 +68,14 @@ class CollectParcelsFromGatewayTest : BaseDataTestCase() {
.thenReturn(StoreParcel.Result.Success(mock()))
whenever(mockInternetGatewayPreferences.getId())
.thenReturn(PDACertPath.INTERNET_GW.subjectId)

Logger.getLogger(CollectParcelsFromGateway::class.java.name).addHandler(testLogHandler)
}

@AfterEach
fun tearDown() {
testLogHandler.close()
Logger.getLogger(CollectParcelsFromGateway::class.java.name).removeHandler(testLogHandler)
}

@Test
Expand Down Expand Up @@ -154,19 +168,61 @@ class CollectParcelsFromGatewayTest : BaseDataTestCase() {

@Test
fun `poWebClient client binding issues are handled`() = testSuspend {
whenever(poWebClient.collectParcels(any(), any())).thenThrow(ClientBindingException(""))
whenever(poWebClient.collectParcels(any(), any())).thenReturn(
flow {
throw ClientBindingException("Message")
},
)

subject.collect(false)

val logRecord = testLogHandler.filterLogs(
Level.SEVERE,
"Could not collect parcels due to client error",
)
.singleOrNull()
assert(logRecord != null)
assert(logRecord?.thrown is ClientBindingException)
verifyNoMoreInteractions(storeParcel, notifyEndpoints)
}

@Test
fun `poWebClient signer issues are handled`() = testSuspend {
whenever(poWebClient.collectParcels(any(), any())).thenThrow(NonceSignerException(""))
whenever(poWebClient.collectParcels(any(), any())).thenReturn(
flow {
throw NonceSignerException("Message")
},
)

subject.collect(false)

val logRecord = testLogHandler.filterLogs(
Level.SEVERE,
"Could not collect parcels due to signing error",
)
.singleOrNull()
assert(logRecord != null)
assert(logRecord?.thrown is NonceSignerException)
verifyNoMoreInteractions(storeParcel, notifyEndpoints)
}

@Test
fun `poWebClient with keepAlive false, server issues are handled`() = testSuspend {
whenever(poWebClient.collectParcels(any(), any())).thenThrow(ServerConnectionException(""))
whenever(poWebClient.collectParcels(any(), any())).thenReturn(
flow {
throw ServerConnectionException("Message")
},
)

subject.collect(false)

val logRecord = testLogHandler.filterLogs(
Level.WARNING,
"Could not collect parcels due to server error",
)
.singleOrNull()
assert(logRecord != null)
assert(logRecord?.thrown is ServerConnectionException)
verifyNoMoreInteractions(storeParcel, notifyEndpoints)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ class RegisterGatewayTest : BaseDataTestCase() {

verify(pgwPreferences).setPublicKey(eq(pnr.gatewayCertificate.subjectPublicKey))
verify(pgwPreferences).setRegistrationState(eq(RegistrationState.Done))
publicKeyStore.retrieve(pnr.gatewayCertificate.subjectId)
publicKeyStore.retrieve(
pnr.privateNodeCertificate.subjectId,
pnr.gatewayCertificate.subjectId,
)
assertEquals(pnr.privateNodeCertificate, localConfig.getParcelDeliveryCertificate())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ abstract class BaseDataTestCase {
protected suspend fun registerInternetGatewaySessionKey() {
publicKeyStore.save(
internetGatewaySessionKeyPair.sessionKey,
KeyPairSet.PRIVATE_GW.public.nodeId,
KeyPairSet.INTERNET_GW.public.nodeId,
)
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/test/java/tech/relaycorp/gateway/test/TestLogHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.relaycorp.gateway.test

import java.util.logging.Handler
import java.util.logging.Level
import java.util.logging.LogRecord

class TestLogHandler : Handler() {
private val logs = mutableListOf<LogRecord>()

override fun publish(record: LogRecord) {
logs.add(record)
}

override fun flush() {}

override fun close() {
logs.clear()
}

fun filterLogs(level: Level, message: String): List<LogRecord> {
return logs.filter { it.level == level && it.message == message }
}
}

0 comments on commit 6e0415a

Please sign in to comment.