...

Source file src/google.golang.org/grpc/clientconn_authority_test.go

Documentation: google.golang.org/grpc

     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  	"google.golang.org/grpc/credentials"
    27  	"google.golang.org/grpc/credentials/insecure"
    28  	"google.golang.org/grpc/testdata"
    29  )
    30  
    31  func (s) TestClientConnAuthority(t *testing.T) {
    32  	serverNameOverride := "over.write.server.name"
    33  	creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), serverNameOverride)
    34  	if err != nil {
    35  		t.Fatalf("credentials.NewClientTLSFromFile(_, %q) failed: %v", err, serverNameOverride)
    36  	}
    37  
    38  	tests := []struct {
    39  		name          string
    40  		target        string
    41  		opts          []DialOption
    42  		wantAuthority string
    43  	}{
    44  		{
    45  			name:          "default",
    46  			target:        "Non-Existent.Server:8080",
    47  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials())},
    48  			wantAuthority: "Non-Existent.Server:8080",
    49  		},
    50  		{
    51  			name:          "override-via-creds",
    52  			target:        "Non-Existent.Server:8080",
    53  			opts:          []DialOption{WithTransportCredentials(creds)},
    54  			wantAuthority: serverNameOverride,
    55  		},
    56  		{
    57  			name:          "override-via-WithAuthority",
    58  			target:        "Non-Existent.Server:8080",
    59  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithAuthority("authority-override")},
    60  			wantAuthority: "authority-override",
    61  		},
    62  		{
    63  			name:          "override-via-creds-and-WithAuthority",
    64  			target:        "Non-Existent.Server:8080",
    65  			opts:          []DialOption{WithTransportCredentials(creds), WithAuthority(serverNameOverride)},
    66  			wantAuthority: serverNameOverride,
    67  		},
    68  		{
    69  			name:          "unix relative",
    70  			target:        "unix:sock.sock",
    71  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials())},
    72  			wantAuthority: "localhost",
    73  		},
    74  		{
    75  			name:   "unix relative with custom dialer",
    76  			target: "unix:sock.sock",
    77  			opts: []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
    78  				return (&net.Dialer{}).DialContext(ctx, "", addr)
    79  			})},
    80  			wantAuthority: "localhost",
    81  		},
    82  		{
    83  			name:          "unix absolute",
    84  			target:        "unix:/sock.sock",
    85  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials())},
    86  			wantAuthority: "localhost",
    87  		},
    88  		{
    89  			name:   "unix absolute with custom dialer",
    90  			target: "unix:///sock.sock",
    91  			opts: []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
    92  				return (&net.Dialer{}).DialContext(ctx, "", addr)
    93  			})},
    94  			wantAuthority: "localhost",
    95  		},
    96  		{
    97  			name:          "localhost colon port",
    98  			target:        "localhost:50051",
    99  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials())},
   100  			wantAuthority: "localhost:50051",
   101  		},
   102  		{
   103  			name:          "colon port",
   104  			target:        ":50051",
   105  			opts:          []DialOption{WithTransportCredentials(insecure.NewCredentials())},
   106  			wantAuthority: "localhost:50051",
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			cc, err := Dial(test.target, test.opts...)
   113  			if err != nil {
   114  				t.Fatalf("Dial(%q) failed: %v", test.target, err)
   115  			}
   116  			defer cc.Close()
   117  			if cc.authority != test.wantAuthority {
   118  				t.Fatalf("cc.authority = %q, want %q", cc.authority, test.wantAuthority)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func (s) TestClientConnAuthority_CredsAndDialOptionMismatch(t *testing.T) {
   125  	serverNameOverride := "over.write.server.name"
   126  	creds, err := credentials.NewClientTLSFromFile(testdata.Path("x509/server_ca_cert.pem"), serverNameOverride)
   127  	if err != nil {
   128  		t.Fatalf("credentials.NewClientTLSFromFile(_, %q) failed: %v", err, serverNameOverride)
   129  	}
   130  	opts := []DialOption{WithTransportCredentials(creds), WithAuthority("authority-override")}
   131  	if cc, err := NewClient("Non-Existent.Server:8000", opts...); err == nil {
   132  		cc.Close()
   133  		t.Fatal("grpc.NewClient() succeeded when expected to fail")
   134  	}
   135  }
   136  

View as plain text