Skip to content

Commit

Permalink
Fixed textfile packet dumper test
Browse files Browse the repository at this point in the history
  • Loading branch information
compscidr committed Aug 8, 2024
1 parent 65935d1 commit 4ce218f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.jasonernst.packetdumper.stringdumper.StringPacketDumper
import org.slf4j.LoggerFactory
import java.io.BufferedWriter
import java.io.File
import java.io.IOException
import java.nio.ByteBuffer
import java.time.LocalDateTime
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -42,7 +41,6 @@ class TextFilePacketDumper(
}
filename = "$path/${name}_${LocalDateTime.now()}.dump"
file = File(filename)
bufferedWriter = file.bufferedWriter()
logger.debug("TextFilePacketDumper opened file $filename")
isOpen.set(true)
}
Expand All @@ -52,16 +50,6 @@ class TextFilePacketDumper(
logger.error("Trying to close a file that is already closed")
return
}
try {
bufferedWriter.flush()
} catch (e: Exception) {
logger.error("Error flushing dump file", e)
}
try {
bufferedWriter.close()
} catch (e: Exception) {
logger.error("Error closing dump file", e)
}
isOpen.set(false)
}

Expand All @@ -80,16 +68,6 @@ class TextFilePacketDumper(
return
}
val output = stringDumper.dumpBufferToString(buffer, offset, length, addresses, etherType)
logger.debug("Intermediary output: $output")
try {
bufferedWriter.write(output)
bufferedWriter.write("\n")
bufferedWriter.flush()
} catch (e: IOException) {
if (!loggedError) {
logger.error("Error writing to dump file", e)
loggedError = true
}
}
file.writeText(output)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.jasonernst.packetdumper.EtherType
import com.jasonernst.packetdumper.stringdumper.StringPacketDumper
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import java.io.File
Expand Down Expand Up @@ -34,8 +33,7 @@ class TestTextFilePacketDumper {

// re-open the file
val file = File(dumper.filename)
val fileLines = file.readLines()
val readBuffer = ByteBuffer.wrap(fileLines.joinToString().toByteArray())
val text = file.readText()

if (etherType != null) {
TODO() // need to strip the dummy ethernet header
Expand All @@ -45,6 +43,9 @@ class TestTextFilePacketDumper {
TODO() // need to strip them
}

// 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())

return readBuffer
}

Expand All @@ -54,7 +55,7 @@ class TestTextFilePacketDumper {

// delete the file created for the test to cleanup
try {
// File(dumper.filename).delete()
File(dumper.filename).delete()
logger.debug("Deleted file ${dumper.filename}")
} catch (e: Exception) {
// ignore
Expand All @@ -66,12 +67,24 @@ class TestTextFilePacketDumper {
dumper.close()
}

@Disabled("This is broken atm")
/**
* Test writing and reading from a file without using the dumper, just as a sanity check.
*/
@Test fun testFileStringWriteRead() {
dumper.open()
val file = File(dumper.filename)
file.writeText("Hello, World!")
file.readText().also {
assertEquals("Hello, World!", it)
}
file.delete()
}

@Test
fun testDumpBuffer() {
dumper.open()
val stringDumper = StringPacketDumper()
val buffer = ByteBuffer.wrap("Hello, World!".toByteArray())
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())}")
Expand Down

0 comments on commit 4ce218f

Please sign in to comment.