-
Notifications
You must be signed in to change notification settings - Fork 1
/
remote_test.go
241 lines (198 loc) · 5.62 KB
/
remote_test.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2018 Dan Jacques. All rights reserved.
// Use of this source code is governed under the MIT License
// that can be found in the LICENSE file.
package device
import (
"net"
"sync"
"time"
"github.com/danjacques/gopushpixels/protocol"
"github.com/danjacques/gopushpixels/protocol/pixelpusher"
"github.com/danjacques/gopushpixels/support/network"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Remote Device (Connected)", func() {
// Open up a local UDP connection to pretend to be the remote system.
var rc *remoteConn
BeforeEach(func() {
// The buffer size is the amount of data that the packet channel can buffer.
rc = newRemoteConn(8)
err := rc.add("orig")
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() { rc.closeAll() })
// Create discovery headers for a PixelPusher.
//
// We choose this device because it can specify its own port.
var dh *protocol.DiscoveryHeaders
BeforeEach(func() {
dh = &protocol.DiscoveryHeaders{
DeviceHeader: protocol.DeviceHeader{
DeviceType: protocol.PixelPusherDeviceType,
MacAddress: [6]byte{0xf0, 0x0d, 0xfa, 0xce, 0xd0, 0x65},
},
PixelPusher: &pixelpusher.Device{},
}
dh.PixelPusher.GroupOrdinal = 4
dh.PixelPusher.ControllerOrdinal = 8
// Configure these headers to broadcast for "orig".
rc.setDiscoveryHeaders("orig", dh)
})
// Initialize our remote device for this round.
var r *Remote
BeforeEach(func() {
r = MakeRemote("test device", dh)
})
AfterEach(func() {
r.MarkDone()
})
It("reports its basic fields", func() {
By("id")
Expect(r.ID()).To(Equal("test device"))
By("discovery headers")
Expect(r.DiscoveryHeaders()).To(Equal(dh))
By("ordinal")
ord := r.Ordinal()
Expect(ord).To(Equal(Ordinal{
Group: 4,
Controller: 8,
}))
By("string")
Expect(r.String()).To(MatchRegexp(`"test device" @.+ \(PIXELPUSHER\)`))
})
Context("with a Sender", func() {
var s Sender
BeforeEach(func() {
var err error
s, err = r.Sender()
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func(done Done) {
defer close(done)
if s != nil {
err := s.Close()
Expect(err).ToNot(HaveOccurred())
}
})
It("can send packets", func(done Done) {
defer close(done)
By("first packet")
err := s.SendDatagram([]byte("Her Glorious Majesty, Packet the First"))
Expect(err).ToNot(HaveOccurred())
By("second packet")
err = s.SendDatagram([]byte("The Usurper, Packet II"))
Expect(err).ToNot(HaveOccurred())
By("collect")
Expect(<-rc.packetC).To(Equal(&remoteConnPacket{
id: "orig",
pkt: []byte("Her Glorious Majesty, Packet the First"),
}))
Expect(<-rc.packetC).To(Equal(&remoteConnPacket{
id: "orig",
pkt: []byte("The Usurper, Packet II"),
}))
By("reports that packets have been sent")
info := r.Info()
Expect(info.BytesSent).To(BeNumerically(">", 0))
Expect(info.PacketsSent).To(BeEquivalentTo(2))
})
Context("when the port dynamically changes", func() {
var ndh *protocol.DiscoveryHeaders
BeforeEach(func() {
err := rc.add("mod")
Expect(err).ToNot(HaveOccurred())
ndh = dh.Clone()
rc.setDiscoveryHeaders("mod", ndh)
})
It("can dynamically change ports when a new header is sent", func(done Done) {
defer close(done)
By("receive a packet on original port")
err := s.SendDatagram([]byte("ohai there!"))
Expect(err).ToNot(HaveOccurred())
Expect(<-rc.packetC).To(Equal(&remoteConnPacket{
id: "orig",
pkt: []byte("ohai there!"),
}))
By("change port")
r.UpdateHeaders(time.Now(), ndh)
By("writes a packet to the new port")
err = s.SendDatagram([]byte("changed port, u keep up?"))
Expect(err).ToNot(HaveOccurred())
Expect(<-rc.packetC).To(Equal(&remoteConnPacket{
id: "mod",
pkt: []byte("changed port, u keep up?"),
}))
By("reports that packets have been sent")
info := r.Info()
Expect(info.BytesSent).To(BeNumerically(">", 0))
Expect(info.PacketsSent).To(BeEquivalentTo(2))
})
})
})
Context("when marked Done", func() {
BeforeEach(func() { r.MarkDone() })
It("reports itself as Done", func() {
Expect(IsDone(r)).To(BeTrue())
})
})
})
var _ = Describe("Remote Stub", func() {
})
// remoteConn manages labelled local UDP connections and captures/consumes
// packets sent to them.
//
// This is probably overkill, but is a nice facility to have, and is one of the
// (ideally) few things that is actually bound to a real system resource.
type remoteConn struct {
packetC chan *remoteConnPacket
conns map[string]*net.UDPConn
wg sync.WaitGroup
}
func newRemoteConn(bufferSize int) *remoteConn {
return &remoteConn{
packetC: make(chan *remoteConnPacket, bufferSize),
}
}
func (rc *remoteConn) add(id string) error {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
})
if err != nil {
return err
}
if rc.conns == nil {
rc.conns = make(map[string]*net.UDPConn)
}
rc.conns[id] = conn
rc.wg.Add(1)
go rc.listenOn(id, conn)
return nil
}
func (rc *remoteConn) setDiscoveryHeaders(id string, dh *protocol.DiscoveryHeaders) {
conn := rc.conns[id]
addr := conn.LocalAddr().(*net.UDPAddr)
dh.SetIP4Address(addr.IP)
dh.PixelPusher.MyPort = uint16(addr.Port)
}
func (rc *remoteConn) closeAll() {
for _, conn := range rc.conns {
_ = conn.Close()
}
rc.conns = nil
}
func (rc *remoteConn) listenOn(id string, conn *net.UDPConn) {
defer rc.wg.Done()
for {
buf := make([]byte, network.MaxUDPSize)
amt, _, _, _, err := conn.ReadMsgUDP(buf, nil)
if err != nil {
return
}
rc.packetC <- &remoteConnPacket{id, buf[:amt]}
}
}
type remoteConnPacket struct {
id string
pkt []byte
}