Skip to content

Commit

Permalink
fix(Messaging): Demote certain public API members to internal
Browse files Browse the repository at this point in the history
Such as the creation date of parcels
  • Loading branch information
gnarea committed Mar 10, 2021
1 parent cd11c82 commit 33189dc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import tech.relaycorp.relaydroid.endpoint.UnknownFirstPartyEndpointException
import tech.relaycorp.relaydroid.endpoint.UnknownThirdPartyEndpointException
import tech.relaycorp.relaydroid.storage.persistence.PersistenceException
import tech.relaycorp.relaynet.messages.Parcel
import java.time.ZonedDateTime
import tech.relaycorp.relaynet.messages.InvalidMessageException
import tech.relaycorp.relaynet.wrappers.cms.EnvelopedDataException

Expand All @@ -16,12 +15,8 @@ public class IncomingMessage internal constructor(
public val content: ByteArray,
public val senderEndpoint: ThirdPartyEndpoint,
public val recipientEndpoint: FirstPartyEndpoint,
creationDate: ZonedDateTime,
expiryDate: ZonedDateTime,
public val ack: suspend () -> Unit
) : Message(
id, senderEndpoint, recipientEndpoint, creationDate, expiryDate
) {
) : Message(id) {

internal companion object {
@Throws(
Expand Down Expand Up @@ -53,8 +48,6 @@ public class IncomingMessage internal constructor(
content = serviceMessage.content,
senderEndpoint = sender,
recipientEndpoint = recipientEndpoint,
creationDate = parcel.creationDate,
expiryDate = parcel.expiryDate,
ack = ack
)
}
Expand Down
19 changes: 1 addition & 18 deletions lib/src/main/java/tech/relaycorp/relaydroid/messaging/Message.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
package tech.relaycorp.relaydroid.messaging

import tech.relaycorp.relaydroid.endpoint.Endpoint
import java.time.Duration
import java.time.ZonedDateTime

public abstract class Message(
public val id: MessageId,
senderEndpoint: Endpoint,
recipientEndpoint: Endpoint,
public val creationDate: ZonedDateTime = ZonedDateTime.now(),
public val expiryDate: ZonedDateTime = maxExpiryDate()
) {

internal val ttl get() = Duration.between(creationDate, expiryDate).seconds.toInt()

internal companion object {
internal fun maxExpiryDate() = ZonedDateTime.now().plusDays(30)
}
}
public abstract class Message(public val id: MessageId)
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
package tech.relaycorp.relaydroid.messaging

import java.time.Duration
import java.time.ZonedDateTime
import tech.relaycorp.relaydroid.endpoint.FirstPartyEndpoint
import tech.relaycorp.relaydroid.endpoint.PrivateThirdPartyEndpoint
import tech.relaycorp.relaydroid.endpoint.PublicThirdPartyEndpoint
import tech.relaycorp.relaydroid.endpoint.ThirdPartyEndpoint
import tech.relaycorp.relaynet.issueEndpointCertificate
import tech.relaycorp.relaynet.messages.InvalidMessageException
import tech.relaycorp.relaynet.messages.Parcel
import tech.relaycorp.relaynet.messages.payloads.ServiceMessage
import tech.relaycorp.relaynet.ramf.RAMFException
import tech.relaycorp.relaynet.wrappers.x509.Certificate
import java.time.ZonedDateTime

public class OutgoingMessage
private constructor(
public val senderEndpoint: FirstPartyEndpoint,
public val recipientEndpoint: ThirdPartyEndpoint,
creationDate: ZonedDateTime = ZonedDateTime.now(),
expiryDate: ZonedDateTime = maxExpiryDate(),
id: MessageId = MessageId.generate()
) : Message(
id, senderEndpoint, recipientEndpoint, creationDate, expiryDate
) {
public val expiryDate: ZonedDateTime = maxExpiryDate(),
id: MessageId,
internal val creationDate: ZonedDateTime = ZonedDateTime.now()
) : Message(id) {

internal lateinit var parcel: Parcel
private set

internal val ttl get() = Duration.between(creationDate, expiryDate).seconds.toInt()

public companion object {
internal fun maxExpiryDate() = ZonedDateTime.now().plusDays(30)

public suspend fun build(
type: String,
content: ByteArray,
senderEndpoint: FirstPartyEndpoint,
recipientEndpoint: ThirdPartyEndpoint,
creationDate: ZonedDateTime = ZonedDateTime.now(),
expiryDate: ZonedDateTime = maxExpiryDate(),
id: MessageId = MessageId.generate()
): OutgoingMessage {
val message = OutgoingMessage(
senderEndpoint, recipientEndpoint, creationDate, expiryDate, id
)
val message = OutgoingMessage(senderEndpoint, recipientEndpoint, expiryDate, id)
message.parcel = message.buildParcel(type, content)
try {
message.parcel.validate(null)
} catch (exp: RAMFException) {
throw InvalidMessageException("Invalid outgoing message", exp)
}
return message
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@ internal class IncomingMessageTest {
assertEquals(serviceMessage.type, message.type)
assertArrayEquals(serviceMessage.content, message.content)
assertEquals(parcel.id, message.id.value)
assertSameDateTime(parcel.creationDate, message.creationDate)
assertSameDateTime(parcel.expiryDate, message.expiryDate)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
package tech.relaycorp.relaydroid.messaging

import java.time.ZonedDateTime
import kotlin.random.Random
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import tech.relaycorp.relaydroid.endpoint.PublicThirdPartyEndpoint
import tech.relaycorp.relaydroid.test.FirstPartyEndpointFactory
import tech.relaycorp.relaydroid.test.MessageFactory
import tech.relaycorp.relaydroid.test.ThirdPartyEndpointFactory
import tech.relaycorp.relaydroid.test.assertSameDateTime
import tech.relaycorp.relaynet.messages.InvalidMessageException
import tech.relaycorp.relaynet.ramf.RecipientAddressType
import tech.relaycorp.relaynet.testing.pki.PDACertPath
import java.time.ZonedDateTime
import tech.relaycorp.relaynet.testing.pki.KeyPairSet

internal class OutgoingMessageTest {

@Test(expected = InvalidMessageException::class)
internal fun buildInvalidMessage() = runBlockingTest {
OutgoingMessage.build(
"the type",
ByteArray(0),
FirstPartyEndpointFactory.build(),
ThirdPartyEndpointFactory.buildPublic(),
creationDate = ZonedDateTime.now().plusDays(1)
)
}

@Test
fun buildForPublicRecipient_checkBaseValues() = runBlockingTest {
val message = MessageFactory.buildOutgoing(RecipientAddressType.PUBLIC)
Expand All @@ -40,6 +27,23 @@ internal class OutgoingMessageTest {
assertEquals(message.ttl, parcel.ttl)
}

@Test
fun buildForPublicRecipient_checkTTL() = runBlockingTest {
val senderEndpoint = FirstPartyEndpointFactory.build()
val recipientEndpoint = ThirdPartyEndpointFactory.buildPublic()

val message = OutgoingMessage.build(
"the type",
Random.Default.nextBytes(10),
senderEndpoint = senderEndpoint,
recipientEndpoint = recipientEndpoint,
expiryDate = ZonedDateTime.now().plusMinutes(1)
)

assertTrue(58 < message.ttl)
assertTrue(message.ttl <= 60)
}

@Test
fun buildForPublicRecipient_checkServiceMessage() = runBlockingTest {
val message = MessageFactory.buildOutgoing(RecipientAddressType.PUBLIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import tech.relaycorp.relaydroid.messaging.MessageId
import tech.relaycorp.relaydroid.messaging.OutgoingMessage
import tech.relaycorp.relaynet.messages.payloads.ServiceMessage
import tech.relaycorp.relaynet.ramf.RecipientAddressType
import java.time.ZonedDateTime
import java.util.UUID

internal object MessageFactory {
Expand All @@ -23,9 +22,6 @@ internal object MessageFactory {
type = serviceMessage.type,
content = serviceMessage.content,
senderEndpoint = ThirdPartyEndpointFactory.buildPublic(),
recipientEndpoint = FirstPartyEndpointFactory.build(),
creationDate = ZonedDateTime.now(),
expiryDate = ZonedDateTime.now().plusDays(1),
ack = {}
)
recipientEndpoint = FirstPartyEndpointFactory.build()
) {}
}

0 comments on commit 33189dc

Please sign in to comment.