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

import (
4
	"context"
5
6
	"crypto/rand"
	"crypto/rsa"
7
	"crypto/tls"
8
	"crypto/x509"
9
	"io/ioutil"
10

11
12
13
14
	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"
15
16
17
18
19

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

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

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

34
	runServer := func(tr tpt.Transport) (ma.Multiaddr, <-chan tpt.Conn) {
35
36
37
38
39
40
		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())
41
			ln, err := tr.Listen(addr)
42
43
44
45
46
47
			Expect(err).ToNot(HaveOccurred())
			addrChan <- ln.Multiaddr()
			conn, err := ln.Accept()
			Expect(err).ToNot(HaveOccurred())
			connChan <- conn
		}()
48
49
50
51
52
53
		return <-addrChan, connChan
	}

	// modify the cert chain such that verificiation will fail
	invalidateCertChain := func(tlsConf *tls.Config) {
		tlsConf.Certificates[0].Certificate = [][]byte{tlsConf.Certificates[0].Certificate[0]}
54
	}
55

56
57
58
59
60
61
62
63
	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())
64
65
	})

66
	It("handshakes", func() {
67
68
69
70
		serverTransport, err := NewTransport(serverKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

71
72
73
74
75
76
77
78
79
80
81
82
83
		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		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()))
84
85
	})

86
	It("opens and accepts streams", func() {
87
88
89
90
		serverTransport, err := NewTransport(serverKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

91
92
		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
93
		conn, err := clientTransport.Dial(context.Background(), serverAddr, serverID)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
		Expect(err).ToNot(HaveOccurred())
		serverConn := <-serverConnChan

		str, err := conn.OpenStream()
		Expect(err).ToNot(HaveOccurred())
		_, err = str.Write([]byte("foobar"))
		Expect(err).ToNot(HaveOccurred())
		str.Close()
		sstr, err := serverConn.AcceptStream()
		Expect(err).ToNot(HaveOccurred())
		data, err := ioutil.ReadAll(sstr)
		Expect(err).ToNot(HaveOccurred())
		Expect(data).To(Equal([]byte("foobar")))
	})

109
110
111
	It("fails if the peer ID doesn't match", func() {
		thirdPartyID, err := peer.IDFromPrivateKey(createPeer())
		Expect(err).ToNot(HaveOccurred())
112

113
114
115
116
		serverTransport, err := NewTransport(serverKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

117
118
119
120
		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		// dial, but expect the wrong peer ID
		_, err = clientTransport.Dial(context.Background(), serverAddr, thirdPartyID)
121
122
123
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(ContainSubstring("TLS handshake error: bad certificate"))
		Consistently(serverConnChan).ShouldNot(Receive())
124
	})
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

	It("fails if the client presents an invalid cert chain", func() {
		serverTransport, err := NewTransport(serverKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

		clientTransport, err := NewTransport(clientKey)
		invalidateCertChain(clientTransport.(*transport).tlsConf)
		Expect(err).ToNot(HaveOccurred())
		conn, err := clientTransport.Dial(context.Background(), serverAddr, serverID)
		Expect(err).ToNot(HaveOccurred())
		Eventually(func() bool { return conn.IsClosed() }).Should(BeTrue())
		Consistently(serverConnChan).ShouldNot(Receive())
	})

	It("fails if the server presents an invalid cert chain", func() {
		serverTransport, err := NewTransport(serverKey)
		invalidateCertChain(serverTransport.(*transport).tlsConf)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

		clientTransport, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		_, err = clientTransport.Dial(context.Background(), serverAddr, serverID)
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(ContainSubstring("TLS handshake error: bad certificate"))
		Consistently(serverConnChan).ShouldNot(Receive())
	})

	It("keeps accepting connections after a failed connection attempt", func() {
		serverTransport, err := NewTransport(serverKey)
		Expect(err).ToNot(HaveOccurred())
		serverAddr, serverConnChan := runServer(serverTransport)

		// first dial with an invalid cert chain
		clientTransport1, err := NewTransport(clientKey)
		invalidateCertChain(clientTransport1.(*transport).tlsConf)
		Expect(err).ToNot(HaveOccurred())
		_, err = clientTransport1.Dial(context.Background(), serverAddr, serverID)
		Expect(err).ToNot(HaveOccurred())
		Consistently(serverConnChan).ShouldNot(Receive())

		// then dial with a valid client
		clientTransport2, err := NewTransport(clientKey)
		Expect(err).ToNot(HaveOccurred())
		_, err = clientTransport2.Dial(context.Background(), serverAddr, serverID)
		Expect(err).ToNot(HaveOccurred())
		Eventually(serverConnChan).Should(Receive())
	})
174
})