Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: use "localhost:port" as authority if target is ":port" #4017

Merged
merged 8 commits into from Nov 12, 2020
65 changes: 35 additions & 30 deletions test/authority_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"os"
"strings"
"sync"
"testing"
"time"

Expand All @@ -34,43 +35,42 @@ import (
testpb "google.golang.org/grpc/test/grpc_testing"
)

func authorityChecker(expectedAuthority *string) func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
return func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "failed to parse metadata")
}
auths, ok := md[":authority"]
if !ok {
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if len(auths) != 1 {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("no authority header, auths = %v", auths))
}
if auths[0] != *expectedAuthority {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid authority header %v, expected %v", auths[0], *expectedAuthority))
}
return &testpb.Empty{}, nil
func authorityChecker(ctx context.Context, expectedAuthority string) (*testpb.Empty, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "failed to parse metadata")
}
auths, ok := md[":authority"]
if !ok {
return nil, status.Error(codes.InvalidArgument, "no authority header")
}
if len(auths) != 1 {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("no authority header, auths = %v", auths))
}
if auths[0] != expectedAuthority {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid authority header %v, expected %v", auths[0], expectedAuthority))
}
return &testpb.Empty{}, nil
}

func runUnixTest(t *testing.T, address, target, expectedAuthority string, dialer func(context.Context, string) (net.Conn, error)) {
if err := os.RemoveAll(address); err != nil {
t.Fatalf("Error removing socket file %v: %v\n", address, err)
}
ss := &stubServer{
emptyCall: authorityChecker(&expectedAuthority),
network: "unix",
address: address,
target: target,
emptyCall: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
return authorityChecker(ctx, expectedAuthority)
},
network: "unix",
address: address,
target: target,
}
opts := []grpc.DialOption{}
if dialer != nil {
opts = append(opts, grpc.WithContextDialer(dialer))
}
if err := ss.Start(nil, opts...); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
return
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -155,27 +155,32 @@ func (s) TestUnixCustomDialer(t *testing.T) {

func (s) TestColonPortAuthority(t *testing.T) {
expectedAuthority := ""
var authorityMu sync.Mutex
ss := &stubServer{
emptyCall: authorityChecker(&expectedAuthority),
network: "tcp",
emptyCall: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
authorityMu.Lock()
defer authorityMu.Unlock()
return authorityChecker(ctx, expectedAuthority)
},
network: "tcp",
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
return
}
defer ss.Stop()
s := strings.Split(ss.address, ":")
if len(s) != 2 {
t.Fatalf("No port in address: %v", ss.address)
return
_, port, err := net.SplitHostPort(ss.address)
if err != nil {
t.Fatalf("Failed splitting host from post: %v", err)
}
expectedAuthority = "localhost:" + s[1]
authorityMu.Lock()
expectedAuthority = "localhost:" + port
authorityMu.Unlock()
// ss.Start dials, but not the ":[port]" target that is being tested here.
// Dial again, with ":[port]" as the target.
cc, err := grpc.Dial(":"+s[1], grpc.WithInsecure())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/s[1]/port/

if err != nil {
t.Fatalf("grpc.Dial(%q) = %v", ss.target, err)
return
}
defer cc.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down