forked from lightninglabs/aperture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmail_server_test.go
507 lines (424 loc) · 13 KB
/
hashmail_server_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package aperture
import (
"context"
"crypto/rand"
"fmt"
"math"
"net"
"net/http"
"testing"
"time"
"github.com/lightninglabs/lightning-node-connect/hashmailrpc"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/signal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)
var (
testApertureAddress = "localhost:8082"
testSID = streamID{1, 2, 3}
testStreamDesc = &hashmailrpc.CipherBoxDesc{
StreamId: testSID[:],
}
testMessage = []byte("I'm a message!")
apertureStartTimeout = 3 * time.Second
)
func init() {
logWriter := build.NewRotatingLogWriter()
SetupLoggers(logWriter, signal.Interceptor{})
_ = build.ParseAndSetDebugLevels("trace,PRXY=warn", logWriter)
}
func TestHashMailServerReturnStream(t *testing.T) {
ctxb := context.Background()
setupAperture(t)
// Create a client and connect it to the server.
conn, err := grpc.Dial(testApertureAddress, grpc.WithInsecure())
require.NoError(t, err)
client := hashmailrpc.NewHashMailClient(conn)
// We'll create a new cipher box that we're going to subscribe to
// multiple times to check disconnecting returns the read stream.
resp, err := client.NewCipherBox(ctxb, &hashmailrpc.CipherBoxAuth{
Auth: &hashmailrpc.CipherBoxAuth_LndAuth{},
Desc: testStreamDesc,
})
require.NoError(t, err)
require.NotNil(t, resp.GetSuccess())
// First we make sure there is something to read on the other end of
// that stream by writing something to it.
sendCtx, sendCancel := context.WithCancel(context.Background())
defer sendCancel()
writeStream, err := client.SendStream(sendCtx)
require.NoError(t, err)
err = writeStream.Send(&hashmailrpc.CipherBox{
Desc: testStreamDesc,
Msg: testMessage,
})
require.NoError(t, err)
// We need to wait a bit to make sure the message is really sent.
time.Sleep(100 * time.Millisecond)
// Connect, wait for the stream to be ready, read something, then
// disconnect immediately.
msg, err := readMsgFromStream(t, client)
require.NoError(t, err)
require.Equal(t, testMessage, msg.Msg)
// Make sure we can connect again immediately and try to read something.
// There is no message to read before we cancel the request so we expect
// an EOF error to be returned upon connection close/context cancel.
_, err = readMsgFromStream(t, client)
require.Error(t, err)
require.Contains(t, err.Error(), "context canceled")
// Send then receive yet another message to make sure the stream is
// still operational.
testMessage2 := append(testMessage, []byte("test")...) //nolint:gocritic
err = writeStream.Send(&hashmailrpc.CipherBox{
Desc: testStreamDesc,
Msg: testMessage2,
})
require.NoError(t, err)
// We need to wait a bit to make sure the message is really sent.
time.Sleep(100 * time.Millisecond)
msg, err = readMsgFromStream(t, client)
require.NoError(t, err)
require.Equal(t, testMessage2, msg.Msg)
// Clean up the stream now.
_, err = client.DelCipherBox(ctxb, &hashmailrpc.CipherBoxAuth{
Auth: &hashmailrpc.CipherBoxAuth_LndAuth{},
Desc: testStreamDesc,
})
require.NoError(t, err)
}
func TestHashMailServerLargeMessage(t *testing.T) {
ctxb := context.Background()
setupAperture(t)
// Create a client and connect it to the server.
conn, err := grpc.Dial(testApertureAddress, grpc.WithInsecure())
require.NoError(t, err)
client := hashmailrpc.NewHashMailClient(conn)
// We'll create a new cipher box that we're going to subscribe to
// multiple times to check disconnecting returns the read stream.
resp, err := client.NewCipherBox(ctxb, &hashmailrpc.CipherBoxAuth{
Auth: &hashmailrpc.CipherBoxAuth_LndAuth{},
Desc: testStreamDesc,
})
require.NoError(t, err)
require.NotNil(t, resp.GetSuccess())
// Let's create a long message and try to send it.
var largeMessage [512 * DefaultBufSize]byte
_, err = rand.Read(largeMessage[:])
require.NoError(t, err)
sendCtx, sendCancel := context.WithCancel(context.Background())
defer sendCancel()
writeStream, err := client.SendStream(sendCtx)
require.NoError(t, err)
err = writeStream.Send(&hashmailrpc.CipherBox{
Desc: testStreamDesc,
Msg: largeMessage[:],
})
require.NoError(t, err)
// We need to wait a bit to make sure the message is really sent.
time.Sleep(100 * time.Millisecond)
// Connect, wait for the stream to be ready, read something, then
// disconnect immediately.
msg, err := readMsgFromStream(t, client)
require.NoError(t, err)
require.Equal(t, largeMessage[:], msg.Msg)
}
func setupAperture(t *testing.T) {
apertureCfg := &Config{
Insecure: true,
ListenAddr: testApertureAddress,
Authenticator: &AuthConfig{
Disable: true,
},
Etcd: &EtcdConfig{},
HashMail: &HashMailConfig{
Enabled: true,
MessageRate: time.Millisecond,
MessageBurstAllowance: math.MaxUint32,
},
Prometheus: &PrometheusConfig{},
Tor: &TorConfig{},
}
aperture := NewAperture(apertureCfg)
errChan := make(chan error)
require.NoError(t, aperture.Start(errChan))
// Any error while starting?
select {
case err := <-errChan:
t.Fatalf("error starting aperture: %v", err)
default:
}
err := wait.NoError(func() error {
apertureAddr := fmt.Sprintf("http://%s/dummy",
testApertureAddress)
resp, err := http.Get(apertureAddr)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("invalid status: %d", resp.StatusCode)
}
return nil
}, apertureStartTimeout)
require.NoError(t, err)
}
func readMsgFromStream(t *testing.T,
client hashmailrpc.HashMailClient) (*hashmailrpc.CipherBox, error) {
ctxc, cancel := context.WithCancel(context.Background())
readStream, err := client.RecvStream(ctxc, testStreamDesc)
require.NoError(t, err)
// Wait a bit again to make sure the request is actually sent before our
// context is canceled already again.
time.Sleep(100 * time.Millisecond)
// We'll start a read on the stream in the background.
var (
goroutineStarted = make(chan struct{})
resultChan = make(chan *hashmailrpc.CipherBox)
errChan = make(chan error)
)
go func() {
close(goroutineStarted)
box, err := readStream.Recv()
if err != nil {
errChan <- err
return
}
resultChan <- box
}()
// Give the goroutine a chance to actually run, so block the main thread
// until it did.
<-goroutineStarted
time.Sleep(200 * time.Millisecond)
// Now close and cancel the stream to make sure the server can clean it
// up and release it.
require.NoError(t, readStream.CloseSend())
cancel()
// Interpret the result.
select {
case err := <-errChan:
return nil, err
case box := <-resultChan:
return box, nil
}
}
type statusState struct {
readOccupied bool
writeOccupied bool
}
// TestStaleMailboxCleanup tests that the streamStatus behaves as expected and
// that it correctly tears down a mailbox if it becomes stale.
func TestStaleMailboxCleanup(t *testing.T) {
tests := []struct {
name string
staleTimeout time.Duration
senderConnected statusState
readerConnected statusState
senderDisconnected statusState
expectStaleMailboxRemoval bool
}{
{
name: "tear down stale mailbox",
staleTimeout: 500 * time.Millisecond,
senderConnected: statusState{
writeOccupied: true,
},
readerConnected: statusState{
writeOccupied: true,
readOccupied: true,
},
senderDisconnected: statusState{
writeOccupied: false,
readOccupied: true,
},
expectStaleMailboxRemoval: true,
},
{
name: "dont tear down stale mailbox",
staleTimeout: -1,
senderConnected: statusState{
writeOccupied: false,
readOccupied: false,
},
readerConnected: statusState{
writeOccupied: false,
readOccupied: false,
},
senderDisconnected: statusState{
writeOccupied: false,
readOccupied: false,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
// Set up a new hashmail server.
hm := newHashMailHarness(t, hashMailServerConfig{
staleTimeout: test.staleTimeout,
})
// Create two clients of the hashmail server.
conn1 := hm.newClientConn()
conn2 := hm.newClientConn()
client1 := hashmailrpc.NewHashMailClient(conn1)
client2 := hashmailrpc.NewHashMailClient(conn2)
// Let client 1 create a mailbox on the server.
resp, err := client1.NewCipherBox(
ctx, &hashmailrpc.CipherBoxAuth{
Auth: &hashmailrpc.CipherBoxAuth_LndAuth{},
Desc: testStreamDesc,
},
)
require.NoError(t, err)
require.NotNil(t, resp.GetSuccess())
// Assert that neither of the mailbox streams are
// occupied to start with.
hm.assertStreamsOccupied(statusState{
readOccupied: false,
writeOccupied: false,
})
// Let client 1 take the send-stream and write to it.
err = sendToStream(client1)
require.NoError(t, err)
hm.assertStreamsOccupied(test.senderConnected)
// Let client 2 take the read stream and receive from
// it.
err = recvFromStream(client2)
require.NoError(t, err)
hm.assertStreamsOccupied(test.readerConnected)
// Ensure that attempting to take the read stream and
// receive from it while it is currently occupied will
// result in an error.
err = recvFromStream(client2)
require.Error(t, err)
assert.Contains(t, err.Error(), "read stream occupied")
hm.assertStreamsOccupied(test.readerConnected)
// Disconnect client 1. This should release the
// send-stream.
require.NoError(t, conn1.Close())
hm.assertStreamsOccupied(test.senderDisconnected)
// Disconnect client 1. This should release the
// read-stream.
require.NoError(t, conn2.Close())
// Assert that neither of the streams are occupied.
hm.assertStreamsOccupied(statusState{
readOccupied: false,
writeOccupied: false,
})
// Assert that the stream is torn down.
hm.assertStreamExists(!test.expectStaleMailboxRemoval)
})
}
}
// hashMailHarness is a test harness that spins up a hashmail server for
// testing purposes.
type hashMailHarness struct {
t *testing.T
server *hashMailServer
lis *bufconn.Listener
}
// newHashMailHarness spins up a new hashmail server and serves it on a bufconn
// listener.
func newHashMailHarness(t *testing.T,
cfg hashMailServerConfig) *hashMailHarness {
hm := newHashMailServer(cfg)
lis := bufconn.Listen(1024 * 1024)
hashMailGRPC := grpc.NewServer()
t.Cleanup(hashMailGRPC.Stop)
hashmailrpc.RegisterHashMailServer(hashMailGRPC, hm)
go func() {
require.NoError(t, hashMailGRPC.Serve(lis))
}()
return &hashMailHarness{
t: t,
server: hm,
lis: lis,
}
}
// newClientConn creates a new client of the hashMailHarness server.
func (h *hashMailHarness) newClientConn() *grpc.ClientConn {
conn, err := grpc.Dial("bufnet", grpc.WithContextDialer(
func(ctx context.Context, s string) (net.Conn, error) {
return h.lis.Dial()
}), grpc.WithInsecure(),
)
require.NoError(h.t, err)
h.t.Cleanup(func() {
_ = conn.Close()
})
return conn
}
// assertStreamOccupied checks that the current state of the stream's read and
// writes streams are the same as the expected state.
func (h *hashMailHarness) assertStreamsOccupied(state statusState) {
err := wait.Predicate(func() bool {
h.server.Lock()
defer h.server.Unlock()
stream, ok := h.server.streams[testSID]
if !ok {
return false
}
stream.status.Lock()
defer stream.status.Unlock()
if stream.status.readStreamOccupied != state.readOccupied {
return false
}
return stream.status.writeStreamOccupied == state.writeOccupied
}, time.Second)
require.NoError(h.t, err)
}
// assertStreamExists ensures that the test stream does or does not exist
// depending on the value of the boolean passed in.
func (h *hashMailHarness) assertStreamExists(exists bool) {
err := wait.Predicate(func() bool {
h.server.Lock()
defer h.server.Unlock()
_, ok := h.server.streams[testSID]
return ok == exists
}, time.Second)
require.NoError(h.t, err)
}
// sendToStream is a helper function that attempts to send dummy data to the
// test stream using the given client.
func sendToStream(client hashmailrpc.HashMailClient) error {
writeStream, err := client.SendStream(context.Background())
if err != nil {
return err
}
return writeStream.Send(&hashmailrpc.CipherBox{
Desc: testStreamDesc,
Msg: testMessage,
})
}
// recvFromStream is a helper function that attempts to receive dummy data from
// the test stream using the given client.
func recvFromStream(client hashmailrpc.HashMailClient) error {
readStream, err := client.RecvStream(
context.Background(), testStreamDesc,
)
if err != nil {
return err
}
recvChan := make(chan *hashmailrpc.CipherBox)
errChan := make(chan error)
go func() {
box, err := readStream.Recv()
if err != nil {
errChan <- err
}
recvChan <- box
}()
select {
case <-time.After(time.Second):
return fmt.Errorf("timed out waiting to receive from receive " +
"stream")
case err := <-errChan:
return err
case <-recvChan:
}
return nil
}