Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement adaptive relay #23

Closed
gnarea opened this issue Aug 14, 2019 · 1 comment
Closed

Implement adaptive relay #23

gnarea opened this issue Aug 14, 2019 · 1 comment

Comments

@gnarea
Copy link
Member

gnarea commented Aug 14, 2019

Per https://specs.relaynet.link/RS-017

See also: AwalaApp/specs#68

@gnarea gnarea transferred this issue from relaycorp/relaynet-courier-android May 6, 2020
@gnarea
Copy link
Member Author

gnarea commented Oct 21, 2020

Here's a working example of registering + delivering parcels + collecting parcels from a public gateway:

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import tech.relaycorp.relaynet.bindings.pdc.Signer
import tech.relaycorp.relaynet.bindings.pdc.StreamingMode
import tech.relaycorp.relaynet.issueEndpointCertificate
import tech.relaycorp.relaynet.issueParcelDeliveryAuthorization
import tech.relaycorp.relaynet.messages.Parcel
import tech.relaycorp.relaynet.messages.control.PrivateNodeRegistrationRequest
import tech.relaycorp.relaynet.wrappers.generateRSAKeyPair
import kotlin.test.assertEquals

class RealTest {
    private val localGatewayPort = 8082

    @Test
    fun test_connection(): Unit = runBlocking {
        val privateGatewayKeyPair = generateRSAKeyPair()
        val client = PoWebClient.initLocal(localGatewayPort) // <-- TODO: Replace with `initRemote()`
        client.use {
            val registrationRequest = client.preRegisterNode(privateGatewayKeyPair.public)
            val registration =
                client.registerNode(registrationRequest.serialize(privateGatewayKeyPair.private))

            val recipientKeyPair = generateRSAKeyPair()
            val recipientCertificate = issueEndpointCertificate(
                recipientKeyPair.public,
                privateGatewayKeyPair.private,
                registration.privateNodeCertificate.expiryDate,
                registration.privateNodeCertificate,
                registration.privateNodeCertificate.startDate
            )
            val senderKeyPair = generateRSAKeyPair()
            val senderCertificate = issueParcelDeliveryAuthorization(
                senderKeyPair.public,
                recipientKeyPair.private,
                recipientCertificate.expiryDate,
                recipientCertificate,
                recipientCertificate.startDate
            )

            val parcel = Parcel(
                recipientCertificate.subjectPrivateAddress,
                ByteArray(0),
                senderCertificate,
                senderCertificateChain = setOf(
                    recipientCertificate,
                    registration.privateNodeCertificate
                )
            )

            val privateGatewaySigner =
                Signer(registration.privateNodeCertificate, privateGatewayKeyPair.private)
            client.deliverParcel(
                parcel.serialize(senderKeyPair.private),
                privateGatewaySigner
            )

            delay(3_000)

            val parcelsCollected = client.collectParcels(
                arrayOf(privateGatewaySigner),
                StreamingMode.KeepAlive
            ).map {
                it.ack()
                it.deserializeAndValidateParcel()
            }.take(1).toList()
            assertEquals(1, parcelsCollected.size)
            assertEquals(parcel.id, parcelsCollected.first().id)
        }
    }
}

And here's the documentation on how to write unit tests with a mock client: https://github.com/relaycorp/relaynet-jvm-testing#mock-pdc-client

@gnarea gnarea closed this as completed Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant