From 0f2c4816e8489a8695a3b9769dc73682e9a5c96e Mon Sep 17 00:00:00 2001 From: twystd Date: Fri, 21 Jun 2024 09:38:45 -0700 Subject: [PATCH] Removed extraneous ASCII from dumped messages --- Makefile | 2 +- TODO.md | 4 +-- messages/message.go | 25 +++++++++++++--- messages/message_test.go | 63 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 messages/message_test.go diff --git a/Makefile b/Makefile index d487577..5d991be 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ test: build go test ./... benchmark: build - go test -bench ./... + go test -bench=. ./... coverage: build go test -cover ./... diff --git a/TODO.md b/TODO.md index 630bc5f..1782fb4 100644 --- a/TODO.md +++ b/TODO.md @@ -8,9 +8,9 @@ - [ ] Rework any remaining Time pointers - (?) Replace (* UHPPOTE) in API functions with non-pointer version - [ ] `dump` - - [ ] Remove extraneous ASCII + - [x] Remove extraneous ASCII - [ ] Remove trailing spaces - - [ ] Identify UDP/TCP packets +- [ ] logging: identify UDP/TCP packets ## TODO diff --git a/messages/message.go b/messages/message.go index b4333ec..32e1ecf 100644 --- a/messages/message.go +++ b/messages/message.go @@ -1,8 +1,8 @@ package messages import ( - "encoding/hex" - "regexp" + "fmt" + "strings" ) type Request interface { @@ -12,7 +12,24 @@ type Response interface { } func dump(m []byte, prefix string) string { - regex := regexp.MustCompile("(?m)^(.*)") + var b strings.Builder - return regex.ReplaceAllString(hex.Dump(m), prefix+"$1") + for ix := 0; ix < len(m); ix += 16 { + chunk := m[ix:] + + fmt.Fprintf(&b, "%s%08x ", prefix, ix) + for i := 0; i < 8 && i < len(chunk); i++ { + fmt.Fprintf(&b, " %02x", chunk[i]) + } + + fmt.Fprintf(&b, " ") + for i := 8; i < 16 && i < len(chunk); i++ { + fmt.Fprintf(&b, " %02x", chunk[i]) + } + + fmt.Fprintln(&b) + } + + return b.String() } + diff --git a/messages/message_test.go b/messages/message_test.go new file mode 100644 index 0000000..0d1e74a --- /dev/null +++ b/messages/message_test.go @@ -0,0 +1,63 @@ +package messages + +import ( + "math/rand" + "testing" + "time" +) + +func TestDump(t *testing.T) { + message := []byte{ + 0x17, 0x20, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x39, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x01, + 0xaa, 0xe8, 0x5d, 0x00, 0x20, 0x19, 0x04, 0x19, 0x17, 0x00, 0x09, 0x06, 0x01, 0x00, 0x01, 0x01, + 0x00, 0x00, 0x01, 0x01, 0x09, 0x14, 0x37, 0x02, 0x11, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x2b, 0x04, 0x01, 0x19, 0x04, 0x20, 0x00, 0x00, 0x93, 0x26, 0x04, 0x88, 0x08, 0x92, 0x00, 0x00, + } + + expected := + `00000000 17 20 00 00 2d 55 39 19 39 00 00 00 01 00 03 01 +00000010 aa e8 5d 00 20 19 04 19 17 00 09 06 01 00 01 01 +00000020 00 00 01 01 09 14 37 02 11 00 00 00 21 00 00 00 +00000030 2b 04 01 19 04 20 00 00 93 26 04 88 08 92 00 00 +` + + s := dump(message, "") + + if s != expected { + t.Errorf("incorrectly 'dumped' message\n expected:\n%v\n got:\n%v", expected, s) + } +} + +func TestDumpWithPrefix(t *testing.T) { + message := []byte{ + 0x17, 0x20, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x39, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x01, + 0xaa, 0xe8, 0x5d, 0x00, 0x20, 0x19, 0x04, 0x19, 0x17, 0x00, 0x09, 0x06, 0x01, 0x00, 0x01, 0x01, + 0x00, 0x00, 0x01, 0x01, 0x09, 0x14, 0x37, 0x02, 0x11, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x2b, 0x04, 0x01, 0x19, 0x04, 0x20, 0x00, 0x00, 0x93, 0x26, 0x04, 0x88, 0x08, 0x92, 0x00, 0x00, + } + + expected := + ` 00000000 17 20 00 00 2d 55 39 19 39 00 00 00 01 00 03 01 + 00000010 aa e8 5d 00 20 19 04 19 17 00 09 06 01 00 01 01 + 00000020 00 00 01 01 09 14 37 02 11 00 00 00 21 00 00 00 + 00000030 2b 04 01 19 04 20 00 00 93 26 04 88 08 92 00 00 +` + + s := dump(message, " ") + + if s != expected { + t.Errorf("incorrectly 'dumped' message\n expected:\n%v\n got:\n%v", expected, s) + } +} + +func BenchmarkDump(b *testing.B) { + m := make([]byte, 64) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + + r.Read(m) + + for n := 0; n < b.N; n++ { + dump(m, "") + } +} +