From 162d593514571d0de479906c37a60eb90fa99db0 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 18 Jun 2024 21:36:25 -0700 Subject: [PATCH] net/flowtrack: fix, test String method I meant to do this in the earlier change and had a git fail. To atone, add a test too while I'm here. Updates #12486 Updates #12507 Change-Id: I4943b454a2530cb5047636f37136aa2898d2ffc7 Signed-off-by: Brad Fitzpatrick --- net/flowtrack/flowtrack.go | 4 +++- net/flowtrack/flowtrack_test.go | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/net/flowtrack/flowtrack.go b/net/flowtrack/flowtrack.go index 980e35445..8b3d799f7 100644 --- a/net/flowtrack/flowtrack.go +++ b/net/flowtrack/flowtrack.go @@ -56,7 +56,9 @@ func (t Tuple) SrcPort() uint16 { return t.srcPort } func (t Tuple) DstPort() uint16 { return t.dstPort } func (t Tuple) String() string { - return fmt.Sprintf("(%v %v => %v)", t.proto, t.src, t.dst) + return fmt.Sprintf("(%v %v => %v)", t.proto, + netip.AddrPortFrom(t.SrcAddr(), t.srcPort), + netip.AddrPortFrom(t.DstAddr(), t.dstPort)) } func (t Tuple) MarshalJSON() ([]byte, error) { diff --git a/net/flowtrack/flowtrack_test.go b/net/flowtrack/flowtrack_test.go index c9289c618..1a13f7753 100644 --- a/net/flowtrack/flowtrack_test.go +++ b/net/flowtrack/flowtrack_test.go @@ -100,10 +100,15 @@ func BenchmarkMapKeys(b *testing.B) { }) } -func TestJSON(t *testing.T) { +func TestStringJSON(t *testing.T) { v := MakeTuple(123, netip.MustParseAddrPort("1.2.3.4:5"), netip.MustParseAddrPort("6.7.8.9:10")) + + if got, want := v.String(), "(IPProto-123 1.2.3.4:5 => 6.7.8.9:10)"; got != want { + t.Errorf("String = %q; want %q", got, want) + } + got, err := json.Marshal(v) if err != nil { t.Fatal(err)