test(chromium): cover concurrent cookie isolation (#1747)

Co-authored-by: marcel299 <29774059+marcel299@users.noreply.github.com>
This commit is contained in:
Marcel Danilo
2026-11-29 15:33:42 +00:00
committed by GitHub
parent 153339ada3
commit b0ad4341a6

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
@@ -13,6 +14,8 @@ import (
"time"
"github.com/chromedp/cdproto"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/dlclark/regexp2/v2"
)
@@ -39,6 +42,124 @@ func TestChromiumBrowser_Start_honorsDoneContext(t *testing.T) {
}
}
func TestChromiumBrowser_ConcurrentConversionsIsolateCookies(t *testing.T) {
binPath := os.Getenv("CHROMIUM_BIN_PATH")
if binPath == "" {
for _, name := range []string{"google-chrome", "chromium"} {
if path, err := exec.LookPath(name); err == nil {
binPath = path
break
}
}
}
if binPath == "" {
t.Skip("Chromium or Google Chrome is not installed")
}
logger := slog.New(slog.DiscardHandler)
b := newChromiumBrowser(browserArguments{
binPath: binPath,
hyphenDataDirPath: t.TempDir(),
wsUrlReadTimeout: 15 * time.Second,
}).(*chromiumBrowser)
if err := b.Start(context.Background(), logger); err != nil {
t.Fatalf("start Chromium: %v", err)
}
profile := b.userProfileDirPath
t.Cleanup(func() {
b.ctxMu.Lock()
stop := b.cancelFunc
b.ctx = nil
b.cancelFunc = nil
b.isStarted.Store(false)
b.ctxMu.Unlock()
if stop != nil {
stop()
}
if err := b.stopPinningProxy(); err != nil {
t.Errorf("stop pinning proxy: %v", err)
}
deadline := time.Now().Add(5 * time.Second)
for {
err := os.RemoveAll(profile)
if err == nil {
break
}
if time.Now().After(deadline) {
t.Errorf("remove Chromium profile: %v", err)
break
}
time.Sleep(100 * time.Millisecond)
}
})
const cookieURL = "http://example.test/"
localPage := "file://" + filepath.ToSlash(filepath.Join(t.TempDir(), "page.html"))
ready := make(chan struct{}, 2)
release := make(chan struct{})
type result struct {
identity string
cookies []*network.Cookie
err error
}
results := make(chan result, 2)
for _, identity := range []string{"alice", "bob"} {
go func() {
var cookies []*network.Cookie
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err := b.do(ctx, logger, localPage, Options{LocalPage: localPage}, newNetworkAggregate(), chromedp.Tasks{
network.Enable(),
setCookiesActionFunc(logger, []Cookie{{Name: "session", Value: identity, Domain: "example.test", Path: "/", HttpOnly: true}}),
chromedp.ActionFunc(func(ctx context.Context) error {
ready <- struct{}{}
select {
case <-release:
return nil
case <-ctx.Done():
return ctx.Err()
}
}),
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
cookies, err = network.GetCookies().WithURLs([]string{cookieURL}).Do(ctx)
return err
}),
})
results <- result{identity: identity, cookies: cookies, err: err}
}()
}
for range 2 {
select {
case <-ready:
case result := <-results:
close(release)
t.Fatalf("%s conversion ended before both cookies were set: %v", result.identity, result.err)
case <-time.After(15 * time.Second):
close(release)
t.Fatal("conversions did not both set their cookies")
}
}
close(release)
for range 2 {
result := <-results
if result.err != nil {
t.Errorf("%s conversion: %v", result.identity, result.err)
continue
}
var observed []string
for _, cookie := range result.cookies {
observed = append(observed, cookie.Name+"="+cookie.Value)
}
if !slices.Equal(observed, []string{"session=" + result.identity}) {
t.Errorf("%s conversion saw cookies %v, want only its own session", result.identity, observed)
}
}
}
// A failed start must not leave its user profile directory behind: every
// supervisor retry creates a new one.
func TestChromiumBrowser_Start_removesProfileOnFailure(t *testing.T) {