2019-07-31 04:46:24 +08:00
|
|
|
package insecure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-08 18:34:32 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p-core/sec"
|
2019-07-31 04:46:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run a set of sessions through the session setup and verification.
|
|
|
|
func TestConnections(t *testing.T) {
|
2021-09-08 18:34:32 +08:00
|
|
|
clientTpt := newTestTransport(t, crypto.RSA, 2048)
|
|
|
|
serverTpt := newTestTransport(t, crypto.Ed25519, 1024)
|
|
|
|
|
|
|
|
clientConn, serverConn, clientErr, serverErr := connect(t, clientTpt, serverTpt, serverTpt.LocalPeer(), "")
|
|
|
|
require.NoError(t, clientErr)
|
|
|
|
require.NoError(t, serverErr)
|
|
|
|
testIDs(t, clientTpt, serverTpt, clientConn, serverConn)
|
|
|
|
testKeys(t, clientTpt, serverTpt, clientConn, serverConn)
|
|
|
|
testReadWrite(t, clientConn, serverConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerIdMatchInbound(t *testing.T) {
|
|
|
|
clientTpt := newTestTransport(t, crypto.RSA, 2048)
|
|
|
|
serverTpt := newTestTransport(t, crypto.Ed25519, 1024)
|
2019-07-31 04:46:24 +08:00
|
|
|
|
2021-09-08 18:34:32 +08:00
|
|
|
clientConn, serverConn, clientErr, serverErr := connect(t, clientTpt, serverTpt, serverTpt.LocalPeer(), clientTpt.LocalPeer())
|
|
|
|
require.NoError(t, clientErr)
|
|
|
|
require.NoError(t, serverErr)
|
|
|
|
testIDs(t, clientTpt, serverTpt, clientConn, serverConn)
|
|
|
|
testKeys(t, clientTpt, serverTpt, clientConn, serverConn)
|
|
|
|
testReadWrite(t, clientConn, serverConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerIDMismatchInbound(t *testing.T) {
|
|
|
|
clientTpt := newTestTransport(t, crypto.RSA, 2048)
|
|
|
|
serverTpt := newTestTransport(t, crypto.Ed25519, 1024)
|
|
|
|
|
|
|
|
_, _, _, serverErr := connect(t, clientTpt, serverTpt, serverTpt.LocalPeer(), "a-random-peer")
|
|
|
|
require.Error(t, serverErr)
|
|
|
|
require.Contains(t, serverErr.Error(), "remote peer sent unexpected peer ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerIDMismatchOutbound(t *testing.T) {
|
|
|
|
clientTpt := newTestTransport(t, crypto.RSA, 2048)
|
|
|
|
serverTpt := newTestTransport(t, crypto.Ed25519, 1024)
|
|
|
|
|
|
|
|
_, _, clientErr, _ := connect(t, clientTpt, serverTpt, "a random peer", "")
|
|
|
|
require.Error(t, clientErr)
|
|
|
|
require.Contains(t, clientErr.Error(), "remote peer sent unexpected peer ID")
|
2019-07-31 04:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestTransport(t *testing.T, typ, bits int) *Transport {
|
2021-09-08 18:34:32 +08:00
|
|
|
priv, pub, err := crypto.GenerateKeyPair(typ, bits)
|
|
|
|
require.NoError(t, err)
|
2019-07-31 04:46:24 +08:00
|
|
|
id, err := peer.IDFromPublicKey(pub)
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, err)
|
2019-07-31 04:46:24 +08:00
|
|
|
return NewWithIdentity(id, priv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new pair of connected TCP sockets.
|
|
|
|
func newConnPair(t *testing.T) (net.Conn, net.Conn) {
|
|
|
|
lstnr, err := net.Listen("tcp", "localhost:0")
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, err, "failed to listen")
|
2019-07-31 04:46:24 +08:00
|
|
|
|
|
|
|
var clientErr error
|
|
|
|
var client net.Conn
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2021-09-08 18:34:32 +08:00
|
|
|
addr := lstnr.Addr()
|
2019-07-31 04:46:24 +08:00
|
|
|
client, clientErr = net.Dial(addr.Network(), addr.String())
|
|
|
|
}()
|
|
|
|
|
|
|
|
server, err := lstnr.Accept()
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, err, "failed to accept")
|
2019-07-31 04:46:24 +08:00
|
|
|
|
2021-09-08 18:34:32 +08:00
|
|
|
<-done
|
2019-07-31 04:46:24 +08:00
|
|
|
lstnr.Close()
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, clientErr, "failed to connect")
|
2019-07-31 04:46:24 +08:00
|
|
|
return client, server
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:34:32 +08:00
|
|
|
func connect(t *testing.T, clientTpt, serverTpt *Transport, clientExpectsID, serverExpectsID peer.ID) (clientConn sec.SecureConn, serverConn sec.SecureConn, clientErr, serverErr error) {
|
2019-07-31 04:46:24 +08:00
|
|
|
client, server := newConnPair(t)
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
2021-09-08 18:34:32 +08:00
|
|
|
clientConn, clientErr = clientTpt.SecureOutbound(context.TODO(), client, clientExpectsID)
|
2019-07-31 04:46:24 +08:00
|
|
|
}()
|
2021-09-08 18:34:32 +08:00
|
|
|
serverConn, serverErr = serverTpt.SecureInbound(context.TODO(), server, serverExpectsID)
|
2019-07-31 04:46:24 +08:00
|
|
|
<-done
|
2021-09-08 18:34:32 +08:00
|
|
|
return
|
2019-07-31 04:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the peer IDs
|
|
|
|
func testIDs(t *testing.T, clientTpt, serverTpt *Transport, clientConn, serverConn sec.SecureConn) {
|
2021-09-08 18:34:32 +08:00
|
|
|
require.Equal(t, clientConn.LocalPeer(), clientTpt.LocalPeer(), "Client Local Peer ID mismatch.")
|
|
|
|
require.Equal(t, clientConn.RemotePeer(), serverTpt.LocalPeer(), "Client Remote Peer ID mismatch.")
|
|
|
|
require.Equal(t, clientConn.LocalPeer(), serverConn.RemotePeer(), "Server Local Peer ID mismatch.")
|
2019-07-31 04:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the keys
|
|
|
|
func testKeys(t *testing.T, clientTpt, serverTpt *Transport, clientConn, serverConn sec.SecureConn) {
|
|
|
|
sk := serverConn.LocalPrivateKey()
|
2021-09-08 18:34:32 +08:00
|
|
|
require.True(t, sk.Equals(serverTpt.LocalPrivateKey()), "private key mismatch")
|
|
|
|
require.True(t, sk.GetPublic().Equals(clientConn.RemotePublicKey()), "public key mismatch")
|
2019-07-31 04:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check sending and receiving messages
|
|
|
|
func testReadWrite(t *testing.T, clientConn, serverConn sec.SecureConn) {
|
|
|
|
before := []byte("hello world")
|
|
|
|
_, err := clientConn.Write(before)
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, err)
|
2019-07-31 04:46:24 +08:00
|
|
|
|
|
|
|
after := make([]byte, len(before))
|
|
|
|
_, err = io.ReadFull(serverConn, after)
|
2021-09-08 18:34:32 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, before, after, "message mismatch")
|
2019-07-31 04:46:24 +08:00
|
|
|
}
|