2019-11-06 08:06:00 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2019-11-06 08:06:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func connReadStub(conn net.Conn) {
|
|
|
|
b := make([]byte, 1024)
|
|
|
|
var err error
|
2023-03-29 03:19:23 +00:00
|
|
|
for err == nil {
|
|
|
|
_, err = conn.Read(b)
|
2019-11-06 08:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHandshake(t *testing.T) {
|
|
|
|
server, client := net.Pipe()
|
|
|
|
|
2022-11-17 14:07:19 +00:00
|
|
|
tcpS := NewTCPPeer(server, "", newTestServer(t, ServerConfig{}))
|
2022-11-29 14:43:08 +00:00
|
|
|
tcpS.server.transports[0].Accept() // properly initialize the address list
|
2022-11-17 14:07:19 +00:00
|
|
|
tcpC := NewTCPPeer(client, "", newTestServer(t, ServerConfig{}))
|
2022-11-29 14:43:08 +00:00
|
|
|
tcpC.server.transports[0].Accept()
|
2019-11-06 08:06:00 +00:00
|
|
|
|
|
|
|
// Something should read things written into the pipe.
|
|
|
|
go connReadStub(tcpS.conn)
|
|
|
|
go connReadStub(tcpC.conn)
|
|
|
|
|
|
|
|
// No handshake yet.
|
|
|
|
require.Equal(t, false, tcpS.Handshaked())
|
|
|
|
require.Equal(t, false, tcpC.Handshaked())
|
|
|
|
|
|
|
|
// No ordinary messages can be written.
|
2022-10-12 12:27:08 +00:00
|
|
|
require.Error(t, tcpS.EnqueueP2PMessage(&Message{}))
|
|
|
|
require.Error(t, tcpC.EnqueueP2PMessage(&Message{}))
|
2019-11-06 08:06:00 +00:00
|
|
|
|
|
|
|
// Try to mess with VersionAck on both client and server, it should fail.
|
|
|
|
require.Error(t, tcpS.SendVersionAck(&Message{}))
|
|
|
|
require.Error(t, tcpS.HandleVersionAck())
|
|
|
|
require.Error(t, tcpC.SendVersionAck(&Message{}))
|
|
|
|
require.Error(t, tcpC.HandleVersionAck())
|
|
|
|
|
|
|
|
// No handshake yet.
|
|
|
|
require.Equal(t, false, tcpS.Handshaked())
|
|
|
|
require.Equal(t, false, tcpC.Handshaked())
|
|
|
|
|
|
|
|
// Now send and handle versions, but in a different order on client and
|
|
|
|
// server.
|
2020-01-21 14:26:08 +00:00
|
|
|
require.NoError(t, tcpC.SendVersion())
|
2019-11-06 15:05:50 +00:00
|
|
|
require.Error(t, tcpC.HandleVersionAck()) // Didn't receive version yet.
|
2019-11-06 08:06:00 +00:00
|
|
|
require.NoError(t, tcpS.HandleVersion(&payload.Version{}))
|
2019-11-06 15:05:50 +00:00
|
|
|
require.Error(t, tcpS.SendVersionAck(&Message{})) // Didn't send version yet.
|
2019-11-06 08:06:00 +00:00
|
|
|
require.NoError(t, tcpC.HandleVersion(&payload.Version{}))
|
2020-01-21 14:26:08 +00:00
|
|
|
require.NoError(t, tcpS.SendVersion())
|
2019-11-06 08:06:00 +00:00
|
|
|
|
|
|
|
// No handshake yet.
|
|
|
|
require.Equal(t, false, tcpS.Handshaked())
|
|
|
|
require.Equal(t, false, tcpC.Handshaked())
|
|
|
|
|
|
|
|
// These are sent/received and should fail now.
|
2020-01-21 14:26:08 +00:00
|
|
|
require.Error(t, tcpC.SendVersion())
|
2019-11-06 08:06:00 +00:00
|
|
|
require.Error(t, tcpS.HandleVersion(&payload.Version{}))
|
|
|
|
require.Error(t, tcpC.HandleVersion(&payload.Version{}))
|
2020-01-21 14:26:08 +00:00
|
|
|
require.Error(t, tcpS.SendVersion())
|
2019-11-06 08:06:00 +00:00
|
|
|
|
|
|
|
// Now send and handle ACK, again in a different order on client and
|
|
|
|
// server.
|
|
|
|
require.NoError(t, tcpC.SendVersionAck(&Message{}))
|
|
|
|
require.NoError(t, tcpS.HandleVersionAck())
|
|
|
|
require.NoError(t, tcpC.HandleVersionAck())
|
|
|
|
require.NoError(t, tcpS.SendVersionAck(&Message{}))
|
|
|
|
|
|
|
|
// Handshaked now.
|
|
|
|
require.Equal(t, true, tcpS.Handshaked())
|
|
|
|
require.Equal(t, true, tcpC.Handshaked())
|
|
|
|
|
|
|
|
// Subsequent ACKing should fail.
|
|
|
|
require.Error(t, tcpC.SendVersionAck(&Message{}))
|
|
|
|
require.Error(t, tcpS.HandleVersionAck())
|
|
|
|
require.Error(t, tcpC.HandleVersionAck())
|
|
|
|
require.Error(t, tcpS.SendVersionAck(&Message{}))
|
|
|
|
|
|
|
|
// Now regular messaging can proceed.
|
2022-10-12 12:27:08 +00:00
|
|
|
require.NoError(t, tcpS.EnqueueP2PMessage(&Message{}))
|
|
|
|
require.NoError(t, tcpC.EnqueueP2PMessage(&Message{}))
|
2019-11-06 08:06:00 +00:00
|
|
|
}
|