forked from lightninglabs/aperture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmail_server_test.go
244 lines (204 loc) · 6.58 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
package aperture
import (
"context"
"crypto/rand"
"fmt"
"math"
"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/require"
"google.golang.org/grpc"
)
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")...)
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
}
}