vitess.io/vitess@v0.16.2/go/mysql/conn_params_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "testing" 21 22 "vitess.io/vitess/go/vt/vttls" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestConnParams_EnableSSL(t *testing.T) { 28 p := ConnParams{} 29 p.EnableSSL() 30 assert := assert.New(t) 31 assert.EqualValues(vttls.VerifyIdentity, p.SslMode, "should enable strictest mode") 32 assert.EqualValues(vttls.VerifyIdentity, p.EffectiveSslMode(), "should enable strictest mode") 33 } 34 35 func TestConnParams_EffectiveSslModeLegacyFlags(t *testing.T) { 36 p := ConnParams{ 37 Flags: CapabilityClientSSL, 38 } 39 assert := assert.New(t) 40 assert.EqualValues("", p.SslMode, "should enable strictest mode") 41 assert.EqualValues(vttls.VerifyIdentity, p.EffectiveSslMode(), "should enable strictest mode") 42 } 43 44 func TestConnParams_EffectiveSslModeConfigured(t *testing.T) { 45 p := ConnParams{ 46 SslMode: vttls.VerifyCA, 47 Flags: CapabilityClientSSL, 48 } 49 assert := assert.New(t) 50 assert.EqualValues(vttls.VerifyCA, p.SslMode, "should use verify_ca") 51 assert.EqualValues(vttls.VerifyCA, p.EffectiveSslMode(), "should use configured mode") 52 } 53 54 func TestConnParams_SslEnabledNotConfigured(t *testing.T) { 55 p := ConnParams{} 56 assert := assert.New(t) 57 assert.False(p.SslEnabled()) 58 assert.EqualValues("", p.SslMode, "should be empty") 59 assert.EqualValues(vttls.Disabled, p.EffectiveSslMode(), "should have ssl disabled") 60 } 61 62 func TestConnParams_SslEnabledPreferredUnixSocket(t *testing.T) { 63 p := ConnParams{ 64 SslMode: vttls.Preferred, 65 UnixSocket: "/tmp/mysql.sock", 66 } 67 assert := assert.New(t) 68 assert.False(p.SslEnabled()) 69 } 70 71 func TestConnParams_SslEnabledPreferredWithHost(t *testing.T) { 72 p := ConnParams{ 73 Host: "localhost", 74 SslMode: vttls.Preferred, 75 } 76 assert := assert.New(t) 77 assert.True(p.SslEnabled()) 78 } 79 80 func TestConnParams_SslEnabledDisabled(t *testing.T) { 81 p := ConnParams{ 82 SslMode: vttls.Disabled, 83 } 84 assert := assert.New(t) 85 assert.False(p.SslEnabled()) 86 } 87 88 func TestConnParams_SslEnabledRequired(t *testing.T) { 89 p := ConnParams{ 90 SslMode: vttls.Required, 91 } 92 assert := assert.New(t) 93 assert.True(p.SslEnabled()) 94 } 95 96 func TestConnParams_SslEnabledVerifyCA(t *testing.T) { 97 p := ConnParams{ 98 SslMode: vttls.VerifyCA, 99 } 100 assert := assert.New(t) 101 assert.True(p.SslEnabled()) 102 } 103 104 func TestConnParams_SslEnabledVerifyIdentity(t *testing.T) { 105 p := ConnParams{ 106 SslMode: vttls.VerifyIdentity, 107 } 108 assert := assert.New(t) 109 assert.True(p.SslEnabled()) 110 } 111 112 func TestConnParams_SslRequiredDisabled(t *testing.T) { 113 p := ConnParams{ 114 SslMode: vttls.Disabled, 115 } 116 assert := assert.New(t) 117 assert.False(p.SslRequired()) 118 } 119 120 func TestConnParams_SslRequiredPreferred(t *testing.T) { 121 p := ConnParams{ 122 SslMode: vttls.Preferred, 123 } 124 assert := assert.New(t) 125 assert.False(p.SslRequired()) 126 } 127 128 func TestConnParams_SslRequiredRequired(t *testing.T) { 129 p := ConnParams{ 130 SslMode: vttls.Required, 131 } 132 assert := assert.New(t) 133 assert.True(p.SslRequired()) 134 } 135 136 func TestConnParams_SslRequiredVerifyCA(t *testing.T) { 137 p := ConnParams{ 138 SslMode: vttls.VerifyCA, 139 } 140 assert := assert.New(t) 141 assert.True(p.SslRequired()) 142 } 143 144 func TestConnParams_SslRequiredVerifyIdentity(t *testing.T) { 145 p := ConnParams{ 146 SslMode: vttls.VerifyIdentity, 147 } 148 assert := assert.New(t) 149 assert.True(p.SslRequired()) 150 }