google.golang.org/grpc@v1.72.2/default_dial_option_server_option_test.go (about) 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 "net/url" 24 "strings" 25 "testing" 26 27 "google.golang.org/grpc/credentials/insecure" 28 "google.golang.org/grpc/internal" 29 ) 30 31 func (s) TestAddGlobalDialOptions(t *testing.T) { 32 // Ensure the Dial fails without credentials 33 if _, err := Dial("fake"); err == nil { 34 t.Fatalf("Dialing without a credential did not fail") 35 } else { 36 if !strings.Contains(err.Error(), "no transport security set") { 37 t.Fatalf("Dialing failed with unexpected error: %v", err) 38 } 39 } 40 41 // Set and check the DialOptions 42 opts := []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials())} 43 internal.AddGlobalDialOptions.(func(opt ...DialOption))(opts...) 44 defer internal.ClearGlobalDialOptions() 45 for i, opt := range opts { 46 if globalDialOptions[i] != opt { 47 t.Fatalf("Unexpected global dial option at index %d: %v != %v", i, globalDialOptions[i], opt) 48 } 49 } 50 51 // Ensure the Dial passes with the extra dial options 52 if cc, err := Dial("fake"); err != nil { 53 t.Fatalf("Dialing with insecure credential failed: %v", err) 54 } else { 55 cc.Close() 56 } 57 58 internal.ClearGlobalDialOptions() 59 if len(globalDialOptions) != 0 { 60 t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions)) 61 } 62 } 63 64 // TestDisableGlobalOptions tests dialing with the disableGlobalDialOptions dial 65 // option. Dialing with this set should not pick up global options. 66 func (s) TestDisableGlobalOptions(t *testing.T) { 67 // Set transport credentials as a global option. 68 internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials())) 69 defer internal.ClearGlobalDialOptions() 70 // Dial with the disable global options dial option. This dial should fail 71 // due to the global dial options with credentials not being picked up due 72 // to global options being disabled. 73 noTSecStr := "no transport security set" 74 if _, err := Dial("fake", internal.DisableGlobalDialOptions.(func() DialOption)()); !strings.Contains(fmt.Sprint(err), noTSecStr) { 75 t.Fatalf("Dialing received unexpected error: %v, want error containing \"%v\"", err, noTSecStr) 76 } 77 } 78 79 type testPerTargetDialOption struct{} 80 81 func (do *testPerTargetDialOption) DialOptionForTarget(parsedTarget url.URL) DialOption { 82 if parsedTarget.Scheme == "passthrough" { 83 return WithTransportCredentials(insecure.NewCredentials()) // credentials provided, should pass NewClient. 84 } 85 return EmptyDialOption{} // no credentials, should fail NewClient 86 } 87 88 // TestGlobalPerTargetDialOption configures a global per target dial option that 89 // produces transport credentials for channels using "passthrough" scheme. 90 // Channels that use the passthrough scheme should be successfully created due 91 // to picking up transport credentials, whereas other channels should fail at 92 // creation due to not having transport credentials. 93 func (s) TestGlobalPerTargetDialOption(t *testing.T) { 94 internal.AddGlobalPerTargetDialOptions.(func(opt any))(&testPerTargetDialOption{}) 95 defer internal.ClearGlobalPerTargetDialOptions() 96 noTSecStr := "no transport security set" 97 if _, err := NewClient("dns:///fake"); !strings.Contains(fmt.Sprint(err), noTSecStr) { 98 t.Fatalf("Dialing received unexpected error: %v, want error containing \"%v\"", err, noTSecStr) 99 } 100 cc, err := NewClient("passthrough:///nice") 101 if err != nil { 102 t.Fatalf("Dialing with insecure credentials failed: %v", err) 103 } 104 cc.Close() 105 } 106 107 func (s) TestAddGlobalServerOptions(t *testing.T) { 108 const maxRecvSize = 998765 109 // Set and check the ServerOptions 110 opts := []ServerOption{Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize)} 111 internal.AddGlobalServerOptions.(func(opt ...ServerOption))(opts...) 112 defer internal.ClearGlobalServerOptions() 113 for i, opt := range opts { 114 if globalServerOptions[i] != opt { 115 t.Fatalf("Unexpected global server option at index %d: %v != %v", i, globalServerOptions[i], opt) 116 } 117 } 118 119 // Ensure the extra server options applies to new servers 120 s := NewServer() 121 if s.opts.maxReceiveMessageSize != maxRecvSize { 122 t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize) 123 } 124 125 internal.ClearGlobalServerOptions() 126 if len(globalServerOptions) != 0 { 127 t.Fatalf("Unexpected len of globalServerOptions: %d != 0", len(globalServerOptions)) 128 } 129 } 130 131 // TestJoinDialOption tests the join dial option. It configures a joined dial 132 // option with three individual dial options, and verifies that all three are 133 // successfully applied. 134 func (s) TestJoinDialOption(t *testing.T) { 135 const maxRecvSize = 998765 136 const initialWindowSize = 100 137 jdo := newJoinDialOption(WithTransportCredentials(insecure.NewCredentials()), WithReadBufferSize(maxRecvSize), WithInitialWindowSize(initialWindowSize)) 138 cc, err := Dial("fake", jdo) 139 if err != nil { 140 t.Fatalf("Dialing with insecure credentials failed: %v", err) 141 } 142 defer cc.Close() 143 if cc.dopts.copts.ReadBufferSize != maxRecvSize { 144 t.Fatalf("Unexpected cc.dopts.copts.ReadBufferSize: %d != %d", cc.dopts.copts.ReadBufferSize, maxRecvSize) 145 } 146 if cc.dopts.copts.InitialWindowSize != initialWindowSize { 147 t.Fatalf("Unexpected cc.dopts.copts.InitialWindowSize: %d != %d", cc.dopts.copts.InitialWindowSize, initialWindowSize) 148 } 149 } 150 151 // TestJoinServerOption tests the join server option. It configures a joined 152 // server option with three individual server options, and verifies that all 153 // three are successfully applied. 154 func (s) TestJoinServerOption(t *testing.T) { 155 const maxRecvSize = 998765 156 const initialWindowSize = 100 157 jso := newJoinServerOption(Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize), InitialWindowSize(initialWindowSize)) 158 s := NewServer(jso) 159 if s.opts.maxReceiveMessageSize != maxRecvSize { 160 t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize) 161 } 162 if s.opts.initialWindowSize != initialWindowSize { 163 t.Fatalf("Unexpected s.opts.initialWindowSize: %d != %d", s.opts.initialWindowSize, initialWindowSize) 164 } 165 } 166 167 // funcTestHeaderListSizeDialOptionServerOption tests 168 func (s) TestHeaderListSizeDialOptionServerOption(t *testing.T) { 169 const maxHeaderListSize uint32 = 998765 170 clientHeaderListSize := WithMaxHeaderListSize(maxHeaderListSize) 171 if clientHeaderListSize.(MaxHeaderListSizeDialOption).MaxHeaderListSize != maxHeaderListSize { 172 t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", clientHeaderListSize, maxHeaderListSize) 173 } 174 serverHeaderListSize := MaxHeaderListSize(maxHeaderListSize) 175 if serverHeaderListSize.(MaxHeaderListSizeServerOption).MaxHeaderListSize != maxHeaderListSize { 176 t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", serverHeaderListSize, maxHeaderListSize) 177 } 178 }