github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/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  	http2 "github.com/ethereum/go-ethereum/common/http"
    15  	"github.com/ethereum/go-ethereum/private/engine/constellation"
    16  	"github.com/ethereum/go-ethereum/private/engine/tessera"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestFromEnvironmentOrNil_whenNoConfig(t *testing.T) {
    21  	defer func() {
    22  		if r := recover(); r != nil {
    23  			t.Errorf("panic received: %s", r)
    24  		}
    25  	}()
    26  	os.Unsetenv("ARBITRARY_CONFIG_ENV")
    27  	cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
    28  
    29  	assert.NoError(t, err, "unexpected error")
    30  	assert.Equal(t, cfg.ConnectionType, http2.NoConnection, "expected no instance to be set")
    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  	cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
    48  	assert.NoError(t, err, "unexpected error")
    49  
    50  	p, err := NewPrivateTxManager(cfg)
    51  	assert.NoError(t, err, "unexpected error")
    52  	if !constellation.Is(p) {
    53  		t.Errorf("expected Constellation to be used but found %v", reflect.TypeOf(p))
    54  	}
    55  }
    56  
    57  func TestFromEnvironmentOrNil_whenUsingUnixSocketWithTessera(t *testing.T) {
    58  	if runtime.GOOS == "windows" {
    59  		t.Skip("this test case is not supported for windows")
    60  	}
    61  	testServer, socketFile := startUnixSocketHTTPServer(t, map[string]http.HandlerFunc{
    62  		"/upcheck": MockEmptySuccessHandler,
    63  		"/version": MockEmptySuccessHandler,
    64  	})
    65  	defer testServer.Close()
    66  	defer func() {
    67  		if r := recover(); r != nil {
    68  			t.Errorf("panic received: %s", r)
    69  		}
    70  	}()
    71  	os.Setenv("ARBITRARY_CONFIG_ENV", socketFile)
    72  	cfg, err := FromEnvironmentOrNil("ARBITRARY_CONFIG_ENV")
    73  	assert.NoError(t, err, "unexpected error")
    74  
    75  	p, err := NewPrivateTxManager(cfg)
    76  	assert.NoError(t, err, "unexpected error")
    77  	if !tessera.Is(p) {
    78  		t.Errorf("expected Tessera to be used but found %v", reflect.TypeOf(p))
    79  	}
    80  }
    81  
    82  func MockEmptySuccessHandler(_ http.ResponseWriter, _ *http.Request) {
    83  
    84  }
    85  
    86  func startUnixSocketHTTPServer(t *testing.T, handlers map[string]http.HandlerFunc) (*httptest.Server, string) {
    87  	tmpFile := filepath.Join(os.TempDir(), "temp.sock")
    88  	syscall.Unlink(tmpFile)
    89  	l, err := net.Listen("unix", tmpFile)
    90  	if err != nil {
    91  		t.Fatalf("can't start a unix socket server due to %s", err)
    92  	}
    93  	os.Chmod(tmpFile, 0600)
    94  	mux := http.NewServeMux()
    95  	for k, v := range handlers {
    96  		mux.HandleFunc(k, v)
    97  	}
    98  
    99  	testServer := httptest.Server{
   100  		Listener: l,
   101  		Config:   &http.Server{Handler: mux},
   102  	}
   103  	testServer.Start()
   104  	t.Log("Unix Socket HTTP server started")
   105  	return &testServer, tmpFile
   106  }