-
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(RAMF): Implement payload unwrapping (#68)
- Loading branch information
Showing
19 changed files
with
275 additions
and
105 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
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/EmptyPayloadPlaintext.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,28 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import tech.relaycorp.relaynet.ramf.RAMFException | ||
|
||
/** | ||
* Empty payload plaintext. | ||
*/ | ||
class EmptyPayloadPlaintext : PayloadPlaintext { | ||
/** | ||
* Serialize empty payload plaintext. | ||
*/ | ||
override fun serialize() = ByteArray(0) | ||
|
||
companion object { | ||
/** | ||
* Deserialize empty payload plaintext. | ||
* | ||
* @throws RAMFException if `serialization` is not empty | ||
*/ | ||
@Throws(RAMFException::class) | ||
fun deserialize(serialization: ByteArray): EmptyPayloadPlaintext { | ||
if (serialization.isNotEmpty()) { | ||
throw RAMFException("Payload is not empty") | ||
} | ||
return EmptyPayloadPlaintext() | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/tech/relaycorp/relaynet/messages/payloads/ServiceMessage.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,7 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
class ServiceMessage : PayloadPlaintext { | ||
override fun serialize(): ByteArray { | ||
TODO("Not yet implemented") | ||
} | ||
} |
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
35 changes: 16 additions & 19 deletions
35
src/test/kotlin/tech/relaycorp/relaynet/messages/CargoCollectionAuthorizationTest.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 |
---|---|---|
@@ -1,27 +1,24 @@ | ||
package tech.relaycorp.relaynet.messages | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.TestFactory | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageCompanionTests | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageConstructorTests | ||
import tech.relaycorp.relaynet.CERTIFICATE | ||
import tech.relaycorp.relaynet.ramf.RAMFSerializationTestCase | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import kotlin.test.Test | ||
|
||
class CargoCollectionAuthorizationTest { | ||
@TestFactory | ||
fun makeConstructorTests() = | ||
makeRAMFMessageConstructorTests( | ||
::CargoCollectionAuthorization, | ||
{ r: String, p: ByteArray, s: Certificate -> CargoCollectionAuthorization(r, p, s) }, | ||
0x44, | ||
0x00 | ||
internal class CargoCollectionAuthorizationTest : | ||
RAMFSerializationTestCase<CargoCollectionAuthorization>( | ||
::CargoCollectionAuthorization, | ||
{ r: String, p: ByteArray, s: Certificate -> CargoCollectionAuthorization(r, p, s) }, | ||
0x44, | ||
0x00, | ||
CargoCollectionAuthorization.Companion | ||
) { | ||
@Test | ||
fun `Payload deserialization should be delegated to EmptyPayloadPlaintext`() { | ||
val cca = CargoCollectionAuthorization( | ||
"https://gb.relaycorp.tech", "".toByteArray(), CERTIFICATE | ||
) | ||
|
||
@Nested | ||
inner class Companion { | ||
@TestFactory | ||
fun makeDeserializationTests() = makeRAMFMessageCompanionTests( | ||
CargoCollectionAuthorization.Companion, | ||
::CargoCollectionAuthorization | ||
) | ||
cca.deserializePayload("".toByteArray()) | ||
} | ||
} |
38 changes: 22 additions & 16 deletions
38
src/test/kotlin/tech/relaycorp/relaynet/messages/CargoTest.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 |
---|---|---|
@@ -1,23 +1,29 @@ | ||
package tech.relaycorp.relaynet.messages | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.TestFactory | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageCompanionTests | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageConstructorTests | ||
import tech.relaycorp.relaynet.CERTIFICATE | ||
import tech.relaycorp.relaynet.messages.payloads.CargoMessageSet | ||
import tech.relaycorp.relaynet.ramf.RAMFSerializationTestCase | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CargoTest { | ||
@TestFactory | ||
fun makeConstructorTests() = makeRAMFMessageConstructorTests( | ||
::Cargo, | ||
{ r: String, p: ByteArray, s: Certificate -> Cargo(r, p, s) }, | ||
0x43, | ||
0x00 | ||
) | ||
internal class CargoTest : RAMFSerializationTestCase<Cargo>( | ||
::Cargo, | ||
{ r: String, p: ByteArray, s: Certificate -> Cargo(r, p, s) }, | ||
0x43, | ||
0x00, | ||
Cargo.Companion | ||
) { | ||
@Test | ||
fun `Payload deserialization should be delegated to CargoMessageSet`() { | ||
val cargoMessageSet = CargoMessageSet(arrayOf("msg1".toByteArray(), "msg2".toByteArray())) | ||
val cargo = Cargo("https://gb.relaycorp.tech", "".toByteArray(), CERTIFICATE) | ||
|
||
@Nested | ||
inner class Companion { | ||
@TestFactory | ||
fun makeDeserializationTests() = makeRAMFMessageCompanionTests(Cargo.Companion, ::Cargo) | ||
val payloadDeserialized = cargo.deserializePayload(cargoMessageSet.serialize()) | ||
|
||
assertEquals( | ||
cargoMessageSet.messages.map { it.asList() }, | ||
payloadDeserialized.messages.map { it.asList() } | ||
) | ||
} | ||
} |
32 changes: 16 additions & 16 deletions
32
src/test/kotlin/tech/relaycorp/relaynet/messages/ParcelTest.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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package tech.relaycorp.relaynet.messages | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.TestFactory | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageCompanionTests | ||
import tech.relaycorp.relaynet.ramf.makeRAMFMessageConstructorTests | ||
import org.junit.jupiter.api.assertThrows | ||
import tech.relaycorp.relaynet.CERTIFICATE | ||
import tech.relaycorp.relaynet.ramf.RAMFSerializationTestCase | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import kotlin.test.Test | ||
|
||
class ParcelTest { | ||
@TestFactory | ||
fun makeConstructorTests() = makeRAMFMessageConstructorTests( | ||
::Parcel, | ||
{ r: String, p: ByteArray, s: Certificate -> Parcel(r, p, s) }, | ||
0x50, | ||
0x00 | ||
) | ||
internal class ParcelTest : RAMFSerializationTestCase<Parcel>( | ||
::Parcel, | ||
{ r: String, p: ByteArray, s: Certificate -> Parcel(r, p, s) }, | ||
0x50, | ||
0x00, | ||
Parcel.Companion | ||
) { | ||
@Test | ||
fun `Payload deserialization should be delegated to ServiceMessage`() { | ||
val parcel = Parcel("https://gb.relaycorp.tech", "".toByteArray(), CERTIFICATE) | ||
|
||
@Nested | ||
inner class Companion { | ||
@TestFactory | ||
fun makeDeserializationTests() = makeRAMFMessageCompanionTests(Parcel.Companion, ::Parcel) | ||
// TODO | ||
assertThrows<NotImplementedError> { parcel.deserializePayload("invalid".toByteArray()) } | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/kotlin/tech/relaycorp/relaynet/messages/payloads/EmptyPayloadPlaintextTest.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,36 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.assertThrows | ||
import tech.relaycorp.relaynet.ramf.RAMFException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
internal class EmptyPayloadPlaintextTest { | ||
@Nested | ||
inner class Serialize { | ||
@Test | ||
fun `An empty ByteArray should be returned`() { | ||
val payloadPlaintext = EmptyPayloadPlaintext() | ||
|
||
assertEquals(0, payloadPlaintext.serialize().size) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class Deserialize { | ||
@Test | ||
fun `An empty buffer should be accepted`() { | ||
EmptyPayloadPlaintext.deserialize(ByteArray(0)) | ||
} | ||
|
||
@Test | ||
fun `An error should be thrown if buffer is not empty`() { | ||
val exception = assertThrows<RAMFException> { | ||
EmptyPayloadPlaintext.deserialize("a".toByteArray()) | ||
} | ||
|
||
assertEquals("Payload is not empty", exception.message) | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/kotlin/tech/relaycorp/relaynet/messages/payloads/ServiceMessageTest.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,12 @@ | ||
package tech.relaycorp.relaynet.messages.payloads | ||
|
||
import org.junit.jupiter.api.assertThrows | ||
import kotlin.test.Test | ||
|
||
internal class ServiceMessageTest { | ||
@Test | ||
fun deserialize() { | ||
// TODO | ||
assertThrows<NotImplementedError> { ServiceMessage().serialize() } | ||
} | ||
} |
Oops, something went wrong.