linux_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //go:build linux
  2. package main
  3. import (
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "runtime"
  8. "testing"
  9. "time"
  10. )
  11. func TestNativeCStringBounds(t *testing.T) {
  12. b := []byte{'a', 'b', 0}
  13. p := &b[0]
  14. if s, e := cString(p, 2); e != nil || s != "ab" {
  15. t.Fatal(s, e)
  16. }
  17. if _, e := cString(p, 1); e == nil {
  18. t.Fatal("native string budget ignored")
  19. }
  20. if s, e := cString(nil, 1); e != nil || s != "" {
  21. t.Fatal(s, e)
  22. }
  23. runtime.KeepAlive(b)
  24. }
  25. func TestNativeDispatcherValidation(t *testing.T) {
  26. h := &nativeHost{queue: make(chan func(), 32), pages: map[uint64]*nativePage{}, callbacks: map[uintptr]chan evalResult{}}
  27. ctx, cancel := context.WithCancel(context.Background())
  28. defer cancel()
  29. done := make(chan struct{})
  30. go func() {
  31. defer close(done)
  32. for {
  33. select {
  34. case f := <-h.queue:
  35. f()
  36. case <-ctx.Done():
  37. return
  38. }
  39. }
  40. }()
  41. if _, e := h.handle(ctx, "configure", json.RawMessage(`{"Headless":true}`)); e != nil {
  42. t.Fatal(e)
  43. }
  44. if _, e := h.handle(ctx, "configure", json.RawMessage(`{}`)); e == nil {
  45. t.Fatal("duplicate configure accepted")
  46. }
  47. for _, method := range []string{"page.navigate", "page.evaluate", "unknown"} {
  48. if _, e := h.handle(ctx, method, json.RawMessage(`{"page":99,"op":"goto"}`)); e == nil {
  49. t.Fatal("invalid native request accepted", method)
  50. }
  51. }
  52. if _, e := h.handle(ctx, "page.close", json.RawMessage(`{"page":99}`)); e != nil {
  53. t.Fatal(e)
  54. }
  55. if _, e := h.handle(ctx, "unknown", json.RawMessage(`bad`)); e == nil {
  56. t.Fatal("malformed request accepted")
  57. }
  58. canceled, stop := context.WithCancel(ctx)
  59. stop()
  60. if _, e := h.call(canceled, func() (any, error) { return nil, nil }); !errors.Is(e, context.Canceled) {
  61. t.Fatal(e)
  62. }
  63. cancel()
  64. select {
  65. case <-done:
  66. case <-time.After(time.Second):
  67. t.Fatal("dispatcher did not stop")
  68. }
  69. }