conn_test.go 4.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package libp2pquic

import (
	"errors"
	"net"

	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
}

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 }

var _ quic.Stream = &mockStream{}

type mockQuicSession struct {
28
29
	closed              bool
	waitUntilClosedChan chan struct{} // close this chan to make WaitUntilClosed return
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

	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
}
46
47
48
49
50
51
52
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 }
53
func (s *mockQuicSession) WaitUntilClosed()     { <-s.waitUntilClosedChan }
54
55
56
57
58
59
60
61
62

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

	BeforeEach(func() {
		sess = &mockQuicSession{
63
64
65
			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{}),
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		}
		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())
	})

88
89
90
91
92
93
	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())
	})

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	Context("opening streams", func() {
		It("opens streams", func() {
			s := &mockStream{id: 1337}
			sess.streamToOpen = s
			str, err := conn.OpenStream()
			Expect(err).ToNot(HaveOccurred())
			Expect(str.(*quicStream).Stream).To(Equal(s))
		})

		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())
			Expect(str.(*quicStream).Stream).To(Equal(s))
		})

		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())
			Expect(handlerCalledWith.(*quicStream).Stream).To(Equal(str))
			// 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())
		})
	})

})