From 021d804fa40abde592ea2ec4513d9bda425603c2 Mon Sep 17 00:00:00 2001 From: clonemycode Date: Mon, 2 Jun 2025 02:03:41 +0800 Subject: [PATCH] refactor: replace []byte(fmt.Sprintf) with fmt.Appendf Signed-off-by: clonemycode --- common/math/big.go | 2 +- common/math/big_test.go | 43 +++++++++++++++++++++++++++++++++++++ common/math/integer.go | 2 +- common/math/integer_test.go | 38 ++++++++++++++++++++++++++++++++ p2p/nat/nat.go | 2 +- p2p/nat/nat_test.go | 37 +++++++++++++++++++++++++++++++ p2p/nat/natpmp.go | 2 +- p2p/nat/natupnp_test.go | 37 +++++++++++++++++++++++++++++++ 8 files changed, 159 insertions(+), 4 deletions(-) diff --git a/common/math/big.go b/common/math/big.go index 825f4baec9..493c2b7861 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -72,7 +72,7 @@ func (i *HexOrDecimal256) MarshalText() ([]byte, error) { if i == nil { return []byte("0x0"), nil } - return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil + return fmt.Appendf(nil, "%#x", (*big.Int)(i)), nil } // Decimal256 unmarshals big.Int as a decimal string. When unmarshalling, diff --git a/common/math/big_test.go b/common/math/big_test.go index 6f701b621a..7f6aa37d6b 100644 --- a/common/math/big_test.go +++ b/common/math/big_test.go @@ -157,3 +157,46 @@ func TestU256Bytes(t *testing.T) { t.Errorf("expected %x got %x", ubytes, unsigned) } } + + +func TestHexOrDecimal256_MarshalText(t *testing.T) { + tests := []struct { + name string + input *HexOrDecimal256 + want []byte + wantErr bool + }{ + { + name: "nil input", + input: nil, + want: []byte("0x0"), + }, + { + name: "zero value", + input: (*HexOrDecimal256)(big.NewInt(0)), + want: []byte("0x0"), + }, + { + name: "positive value", + input: (*HexOrDecimal256)(big.NewInt(0x123abc)), + want: []byte("0x123abc"), + }, + { + name: "large positive value", + input: (*HexOrDecimal256)(new(big.Int).SetBytes(bytes.Repeat([]byte{0xff}, 32))), + want: []byte("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.input.MarshalText() + if (err != nil) != tt.wantErr { + t.Errorf("HexOrDecimal256.MarshalText() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !bytes.Equal(got, tt.want) { + t.Errorf("HexOrDecimal256.MarshalText() = %s, want %s", got, tt.want) + } + }) + } +} diff --git a/common/math/integer.go b/common/math/integer.go index c125d9d5d4..883fe23adf 100644 --- a/common/math/integer.go +++ b/common/math/integer.go @@ -48,7 +48,7 @@ func (i *HexOrDecimal64) UnmarshalText(input []byte) error { // MarshalText implements encoding.TextMarshaler. func (i HexOrDecimal64) MarshalText() ([]byte, error) { - return []byte(fmt.Sprintf("%#x", uint64(i))), nil + return fmt.Appendf(nil, "%#x", uint64(i)), nil } // ParseUint64 parses s as an integer in decimal or hexadecimal syntax. diff --git a/common/math/integer_test.go b/common/math/integer_test.go index 4643a43f20..62218a75bd 100644 --- a/common/math/integer_test.go +++ b/common/math/integer_test.go @@ -17,6 +17,7 @@ package math import ( + "bytes" "math" "testing" ) @@ -115,3 +116,40 @@ func TestMustParseUint64Panic(t *testing.T) { }() MustParseUint64("ggg") } + +func TestHexOrDecimal64_MarshalText(t *testing.T) { + tests := []struct { + name string + input HexOrDecimal64 + want []byte + wantErr bool + }{ + { + name: "zero value", + input: HexOrDecimal64(0), + want: []byte("0x0"), + }, + { + name: "positive value", + input: HexOrDecimal64(0x123abc), + want: []byte("0x123abc"), + }, + { + name: "max uint64 value", + input: HexOrDecimal64(math.MaxUint64), + want: []byte("0xffffffffffffffff"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.input.MarshalText() + if (err != nil) != tt.wantErr { + t.Errorf("HexOrDecimal64.MarshalText() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !bytes.Equal(got, tt.want) { + t.Errorf("HexOrDecimal64.MarshalText() = %s, want %s", got, tt.want) + } + }) + } +} diff --git a/p2p/nat/nat.go b/p2p/nat/nat.go index d9698b1428..e8ce30f9ef 100644 --- a/p2p/nat/nat.go +++ b/p2p/nat/nat.go @@ -142,7 +142,7 @@ type ExtIP net.IP func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil } func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) } -func (n ExtIP) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("extip:%v", net.IP(n))), nil } +func (n ExtIP) MarshalText() ([]byte, error) { return fmt.Appendf(nil, "extip:%v", net.IP(n)), nil } // These do nothing. diff --git a/p2p/nat/nat_test.go b/p2p/nat/nat_test.go index 8dd5644fd6..a725aa80ed 100644 --- a/p2p/nat/nat_test.go +++ b/p2p/nat/nat_test.go @@ -84,3 +84,40 @@ func TestParseStun(t *testing.T) { assert.Equal(t, stun.serverList, tc.want.serverList) } } + +func TestExtIP_MarshalText(t *testing.T) { + tests := []struct { + name string + input ExtIP + want []byte + wantErr bool + }{ + { + name: "valid ipv4", + input: ExtIP{192, 168, 1, 1}, + want: []byte("extip:192.168.1.1"), + }, + { + name: "valid ipv6", + input: ExtIP(net.ParseIP("2001:db8::68")), // Create ExtIP from net.IP + want: []byte("extip:2001:db8::68"), + }, + { + name: "zero ip", + input: ExtIP{0, 0, 0, 0}, + want: []byte("extip:0.0.0.0"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.input.MarshalText() + if (err != nil) != tt.wantErr { + t.Errorf("ExtIP.MarshalText() error = %v, wantErr %v", err, tt.wantErr) + return + } + if string(got) != string(tt.want) { // Compare as strings for better error messages with IPs + t.Errorf("ExtIP.MarshalText() = %s, want %s", got, tt.want) + } + }) + } +} diff --git a/p2p/nat/natpmp.go b/p2p/nat/natpmp.go index a34c103e32..b56eb4ff47 100644 --- a/p2p/nat/natpmp.go +++ b/p2p/nat/natpmp.go @@ -72,7 +72,7 @@ func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) { } func (n *pmp) MarshalText() ([]byte, error) { - return []byte(fmt.Sprintf("natpmp:%v", n.gw)), nil + return fmt.Appendf(nil, "natpmp:%v", n.gw), nil } func discoverPMP() Interface { diff --git a/p2p/nat/natupnp_test.go b/p2p/nat/natupnp_test.go index 9072451d50..d3a516e3ee 100644 --- a/p2p/nat/natupnp_test.go +++ b/p2p/nat/natupnp_test.go @@ -222,6 +222,43 @@ func (dev *fakeIGD) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func TestPMP_MarshalText(t *testing.T) { + tests := []struct { + name string + input *pmp + want []byte + wantErr bool + }{ + { + name: "valid pmp with gateway IP", + input: &pmp{gw: net.ParseIP("192.168.1.254")}, + want: []byte("natpmp:192.168.1.254"), + }, + { + name: "pmp with nil gateway IP", + input: &pmp{gw: nil}, + want: []byte("natpmp:"), // Behavior of net.IP.String() for nil IP + }, + { + name: "pmp with zero gateway IP", + input: &pmp{gw: net.IPv4zero}, + want: []byte("natpmp:0.0.0.0"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.input.MarshalText() + if (err != nil) != tt.wantErr { + t.Errorf("pmp.MarshalText() error = %v, wantErr %v", err, tt.wantErr) + return + } + if string(got) != string(tt.want) { + t.Errorf("pmp.MarshalText() = %s, want %s", got, tt.want) + } + }) + } +} + func (dev *fakeIGD) replaceListenAddr(resp string) string { return strings.ReplaceAll(resp, "{{listenAddr}}", dev.listener.Addr().String()) }