conn_test.go 4.82 KB
Newer Older
1
2
3
4
5
package libp2pquic

import (
	"errors"
	"net"
6
	"time"
7
8
9
10
11
12
13
14
15
16
17
18
19

	smux "github.com/jbenet/go-stream-muxer"
	quic "github.com/lucas-clemente/quic-go"
	"github.com/lucas-clemente/quic-go/protocol"

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

type mockStream struct {
	id protocol.StreamID
}

20
21
22
23
24
25
26
27
func (s *mockStream) Close() error                     { return nil }
func (s *mockStream) Reset(error)                      { return }
func (s *mockStream) Read([]byte) (int, error)         { return 0, nil }
func (s *mockStream) Write([]byte) (int, error)        { return 0, nil }
func (s *mockStream) StreamID() protocol.StreamID      { return s.id }
func (s *mockStream) SetReadDeadline(time.Time) error  { panic("not implemented") }
func (s *mockStream) SetWriteDeadline(time.Time) error { panic("not implemented") }
func (s *mockStream) SetDeadline(time.Time) error      { panic("not implemented") }
28
29
30
31

var _ quic.Stream = &mockStream{}

type mockQuicSession struct {
32
33
	closed              bool
	waitUntilClosedChan chan struct{} // close this chan to make WaitUntilClosed return
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

	localAddr  net.Addr
	remoteAddr net.Addr

	streamToAccept  quic.Stream
	streamAcceptErr error

	streamToOpen  quic.Stream
	streamOpenErr error
}

var _ quic.Session = &mockQuicSession{}

func (s *mockQuicSession) AcceptStream() (quic.Stream, error) {
	return s.streamToAccept, s.streamAcceptErr
}
50
51
52
53
54
55
56
func (s *mockQuicSession) OpenStream() (quic.Stream, error) { return s.streamToOpen, s.streamOpenErr }
func (s *mockQuicSession) OpenStreamSync() (quic.Stream, error) {
	return s.streamToOpen, s.streamOpenErr
}
func (s *mockQuicSession) Close(error) error    { s.closed = true; return nil }
func (s *mockQuicSession) LocalAddr() net.Addr  { return s.localAddr }
func (s *mockQuicSession) RemoteAddr() net.Addr { return s.remoteAddr }
57
func (s *mockQuicSession) WaitUntilClosed()     { <-s.waitUntilClosedChan }
58
59
60
61
62
63
64
65
66

var _ = Describe("Conn", func() {
	var (
		conn *quicConn
		sess *mockQuicSession
	)

	BeforeEach(func() {
		sess = &mockQuicSession{
67
68
69
			localAddr:           &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1337},
			remoteAddr:          &net.UDPAddr{IP: net.IPv4(192, 168, 13, 37), Port: 1234},
			waitUntilClosedChan: make(chan struct{}),
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		}
		var err error
		conn, err = newQuicConn(sess, nil)
		Expect(err).ToNot(HaveOccurred())
	})

	It("has the correct local address", func() {
		Expect(conn.LocalAddr()).To(Equal(sess.localAddr))
		Expect(conn.LocalMultiaddr().String()).To(Equal("/ip4/127.0.0.1/udp/1337/quic"))
	})

	It("has the correct remote address", func() {
		Expect(conn.RemoteAddr()).To(Equal(sess.remoteAddr))
		Expect(conn.RemoteMultiaddr().String()).To(Equal("/ip4/192.168.13.37/udp/1234/quic"))
	})

	It("closes", func() {
		err := conn.Close()
		Expect(err).ToNot(HaveOccurred())
		Expect(sess.closed).To(BeTrue())
	})

92
93
94
95
96
97
	It("says if it is closed", func() {
		Consistently(func() bool { return conn.IsClosed() }).Should(BeFalse())
		close(sess.waitUntilClosedChan)
		Eventually(func() bool { return conn.IsClosed() }).Should(BeTrue())
	})

98
99
100
101
102
103
	Context("opening streams", func() {
		It("opens streams", func() {
			s := &mockStream{id: 1337}
			sess.streamToOpen = s
			str, err := conn.OpenStream()
			Expect(err).ToNot(HaveOccurred())
104
			Expect(str).To(Equal(s))
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		})

		It("errors when it can't open a stream", func() {
			testErr := errors.New("stream open err")
			sess.streamOpenErr = testErr
			_, err := conn.OpenStream()
			Expect(err).To(MatchError(testErr))
		})
	})

	Context("accepting streams", func() {
		It("accepts streams", func() {
			s := &mockStream{id: 1337}
			sess.streamToAccept = s
			str, err := conn.AcceptStream()
			Expect(err).ToNot(HaveOccurred())
121
			Expect(str).To(Equal(s))
122
123
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
		})

		It("errors when it can't open a stream", func() {
			testErr := errors.New("stream open err")
			sess.streamAcceptErr = testErr
			_, err := conn.AcceptStream()
			Expect(err).To(MatchError(testErr))
		})
	})

	Context("serving", func() {
		var (
			handler           func(smux.Stream)
			handlerCalled     bool
			handlerCalledWith smux.Stream
		)

		BeforeEach(func() {
			handlerCalled = false
			handlerCalledWith = nil
			handler = func(s smux.Stream) {
				handlerCalledWith = s
				handlerCalled = true
			}
		})

		It("calls the handler", func() {
			str := &mockStream{id: 5}
			sess.streamToAccept = str
			var returned bool
			go func() {
				conn.Serve(handler)
				returned = true
			}()
			Eventually(func() bool { return handlerCalled }).Should(BeTrue())
157
			Expect(handlerCalledWith).To(Equal(str))
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
			// make the go-routine return
			sess.streamAcceptErr = errors.New("stop test")
		})

		It("returns when accepting a stream errors", func() {
			sess.streamAcceptErr = errors.New("accept err")
			var returned bool
			go func() {
				conn.Serve(handler)
				returned = true
			}()
			Eventually(func() bool { return returned }).Should(BeTrue())
			Expect(handlerCalled).To(BeFalse())
		})
	})

})