gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/clientconn_authority_test.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package grpc 20 21 import ( 22 "context" 23 "net" 24 "testing" 25 26 "gitee.com/ks-custle/core-gm/grpc/credentials" 27 "gitee.com/ks-custle/core-gm/grpc/testdata" 28 ) 29 30 func (s) TestClientConnAuthority(t *testing.T) { 31 serverNameOverride := "over.write.server.name" 32 creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), serverNameOverride) 33 if err != nil { 34 t.Fatalf("credentials.NewClientTLSFromFile(_, %q) failed: %v", err, serverNameOverride) 35 } 36 37 tests := []struct { 38 name string 39 target string 40 opts []DialOption 41 wantAuthority string 42 }{ 43 { 44 name: "default", 45 target: "Non-Existent.Server:8080", 46 opts: []DialOption{WithInsecure()}, 47 wantAuthority: "Non-Existent.Server:8080", 48 }, 49 { 50 name: "override-via-creds", 51 target: "Non-Existent.Server:8080", 52 opts: []DialOption{WithTransportCredentials(creds)}, 53 wantAuthority: serverNameOverride, 54 }, 55 { 56 name: "override-via-WithAuthority", 57 target: "Non-Existent.Server:8080", 58 opts: []DialOption{WithInsecure(), WithAuthority("authority-override")}, 59 wantAuthority: "authority-override", 60 }, 61 { 62 name: "override-via-creds-and-WithAuthority", 63 target: "Non-Existent.Server:8080", 64 opts: []DialOption{WithTransportCredentials(creds), WithAuthority(serverNameOverride)}, 65 wantAuthority: serverNameOverride, 66 }, 67 { 68 name: "unix relative", 69 target: "unix:sock.sock", 70 opts: []DialOption{WithInsecure()}, 71 wantAuthority: "localhost", 72 }, 73 { 74 name: "unix relative with custom dialer", 75 target: "unix:sock.sock", 76 opts: []DialOption{WithInsecure(), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) { 77 return (&net.Dialer{}).DialContext(ctx, "", addr) 78 })}, 79 wantAuthority: "localhost", 80 }, 81 { 82 name: "unix absolute", 83 target: "unix:/sock.sock", 84 opts: []DialOption{WithInsecure()}, 85 wantAuthority: "localhost", 86 }, 87 { 88 name: "unix absolute with custom dialer", 89 target: "unix:///sock.sock", 90 opts: []DialOption{WithInsecure(), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) { 91 return (&net.Dialer{}).DialContext(ctx, "", addr) 92 })}, 93 wantAuthority: "localhost", 94 }, 95 { 96 name: "localhost colon port", 97 target: "localhost:50051", 98 opts: []DialOption{WithInsecure()}, 99 wantAuthority: "localhost:50051", 100 }, 101 { 102 name: "colon port", 103 target: ":50051", 104 opts: []DialOption{WithInsecure()}, 105 wantAuthority: "localhost:50051", 106 }, 107 } 108 109 for _, test := range tests { 110 t.Run(test.name, func(t *testing.T) { 111 cc, err := Dial(test.target, test.opts...) 112 if err != nil { 113 t.Fatalf("Dial(%q) failed: %v", test.target, err) 114 } 115 defer cc.Close() 116 if cc.authority != test.wantAuthority { 117 t.Fatalf("cc.authority = %q, want %q", cc.authority, test.wantAuthority) 118 } 119 }) 120 } 121 } 122 123 func (s) TestClientConnAuthority_CredsAndDialOptionMismatch(t *testing.T) { 124 serverNameOverride := "over.write.server.name" 125 creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), serverNameOverride) 126 if err != nil { 127 t.Fatalf("credentials.NewClientTLSFromFile(_, %q) failed: %v", err, serverNameOverride) 128 } 129 opts := []DialOption{WithTransportCredentials(creds), WithAuthority("authority-override")} 130 if cc, err := Dial("Non-Existent.Server:8000", opts...); err == nil { 131 cc.Close() 132 t.Fatal("grpc.Dial() succeeded when expected to fail") 133 } 134 }