mirror of https://github.com/grpc/grpc-go.git
grpc: cleanup parse target and authority tests (#4787)
This commit is contained in:
parent
83a3461520
commit
78d3aa8b3e
|
|
@ -0,0 +1,122 @@
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2021 gRPC authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package grpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
"google.golang.org/grpc/testdata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s) TestClientConnAuthority(t *testing.T) {
|
||||||
|
serverNameOverride := "over.write.server.name"
|
||||||
|
creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), serverNameOverride)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("credentials.NewClientTLSFromFile(_, %q) failed: %v", err, serverNameOverride)
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
target string
|
||||||
|
opts []DialOption
|
||||||
|
wantAuthority string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
target: "Non-Existent.Server:8080",
|
||||||
|
opts: []DialOption{WithInsecure()},
|
||||||
|
wantAuthority: "Non-Existent.Server:8080",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "override-via-creds",
|
||||||
|
target: "Non-Existent.Server:8080",
|
||||||
|
opts: []DialOption{WithTransportCredentials(creds)},
|
||||||
|
wantAuthority: serverNameOverride,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "override-via-WithAuthority",
|
||||||
|
target: "Non-Existent.Server:8080",
|
||||||
|
opts: []DialOption{WithInsecure(), WithAuthority("authority-override")},
|
||||||
|
wantAuthority: "authority-override",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "override-via-creds-and-WithAuthority",
|
||||||
|
target: "Non-Existent.Server:8080",
|
||||||
|
// WithAuthority override works only for insecure creds.
|
||||||
|
opts: []DialOption{WithTransportCredentials(creds), WithAuthority("authority-override")},
|
||||||
|
wantAuthority: serverNameOverride,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unix relative",
|
||||||
|
target: "unix:sock.sock",
|
||||||
|
opts: []DialOption{WithInsecure()},
|
||||||
|
wantAuthority: "localhost",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unix relative with custom dialer",
|
||||||
|
target: "unix:sock.sock",
|
||||||
|
opts: []DialOption{WithInsecure(), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
|
return (&net.Dialer{}).DialContext(ctx, "", addr)
|
||||||
|
})},
|
||||||
|
wantAuthority: "localhost",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unix absolute",
|
||||||
|
target: "unix:/sock.sock",
|
||||||
|
opts: []DialOption{WithInsecure()},
|
||||||
|
wantAuthority: "localhost",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unix absolute with custom dialer",
|
||||||
|
target: "unix:///sock.sock",
|
||||||
|
opts: []DialOption{WithInsecure(), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
||||||
|
return (&net.Dialer{}).DialContext(ctx, "", addr)
|
||||||
|
})},
|
||||||
|
wantAuthority: "localhost",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "localhost colon port",
|
||||||
|
target: "localhost:50051",
|
||||||
|
opts: []DialOption{WithInsecure()},
|
||||||
|
wantAuthority: "localhost:50051",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "colon port",
|
||||||
|
target: ":50051",
|
||||||
|
opts: []DialOption{WithInsecure()},
|
||||||
|
wantAuthority: "localhost:50051",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
cc, err := Dial(test.target, test.opts...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Dial(%q) failed: %v", test.target, err)
|
||||||
|
}
|
||||||
|
defer cc.Close()
|
||||||
|
if cc.authority != test.wantAuthority {
|
||||||
|
t.Fatalf("cc.authority = %q, want %q", cc.authority, test.wantAuthority)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,183 @@
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Copyright 2021 gRPC authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package grpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s) TestParsedTarget_Success_WithoutCustomDialer(t *testing.T) {
|
||||||
|
defScheme := resolver.GetDefaultScheme()
|
||||||
|
tests := []struct {
|
||||||
|
target string
|
||||||
|
wantParsed resolver.Target
|
||||||
|
}{
|
||||||
|
// No scheme is specified.
|
||||||
|
{target: "", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: ""}},
|
||||||
|
{target: "://", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "://"}},
|
||||||
|
{target: ":///", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: ":///"}},
|
||||||
|
{target: "://a/", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "://a/"}},
|
||||||
|
{target: ":///a", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: ":///a"}},
|
||||||
|
{target: "://a/b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "://a/b"}},
|
||||||
|
{target: "/", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "/"}},
|
||||||
|
{target: "a/b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a/b"}},
|
||||||
|
{target: "a//b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a//b"}},
|
||||||
|
{target: "google.com", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "google.com"}},
|
||||||
|
{target: "google.com/?a=b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "google.com/?a=b"}},
|
||||||
|
{target: "/unix/socket/address", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "/unix/socket/address"}},
|
||||||
|
|
||||||
|
// An unregistered scheme is specified.
|
||||||
|
{target: "a:///", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a:///"}},
|
||||||
|
{target: "a://b/", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a://b/"}},
|
||||||
|
{target: "a:///b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a:///b"}},
|
||||||
|
{target: "a://b/c", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a://b/c"}},
|
||||||
|
{target: "a:b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a:b"}},
|
||||||
|
{target: "a:/b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a:/b"}},
|
||||||
|
{target: "a://b", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "a://b"}},
|
||||||
|
|
||||||
|
// A registered scheme is specified.
|
||||||
|
{target: "dns:///google.com", wantParsed: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "google.com"}},
|
||||||
|
{target: "dns://a.server.com/google.com", wantParsed: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"}},
|
||||||
|
{target: "dns://a.server.com/google.com/?a=b", wantParsed: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/?a=b"}},
|
||||||
|
{target: "unix:///a/b/c", wantParsed: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "/a/b/c"}},
|
||||||
|
{target: "unix-abstract:a/b/c", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a/b/c"}},
|
||||||
|
{target: "unix-abstract:a b", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a b"}},
|
||||||
|
{target: "unix-abstract:a:b", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a:b"}},
|
||||||
|
{target: "unix-abstract:a-b", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a-b"}},
|
||||||
|
{target: "unix-abstract:/ a///://::!@#$%^&*()b", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/ a///://::!@#$%^&*()b"}},
|
||||||
|
{target: "unix-abstract:passthrough:abc", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "passthrough:abc"}},
|
||||||
|
{target: "unix-abstract:unix:///abc", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "unix:///abc"}},
|
||||||
|
{target: "unix-abstract:///a/b/c", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/a/b/c"}},
|
||||||
|
{target: "unix-abstract:///", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/"}},
|
||||||
|
{target: "unix-abstract://authority", wantParsed: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "//authority"}},
|
||||||
|
{target: "unix://domain", wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "unix://domain"}},
|
||||||
|
{target: "passthrough:///unix:///a/b/c", wantParsed: resolver.Target{Scheme: "passthrough", Authority: "", Endpoint: "unix:///a/b/c"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.target, func(t *testing.T) {
|
||||||
|
cc, err := Dial(test.target, WithInsecure())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Dial(%q) failed: %v", test.target, err)
|
||||||
|
}
|
||||||
|
defer cc.Close()
|
||||||
|
|
||||||
|
if gotParsed := cc.parsedTarget; gotParsed != test.wantParsed {
|
||||||
|
t.Errorf("cc.parsedTarget = %+v, want %+v", gotParsed, test.wantParsed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s) TestParsedTarget_Failure_WithoutCustomDialer(t *testing.T) {
|
||||||
|
targets := []string{
|
||||||
|
"unix://a/b/c",
|
||||||
|
"unix-abstract://authority/a/b/c",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, target := range targets {
|
||||||
|
t.Run(target, func(t *testing.T) {
|
||||||
|
if cc, err := Dial(target, WithInsecure()); err == nil {
|
||||||
|
defer cc.Close()
|
||||||
|
t.Fatalf("Dial(%q) succeeded cc.parsedTarget = %+v, expected to fail", target, cc.parsedTarget)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s) TestParsedTarget_WithCustomDialer(t *testing.T) {
|
||||||
|
defScheme := resolver.GetDefaultScheme()
|
||||||
|
tests := []struct {
|
||||||
|
target string
|
||||||
|
wantParsed resolver.Target
|
||||||
|
wantDialerAddress string
|
||||||
|
}{
|
||||||
|
// unix:[local_path], unix:[/absolute], and unix://[/absolute] have
|
||||||
|
// different behaviors with a custom dialer.
|
||||||
|
{
|
||||||
|
target: "unix:a/b/c",
|
||||||
|
wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "unix:a/b/c"},
|
||||||
|
wantDialerAddress: "unix:a/b/c",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: "unix:/a/b/c",
|
||||||
|
wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "unix:/a/b/c"},
|
||||||
|
wantDialerAddress: "unix:/a/b/c",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: "unix:///a/b/c",
|
||||||
|
wantParsed: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "/a/b/c"},
|
||||||
|
wantDialerAddress: "unix:///a/b/c",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: "dns:///127.0.0.1:50051",
|
||||||
|
wantParsed: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "127.0.0.1:50051"},
|
||||||
|
wantDialerAddress: "127.0.0.1:50051",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: ":///127.0.0.1:50051",
|
||||||
|
wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: ":///127.0.0.1:50051"},
|
||||||
|
wantDialerAddress: ":///127.0.0.1:50051",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: "dns://authority/127.0.0.1:50051",
|
||||||
|
wantParsed: resolver.Target{Scheme: "dns", Authority: "authority", Endpoint: "127.0.0.1:50051"},
|
||||||
|
wantDialerAddress: "127.0.0.1:50051",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: "://authority/127.0.0.1:50051",
|
||||||
|
wantParsed: resolver.Target{Scheme: defScheme, Authority: "", Endpoint: "://authority/127.0.0.1:50051"},
|
||||||
|
wantDialerAddress: "://authority/127.0.0.1:50051",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.target, func(t *testing.T) {
|
||||||
|
addrCh := make(chan string, 1)
|
||||||
|
dialer := func(ctx context.Context, address string) (net.Conn, error) {
|
||||||
|
addrCh <- address
|
||||||
|
return nil, errors.New("dialer error")
|
||||||
|
}
|
||||||
|
|
||||||
|
cc, err := Dial(test.target, WithInsecure(), WithContextDialer(dialer))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Dial(%q) failed: %v", test.target, err)
|
||||||
|
}
|
||||||
|
defer cc.Close()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case addr := <-addrCh:
|
||||||
|
if addr != test.wantDialerAddress {
|
||||||
|
t.Fatalf("address in custom dialer is %q, want %q", addr, test.wantDialerAddress)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("timeout when waiting for custom dialer to be invoked")
|
||||||
|
}
|
||||||
|
if gotParsed := cc.parsedTarget; gotParsed != test.wantParsed {
|
||||||
|
t.Errorf("cc.parsedTarget for dial target %q = %+v, want %+v", test.target, gotParsed, test.wantParsed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -376,62 +376,6 @@ func (s) TestWithTransportCredentialsTLS(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s) TestDefaultAuthority(t *testing.T) {
|
|
||||||
target := "Non-Existent.Server:8080"
|
|
||||||
conn, err := Dial(target, WithInsecure())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
if conn.authority != target {
|
|
||||||
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, target)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s) TestTLSServerNameOverwrite(t *testing.T) {
|
|
||||||
overwriteServerName := "over.write.server.name"
|
|
||||||
creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), overwriteServerName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create credentials %v", err)
|
|
||||||
}
|
|
||||||
conn, err := Dial("passthrough:///Non-Existent.Server:80", WithTransportCredentials(creds))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
if conn.authority != overwriteServerName {
|
|
||||||
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s) TestWithAuthority(t *testing.T) {
|
|
||||||
overwriteServerName := "over.write.server.name"
|
|
||||||
conn, err := Dial("passthrough:///Non-Existent.Server:80", WithInsecure(), WithAuthority(overwriteServerName))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
if conn.authority != overwriteServerName {
|
|
||||||
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s) TestWithAuthorityAndTLS(t *testing.T) {
|
|
||||||
overwriteServerName := "over.write.server.name"
|
|
||||||
creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), overwriteServerName)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create credentials %v", err)
|
|
||||||
}
|
|
||||||
conn, err := Dial("passthrough:///Non-Existent.Server:80", WithTransportCredentials(creds), WithAuthority("no.effect.authority"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Dial(_, _) = _, %v, want _, <nil>", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
if conn.authority != overwriteServerName {
|
|
||||||
t.Fatalf("%v.authority = %v, want %v", conn, conn.authority, overwriteServerName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// When creating a transport configured with n addresses, only calculate the
|
// When creating a transport configured with n addresses, only calculate the
|
||||||
// backoff once per "round" of attempts instead of once per address (n times
|
// backoff once per "round" of attempts instead of once per address (n times
|
||||||
// per "round" of attempts).
|
// per "round" of attempts).
|
||||||
|
|
|
||||||
|
|
@ -1,114 +0,0 @@
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Copyright 2020 gRPC authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package grpcutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"google.golang.org/grpc/resolver"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParseTarget(t *testing.T) {
|
|
||||||
for _, test := range []resolver.Target{
|
|
||||||
{Scheme: "dns", Authority: "", Endpoint: "google.com"},
|
|
||||||
{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"},
|
|
||||||
{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/?a=b"},
|
|
||||||
{Scheme: "passthrough", Authority: "", Endpoint: "/unix/socket/address"},
|
|
||||||
} {
|
|
||||||
str := test.Scheme + "://" + test.Authority + "/" + test.Endpoint
|
|
||||||
got := ParseTarget(str, false)
|
|
||||||
if got != test {
|
|
||||||
t.Errorf("ParseTarget(%q, false) = %+v, want %+v", str, got, test)
|
|
||||||
}
|
|
||||||
got = ParseTarget(str, true)
|
|
||||||
if got != test {
|
|
||||||
t.Errorf("ParseTarget(%q, true) = %+v, want %+v", str, got, test)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseTargetString(t *testing.T) {
|
|
||||||
for _, test := range []struct {
|
|
||||||
targetStr string
|
|
||||||
want resolver.Target
|
|
||||||
wantWithDialer resolver.Target
|
|
||||||
}{
|
|
||||||
{targetStr: "", want: resolver.Target{Scheme: "", Authority: "", Endpoint: ""}},
|
|
||||||
{targetStr: ":///", want: resolver.Target{Scheme: "", Authority: "", Endpoint: ""}},
|
|
||||||
{targetStr: "a:///", want: resolver.Target{Scheme: "a", Authority: "", Endpoint: ""}},
|
|
||||||
{targetStr: "://a/", want: resolver.Target{Scheme: "", Authority: "a", Endpoint: ""}},
|
|
||||||
{targetStr: ":///a", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a"}},
|
|
||||||
{targetStr: "a://b/", want: resolver.Target{Scheme: "a", Authority: "b", Endpoint: ""}},
|
|
||||||
{targetStr: "a:///b", want: resolver.Target{Scheme: "a", Authority: "", Endpoint: "b"}},
|
|
||||||
{targetStr: "://a/b", want: resolver.Target{Scheme: "", Authority: "a", Endpoint: "b"}},
|
|
||||||
{targetStr: "a://b/c", want: resolver.Target{Scheme: "a", Authority: "b", Endpoint: "c"}},
|
|
||||||
{targetStr: "dns:///google.com", want: resolver.Target{Scheme: "dns", Authority: "", Endpoint: "google.com"}},
|
|
||||||
{targetStr: "dns://a.server.com/google.com", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com"}},
|
|
||||||
{targetStr: "dns://a.server.com/google.com/?a=b", want: resolver.Target{Scheme: "dns", Authority: "a.server.com", Endpoint: "google.com/?a=b"}},
|
|
||||||
|
|
||||||
{targetStr: "/", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/"}},
|
|
||||||
{targetStr: "google.com", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com"}},
|
|
||||||
{targetStr: "google.com/?a=b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "google.com/?a=b"}},
|
|
||||||
{targetStr: "/unix/socket/address", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "/unix/socket/address"}},
|
|
||||||
|
|
||||||
// If we can only parse part of the target.
|
|
||||||
{targetStr: "://", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "://"}},
|
|
||||||
{targetStr: "unix://domain", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "unix://domain"}},
|
|
||||||
{targetStr: "unix://a/b/c", want: resolver.Target{Scheme: "unix", Authority: "a", Endpoint: "/b/c"}},
|
|
||||||
{targetStr: "a:b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a:b"}},
|
|
||||||
{targetStr: "a/b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a/b"}},
|
|
||||||
{targetStr: "a:/b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a:/b"}},
|
|
||||||
{targetStr: "a//b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a//b"}},
|
|
||||||
{targetStr: "a://b", want: resolver.Target{Scheme: "", Authority: "", Endpoint: "a://b"}},
|
|
||||||
|
|
||||||
// Unix cases without custom dialer.
|
|
||||||
// unix:[local_path], unix:[/absolute], and unix://[/absolute] have different
|
|
||||||
// behaviors with a custom dialer, to prevent behavior changes with custom dialers.
|
|
||||||
{targetStr: "unix:a/b/c", want: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "a/b/c"}, wantWithDialer: resolver.Target{Scheme: "", Authority: "", Endpoint: "unix:a/b/c"}},
|
|
||||||
{targetStr: "unix:/a/b/c", want: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "/a/b/c"}, wantWithDialer: resolver.Target{Scheme: "", Authority: "", Endpoint: "unix:/a/b/c"}},
|
|
||||||
{targetStr: "unix:///a/b/c", want: resolver.Target{Scheme: "unix", Authority: "", Endpoint: "/a/b/c"}},
|
|
||||||
|
|
||||||
{targetStr: "unix-abstract:a/b/c", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a/b/c"}},
|
|
||||||
{targetStr: "unix-abstract:a b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a b"}},
|
|
||||||
{targetStr: "unix-abstract:a:b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a:b"}},
|
|
||||||
{targetStr: "unix-abstract:a-b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "a-b"}},
|
|
||||||
{targetStr: "unix-abstract:/ a///://::!@#$%^&*()b", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/ a///://::!@#$%^&*()b"}},
|
|
||||||
{targetStr: "unix-abstract:passthrough:abc", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "passthrough:abc"}},
|
|
||||||
{targetStr: "unix-abstract:unix:///abc", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "unix:///abc"}},
|
|
||||||
{targetStr: "unix-abstract:///a/b/c", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/a/b/c"}},
|
|
||||||
{targetStr: "unix-abstract://authority/a/b/c", want: resolver.Target{Scheme: "unix-abstract", Authority: "authority", Endpoint: "/a/b/c"}},
|
|
||||||
{targetStr: "unix-abstract:///", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "/"}},
|
|
||||||
{targetStr: "unix-abstract://authority", want: resolver.Target{Scheme: "unix-abstract", Authority: "", Endpoint: "//authority"}},
|
|
||||||
|
|
||||||
{targetStr: "passthrough:///unix:///a/b/c", want: resolver.Target{Scheme: "passthrough", Authority: "", Endpoint: "unix:///a/b/c"}},
|
|
||||||
} {
|
|
||||||
got := ParseTarget(test.targetStr, false)
|
|
||||||
if got != test.want {
|
|
||||||
t.Errorf("ParseTarget(%q, false) = %+v, want %+v", test.targetStr, got, test.want)
|
|
||||||
}
|
|
||||||
wantWithDialer := test.wantWithDialer
|
|
||||||
if wantWithDialer == (resolver.Target{}) {
|
|
||||||
wantWithDialer = test.want
|
|
||||||
}
|
|
||||||
got = ParseTarget(test.targetStr, true)
|
|
||||||
if got != wantWithDialer {
|
|
||||||
t.Errorf("ParseTarget(%q, true) = %+v, want %+v", test.targetStr, got, wantWithDialer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue