...

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

Documentation: google.golang.org/grpc

     1  /*
     2   *
     3   * Copyright 2022 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  	"fmt"
    23  	"strings"
    24  	"testing"
    25  
    26  	"google.golang.org/grpc/credentials/insecure"
    27  	"google.golang.org/grpc/internal"
    28  )
    29  
    30  func (s) TestAddGlobalDialOptions(t *testing.T) {
    31  	// Ensure the Dial fails without credentials
    32  	if _, err := Dial("fake"); err == nil {
    33  		t.Fatalf("Dialing without a credential did not fail")
    34  	} else {
    35  		if !strings.Contains(err.Error(), "no transport security set") {
    36  			t.Fatalf("Dialing failed with unexpected error: %v", err)
    37  		}
    38  	}
    39  
    40  	// Set and check the DialOptions
    41  	opts := []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials())}
    42  	internal.AddGlobalDialOptions.(func(opt ...DialOption))(opts...)
    43  	for i, opt := range opts {
    44  		if globalDialOptions[i] != opt {
    45  			t.Fatalf("Unexpected global dial option at index %d: %v != %v", i, globalDialOptions[i], opt)
    46  		}
    47  	}
    48  
    49  	// Ensure the Dial passes with the extra dial options
    50  	if cc, err := Dial("fake"); err != nil {
    51  		t.Fatalf("Dialing with insecure credential failed: %v", err)
    52  	} else {
    53  		cc.Close()
    54  	}
    55  
    56  	internal.ClearGlobalDialOptions()
    57  	if len(globalDialOptions) != 0 {
    58  		t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions))
    59  	}
    60  }
    61  
    62  // TestDisableGlobalOptions tests dialing with the disableGlobalDialOptions dial
    63  // option. Dialing with this set should not pick up global options.
    64  func (s) TestDisableGlobalOptions(t *testing.T) {
    65  	// Set transport credentials as a global option.
    66  	internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials()))
    67  	// Dial with the disable global options dial option. This dial should fail
    68  	// due to the global dial options with credentials not being picked up due
    69  	// to global options being disabled.
    70  	noTSecStr := "no transport security set"
    71  	if _, err := Dial("fake", internal.DisableGlobalDialOptions.(func() DialOption)()); !strings.Contains(fmt.Sprint(err), noTSecStr) {
    72  		t.Fatalf("Dialing received unexpected error: %v, want error containing \"%v\"", err, noTSecStr)
    73  	}
    74  	internal.ClearGlobalDialOptions()
    75  }
    76  
    77  func (s) TestAddGlobalServerOptions(t *testing.T) {
    78  	const maxRecvSize = 998765
    79  	// Set and check the ServerOptions
    80  	opts := []ServerOption{Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize)}
    81  	internal.AddGlobalServerOptions.(func(opt ...ServerOption))(opts...)
    82  	for i, opt := range opts {
    83  		if globalServerOptions[i] != opt {
    84  			t.Fatalf("Unexpected global server option at index %d: %v != %v", i, globalServerOptions[i], opt)
    85  		}
    86  	}
    87  
    88  	// Ensure the extra server options applies to new servers
    89  	s := NewServer()
    90  	if s.opts.maxReceiveMessageSize != maxRecvSize {
    91  		t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize)
    92  	}
    93  
    94  	internal.ClearGlobalServerOptions()
    95  	if len(globalServerOptions) != 0 {
    96  		t.Fatalf("Unexpected len of globalServerOptions: %d != 0", len(globalServerOptions))
    97  	}
    98  }
    99  
   100  // TestJoinDialOption tests the join dial option. It configures a joined dial
   101  // option with three individual dial options, and verifies that all three are
   102  // successfully applied.
   103  func (s) TestJoinDialOption(t *testing.T) {
   104  	const maxRecvSize = 998765
   105  	const initialWindowSize = 100
   106  	jdo := newJoinDialOption(WithTransportCredentials(insecure.NewCredentials()), WithReadBufferSize(maxRecvSize), WithInitialWindowSize(initialWindowSize))
   107  	cc, err := Dial("fake", jdo)
   108  	if err != nil {
   109  		t.Fatalf("Dialing with insecure credentials failed: %v", err)
   110  	}
   111  	defer cc.Close()
   112  	if cc.dopts.copts.ReadBufferSize != maxRecvSize {
   113  		t.Fatalf("Unexpected cc.dopts.copts.ReadBufferSize: %d != %d", cc.dopts.copts.ReadBufferSize, maxRecvSize)
   114  	}
   115  	if cc.dopts.copts.InitialWindowSize != initialWindowSize {
   116  		t.Fatalf("Unexpected cc.dopts.copts.InitialWindowSize: %d != %d", cc.dopts.copts.InitialWindowSize, initialWindowSize)
   117  	}
   118  }
   119  
   120  // TestJoinServerOption tests the join server option. It configures a joined
   121  // server option with three individual server options, and verifies that all
   122  // three are successfully applied.
   123  func (s) TestJoinServerOption(t *testing.T) {
   124  	const maxRecvSize = 998765
   125  	const initialWindowSize = 100
   126  	jso := newJoinServerOption(Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize), InitialWindowSize(initialWindowSize))
   127  	s := NewServer(jso)
   128  	if s.opts.maxReceiveMessageSize != maxRecvSize {
   129  		t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize)
   130  	}
   131  	if s.opts.initialWindowSize != initialWindowSize {
   132  		t.Fatalf("Unexpected s.opts.initialWindowSize: %d != %d", s.opts.initialWindowSize, initialWindowSize)
   133  	}
   134  }
   135  
   136  // funcTestHeaderListSizeDialOptionServerOption tests
   137  func (s) TestHeaderListSizeDialOptionServerOption(t *testing.T) {
   138  	const maxHeaderListSize uint32 = 998765
   139  	clientHeaderListSize := WithMaxHeaderListSize(maxHeaderListSize)
   140  	if clientHeaderListSize.(MaxHeaderListSizeDialOption).MaxHeaderListSize != maxHeaderListSize {
   141  		t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", clientHeaderListSize, maxHeaderListSize)
   142  	}
   143  	serverHeaderListSize := MaxHeaderListSize(maxHeaderListSize)
   144  	if serverHeaderListSize.(MaxHeaderListSizeServerOption).MaxHeaderListSize != maxHeaderListSize {
   145  		t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", serverHeaderListSize, maxHeaderListSize)
   146  	}
   147  }
   148  

View as plain text