Skip to content

Commit

Permalink
made the ethernet header fields accessible and added some tests for e…
Browse files Browse the repository at this point in the history
…thernet header
  • Loading branch information
compscidr committed Aug 14, 2024
1 parent 47c4123 commit 625b1d9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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()))
}
}

0 comments on commit 625b1d9

Please sign in to comment.