package wasm import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "os/exec" "regexp" "strings" "testing" "time" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/runtime" "github.com/chromedp/chromedp" ) func run(t *testing.T, cmdline string) error { args := strings.Fields(cmdline) return runargs(t, args...) } func runargs(t *testing.T, args ...string) error { cmd := exec.Command(args[0], args[1:]...) b, err := cmd.CombinedOutput() t.Logf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b) if err != nil { return err } return nil } func chromectx(t *testing.T) context.Context { // see https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.NoSandbox, chromedp.Flag("disable-web-security", true), chromedp.Flag("safebrowsing-disable-auto-update", true), chromedp.IgnoreCertErrors, chromedp.Flag("disable-sync", true), chromedp.Flag("disable-default-apps", true), chromedp.NoFirstRun, chromedp.Headless, chromedp.WSURLReadTimeout(45*time.Second), ) allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) t.Cleanup(cancel) // looks for locally installed Chrome ctx, ccancel := chromedp.NewContext(allocCtx, chromedp.WithErrorf(t.Errorf), chromedp.WithDebugf(t.Logf), chromedp.WithLogf(t.Logf)) t.Cleanup(ccancel) // Wait for browser to be ready. err := chromedp.Run(ctx) if err != nil { t.Fatalf("failed to start browser: %s", err.Error()) } ctx, tcancel := context.WithTimeout(ctx, 30*time.Second) t.Cleanup(tcancel) return ctx } func startServer(t *testing.T) (string, *httptest.Server) { tmpDir := t.TempDir() fsh := http.FileServer(http.Dir(tmpDir)) h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/wasm_exec.js" { http.ServeFile(w, r, "../../targets/wasm_exec.js") return } if r.URL.Path == "/run" { fmt.Fprintf(w, `