arblant
firefox-vpn-client / core / internal / update / apply_test.go
apply_test.go196 lines · 5.5 KB
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
package update

import (
	"encoding/json"
	"errors"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

// fakeProcesses replaces the processes Apply waits for and starts, and
// Windows' list of installed apps.
type fakeProcesses struct {
	waited  []int
	waitErr error
	started []string
	marked  []string
}

func (f *fakeProcesses) install(t *testing.T) {
	t.Helper()
	waitExited = func(pids []int, _ time.Duration) error {
		f.waited = pids
		return f.waitErr
	}
	launchApp = func(appDir string, args []string) error {
		f.started = append([]string{appDir}, args...)
		return nil
	}
	markInstalled = func(appDir, v string) error {
		f.marked = []string{appDir, v}
		return nil
	}
	t.Cleanup(func() {
		waitExited, launchApp, markInstalled = waitForExit, startApp, setInstalledVersion
	})
}

func readResult(t *testing.T, dataDir string) Result {
	t.Helper()
	var r Result
	if err := json.Unmarshal([]byte(read(t, filepath.Join(dataDir, "update", "result.json"))), &r); err != nil {
		t.Fatal(err)
	}
	return r
}

func TestApplySwapsAPortableCopyAndStartsTheApp(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	newRelease(t, "1.2.3").serve(t)
	appDir, dataDir := appFolder(t, "1.0.0", false)
	installed(t, appDir, dataDir, "--autostart")
	var f fakeProcesses
	f.install(t)
	if err := Apply(dataDir); err != nil {
		t.Fatal(err)
	}
	if len(f.waited) != 2 || f.waited[0] != 4242 {
		t.Fatalf("waited for %v", f.waited)
	}
	if read(t, filepath.Join(appDir, "core", "firefox-vpn-core.exe")) != "core 1.2.3" || read(t, filepath.Join(appDir, "user-data", "config.json")) != "the user's settings" {
		t.Fatal("not swapped")
	}
	if r := readResult(t, dataDir); !r.OK || r.From != "1.0.0" || r.To != "1.2.3" || !r.On {
		t.Fatalf("%+v", r)
	}
	if strings.Join(f.started, " ") != appDir+" --autostart" {
		t.Fatalf("started %v", f.started)
	}
	if _, err := os.Stat(filepath.Join(dataDir, "update", "pending.json")); !errors.Is(err, os.ErrNotExist) {
		t.Fatal("the instructions stayed")
	}
	// The new version's core reads the result and cleans up.
	m := New(Options{DataDir: dataDir, AppDir: appDir, Current: "1.2.3"})
	if s := m.Status(); s.Last == nil || !s.Last.OK {
		t.Fatalf("%+v", s)
	}
	if _, err := os.Stat(filepath.Join(dataDir, "update", "backup")); !errors.Is(err, os.ErrNotExist) {
		t.Fatal("the old files stayed")
	}
}

// A copy Setup installed is swapped the same way, keeps Setup's
// uninstaller, and gets its new version in Windows' list.
func TestApplySwapsAnInstalledCopyAndSetsItsVersion(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	newRelease(t, "1.2.3").serve(t)
	appDir, dataDir := appFolder(t, "1.0.0", true)
	installed(t, appDir, dataDir)
	var f fakeProcesses
	f.install(t)
	if err := Apply(dataDir); err != nil {
		t.Fatal(err)
	}
	if read(t, filepath.Join(appDir, "core", "firefox-vpn-core.exe")) != "core 1.2.3" || read(t, filepath.Join(appDir, "unins000.exe")) != "uninstaller" {
		t.Fatal("not swapped, or the uninstaller went")
	}
	if strings.Join(f.marked, " ") != appDir+" 1.2.3" {
		t.Fatalf("marked %v", f.marked)
	}
}

// After a failed swap the old version starts and says why, and Windows'
// list keeps its version.
func TestAFailedApplyStartsTheOldVersion(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	newRelease(t, "1.2.3").serve(t)
	appDir, dataDir := appFolder(t, "1.0.0", true)
	installed(t, appDir, dataDir)
	renameWait = 50 * time.Millisecond
	rename = func(from, to string) error {
		if filepath.Base(from) == "data" && strings.Contains(from, filepath.Join("update", "new")) {
			return errors.New("in use")
		}
		return os.Rename(from, to)
	}
	t.Cleanup(func() { rename, renameWait = os.Rename, 10*time.Second })
	var f fakeProcesses
	f.install(t)
	if err := Apply(dataDir); err == nil {
		t.Fatal("no error")
	}
	if read(t, filepath.Join(appDir, "core", "firefox-vpn-core.exe")) != "core 1.0.0" || read(t, filepath.Join(appDir, "data", "app.so")) != "assets 1.0.0" {
		t.Fatal("the old version is not back")
	}
	if r := readResult(t, dataDir); r.OK || !strings.Contains(r.Error, "data") {
		t.Fatalf("%+v", r)
	}
	if len(f.started) != 1 || f.started[0] != appDir || f.marked != nil {
		t.Fatalf("started %v, marked %v", f.started, f.marked)
	}
}

func TestApplyLeavesARunningAppAlone(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	newRelease(t, "1.2.3").serve(t)
	appDir, dataDir := appFolder(t, "1.0.0", false)
	installed(t, appDir, dataDir)
	var f fakeProcesses
	f.waitErr = errors.New("the app did not close")
	f.install(t)
	if err := Apply(dataDir); err == nil {
		t.Fatal("no error")
	}
	if read(t, filepath.Join(appDir, "firefox-vpn.exe")) != "app 1.0.0" || f.started != nil {
		t.Fatal("changed a running app")
	}
	if r := readResult(t, dataDir); r.OK {
		t.Fatalf("%+v", r)
	}
}

func TestWaitForExitWaitsForRealProcesses(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	// This process does not exit: the wait times out. A process id that
	// does not exist counts as exited.
	if err := waitForExit([]int{os.Getpid()}, 50*time.Millisecond); err == nil {
		t.Fatal("this process counted as exited")
	}
	if err := waitForExit([]int{1 << 30}, time.Second); err != nil {
		t.Fatal(err)
	}
}

func TestOnlyOneUpdateOfAnAppRunsAtATime(t *testing.T) {
	if !platformSupported {
		t.Skip("Windows only")
	}
	appDir := t.TempDir()
	unlock, err := lockApp(appDir)
	if err != nil {
		t.Fatal(err)
	}
	if !updating(appDir) || updating(t.TempDir()) {
		t.Fatal("updating() is wrong")
	}
	if _, err := lockApp(appDir); err == nil {
		t.Fatal("locked twice")
	}
	unlock()
	if updating(appDir) {
		t.Fatal("still updating")
	}
}