Skip to content

Commit

Permalink
Removed extraneous ASCII from dumped messages
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jun 21, 2024
1 parent 61ff482 commit 0f2c481
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test: build
go test ./...

benchmark: build
go test -bench ./...
go test -bench=. ./...

coverage: build
go test -cover ./...
Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 21 additions & 4 deletions messages/message.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package messages

import (
"encoding/hex"
"regexp"
"fmt"
"strings"
)

type Request interface {
Expand All @@ -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()
}

63 changes: 63 additions & 0 deletions messages/message_test.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
}

0 comments on commit 0f2c481

Please sign in to comment.