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