forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher_test.go
362 lines (285 loc) · 8.53 KB
/
launcher_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
package launcher_test
import (
"archive/zip"
"bytes"
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/go-rod/rod/lib/defaults"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/got"
)
var setup = got.Setup(nil)
func TestDownloadHosts(t *testing.T) {
g := setup(t)
g.Has(launcher.HostGoogle(launcher.RevisionDefault), "https://storage.googleapis.com/chromium-browser-snapshots")
g.Has(launcher.HostNPM(launcher.RevisionDefault), "https://registry.npmmirror.com/-/binary/chromium-browser-snapshots")
g.Has(launcher.HostPlaywright(launcher.RevisionDefault), "https://playwright.azureedge.net/")
}
func TestDownload(t *testing.T) {
g := setup(t)
s := g.Serve()
s.Mux.HandleFunc("/fast/", func(rw http.ResponseWriter, r *http.Request) {
buf := bytes.NewBuffer(nil)
zw := zip.NewWriter(buf)
// folder "to"
h := &zip.FileHeader{Name: "to/"}
h.SetMode(0755)
_, err := zw.CreateHeader(h)
g.E(err)
// file "file.txt"
w, err := zw.CreateHeader(&zip.FileHeader{Name: "to/file.txt"})
g.E(err)
b := []byte(g.RandStr(2 * 1024 * 1024))
g.E(w.Write(b))
g.E(zw.Close())
rw.Header().Add("Content-Length", fmt.Sprintf("%d", buf.Len()))
_, _ = io.Copy(rw, buf)
})
s.Mux.HandleFunc("/slow/", func(rw http.ResponseWriter, r *http.Request) {
t := time.NewTimer(3 * time.Second)
select {
case <-t.C:
case <-r.Context().Done():
t.Stop()
}
})
b, cancel := newBrowser()
b.Logger = utils.LoggerQuiet
defer cancel()
b.Hosts = []launcher.Host{launcher.HostTest(s.URL("/slow")), launcher.HostTest(s.URL("/fast"))}
b.Dir = filepath.Join("tmp", "browser-from-mirror", g.RandStr(16))
g.E(b.Download())
g.Nil(os.Stat(b.Dir))
// download chrome with a proxy
// should fail with self signed certificate
p := httptest.NewTLSServer(&httputil.ReverseProxy{Director: func(_ *http.Request) {}})
defer p.Close()
// invalid proxy URL should trigger an error
err := b.Proxy(`invalid.escaping%%2`)
g.Eq(err.Error(), `parse "invalid.escaping%%2": invalid URL escape "%%2"`)
g.E(b.Proxy(p.URL))
g.NotNil(b.Download())
// should instead be successful with ignore certificate
b.IgnoreCerts = true
g.E(b.Download())
g.Nil(os.Stat(b.Dir))
}
func TestBrowserGet(t *testing.T) {
g := setup(t)
g.Nil(os.Stat(launcher.NewBrowser().MustGet()))
b := launcher.NewBrowser()
b.Revision = 0
b.Logger = utils.LoggerQuiet
_, err := b.Get()
g.Eq(err.Error(), "Can't find a browser binary for your OS, the doc might help https://go-rod.github.io/#/compatibility?id=os")
}
func TestLaunch(t *testing.T) {
g := setup(t)
defaults.Proxy = "test.com"
defer func() { defaults.ResetWith("") }()
l := launcher.New()
defer l.Kill()
u := l.MustLaunch()
g.Regex(`\Aws://.+\z`, u)
parsed, _ := url.Parse(u)
{ // test GetWebSocketDebuggerURL
for _, prefix := range []string{"", ":", "127.0.0.1:", "ws://127.0.0.1:"} {
u2 := launcher.MustResolveURL(prefix + parsed.Port())
g.Regex(u, u2)
}
_, err := launcher.ResolveURL("")
g.Err(err)
}
{
_, err := launcher.NewManaged("")
g.Err(err)
_, err = launcher.NewManaged("1://")
g.Err(err)
_, err = launcher.NewManaged("ws://not-exists")
g.Err(err)
}
{
g.Panic(func() { launcher.New().Set("a=b") })
}
}
func TestLaunchUserMode(t *testing.T) {
g := setup(t)
l := launcher.NewUserMode()
defer l.Kill()
l.Kill() // empty kill should do nothing
has := l.Has("not-exists")
g.False(has)
l.Append("test-append", "a")
f := l.Get("test-append")
g.Eq("a", f)
dir := l.Get(flags.UserDataDir)
port := 58472
l = l.Context(g.Context()).Delete("test").Bin("").
Revision(launcher.RevisionDefault).
Logger(ioutil.Discard).
Leakless(false).Leakless(true).
Headless(false).Headless(true).RemoteDebuggingPort(port).
NoSandbox(true).NoSandbox(false).
Devtools(true).Devtools(false).
StartURL("about:blank").
Proxy("test.com").
UserDataDir("test").UserDataDir(dir).
WorkingDir("").
Env(append(os.Environ(), "TZ=Asia/Tokyo")...)
g.Eq(l.FormatArgs(), []string /* len=6 cap=8 */ {
"--headless",
`--no-startup-window`, /* len=19 */
`--proxy-server=test.com`, /* len=23 */
`--remote-debugging-port=58472`, /* len=29 */
"--test-append=a",
"about:blank",
})
url := l.MustLaunch()
g.Eq(url, launcher.NewUserMode().RemoteDebuggingPort(port).MustLaunch())
}
func TestUserModeErr(t *testing.T) {
g := setup(t)
_, err := launcher.NewUserMode().RemoteDebuggingPort(48277).Bin("not-exists").Launch()
g.Err(err)
_, err = launcher.NewUserMode().RemoteDebuggingPort(58217).Bin("echo").Launch()
g.Err(err)
}
func TestAppMode(t *testing.T) {
g := setup(t)
l := launcher.NewAppMode("http://example.com")
g.Eq(l.Get(flags.App), "http://example.com")
}
func TestGetWebSocketDebuggerURLErr(t *testing.T) {
g := setup(t)
_, err := launcher.ResolveURL("1://")
g.Err(err)
}
func TestLaunchErr(t *testing.T) {
g := setup(t)
g.Panic(func() {
launcher.New().Bin("not-exists").MustLaunch()
})
g.Panic(func() {
launcher.New().Headless(false).Bin("not-exists").MustLaunch()
})
g.Panic(func() {
launcher.New().ClientHeader()
})
{
l := launcher.New().XVFB()
_, _ = l.Launch()
l.Kill()
}
}
func newBrowser() (*launcher.Browser, func()) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
b := launcher.NewBrowser()
if !testing.Verbose() {
b.Logger = utils.LoggerQuiet
}
b.Context = ctx
return b, cancel
}
var testProfileDir = flag.Bool("test-profile-dir", false, "set it to test profile dir")
func TestProfileDir(t *testing.T) {
g := setup(t)
url := launcher.New().Headless(false).
ProfileDir("").ProfileDir("test-profile-dir")
if !*testProfileDir {
g.Skip("It's not CI friendly, so we skip it!")
}
url.MustLaunch()
userDataDir := url.Get(flags.UserDataDir)
file, err := os.Stat(filepath.Join(userDataDir, "test-profile-dir"))
g.E(err)
g.True(file.IsDir())
}
func TestBrowserValid(t *testing.T) {
g := setup(t)
b := launcher.NewBrowser()
b.Revision = 0
g.Err(b.Validate())
g.E(utils.Mkdir(filepath.Dir(b.Destination())))
g.Cleanup(func() { _ = os.RemoveAll(b.Destination()) })
g.E(exec.Command("go", "build", "-o", b.Destination(), "./fixtures/chrome-exit-err").CombinedOutput())
g.Has(b.Validate().Error(), "failed to run the browser")
g.E(exec.Command("go", "build", "-o", b.Destination(), "./fixtures/chrome-empty").CombinedOutput())
g.Eq(b.Validate().Error(), "the browser executable doesn't support headless mode")
g.E(exec.Command("go", "build", "-o", b.Destination(), "./fixtures/chrome-lib-missing").CombinedOutput())
g.Nil(b.Validate())
}
func TestIgnoreCerts(t *testing.T) {
g := setup(t)
// https://travistidwell.com/jsencrypt/demo/
testData := []string{
`-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgF9pr2zok5bivQIEUN7Y58a9uB1o
sroMt3hxNfzOh/G+sXgYPPoEl2/Ys/2zbvym7Ze0eGbb6FrV8aueg89TPTNWAKlN
N49q6S3zLG1WmI2rVYz4LtPgpg1YR9FQRIg4Ll0C02daufXgvUBGjIARH19FTw6P
61kEhnEQxUHhdAqbAgMBAAE=
-----END PUBLIC KEY-----
`,
`-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvBTz/TOYc66qB97OyYenSHk4T
hAUKX5RUWZ/80o0zyJoo1dfrrwW9PlT5o4DlGMs0NSbtJ8RMQRTLZwL/zxXjiEMv
dKFs2OrefYKANTc0e2XAtQAm3Is5Ro8AF1S4Fk+eZXr2yZtBRKXvhJ/A2bilVoSn
fmQnyBe7dVU43NXfrQIDAQAB
-----END PUBLIC KEY-----
`,
}
keys := make([]crypto.PublicKey, 0, len(testData))
for _, pubPEM := range testData {
block, _ := pem.Decode([]byte(pubPEM))
if block == nil {
g.Fatal("failed to parse PEM block containing the public key")
return // no-op because g.Fatal calls t.FailNow() but `staticcheck` doesn't know it
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
g.Fatalf("failed to parse DER encoded public key: " + err.Error())
}
keys = append(keys, pub)
}
l := launcher.New()
err := l.IgnoreCerts(keys)
if err != nil {
g.Fatalf("IgnoreCerts: %s", err)
}
expected := "--ignore-certificate-errors-spki-list=" + strings.Join([]string{
"+ZqfrXb+V/36nZecO59bghHlNhiHTzImjYLnNWGUd1I=",
"llpTCSqZ2/IKsMg4tz+o1mCkXIOdKcM6sKu9kC6o7S4=",
}, ",")
g.Has(l.FormatArgs(), expected)
}
func TestIgnoreCerts_InvalidCert(t *testing.T) {
g := setup(t)
l := launcher.New()
err := l.IgnoreCerts([]crypto.PublicKey{nil})
if err == nil {
g.Fatalf("IgnoreCerts: %s", err)
}
}
func TestIgnoreCerts_BrowserProxySkipValidation(t *testing.T) {
g := setup(t)
b := launcher.NewBrowser()
// certificate validation skip is disabled by default
g.False(b.IgnoreCerts)
}