Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,46 @@
t.Errorf("expected %x got %x", ubytes, unsigned)
}
}


Check failure on line 161 in common/math/big_test.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.23.x, ubuntu-latest)

File is not properly formatted (goimports)
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)
}
})
}
}
2 changes: 1 addition & 1 deletion common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions common/math/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package math

import (
"bytes"
"math"
"testing"
)
Expand Down Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
37 changes: 37 additions & 0 deletions p2p/nat/nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion p2p/nat/natpmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions p2p/nat/natupnp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:<nil>"), // 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())
}
Expand Down
Loading