private_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package private
  2. import (
  3. "net"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "path/filepath"
  8. "reflect"
  9. "runtime"
  10. "syscall"
  11. "testing"
  12. http2 "github.com/ethereum/go-ethereum/common/http"
  13. "github.com/ethereum/go-ethereum/private/engine/constellation"
  14. "github.com/ethereum/go-ethereum/private/engine/tessera"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestFromEnvironmentOrNil_whenNoConfig(t *testing.T) {
  18. defer func() {
  19. if r := recover(); r != nil {
  20. t.Errorf("panic received: %s", r)
  21. }
  22. }()
  23. os.Unsetenv("ARBITRARY_CONFIG_ENV")
  24. cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
  25. assert.NoError(t, err, "unexpected error")
  26. assert.Equal(t, cfg.ConnectionType, http2.NoConnection, "expected no instance to be set")
  27. }
  28. func TestFromEnvironmentOrNil_whenUsingUnixSocketWithConstellation(t *testing.T) {
  29. if runtime.GOOS == "windows" {
  30. t.Skip("this test case is not supported for windows")
  31. }
  32. testServer, socketFile := startUnixSocketHTTPServer(t, map[string]http.HandlerFunc{
  33. "/upcheck": MockEmptySuccessHandler,
  34. })
  35. defer testServer.Close()
  36. defer func() {
  37. if r := recover(); r != nil {
  38. t.Errorf("panic received: %s", r)
  39. }
  40. }()
  41. os.Setenv("ARBITRARY_CONFIG_ENV", socketFile)
  42. cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
  43. assert.NoError(t, err, "unexpected error")
  44. p, err := NewPrivateTxManager(cfg)
  45. assert.NoError(t, err, "unexpected error")
  46. if !constellation.Is(p) {
  47. t.Errorf("expected Constellation to be used but found %v", reflect.TypeOf(p))
  48. }
  49. }
  50. func TestFromEnvironmentOrNil_whenUsingUnixSocketWithTessera(t *testing.T) {
  51. if runtime.GOOS == "windows" {
  52. t.Skip("this test case is not supported for windows")
  53. }
  54. testServer, socketFile := startUnixSocketHTTPServer(t, map[string]http.HandlerFunc{
  55. "/upcheck": MockEmptySuccessHandler,
  56. "/version": MockEmptySuccessHandler,
  57. })
  58. defer testServer.Close()
  59. defer func() {
  60. if r := recover(); r != nil {
  61. t.Errorf("panic received: %s", r)
  62. }
  63. }()
  64. os.Setenv("ARBITRARY_CONFIG_ENV", socketFile)
  65. cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
  66. assert.NoError(t, err, "unexpected error")
  67. p, err := NewPrivateTxManager(cfg)
  68. assert.NoError(t, err, "unexpected error")
  69. if !tessera.Is(p) {
  70. t.Errorf("expected Tessera to be used but found %v", reflect.TypeOf(p))
  71. }
  72. }
  73. func MockEmptySuccessHandler(_ http.ResponseWriter, _ *http.Request) {
  74. }
  75. func startUnixSocketHTTPServer(t *testing.T, handlers map[string]http.HandlerFunc) (*httptest.Server, string) {
  76. tmpFile := filepath.Join(os.TempDir(), "temp.sock")
  77. syscall.Unlink(tmpFile)
  78. l, err := net.Listen("unix", tmpFile)
  79. if err != nil {
  80. t.Fatalf("can't start a unix socket server due to %s", err)
  81. }
  82. os.Chmod(tmpFile, 0600)
  83. mux := http.NewServeMux()
  84. for k, v := range handlers {
  85. mux.HandleFunc(k, v)
  86. }
  87. testServer := httptest.Server{
  88. Listener: l,
  89. Config: &http.Server{Handler: mux},
  90. }
  91. testServer.Start()
  92. t.Log("Unix Socket HTTP server started")
  93. return &testServer, tmpFile
  94. }