-
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.
made the ethernet header fields accessible and added some tests for e…
…thernet header
- Loading branch information
Showing
3 changed files
with
66 additions
and
3 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
32 changes: 32 additions & 0 deletions
32
packetdumper/src/test/kotlin/com/jasonernst/packetdumper/ethernet/TestEthernetHeader.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,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())) | ||
} | ||
} |