-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathctx_bridge_test.go
377 lines (297 loc) · 9.13 KB
/
ctx_bridge_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
// Copyright (c) 2025 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package tests
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
"os"
"path/filepath"
"runtime"
"time"
"github.com/ProtonMail/gluon/async"
"github.com/ProtonMail/gluon/imap"
"github.com/ProtonMail/proton-bridge/v3/internal/bridge"
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/ProtonMail/proton-bridge/v3/internal/cookies"
"github.com/ProtonMail/proton-bridge/v3/internal/dialer"
"github.com/ProtonMail/proton-bridge/v3/internal/events"
frontend "github.com/ProtonMail/proton-bridge/v3/internal/frontend/grpc"
"github.com/ProtonMail/proton-bridge/v3/internal/service"
"github.com/ProtonMail/proton-bridge/v3/internal/useragent"
"github.com/ProtonMail/proton-bridge/v3/internal/vault"
"github.com/ProtonMail/proton-bridge/v3/pkg/keychain"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
)
func (t *testCtx) startBridge() error {
logrus.Info("Starting bridge")
eventCh, err := t.initBridge()
if err != nil {
return fmt.Errorf("could not create bridge: %w", err)
}
logrus.Info("Starting frontend service")
if err := t.initFrontendService(eventCh); err != nil {
return fmt.Errorf("could not create frontend service: %w", err)
}
logrus.Info("Starting frontend client")
if err := t.initFrontendClient(); err != nil {
return fmt.Errorf("could not create frontend client: %w", err)
}
t.events.await(events.AllUsersLoaded{}, 30*time.Second)
return nil
}
func (t *testCtx) stopBridge() error {
if err := t.closeFrontendService(context.Background()); err != nil {
return fmt.Errorf("could not close frontend: %w", err)
}
if err := t.closeFrontendClient(); err != nil {
return fmt.Errorf("could not close frontend client: %w", err)
}
if err := t.closeBridge(context.Background()); err != nil {
return fmt.Errorf("could not close bridge: %w", err)
}
return nil
}
func (t *testCtx) initBridge() (<-chan events.Event, error) {
if t.bridge != nil {
return nil, fmt.Errorf("bridge is already started")
}
// Bridge will disable the proxy by default at startup.
t.mocks.ProxyCtl.EXPECT().DisallowProxy()
// Get the path to the vault.
vaultDir, err := t.locator.ProvideSettingsPath()
if err != nil {
return nil, fmt.Errorf("could not get vault dir: %w", err)
}
// Get the default gluon path.
gluonCacheDir, err := t.locator.ProvideGluonDataPath()
if err != nil {
return nil, fmt.Errorf("could not get gluon dir: %w", err)
}
// Create the vault.
vault, corrupt, err := vault.New(vaultDir, gluonCacheDir, t.storeKey, async.NoopPanicHandler{})
if err != nil {
return nil, fmt.Errorf("could not create vault: %w", err)
} else if corrupt != nil {
return nil, fmt.Errorf("vault is corrupt: %w", corrupt)
}
t.vault = vault
// Create the underlying cookie jar.
jar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("could not create cookie jar: %w", err)
}
// Create the persisting cookie jar.
persister, err := cookies.NewCookieJar(jar, vault)
if err != nil {
return nil, fmt.Errorf("could not create cookie persister: %w", err)
}
var (
logIMAP bool
logSMTP bool
)
if len(os.Getenv("FEATURE_TEST_LOG_IMAP")) != 0 {
logIMAP = true
}
if len(os.Getenv("FEATURE_TEST_LOG_SMTP")) != 0 {
logSMTP = true
}
if logIMAP || logSMTP {
logrus.SetLevel(logrus.TraceLevel)
}
rt := t.netCtl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})
// We store the round tripper in the testing context so we can cancel the connection
// when we're turning it down/up
t.rt = &rt
if isBlack() {
// GODT-1602 make sure we don't time out test server
t, ok := rt.(*http.Transport)
if !ok {
panic("expecting http.Transport")
}
dialer.SetBasicTransportTimeouts(t)
}
// Create the bridge.
bridge, eventCh, err := bridge.New(
// App stuff
t.locator,
vault,
t.mocks.Autostarter,
t.mocks.Updater,
t.version,
keychain.NewTestKeychainsList(),
// API stuff
t.api.GetHostURL(),
persister,
useragent.New(),
t.mocks.TLSReporter,
rt,
t.mocks.ProxyCtl,
t.mocks.CrashHandler,
t.reporter,
imap.DefaultEpochUIDValidityGenerator(),
t.heartbeat,
// Logging stuff
logIMAP,
logIMAP,
logSMTP,
)
if err != nil {
return nil, fmt.Errorf("could not create bridge: %w", err)
}
t.bridge = bridge
t.heartbeat.setBridge(bridge)
return t.events.collectFrom(eventCh), nil
}
func (t *testCtx) closeBridge(ctx context.Context) error {
if t.bridge == nil {
return fmt.Errorf("bridge is not started")
}
t.bridge.Close(ctx)
t.bridge = nil
return nil
}
func (t *testCtx) initFrontendService(eventCh <-chan events.Event) error {
if t.service != nil {
return fmt.Errorf("frontend service is already started")
}
// When starting the frontend, we might enable autostart on bridge if it isn't already.
t.mocks.Autostarter.EXPECT().Enable().AnyTimes()
t.mocks.Autostarter.EXPECT().IsEnabled().AnyTimes()
service, err := frontend.NewService(
&async.NoopPanicHandler{},
new(mockRestarter),
t.locator,
t.bridge,
eventCh,
make(chan struct{}),
true,
-1,
)
if err != nil {
return fmt.Errorf("could not create service: %w", err)
}
logrus.Info("Frontend service started")
t.service = service
t.serviceWG.Add(1)
go func() {
defer t.serviceWG.Done()
if err := service.Loop(); err != nil {
panic(err)
}
}()
return nil
}
func (t *testCtx) closeFrontendService(ctx context.Context) error {
if t.service == nil {
return fmt.Errorf("frontend service is not started")
}
if _, err := t.client.Quit(ctx, &emptypb.Empty{}); err != nil {
return fmt.Errorf("could not quit frontend: %w", err)
}
t.serviceWG.Wait()
logrus.Info("Frontend service stopped")
t.service = nil
return nil
}
func (t *testCtx) initFrontendClient() error {
if t.client != nil {
return fmt.Errorf("frontend client is already started")
}
settings, err := t.locator.ProvideSettingsPath()
if err != nil {
return fmt.Errorf("could not get settings path: %w", err)
}
b, err := os.ReadFile(filepath.Join(settings, "grpcServerConfig.json"))
if err != nil {
return fmt.Errorf("could not read grpcServerConfig.json: %w", err)
}
var cfg service.Config
if err := json.Unmarshal(b, &cfg); err != nil {
return fmt.Errorf("could not unmarshal grpcServerConfig.json: %w", err)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM([]byte(cfg.Cert)) {
return fmt.Errorf("failed to append certificates to pool")
}
var target string
if len(cfg.FileSocketPath) != 0 {
target = "unix://" + cfg.FileSocketPath
} else {
target = fmt.Sprintf("%v:%d", constants.Host, cfg.Port)
}
conn, err := grpc.DialContext(
context.Background(),
target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{RootCAs: cp, ServerName: "127.0.0.1"})),
grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(metadata.AppendToOutgoingContext(ctx, "server-token", cfg.Token), method, req, reply, cc, opts...)
}),
)
if err != nil {
return fmt.Errorf("could not dial grpc server: %w", err)
}
client := frontend.NewBridgeClient(conn)
stream, err := client.RunEventStream(context.Background(), &frontend.EventStreamRequest{ClientPlatform: runtime.GOOS})
if err != nil {
return fmt.Errorf("could not start event stream: %w", err)
}
eventCh := async.NewQueuedChannel[*frontend.StreamEvent](0, 0, async.NoopPanicHandler{}, "test-frontend-client")
go func() {
defer eventCh.CloseAndDiscardQueued()
for {
event, err := stream.Recv()
if err != nil {
return
}
eventCh.Enqueue(event)
}
}()
logrus.Info("Frontend client started")
t.client = client
t.clientConn = conn
t.clientEventCh = eventCh
return nil
}
func (t *testCtx) closeFrontendClient() error {
if t.client == nil {
return fmt.Errorf("frontend client is not started")
}
if err := t.clientConn.Close(); err != nil {
return fmt.Errorf("could not close frontend client connection: %w", err)
}
logrus.Info("Frontend client stopped")
t.client = nil
t.clientConn = nil
t.clientEventCh = nil
return nil
}
func (t *testCtx) expectProxyCtlAllowProxy() {
t.mocks.ProxyCtl.EXPECT().AllowProxy()
}
type mockRestarter struct{}
func (m *mockRestarter) Set(_, _ bool) {}
func (m *mockRestarter) AddFlags(_ ...string) {}
func (m *mockRestarter) Override(_ string) {}