| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- //go:build linux
- package main
- import (
- "context"
- "encoding/json"
- "errors"
- "runtime"
- "testing"
- "time"
- )
- func TestNativeCStringBounds(t *testing.T) {
- b := []byte{'a', 'b', 0}
- p := &b[0]
- if s, e := cString(p, 2); e != nil || s != "ab" {
- t.Fatal(s, e)
- }
- if _, e := cString(p, 1); e == nil {
- t.Fatal("native string budget ignored")
- }
- if s, e := cString(nil, 1); e != nil || s != "" {
- t.Fatal(s, e)
- }
- runtime.KeepAlive(b)
- }
- func TestNativeDispatcherValidation(t *testing.T) {
- h := &nativeHost{queue: make(chan func(), 32), pages: map[uint64]*nativePage{}, callbacks: map[uintptr]chan evalResult{}}
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- done := make(chan struct{})
- go func() {
- defer close(done)
- for {
- select {
- case f := <-h.queue:
- f()
- case <-ctx.Done():
- return
- }
- }
- }()
- if _, e := h.handle(ctx, "configure", json.RawMessage(`{"Headless":true}`)); e != nil {
- t.Fatal(e)
- }
- if _, e := h.handle(ctx, "configure", json.RawMessage(`{}`)); e == nil {
- t.Fatal("duplicate configure accepted")
- }
- for _, method := range []string{"page.navigate", "page.evaluate", "unknown"} {
- if _, e := h.handle(ctx, method, json.RawMessage(`{"page":99,"op":"goto"}`)); e == nil {
- t.Fatal("invalid native request accepted", method)
- }
- }
- if _, e := h.handle(ctx, "page.close", json.RawMessage(`{"page":99}`)); e != nil {
- t.Fatal(e)
- }
- if _, e := h.handle(ctx, "unknown", json.RawMessage(`bad`)); e == nil {
- t.Fatal("malformed request accepted")
- }
- canceled, stop := context.WithCancel(ctx)
- stop()
- if _, e := h.call(canceled, func() (any, error) { return nil, nil }); !errors.Is(e, context.Canceled) {
- t.Fatal(e)
- }
- cancel()
- select {
- case <-done:
- case <-time.After(time.Second):
- t.Fatal("dispatcher did not stop")
- }
- }
|