File size: 5,167 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package bridge

import (
	"os"
	"path/filepath"
	"reflect"
	"testing"
)

func TestIsChromeProfileLockError(t *testing.T) {
	t.Parallel()

	msg := "chrome failed to start: [2046:2046:0309/221021.856597:ERROR:chrome/browser/process_singleton_posix.cc:363] The profile appears to be in use by another Chromium process"
	if !isChromeProfileLockError(msg) {
		t.Fatal("expected profile lock error to be detected")
	}
}

func TestParseChromeProfileProcesses(t *testing.T) {
	t.Parallel()

	profileDir := "/data/.config/pinchtab/profiles/default"
	out := []byte("  36 /usr/bin/chromium-browser --user-data-dir=/data/.config/pinchtab/profiles/default --remote-debugging-port=9222\n  99 /usr/bin/chromium-browser --user-data-dir=/tmp/other\n")

	got := parseChromeProfileProcesses(out, profileDir)
	want := []chromeProfileProcess{
		{
			PID:     "36",
			Command: "/usr/bin/chromium-browser --user-data-dir=/data/.config/pinchtab/profiles/default --remote-debugging-port=9222",
		},
	}

	if !reflect.DeepEqual(got, want) {
		t.Fatalf("parseChromeProfileProcesses() = %#v, want %#v", got, want)
	}
}

func TestExtractChromeProfileLockPID(t *testing.T) {
	t.Parallel()

	msg := "The profile appears to be in use by another Chromium process (36) on another computer"
	pid, ok := extractChromeProfileLockPID(msg)
	if !ok {
		t.Fatal("expected pid to be parsed from profile lock error")
	}
	if pid != 36 {
		t.Fatalf("extractChromeProfileLockPID() = %d, want 36", pid)
	}
}

func TestClearStaleChromeProfileLockRemovesSingletonFiles(t *testing.T) {
	profileDir := t.TempDir()
	for _, name := range chromeSingletonFiles {
		if err := os.WriteFile(filepath.Join(profileDir, name), []byte("x"), 0644); err != nil {
			t.Fatalf("write %s: %v", name, err)
		}
	}

	orig := chromeProfileProcessLister
	origPID := chromePIDIsRunning
	origMock := isProfileOwnedByRunningPinchtabMock
	chromeProfileProcessLister = func(string) ([]chromeProfileProcess, error) {
		return nil, nil
	}
	chromePIDIsRunning = func(int) (bool, error) {
		return false, nil
	}
	isProfileOwnedByRunningPinchtabMock = func(string) (bool, int) {
		return false, 0
	}
	t.Cleanup(func() {
		chromeProfileProcessLister = orig
		chromePIDIsRunning = origPID
		isProfileOwnedByRunningPinchtabMock = origMock
	})

	removed, err := clearStaleChromeProfileLock(profileDir, "")
	if err != nil {
		t.Fatalf("clearStaleChromeProfileLock() error = %v", err)
	}
	if !removed {
		t.Fatal("expected singleton files to be removed")
	}

	for _, name := range chromeSingletonFiles {
		if _, err := os.Lstat(filepath.Join(profileDir, name)); !os.IsNotExist(err) {
			t.Fatalf("expected %s to be removed, got err=%v", name, err)
		}
	}
}

func TestClearStaleChromeProfileLockLeavesActiveProfileUntouched(t *testing.T) {
	profileDir := t.TempDir()
	lockPath := filepath.Join(profileDir, chromeSingletonFiles[0])
	if err := os.WriteFile(lockPath, []byte("x"), 0644); err != nil {
		t.Fatalf("write lock file: %v", err)
	}

	orig := chromeProfileProcessLister
	origPID := chromePIDIsRunning
	origMock := isProfileOwnedByRunningPinchtabMock
	chromeProfileProcessLister = func(string) ([]chromeProfileProcess, error) {
		return []chromeProfileProcess{{PID: "36", Command: "/usr/bin/chromium-browser --user-data-dir=" + profileDir}}, nil
	}
	chromePIDIsRunning = func(int) (bool, error) {
		return false, nil
	}
	isProfileOwnedByRunningPinchtabMock = func(string) (bool, int) {
		return true, 1234
	}
	t.Cleanup(func() {
		chromeProfileProcessLister = orig
		chromePIDIsRunning = origPID
		isProfileOwnedByRunningPinchtabMock = origMock
	})

	removed, err := clearStaleChromeProfileLock(profileDir, "")
	if err != nil {
		t.Fatalf("clearStaleChromeProfileLock() error = %v", err)
	}
	if removed {
		t.Fatal("expected active profile lock to remain in place")
	}
	if _, err := os.Lstat(lockPath); err != nil {
		t.Fatalf("expected lock file to remain, got err=%v", err)
	}
}

func TestClearStaleChromeProfileLockFallsBackToPIDProbe(t *testing.T) {
	profileDir := t.TempDir()
	lockPath := filepath.Join(profileDir, chromeSingletonFiles[0])
	if err := os.WriteFile(lockPath, []byte("x"), 0644); err != nil {
		t.Fatalf("write lock file: %v", err)
	}

	orig := chromeProfileProcessLister
	origPID := chromePIDIsRunning
	origMock := isProfileOwnedByRunningPinchtabMock
	chromeProfileProcessLister = func(string) ([]chromeProfileProcess, error) {
		return nil, os.ErrPermission
	}
	chromePIDIsRunning = func(pid int) (bool, error) {
		if pid != 36 {
			t.Fatalf("unexpected pid probe: got %d, want 36", pid)
		}
		return false, nil
	}
	isProfileOwnedByRunningPinchtabMock = func(string) (bool, int) {
		return false, 0
	}
	t.Cleanup(func() {
		chromeProfileProcessLister = orig
		chromePIDIsRunning = origPID
		isProfileOwnedByRunningPinchtabMock = origMock
	})

	removed, err := clearStaleChromeProfileLock(profileDir, "another Chromium process (36)")
	if err != nil {
		t.Fatalf("clearStaleChromeProfileLock() error = %v", err)
	}
	if !removed {
		t.Fatal("expected singleton file to be removed after stale pid probe")
	}
	if _, err := os.Lstat(lockPath); !os.IsNotExist(err) {
		t.Fatalf("expected lock file to be removed, got err=%v", err)
	}
}