diff --git a/types/.testdata/operator.png b/types/.testdata/operator.png new file mode 100644 index 00000000..9726dc0e Binary files /dev/null and b/types/.testdata/operator.png differ diff --git a/types/operator_test.go b/types/operator_test.go index 45604354..f673be18 100644 --- a/types/operator_test.go +++ b/types/operator_test.go @@ -1,15 +1,97 @@ package types import ( + "bytes" + "context" + "encoding/json" "errors" + "fmt" + "net" + "net/http" + "net/http/httptest" + "os" "testing" + "time" "github.com/Layr-Labs/eigensdk-go/utils" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestOperatorValidate(t *testing.T) { + + operatorMetadata := OperatorMetadata{ + Name: "Madhur 1", + Website: "https://www.facebook.com", + Description: "Madhur's first operator is best in this world", + Logo: "SET BELOW ONCE URL IS KNOWN", + Twitter: "https://twitter.com/shrimalmadhur", + } + + operatorPngData, err := os.ReadFile("./.testdata/operator.png") + require.NoError(t, err, "Failed to read operator.png") + operatorPngBuffer := bytes.NewBuffer(operatorPngData) + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/404metadata.json" { + w.WriteHeader(http.StatusNotFound) + return + } + if r.URL.Path == "/operator.png" { + w.Header().Set("Content-Type", "image/png") + http.ServeContent(w, r, "operator.png", time.Now(), bytes.NewReader(operatorPngBuffer.Bytes())) + return + } + marshalledOperatorMetadata, err := json.Marshal(operatorMetadata) + require.NoError(t, err) + _, err = w.Write(marshalledOperatorMetadata) + require.NoError(t, err) + })) + defer ts.Close() + fmt.Println(ts.URL) + + resolver := &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + return nil, nil + }, + } + + // Override the default resolver + net.DefaultResolver = resolver + + // Create a custom dialer + dialer := &net.Dialer{ + Resolver: resolver, + } + + // Override the default HTTP transport + http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { + host, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, err + } + + // Redirect specific hostnames to localhost + switch host { + case "mylocalserver.com": + addr = net.JoinHostPort("127.0.0.1", port) + } + + return dialer.DialContext(ctx, network, addr) + } + + _, port, err := net.SplitHostPort(ts.URL[7:]) + if err != nil { + panic(err) + } + url := fmt.Sprintf("http://mylocalserver.com:%s", port) + operatorMetadata.Logo = url + "/operator.png" + + defer func() { + http.DefaultTransport = &http.Transport{} + }() + var tests = []struct { name string operator Operator @@ -24,7 +106,7 @@ func TestOperatorValidate(t *testing.T) { Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", + MetadataUrl: url + "/metadata.json", }, wantErr: false, }, @@ -34,7 +116,7 @@ func TestOperatorValidate(t *testing.T) { Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: ZeroAddress, StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://madhur-test-public.s3.us-east-2.amazonaws.com/metadata.json", + MetadataUrl: url + "/metadata.json", }, wantErr: false, }, @@ -50,7 +132,7 @@ func TestOperatorValidate(t *testing.T) { expectedErr: utils.WrapError(ErrInvalidMetadataUrl, utils.ErrEmptyUrl), }, { - name: "failed operator validation - localhost metadata url", + name: "failed operator validation - localhost metadata url disallowed", operator: Operator{ Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", @@ -61,7 +143,7 @@ func TestOperatorValidate(t *testing.T) { expectedErr: utils.WrapError(ErrInvalidMetadataUrl, utils.ErrUrlPointingToLocalServer), }, { - name: "failed operator validation - 127.0.0.1 metadata url", + name: "failed operator validation - 127.0.0.1 metadata url disallowed", operator: Operator{ Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", @@ -77,7 +159,7 @@ func TestOperatorValidate(t *testing.T) { Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://example.com/metadata.json", + MetadataUrl: url + "/404metadata.json", }, wantErr: true, expectedErr: utils.WrapError( @@ -91,7 +173,7 @@ func TestOperatorValidate(t *testing.T) { Address: "0xa", DelegationApproverAddress: "0xd5e099c71b797516c10ed0f0d895f429c2781142", StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://example.com/metadata.json", + MetadataUrl: url + "/metadata.json", }, wantErr: true, expectedErr: ErrInvalidOperatorAddress, @@ -102,7 +184,7 @@ func TestOperatorValidate(t *testing.T) { Address: "0xd5e099c71b797516c10ed0f0d895f429c2781142", DelegationApproverAddress: "0x12", StakerOptOutWindowBlocks: 100, - MetadataUrl: "https://example.com/metadata.json", + MetadataUrl: url + "/metadata.json", }, wantErr: true, expectedErr: ErrInvalidDelegationApproverAddress,