2018-03-09 15:55:25 +00:00
|
|
|
package payload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2018-03-09 15:55:25 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testserdes"
|
2020-05-22 09:59:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-08 10:26:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeDecodeAddress(t *testing.T) {
|
|
|
|
var (
|
2022-06-30 13:18:41 +00:00
|
|
|
e, _ = net.ResolveTCPAddr("tcp", "localhost:2000")
|
2019-08-29 15:39:46 +00:00
|
|
|
ts = time.Now()
|
2020-05-22 09:59:18 +00:00
|
|
|
addr = NewAddressAndTime(e, ts, capability.Capabilities{
|
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: uint16(e.Port)},
|
|
|
|
},
|
|
|
|
})
|
2018-03-09 15:55:25 +00:00
|
|
|
)
|
|
|
|
|
2019-08-29 15:39:46 +00:00
|
|
|
assert.Equal(t, ts.UTC().Unix(), int64(addr.Timestamp))
|
network: fix Address test failing on Windows/macOS with go 1.20
See the trace:
```
2023-03-31T07:46:21.1886260Z === RUN TestEncodeDecodeAddress
2023-03-31T07:46:21.1886420Z address_test.go:30:
2023-03-31T07:46:21.1887000Z Error Trace: /Users/runner/work/neo-go/neo-go/pkg/network/payload/address_test.go:30
2023-03-31T07:46:21.1887180Z Error: Not equal:
2023-03-31T07:46:21.1887580Z expected: net.IP{0x7f, 0x0, 0x0, 0x1}
2023-03-31T07:46:21.1888290Z actual : net.IP{0x7f, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
2023-03-31T07:46:21.1888450Z
2023-03-31T07:46:21.1888640Z Diff:
2023-03-31T07:46:21.1888960Z --- Expected
2023-03-31T07:46:21.1889180Z +++ Actual
2023-03-31T07:46:21.1889510Z @@ -1,3 +1,3 @@
2023-03-31T07:46:21.1889870Z -(net.IP) (len=4) {
2023-03-31T07:46:21.1890530Z - 00000000 7f 00 00 01 |....|
2023-03-31T07:46:21.1891140Z +(net.IP) (len=16) {
2023-03-31T07:46:21.1891780Z + 00000000 7f 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 |................|
2023-03-31T07:46:21.1891970Z }
2023-03-31T07:46:21.1892510Z Test: TestEncodeDecodeAddress
2023-03-31T07:46:21.1892770Z --- FAIL: TestEncodeDecodeAddress (0.00s)
```
I'm still not sure what's the original root of this problem, there's nothing
about resolving scheme changes in https://go.dev/doc/go1.20, but it didn't
happen earlier and it works as expected in Ubuntu.
2023-04-03 06:41:32 +00:00
|
|
|
|
|
|
|
// On Windows or macOS localhost can be resolved to 4-bytes IPv4.
|
|
|
|
expected := make(net.IP, 16)
|
|
|
|
copy(expected, e.IP[:])
|
|
|
|
|
2019-09-09 14:54:38 +00:00
|
|
|
aatip := make(net.IP, 16)
|
|
|
|
copy(aatip, addr.IP[:])
|
network: fix Address test failing on Windows/macOS with go 1.20
See the trace:
```
2023-03-31T07:46:21.1886260Z === RUN TestEncodeDecodeAddress
2023-03-31T07:46:21.1886420Z address_test.go:30:
2023-03-31T07:46:21.1887000Z Error Trace: /Users/runner/work/neo-go/neo-go/pkg/network/payload/address_test.go:30
2023-03-31T07:46:21.1887180Z Error: Not equal:
2023-03-31T07:46:21.1887580Z expected: net.IP{0x7f, 0x0, 0x0, 0x1}
2023-03-31T07:46:21.1888290Z actual : net.IP{0x7f, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
2023-03-31T07:46:21.1888450Z
2023-03-31T07:46:21.1888640Z Diff:
2023-03-31T07:46:21.1888960Z --- Expected
2023-03-31T07:46:21.1889180Z +++ Actual
2023-03-31T07:46:21.1889510Z @@ -1,3 +1,3 @@
2023-03-31T07:46:21.1889870Z -(net.IP) (len=4) {
2023-03-31T07:46:21.1890530Z - 00000000 7f 00 00 01 |....|
2023-03-31T07:46:21.1891140Z +(net.IP) (len=16) {
2023-03-31T07:46:21.1891780Z + 00000000 7f 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 |................|
2023-03-31T07:46:21.1891970Z }
2023-03-31T07:46:21.1892510Z Test: TestEncodeDecodeAddress
2023-03-31T07:46:21.1892770Z --- FAIL: TestEncodeDecodeAddress (0.00s)
```
I'm still not sure what's the original root of this problem, there's nothing
about resolving scheme changes in https://go.dev/doc/go1.20, but it didn't
happen earlier and it works as expected in Ubuntu.
2023-04-03 06:41:32 +00:00
|
|
|
|
|
|
|
assert.Equal(t, expected, aatip)
|
2020-05-22 09:59:18 +00:00
|
|
|
assert.Equal(t, 1, len(addr.Capabilities))
|
|
|
|
assert.Equal(t, capability.Capability{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: uint16(e.Port)},
|
|
|
|
}, addr.Capabilities[0])
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, addr, new(AddressAndTime))
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 10:26:18 +00:00
|
|
|
func fillAddressList(al *AddressList) {
|
|
|
|
for i := 0; i < len(al.Addrs); i++ {
|
2022-06-30 13:18:41 +00:00
|
|
|
e, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:20%d", i))
|
2020-10-08 10:26:18 +00:00
|
|
|
al.Addrs[i] = NewAddressAndTime(e, time.Now(), capability.Capabilities{
|
2020-05-22 09:59:18 +00:00
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: 123},
|
|
|
|
},
|
|
|
|
})
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2020-10-08 10:26:18 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-10-08 10:26:18 +00:00
|
|
|
func TestEncodeDecodeAddressList(t *testing.T) {
|
|
|
|
var lenList uint8 = 4
|
|
|
|
addrList := NewAddressList(int(lenList))
|
|
|
|
fillAddressList(addrList)
|
2020-03-26 14:43:24 +00:00
|
|
|
testserdes.EncodeDecodeBinary(t, addrList, new(AddressList))
|
2018-03-09 15:55:25 +00:00
|
|
|
}
|
2020-10-08 10:26:18 +00:00
|
|
|
|
|
|
|
func TestEncodeDecodeBadAddressList(t *testing.T) {
|
|
|
|
var newAL = new(AddressList)
|
|
|
|
addrList := NewAddressList(MaxAddrsCount + 1)
|
|
|
|
fillAddressList(addrList)
|
|
|
|
|
|
|
|
bin, err := testserdes.EncodeBinary(addrList)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = testserdes.DecodeBinary(bin, newAL)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
addrList = NewAddressList(0)
|
|
|
|
bin, err = testserdes.EncodeBinary(addrList)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = testserdes.DecodeBinary(bin, newAL)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2020-12-04 12:25:55 +00:00
|
|
|
|
|
|
|
func TestGetTCPAddress(t *testing.T) {
|
|
|
|
t.Run("bad, no capability", func(t *testing.T) {
|
|
|
|
p := &AddressAndTime{}
|
|
|
|
copy(p.IP[:], net.IPv4(1, 1, 1, 1))
|
|
|
|
p.Capabilities = append(p.Capabilities, capability.Capability{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: 123},
|
|
|
|
})
|
|
|
|
s, err := p.GetTCPAddress()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "1.1.1.1:123", s)
|
|
|
|
})
|
|
|
|
t.Run("bad, no capability", func(t *testing.T) {
|
|
|
|
p := &AddressAndTime{}
|
|
|
|
s, err := p.GetTCPAddress()
|
|
|
|
fmt.Println(s, err)
|
|
|
|
})
|
|
|
|
}
|