native_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package native
  2. import (
  3. "context"
  4. "encoding/json"
  5. "os"
  6. "runtime"
  7. "testing"
  8. "time"
  9. "clusterflow.dev/cfbrowser/api"
  10. "clusterflow.dev/cfbrowser/protocol"
  11. )
  12. func init() {
  13. if os.Getenv("CFBROWSER_TEST_HELPER") != "1" {
  14. return
  15. }
  16. err := protocol.Serve(context.Background(), os.Stdin, os.Stdout, func(ctx context.Context, method string, raw json.RawMessage) (any, error) {
  17. switch method {
  18. case "configure":
  19. if os.Getenv("CFBROWSER_TEST_MODE") == "reject" {
  20. return false, nil
  21. }
  22. if os.Getenv("CFBROWSER_TEST_MODE") == "crash" {
  23. os.Exit(3)
  24. }
  25. return true, nil
  26. case "session.features":
  27. if os.Getenv("CFBROWSER_TEST_MODE") == "old" {
  28. return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "old worker")
  29. }
  30. return map[string]int{"featureVersion": protocol.FeatureVersion}, nil
  31. case "page.new":
  32. return 1, nil
  33. case "page.navigate":
  34. return "https://example.org", nil
  35. case "page.evaluate":
  36. return json.RawMessage(`{"answer":42}`), nil
  37. case "page.close":
  38. return true, nil
  39. }
  40. return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "unknown method")
  41. })
  42. if err != nil {
  43. os.Exit(2)
  44. }
  45. os.Exit(0)
  46. }
  47. func TestCapabilitiesAndStartupValidation(t *testing.T) {
  48. d := &Driver{}
  49. c, e := d.Probe(context.Background())
  50. if e != nil || c.Supports("trustedInput") != (runtime.GOOS == "windows") || c.Supports("downloads") {
  51. t.Fatal(c, e)
  52. }
  53. if c.Engine != nativeEngine() || c.Supports("isolatedWorld") != nativeSupported() {
  54. t.Fatal(c)
  55. }
  56. if _, e = d.Open(context.Background(), api.EngineOptions{}); e == nil {
  57. t.Fatal("missing worker accepted")
  58. }
  59. ctx, cancel := context.WithCancel(context.Background())
  60. cancel()
  61. if _, e = d.Probe(ctx); e == nil {
  62. t.Fatal("canceled probe accepted")
  63. }
  64. if _, e = d.Open(ctx, api.EngineOptions{}); e == nil {
  65. t.Fatal("canceled open accepted")
  66. }
  67. }
  68. func TestWorkerStartupFailure(t *testing.T) {
  69. if !nativeSupported() {
  70. t.Skip("unsupported native platform")
  71. }
  72. t.Setenv("CFBROWSER_TEST_HELPER", "1")
  73. path, e := os.Executable()
  74. if e != nil {
  75. t.Fatal(e)
  76. }
  77. for _, mode := range []string{"reject", "crash", "old"} {
  78. t.Run(mode, func(t *testing.T) {
  79. t.Setenv("CFBROWSER_TEST_MODE", mode)
  80. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  81. defer cancel()
  82. if s, e := (&Driver{WorkerPath: path}).Open(ctx, api.EngineOptions{}); e == nil {
  83. _ = s.Close()
  84. t.Fatal("worker startup failure accepted")
  85. }
  86. })
  87. }
  88. }
  89. func TestWorkerClientLifecycle(t *testing.T) {
  90. if !nativeSupported() {
  91. t.Skip("unsupported native platform")
  92. }
  93. t.Setenv("CFBROWSER_TEST_HELPER", "1")
  94. path, e := os.Executable()
  95. if e != nil {
  96. t.Fatal(e)
  97. }
  98. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  99. defer cancel()
  100. s, e := (&Driver{WorkerPath: path}).Open(ctx, api.EngineOptions{NoSandbox: true})
  101. if e != nil {
  102. t.Fatal(e)
  103. }
  104. defer s.Close()
  105. p, e := s.NewPage(ctx)
  106. if e != nil {
  107. t.Fatal(e)
  108. }
  109. if v, e := p.Navigate(ctx, "", "goto"); e != nil || v != "https://example.org" {
  110. t.Fatal(v, e)
  111. }
  112. if v, e := p.Evaluate(ctx, "ignored"); e != nil || string(v) != `{"answer":42}` {
  113. t.Fatal(string(v), e)
  114. }
  115. if e = p.Input(ctx, "click", "", 0, 0); e == nil {
  116. t.Fatal("native trusted input claimed")
  117. }
  118. if _, e = p.Capture(ctx); e == nil {
  119. t.Fatal("native capture claimed")
  120. }
  121. if len(s.Downloads()) != 0 {
  122. t.Fatal("fake downloads")
  123. }
  124. if e = p.Close(); e != nil {
  125. t.Fatal(e)
  126. }
  127. if e = p.Close(); e != nil {
  128. t.Fatal(e)
  129. }
  130. if e = s.Close(); e != nil {
  131. t.Fatal(e)
  132. }
  133. }