Skip to content

Commit

Permalink
PaxePacketTest fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
simbo1905 committed Jan 1, 2025
1 parent 59338bb commit 1fd3956
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
12 changes: 10 additions & 2 deletions nodes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fast tests
mvn -DNO_LOGGING=true -B package
# tests
mvn -DNO_LOGGING=true -B clean package

# run just one test
mvn -DskipTests clean package && \
Expand All @@ -9,6 +9,14 @@ mvn -DskipTests clean package && \
mvn -DskipTests clean package && \
mvn test -pl trex-paxe

# Grab the files for LLM

rm all.java
find trex-lib/src/main/ trex-paxe/src/ -name \*.java | while read JAVA ; do echo "" >> all.java; \
echo $JAVA >> all.java; \
cat $JAVA >> all.java ;\
done;

# jshell

jshell --enable-preview --class-path ./trex-locks/target/classes:./trex-locks/target/test-classes:./trex-lib/target/classes:./trex-lib/target/test-classes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.github.trex_paxos.paxe;

public record NodeId(byte value) implements Comparable<NodeId> {
public record NodeId(short value) implements Comparable<NodeId> {
public NodeId {
if (value < 0) throw new IllegalArgumentException("Node ID must be non-negative");
}

@Override
public int compareTo(NodeId other) {
return Byte.compare(value, other.value);
return Short.compare(value, other.value);
}

@Override
Expand Down
19 changes: 10 additions & 9 deletions trex-paxe/src/main/java/com/github/trex_paxos/paxe/PaxePacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public record PaxePacket(
byte[] authTag,
byte[] payload) {

public static final int HEADER_SIZE = 4; // from, to, channel, flags
public static final int HEADER_SIZE = 6; // from(2), to(2), channel(1), flags(1)
public static final int AUTHENCIATED_DATA_SIZE = 5; // from(2), to(2), channel(1)
public static final int NONCE_SIZE = 12;
public static final int AUTH_TAG_SIZE = 16;

public PaxePacket {
Objects.requireNonNull(from, "from cannot be null");
Objects.requireNonNull(to, "to cannot be null");
Expand All @@ -40,8 +41,8 @@ public record PaxePacket(
public byte[] toBytes() {
var size = HEADER_SIZE + NONCE_SIZE + AUTH_TAG_SIZE + payload.length;
var buffer = ByteBuffer.allocate(size);
buffer.put(from.value());
buffer.put(to.value());
buffer.putShort(from.value());
buffer.putShort(to.value());
buffer.put(channel.value());
buffer.put(flags);
buffer.put(nonce);
Expand All @@ -52,8 +53,8 @@ public byte[] toBytes() {

public static PaxePacket fromBytes(byte[] bytes) {
var buffer = ByteBuffer.wrap(bytes);
var from = new NodeId(buffer.get());
var to = new NodeId(buffer.get());
var from = new NodeId(buffer.getShort());
var to = new NodeId(buffer.getShort());
var channel = new Channel(buffer.get());
var flags = buffer.get();

Expand All @@ -70,9 +71,9 @@ public static PaxePacket fromBytes(byte[] bytes) {
}

public byte[] authenticatedData() {
var buffer = ByteBuffer.allocate(3);
buffer.put(from.value());
buffer.put(to.value());
var buffer = ByteBuffer.allocate(AUTHENCIATED_DATA_SIZE);
buffer.putShort(from.value());
buffer.putShort(to.value());
buffer.put(channel.value());
return buffer.array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PaxePacketTest {

@Test
void testConstructorAndGetters() {
NodeId from = new NodeId((byte) 1);
NodeId to = new NodeId((byte) 2);
NodeId from = new NodeId((short) 1);
NodeId to = new NodeId((short) 2);
Channel channel = new Channel((byte) 3);
byte flags = 0x04;
byte[] nonce = new byte[PaxePacket.NONCE_SIZE];
Expand All @@ -34,21 +34,21 @@ void testConstructorAndGetters() {
@Test
void testConstructorWithInvalidNonceSize() {
assertThrows(IllegalArgumentException.class, () ->
new PaxePacket(new NodeId((byte) 1), new NodeId((byte) 2), new Channel((byte) 3),
new PaxePacket(new NodeId((short) 1), new NodeId( (short)2), new Channel((byte) 3),
(byte) 0, new byte[PaxePacket.NONCE_SIZE - 1], new byte[PaxePacket.AUTH_TAG_SIZE], new byte[0]));
}

@Test
void testConstructorWithInvalidAuthTagSize() {
assertThrows(IllegalArgumentException.class, () ->
new PaxePacket(new NodeId((byte) 1), new NodeId((byte) 2), new Channel((byte) 3),
new PaxePacket(new NodeId((short) 1), new NodeId((short) 2), new Channel((byte) 3),
(byte) 0, new byte[PaxePacket.NONCE_SIZE], new byte[PaxePacket.AUTH_TAG_SIZE - 1], new byte[0]));
}

@Test
void testToBytes() {
NodeId from = new NodeId((byte) 1);
NodeId to = new NodeId((byte) 2);
NodeId from = new NodeId((short) 1);
NodeId to = new NodeId((short) 2);
Channel channel = new Channel((byte) 3);
byte flags = 0x04;
byte[] nonce = new byte[PaxePacket.NONCE_SIZE];
Expand All @@ -59,19 +59,19 @@ void testToBytes() {
byte[] bytes = packet.toBytes();

assertEquals(PaxePacket.HEADER_SIZE + PaxePacket.NONCE_SIZE + PaxePacket.AUTH_TAG_SIZE + payload.length, bytes.length);
assertEquals(from.value(), bytes[0]);
assertEquals(to.value(), bytes[1]);
assertEquals(channel.value(), bytes[2]);
assertEquals(flags, bytes[3]);
assertArrayEquals(nonce, Arrays.copyOfRange(bytes, 4, 4 + PaxePacket.NONCE_SIZE));
assertArrayEquals(authTag, Arrays.copyOfRange(bytes, 4 + PaxePacket.NONCE_SIZE, 4 + PaxePacket.NONCE_SIZE + PaxePacket.AUTH_TAG_SIZE));
assertArrayEquals(payload, Arrays.copyOfRange(bytes, 4 + PaxePacket.NONCE_SIZE + PaxePacket.AUTH_TAG_SIZE, bytes.length));
assertEquals((short) ((bytes[0] << 8) | (bytes[1] & 0xFF)), from.value());
assertEquals((short) ((bytes[2] << 8) | (bytes[3] & 0xFF)), to.value());
assertEquals(channel.value(), bytes[4]);
assertEquals(flags, bytes[5]);
assertArrayEquals(nonce, Arrays.copyOfRange(bytes, 6, 6 + PaxePacket.NONCE_SIZE));
assertArrayEquals(authTag, Arrays.copyOfRange(bytes, 6 + PaxePacket.NONCE_SIZE, 6 + PaxePacket.NONCE_SIZE + PaxePacket.AUTH_TAG_SIZE));
assertArrayEquals(payload, Arrays.copyOfRange(bytes, 6 + PaxePacket.NONCE_SIZE + PaxePacket.AUTH_TAG_SIZE, bytes.length));
}

@Test
void testFromBytes() {
NodeId from = new NodeId((byte) 1);
NodeId to = new NodeId((byte) 2);
NodeId from = new NodeId((short) 1);
NodeId to = new NodeId((short) 2);
Channel channel = new Channel((byte) 3);
byte flags = 0x04;
byte[] nonce = new byte[PaxePacket.NONCE_SIZE];
Expand All @@ -88,17 +88,19 @@ void testFromBytes() {

@Test
void testAuthenticatedData() {
NodeId from = new NodeId((byte) 1);
NodeId to = new NodeId((byte) 2);
NodeId from = new NodeId((short) 1);
NodeId to = new NodeId((short) 2);
Channel channel = new Channel((byte) 3);
PaxePacket packet = new PaxePacket(from, to, channel, (byte) 0, new byte[PaxePacket.NONCE_SIZE], new byte[PaxePacket.AUTH_TAG_SIZE], new byte[0]);

byte[] authenticatedData = packet.authenticatedData();

assertEquals(3, authenticatedData.length);
assertEquals(from.value(), authenticatedData[0]);
assertEquals(to.value(), authenticatedData[1]);
assertEquals(channel.value(), authenticatedData[2]);
assertEquals(PaxePacket.AUTHENCIATED_DATA_SIZE, authenticatedData.length);
assertEquals((byte) (from.value() >> 8), authenticatedData[0]);
assertEquals((byte) from.value(), authenticatedData[1]);
assertEquals((byte) (to.value() >> 8), authenticatedData[2]);
assertEquals((byte) to.value(), authenticatedData[3]);
assertEquals(channel.value(), authenticatedData[4]);
}

@Test
Expand All @@ -108,10 +110,10 @@ void testEncryptDecrypt() throws GeneralSecurityException {
SecretKey key = keyGen.generateKey();

// Create input data
NodeId from = new NodeId((byte) 1);
NodeId from = new NodeId((short) 1);
PaxeMessage originalMessage = new PaxeMessage(
from,
new NodeId((byte) 2),
new NodeId((short) 2),
new Channel((byte) 1),
"Hello, World!".getBytes()
);
Expand Down

0 comments on commit 1fd3956

Please sign in to comment.