From cdb42f0d6b47a60ceb647e3ac6a6ce66352dbae4 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Thu, 18 Jun 2020 20:42:29 +0200 Subject: [test] minimal qml tests just a minimal boilerplate. the idea is to import the qml files and assert that the states/widgets change accordingly if we mock the backend status. - Closes: #300 --- pkg/backend/api.go | 18 +++++++++++++----- pkg/backend/init.go | 12 ++++++------ pkg/backend/mocks.go | 15 ++++++++------- pkg/bitmask/init.go | 28 +++++++++++++++++----------- 4 files changed, 44 insertions(+), 29 deletions(-) (limited to 'pkg') diff --git a/pkg/backend/api.go b/pkg/backend/api.go index a19fd40..fea38db 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -45,10 +45,18 @@ func SubscribeToEvent(event string, f unsafe.Pointer) { subscribe(event, f) } -func InitializeBitmaskContext() { +type InitOpts struct { + Provider string + AppName string + SkipLaunch bool +} + +func InitializeBitmaskContext(opts *InitOpts) { p := bitmask.GetConfiguredProvider() + opts.Provider = p.Provider + opts.AppName = p.AppName - initOnce.Do(func() { initializeContext(p.Provider, p.AppName) }) + initOnce.Do(func() { initializeContext(opts) }) runDonationReminder() go ctx.updateStatus() } @@ -62,7 +70,7 @@ func InstallHelpers() { pickle.InstallHelpers() } -func MockUIInteraction() { - log.Println("mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.") - go mockUI() +func EnableMockBackend() { + log.Println("[+] Mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.") + go enableMockBackend() } diff --git a/pkg/backend/init.go b/pkg/backend/init.go index 5abb05e..79efdc7 100644 --- a/pkg/backend/init.go +++ b/pkg/backend/init.go @@ -12,11 +12,11 @@ import ( // initializeContext initializes an empty connStatus and assigns it to the // global ctx holder. This is expected to be called only once, so the public // api uses the sync.Once primitive to call this. -func initializeContext(provider, appName string) { +func initializeContext(opts *InitOpts) { var st status = off ctx = &connectionCtx{ - AppName: appName, - Provider: provider, + AppName: opts.AppName, + Provider: opts.Provider, TosURL: config.TosURL, HelpURL: config.HelpURL, DonateURL: config.DonateURL, @@ -28,7 +28,7 @@ func initializeContext(provider, appName string) { errCh := make(chan string) go trigger(OnStatusChanged) go checkErrors(errCh) - initializeBitmask(errCh) + initializeBitmask(errCh, opts) } func checkErrors(errCh chan string) { @@ -39,14 +39,14 @@ func checkErrors(errCh chan string) { } } -func initializeBitmask(errCh chan string) { +func initializeBitmask(errCh chan string, opts *InitOpts) { if ctx == nil { log.Println("bug: cannot initialize bitmask, ctx is nil!") os.Exit(1) } bitmask.InitializeLogger() - b, err := bitmask.InitializeBitmask() + b, err := bitmask.InitializeBitmask(opts.SkipLaunch) if err != nil { log.Println("error: cannot initialize bitmask") errCh <- err.Error() diff --git a/pkg/backend/mocks.go b/pkg/backend/mocks.go index a8ede73..226fa4e 100644 --- a/pkg/backend/mocks.go +++ b/pkg/backend/mocks.go @@ -9,6 +9,14 @@ import ( * should also show a good way of writing functionality tests just for the Qml * layer */ +func enableMockBackend() { + log.Println("[+] You should not use this in production!") + http.HandleFunc("/on", mockUIOn) + http.HandleFunc("/off", mockUIOff) + http.HandleFunc("/failed", mockUIFailed) + http.ListenAndServe(":8080", nil) +} + func mockUIOn(w http.ResponseWriter, r *http.Request) { log.Println("changing status: on") setStatus(on) @@ -23,10 +31,3 @@ func mockUIFailed(w http.ResponseWriter, r *http.Request) { log.Println("changing status: failed") setStatus(failed) } - -func mockUI() { - http.HandleFunc("/on", mockUIOn) - http.HandleFunc("/off", mockUIOff) - http.HandleFunc("/failed", mockUIFailed) - http.ListenAndServe(":8080", nil) -} diff --git a/pkg/bitmask/init.go b/pkg/bitmask/init.go index 33a5911..a96ab87 100644 --- a/pkg/bitmask/init.go +++ b/pkg/bitmask/init.go @@ -56,14 +56,18 @@ func initBitmask(printer *message.Printer) (Bitmask, error) { return b, err } -func InitializeBitmask() (Bitmask, error) { +func InitializeBitmask(skipLaunch bool) (Bitmask, error) { + if skipLaunch { + log.Println("Initializing bitmask, but not launching it...") + } if _, err := os.Stat(config.Path); os.IsNotExist(err) { os.MkdirAll(config.Path, os.ModePerm) } err := pid.AcquirePID() if err != nil { - log.Fatal(err) + log.Println("Error acquiring PID:", err) + return nil, err } defer pid.ReleasePID() @@ -75,13 +79,21 @@ func InitializeBitmask() (Bitmask, error) { return nil, err } - err = checkAndStartBitmask(b, conf) + err = setTransport(b, conf) if err != nil { return nil, err } + if !skipLaunch { + err := maybeStartVPN(b, conf) + if err != nil { + log.Println("Error starting VPN: ", err) + return nil, err + } + } + var as Autostart - if conf.DisableAustostart { + if skipLaunch || conf.DisableAustostart { as = &dummyAutostart{} } else { as = newAutostart(config.ApplicationName, "") @@ -103,7 +115,7 @@ func initPrinter() *message.Printer { return message.NewPrinter(message.MatchLanguage(locale, "en")) } -func checkAndStartBitmask(b Bitmask, conf *config.Config) error { +func setTransport(b Bitmask, conf *config.Config) error { if conf.Obfs4 { err := b.UseTransport("obfs4") if err != nil { @@ -111,12 +123,6 @@ func checkAndStartBitmask(b Bitmask, conf *config.Config) error { return err } } - - err := maybeStartVPN(b, conf) - if err != nil { - log.Println("Error starting VPN: ", err) - return err - } return nil } -- cgit v1.2.3