-
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.
feat(CargoMessageSet): Implement method to iterate over messages (#72)
- Loading branch information
Showing
9 changed files
with
159 additions
and
28 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
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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessage.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,31 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import tech.relaycorp.relaynet.messages.PARCEL_SERIALIZER | ||
import tech.relaycorp.relaynet.messages.ParcelCollectionAck | ||
|
||
/** | ||
* Message encapsulated in a cargo message set, classified with its type. | ||
*/ | ||
class CargoMessage(val message: ByteArray) { | ||
var type: Type? = null | ||
private set | ||
|
||
init { | ||
if (10 <= message.size) { | ||
val formatSignature = message.slice(0..9) | ||
for (typeEnum in Type.values()) { | ||
if (typeEnum.formatSignature == formatSignature) { | ||
type = typeEnum | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
enum class Type(internal val formatSignature: List<Byte>) { | ||
PARCEL(PARCEL_SERIALIZER.formatSignature.asList()), | ||
PCA(ParcelCollectionAck.FORMAT_SIGNATURE.asList()) | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/test/kotlin/tech/relaycorp/relaynet/messages/payloads/CargoMessageTest.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,51 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import tech.relaycorp.relaynet.CERTIFICATE | ||
import tech.relaycorp.relaynet.KEY_PAIR | ||
import tech.relaycorp.relaynet.messages.Parcel | ||
import tech.relaycorp.relaynet.messages.ParcelCollectionAck | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
|
||
class CargoMessageTest { | ||
@Nested | ||
inner class Constructor { | ||
private val recipientEndpointAddress = "https://foo.relaycorp.tech" | ||
|
||
@Test | ||
fun `Parcels should be correctly classified as such`() { | ||
val parcel = Parcel(recipientEndpointAddress, "".toByteArray(), CERTIFICATE) | ||
val parcelSerialized = parcel.serialize(KEY_PAIR.private) | ||
|
||
val cargoMessage = CargoMessage(parcelSerialized) | ||
|
||
assertEquals(CargoMessage.Companion.Type.PARCEL, cargoMessage.type) | ||
} | ||
|
||
@Test | ||
fun `PCAs should be correctly classified as such`() { | ||
val pca = ParcelCollectionAck("0deadbeef", recipientEndpointAddress, "parcel-id") | ||
val pcaSerialized = pca.serialize() | ||
|
||
val cargoMessage = CargoMessage(pcaSerialized) | ||
|
||
assertEquals(CargoMessage.Companion.Type.PCA, cargoMessage.type) | ||
} | ||
|
||
@Test | ||
fun `Messages too short to contain format signature should not be assigned a type`() { | ||
val cargoMessage = CargoMessage("RelaynetP".toByteArray()) | ||
|
||
assertNull(cargoMessage.type) | ||
} | ||
|
||
@Test | ||
fun `Invalid messages should not be assigned a type`() { | ||
val cargoMessage = CargoMessage("RelaynetyP0".toByteArray()) | ||
|
||
assertNull(cargoMessage.type) | ||
} | ||
} | ||
} |
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