conn_test.go 2.88 KB
Newer Older
1
2
3
package libp2pquic

import (
4
	"context"
5
6
7
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
8

9
10
11
12
	ic "github.com/libp2p/go-libp2p-crypto"
	peer "github.com/libp2p/go-libp2p-peer"
	tpt "github.com/libp2p/go-libp2p-transport"
	ma "github.com/multiformats/go-multiaddr"
13
14
15
16
17

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

18
var _ = Describe("Connection", func() {
19
	var (
20
21
		serverKey, clientKey ic.PrivKey
		serverID, clientID   peer.ID
22
23
	)

24
25
	createPeer := func() ic.PrivKey {
		key, err := rsa.GenerateKey(rand.Reader, 1024)
26
		Expect(err).ToNot(HaveOccurred())
27
28
29
30
		priv, err := ic.UnmarshalRsaPrivateKey(x509.MarshalPKCS1PrivateKey(key))
		Expect(err).ToNot(HaveOccurred())
		return priv
	}
31

32
33
	runServer := func() (<-chan ma.Multiaddr, <-chan tpt.Conn) {
		serverTransport, err := NewTransport(serverKey)
34
		Expect(err).ToNot(HaveOccurred())
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
		addrChan := make(chan ma.Multiaddr)
		connChan := make(chan tpt.Conn)
		go func() {
			defer GinkgoRecover()
			addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
			Expect(err).ToNot(HaveOccurred())
			ln, err := serverTransport.Listen(addr)
			Expect(err).ToNot(HaveOccurred())
			addrChan <- ln.Multiaddr()
			conn, err := ln.Accept()
			Expect(err).ToNot(HaveOccurred())
			connChan <- conn
		}()
		return addrChan, connChan
	}
50

51
52
53
54
55
56
57
58
	BeforeEach(func() {
		var err error
		serverKey = createPeer()
		serverID, err = peer.IDFromPrivateKey(serverKey)
		Expect(err).ToNot(HaveOccurred())
		clientKey = createPeer()
		clientID, err = peer.IDFromPrivateKey(clientKey)
		Expect(err).ToNot(HaveOccurred())
59
60
	})

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	It("handshakes", func() {
		serverAddrChan, serverConnChan := runServer()
		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr := <-serverAddrChan
		conn, err := clientTransport.Dial(context.Background(), serverAddr, serverID)
		Expect(err).ToNot(HaveOccurred())
		serverConn := <-serverConnChan
		Expect(conn.LocalPeer()).To(Equal(clientID))
		Expect(conn.LocalPrivateKey()).To(Equal(clientKey))
		Expect(conn.RemotePeer()).To(Equal(serverID))
		Expect(conn.RemotePublicKey()).To(Equal(serverKey.GetPublic()))
		Expect(serverConn.LocalPeer()).To(Equal(serverID))
		Expect(serverConn.LocalPrivateKey()).To(Equal(serverKey))
		Expect(serverConn.RemotePeer()).To(Equal(clientID))
		Expect(serverConn.RemotePublicKey()).To(Equal(clientKey.GetPublic()))
77
78
	})

79
80
81
	It("fails if the peer ID doesn't match", func() {
		thirdPartyID, err := peer.IDFromPrivateKey(createPeer())
		Expect(err).ToNot(HaveOccurred())
82

83
		serverAddrChan, serverConnChan := runServer()
84
85
86
87
88
		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr := <-serverAddrChan
		// dial, but expect the wrong peer ID
		_, err = clientTransport.Dial(context.Background(), serverAddr, thirdPartyID)
89
90
91
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(ContainSubstring("TLS handshake error: bad certificate"))
		Consistently(serverConnChan).ShouldNot(Receive())
92
93
	})
})