diff --git a/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/EthernetHeader.kt b/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/EthernetHeader.kt index 96c49d9..dbc745a 100644 --- a/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/EthernetHeader.kt +++ b/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/EthernetHeader.kt @@ -11,9 +11,9 @@ import java.nio.ByteBuffer * https://en.wikipedia.org/wiki/Ethernet_frame */ class EthernetHeader( - private var destination: MacAddress, - private var source: MacAddress, - private var type: EtherType, + val destination: MacAddress, + val source: MacAddress, + val type: EtherType, ) { companion object { const val IP4_VERSION: UByte = 4u @@ -90,4 +90,22 @@ class EthernetHeader( } override fun toString(): String = "EthernetHeader(destination=$destination, source=$source, type=$type, size=${size()})" + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is EthernetHeader) return false + + if (destination != other.destination) return false + if (source != other.source) return false + if (type != other.type) return false + + return true + } + + override fun hashCode(): Int { + var result = destination.hashCode() + result = 31 * result + source.hashCode() + result = 31 * result + type.hashCode() + return result + } } diff --git a/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/MacAddress.kt b/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/MacAddress.kt index d960644..516a05e 100644 --- a/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/MacAddress.kt +++ b/packetdumper/src/main/kotlin/com/jasonernst/packetdumper/ethernet/MacAddress.kt @@ -35,4 +35,17 @@ class MacAddress { val DUMMY_MAC_SOURCE = MacAddress("14:c0:3e:55:0b:35") val DUMMY_MAC_DEST = MacAddress("74:d0:2b:29:a5:18") } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || this::class != other::class) return false + + other as MacAddress + + return bytes.contentEquals(other.bytes) + } + + override fun hashCode(): Int { + return bytes.contentHashCode() + } } diff --git a/packetdumper/src/test/kotlin/com/jasonernst/packetdumper/ethernet/TestEthernetHeader.kt b/packetdumper/src/test/kotlin/com/jasonernst/packetdumper/ethernet/TestEthernetHeader.kt new file mode 100644 index 0000000..e547573 --- /dev/null +++ b/packetdumper/src/test/kotlin/com/jasonernst/packetdumper/ethernet/TestEthernetHeader.kt @@ -0,0 +1,32 @@ +package com.jasonernst.packetdumper.ethernet + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import java.nio.ByteBuffer + +class TestEthernetHeader { + @Test fun serialization() { + val header = EthernetHeader.dummyEthernet(EtherType.IPv4) + val buffer = ByteBuffer.wrap(header.toBytes()) + val parsedHeader = EthernetHeader.fromStream(buffer) + assertEquals(header, parsedHeader) + assertEquals(parsedHeader.size(), EthernetHeader.ETHERNET_HEADER_LENGTH) + } + + @Test fun etherTypeDetection() { + val ipv4Buffer = ByteBuffer.allocate(1) + ipv4Buffer.put(0, EthernetHeader.IP4_VERSION.toByte()) + ipv4Buffer.rewind() + assertEquals(EtherType.IPv4, EthernetHeader.getEtherTypeFromIPVersionByte(ipv4Buffer.get().toUByte())) + + val ipv6Buffer = ByteBuffer.allocate(1) + ipv6Buffer.put(0, EthernetHeader.IP6_VERSION.toByte()) + ipv6Buffer.rewind() + assertEquals(EtherType.IPv6, EthernetHeader.getEtherTypeFromIPVersionByte(ipv6Buffer.get().toUByte())) + + val unknownBuffer = ByteBuffer.allocate(1) + unknownBuffer.put(0, 0.toByte()) + unknownBuffer.rewind() + assertEquals(EtherType.DETECT, EthernetHeader.getEtherTypeFromIPVersionByte(unknownBuffer.get().toUByte())) + } +} \ No newline at end of file