Skip to content

Commit

Permalink
Added remaining text file tests and made some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
compscidr committed Aug 8, 2024
1 parent 4ce218f commit 2552708
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@ class StringPacketDumper(

var totalLength = 0
if (etherType == null) {
buffer.position(offset)
totalLength = minOf(length, buffer.remaining())
conversionBuffer.position(offset)
totalLength = minOf(length, conversionBuffer.remaining())
if (totalLength < length) {
logger.warn("Trying to dump more bytes than are in the buffer. Dumping up to buffer limit.")
}
}

val output = StringBuilder()
var i = 0
while (buffer.hasRemaining()) {
while (conversionBuffer.hasRemaining()) {
if (addresses && (i % 16 == 0)) {
output.append(String.format("%08X ", i))
}
output.append(String.format("%02X", buffer.get()))
output.append(String.format("%02X", conversionBuffer.get()))
i++
if (i % 16 == 0) {
output.append("\n")
} else if (buffer.hasRemaining()) {
} else if (conversionBuffer.hasRemaining()) {
output.append(" ")
}
}
buffer.position(startingPosition)
conversionBuffer.position(startingPosition)
return output.toString()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jasonernst.packetdumper.filedumper

import com.jasonernst.packetdumper.EtherType
import com.jasonernst.packetdumper.EthernetHeader
import com.jasonernst.packetdumper.stringdumper.StringPacketDumper
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
Expand Down Expand Up @@ -28,24 +29,42 @@ class TestTextFilePacketDumper {
addresses: Boolean = false,
etherType: EtherType? = null,
): ByteBuffer {
val stringDumper = StringPacketDumper()
logger.debug("write buffer: ${stringDumper.dumpBufferToString(buffer, 0, buffer.limit())}")
dumper.dumpBuffer(buffer, 0, buffer.limit(), addresses, etherType)
dumper.close()

// re-open the file
val file = File(dumper.filename)
val text = file.readText()
var text = file.readText()
logger.debug("raw text: $text")

if (etherType != null) {
TODO() // need to strip the dummy ethernet header
if (addresses) {
val lines = text.split("\n")
text = ""
for (line in lines) {
text += line.drop(10) + " "
}
text = text.trimEnd() // remove the trailing space
logger.debug("No address text: $text")
}

if (addresses) {
TODO() // need to strip them
if (etherType != null) {
// note: we multiple by 3, because each byte is represented by two hex characters and a
// space character
// EthernetHeader.ETHERNET_HEADER_LENGTH.toInt() * 3
text = text.drop(42)
//text = text.trimEnd() // remove the trailing space
logger.debug("No ether type text: $text")
}

// remove any remaining newlines
text = text.replace("\n", " ")

// each space separated value is a hex value, so we need to turn it back into bytes
val readBuffer = ByteBuffer.wrap(text.split(" ").map { it.toInt(16).toByte() }.toByteArray())

logger.debug("read buffer: ${stringDumper.dumpBufferToString(readBuffer, 0, readBuffer.limit())}")
return readBuffer
}

Expand Down Expand Up @@ -83,11 +102,35 @@ class TestTextFilePacketDumper {
@Test
fun testDumpBuffer() {
dumper.open()
val stringDumper = StringPacketDumper()
val buffer = ByteBuffer.wrap(byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08))
logger.debug("write buffer: ${stringDumper.dumpBufferToString(buffer, 0, buffer.limit())}")
val readBuffer = writeReadBuffer(buffer)
logger.debug("read buffer: ${stringDumper.dumpBufferToString(readBuffer, 0, readBuffer.limit())}")
assertEquals(buffer, readBuffer)
}

@Test
fun testDumpBufferWithAddresses() {
dumper.open()
val buffer =
ByteBuffer.wrap(
byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10),
)
val readBuffer = writeReadBuffer(buffer, addresses = true)
assertEquals(buffer, readBuffer)
}

@Test
fun testDumpBufferWithEtherType() {
dumper.open()
val buffer = ByteBuffer.wrap(byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08))
val readBuffer = writeReadBuffer(buffer, etherType = EtherType.IPv4)
assertEquals(buffer, readBuffer)
}

@Test
fun testDumpBufferWithAddressesAndEtherType() {
dumper.open()
val buffer = ByteBuffer.wrap(byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08))
val readBuffer = writeReadBuffer(buffer, addresses = true, etherType = EtherType.IPv4)
assertEquals(buffer, readBuffer)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jasonernst.packetdumper.stringdumper

import com.jasonernst.packetdumper.EtherType
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.nio.ByteBuffer
Expand Down Expand Up @@ -656,4 +657,16 @@ class TestStringPacketDumper {
// ensure that the buffer position is not changed
assertEquals(0, buffer.position())
}

@Test fun dummyHeader() {
val buffer = ByteBuffer.wrap(byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04))
val hexString = stringPacketDumper.dumpBufferToString(buffer, 0, buffer.limit(), false, EtherType.IPv4)
assertEquals("14 C0 3E 55 0B 35 74 D0 2B 29 A5 18 08 00 00 01\n02 03 04", hexString)
}

@Test fun dummyHeaderWithAddress() {
val buffer = ByteBuffer.wrap(byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04))
val hexString = stringPacketDumper.dumpBufferToString(buffer, 0, buffer.limit(), true, EtherType.IPv4)
assertEquals("00000000 14 C0 3E 55 0B 35 74 D0 2B 29 A5 18 08 00 00 01\n00000010 02 03 04", hexString)
}
}

0 comments on commit 2552708

Please sign in to comment.