2018-01-31 08:27:08 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2018-01-31 08:27:08 +00:00
|
|
|
"testing"
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-02-01 18:54:23 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network/payload"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-06 09:39:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-31 08:27:08 +00:00
|
|
|
)
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func TestSendVersion(t *testing.T) {
|
|
|
|
var (
|
2019-12-30 07:43:05 +00:00
|
|
|
s = newTestServer(t)
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
2019-11-05 12:22:07 +00:00
|
|
|
s.Port = 3000
|
2018-03-14 09:36:59 +00:00
|
|
|
s.UserAgent = "/test/"
|
|
|
|
|
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
assert.Equal(t, CMDVersion, msg.CommandType())
|
|
|
|
assert.IsType(t, msg.Payload, &payload.Version{})
|
|
|
|
version := msg.Payload.(*payload.Version)
|
|
|
|
assert.NotZero(t, version.Nonce)
|
|
|
|
assert.Equal(t, uint16(3000), version.Port)
|
|
|
|
assert.Equal(t, uint64(1), version.Services)
|
|
|
|
assert.Equal(t, uint32(0), version.Version)
|
|
|
|
assert.Equal(t, []byte("/test/"), version.UserAgent)
|
|
|
|
assert.Equal(t, uint32(0), version.StartHeight)
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, p.SendVersion())
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Server should reply with a verack after receiving a valid version.
|
|
|
|
func TestVerackAfterHandleVersionCmd(t *testing.T) {
|
|
|
|
var (
|
2019-12-30 07:43:05 +00:00
|
|
|
s = newTestServer(t)
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
2019-09-09 14:54:38 +00:00
|
|
|
na, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:3000")
|
|
|
|
p.netaddr = *na
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
// Should have a verack
|
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
assert.Equal(t, CMDVerack, msg.CommandType())
|
|
|
|
}
|
|
|
|
version := payload.NewVersion(1337, 3000, "/NEO-GO/", 0, true)
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, s.handleVersionCmd(p, version))
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Server should not reply with a verack after receiving a
|
|
|
|
// invalid version and disconnects the peer.
|
|
|
|
func TestServerNotSendsVerack(t *testing.T) {
|
|
|
|
var (
|
2019-12-30 07:43:05 +00:00
|
|
|
s = newTestServer(t)
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
|
|
|
p2 = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
s.id = 1
|
|
|
|
go s.run()
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-09-09 14:54:38 +00:00
|
|
|
na, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:3000")
|
|
|
|
p.netaddr = *na
|
2019-11-06 09:39:17 +00:00
|
|
|
p2.netaddr = *na
|
2018-03-14 09:36:59 +00:00
|
|
|
s.register <- p
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// identical id's
|
2019-10-17 08:19:24 +00:00
|
|
|
version := payload.NewVersion(1, 3000, "/NEO-GO/", 0, true)
|
|
|
|
err := s.handleVersionCmd(p, version)
|
2018-03-14 09:36:59 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, errIdenticalID, err)
|
2019-11-06 09:39:17 +00:00
|
|
|
|
|
|
|
// Different IDs, make handshake pass.
|
|
|
|
version.Nonce = 2
|
|
|
|
require.NoError(t, s.handleVersionCmd(p, version))
|
|
|
|
require.NoError(t, p.HandleVersionAck())
|
|
|
|
require.Equal(t, true, p.Handshaked())
|
|
|
|
|
|
|
|
// Second handshake from the same peer should fail.
|
|
|
|
s.register <- p2
|
|
|
|
err = s.handleVersionCmd(p2, version)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
require.Equal(t, errAlreadyConnected, err)
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
2018-03-10 12:04:06 +00:00
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func TestRequestHeaders(t *testing.T) {
|
|
|
|
var (
|
2019-12-30 07:43:05 +00:00
|
|
|
s = newTestServer(t)
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
assert.IsType(t, &payload.GetBlocks{}, msg.Payload)
|
|
|
|
assert.Equal(t, CMDGetHeaders, msg.CommandType())
|
|
|
|
}
|
|
|
|
s.requestHeaders(p)
|
2018-03-10 12:04:06 +00:00
|
|
|
}
|