2022-10-13 08:58:11 +00:00
|
|
|
// Copyright (c) 2022 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/>.
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-09 21:05:52 +00:00
|
|
|
"crypto/tls"
|
2022-08-26 15:00:21 +00:00
|
|
|
"fmt"
|
2022-10-11 17:40:28 +00:00
|
|
|
"net/http/cookiejar"
|
2022-10-28 13:44:32 +00:00
|
|
|
"os"
|
2022-10-24 16:56:15 +00:00
|
|
|
"time"
|
2022-08-26 15:00:21 +00:00
|
|
|
|
2022-10-28 13:44:32 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
|
2022-10-11 17:40:28 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/cookies"
|
2022-08-26 15:00:21 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/events"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/useragent"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
|
2022-10-09 21:05:52 +00:00
|
|
|
"gitlab.protontech.ch/go/liteapi"
|
2022-08-26 15:00:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (t *testCtx) startBridge() error {
|
|
|
|
// Bridge will enable the proxy by default at startup.
|
2022-10-09 21:05:52 +00:00
|
|
|
t.mocks.ProxyCtl.EXPECT().AllowProxy()
|
2022-08-26 15:00:21 +00:00
|
|
|
|
|
|
|
// Get the path to the vault.
|
|
|
|
vaultDir, err := t.locator.ProvideSettingsPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default gluon path.
|
|
|
|
gluonDir, err := t.locator.ProvideGluonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the vault.
|
|
|
|
vault, corrupt, err := vault.New(vaultDir, gluonDir, t.storeKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if corrupt {
|
|
|
|
return fmt.Errorf("vault is corrupt")
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:24:20 +00:00
|
|
|
// Create the underlying cookie jar.
|
2022-10-11 17:40:28 +00:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-11 21:24:20 +00:00
|
|
|
// Create the persisting cookie jar.
|
2022-10-11 17:40:28 +00:00
|
|
|
persister, err := cookies.NewCookieJar(jar, vault)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:44:32 +00:00
|
|
|
var logIMAP bool
|
|
|
|
|
|
|
|
if len(os.Getenv("FEATURE_TEST_LOG_IMAP")) != 0 {
|
|
|
|
logrus.SetLevel(logrus.TraceLevel)
|
|
|
|
logIMAP = true
|
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// Create the bridge.
|
2022-10-11 22:20:04 +00:00
|
|
|
bridge, eventCh, err := bridge.New(
|
2022-10-13 00:33:20 +00:00
|
|
|
// App stuff
|
2022-08-26 15:00:21 +00:00
|
|
|
t.locator,
|
|
|
|
vault,
|
2022-10-11 17:40:28 +00:00
|
|
|
t.mocks.Autostarter,
|
|
|
|
t.mocks.Updater,
|
|
|
|
t.version,
|
|
|
|
|
2022-10-13 00:33:20 +00:00
|
|
|
// API stuff
|
2022-10-11 17:40:28 +00:00
|
|
|
t.api.GetHostURL(),
|
|
|
|
persister,
|
2022-08-26 15:00:21 +00:00
|
|
|
useragent.New(),
|
|
|
|
t.mocks.TLSReporter,
|
2022-10-09 21:05:52 +00:00
|
|
|
liteapi.NewDialer(t.netCtl, &tls.Config{InsecureSkipVerify: true}).GetRoundTripper(),
|
|
|
|
t.mocks.ProxyCtl,
|
2022-10-11 17:40:28 +00:00
|
|
|
|
2022-10-13 00:33:20 +00:00
|
|
|
// Logging stuff
|
2022-10-28 13:44:32 +00:00
|
|
|
logIMAP,
|
|
|
|
logIMAP,
|
2022-10-11 14:03:28 +00:00
|
|
|
false,
|
2022-08-26 15:00:21 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:56:15 +00:00
|
|
|
t.events.collectFrom(eventCh)
|
2022-10-13 00:33:20 +00:00
|
|
|
|
2022-10-11 22:20:04 +00:00
|
|
|
// Wait for the users to be loaded.
|
2022-10-25 10:03:06 +00:00
|
|
|
t.events.await(events.AllUsersLoaded{}, 30*time.Second)
|
2022-10-11 22:20:04 +00:00
|
|
|
|
2022-10-13 00:33:20 +00:00
|
|
|
// Save the bridge to the context.
|
2022-08-26 15:00:21 +00:00
|
|
|
t.bridge = bridge
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testCtx) stopBridge() error {
|
2022-10-26 21:09:05 +00:00
|
|
|
if t.bridge == nil {
|
|
|
|
return fmt.Errorf("bridge is not running")
|
2022-08-26 15:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:09:05 +00:00
|
|
|
t.bridge.Close(context.Background())
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
t.bridge = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|