github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/backends/db_provider/mysql_dbprovider_test.go (about) 1 package db_provider 2 3 import ( 4 "fmt" 5 "path" 6 "runtime" 7 "testing" 8 9 "github.com/prebid/prebid-server/v2/config" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestConnStringMySql(t *testing.T) { 14 15 type TLS struct { 16 RootCert string 17 ClientCert string 18 ClientKey string 19 } 20 21 type Params struct { 22 db string 23 host string 24 port int 25 username string 26 password string 27 QueryString string 28 TLS TLS 29 } 30 31 _, callerFilename, _, _ := runtime.Caller(0) 32 workingDir := path.Dir(callerFilename) 33 34 tests := []struct { 35 name string 36 params Params 37 connString string 38 }{ 39 { 40 params: Params{ 41 db: "", 42 }, 43 connString: "tcp()/", 44 }, 45 { 46 params: Params{ 47 db: "TestDB", 48 }, 49 connString: "tcp()/TestDB", 50 }, 51 { 52 params: Params{ 53 host: "example.com", 54 }, 55 connString: "tcp(example.com)/", 56 }, 57 { 58 params: Params{ 59 port: 20, 60 }, 61 connString: "tcp(:20)/", 62 }, 63 { 64 params: Params{ 65 username: "someuser", 66 }, 67 connString: "someuser@tcp()/", 68 }, 69 { 70 params: Params{ 71 username: "someuser", 72 password: "somepassword", 73 }, 74 connString: "someuser:somepassword@tcp()/", 75 }, 76 { 77 params: Params{ 78 username: "someuser", 79 password: "somepassword:/?#[]@!$&()*+,;=", 80 }, 81 connString: "someuser:somepassword:/?#[]@!$&()*+,;=@tcp()/", 82 }, 83 { 84 params: Params{ 85 db: "TestDB", 86 host: "example.com", 87 port: 20, 88 username: "someuser", 89 password: "somepassword", 90 }, 91 connString: "someuser:somepassword@tcp(example.com:20)/TestDB", 92 }, 93 { 94 params: Params{ 95 db: "TestDB", 96 host: "example.com", 97 port: 20, 98 username: "someuser", 99 password: "somepassword", 100 QueryString: "param=value", 101 }, 102 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?param=value", 103 }, 104 { 105 params: Params{ 106 db: "TestDB", 107 host: "example.com", 108 port: 20, 109 username: "someuser", 110 password: "somepassword", 111 QueryString: "param=value&tls=preferred", 112 }, 113 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?param=value&tls=preferred", 114 }, 115 { 116 params: Params{ 117 db: "TestDB", 118 host: "example.com", 119 port: 20, 120 username: "someuser", 121 password: "somepassword", 122 TLS: TLS{ 123 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 124 }, 125 }, 126 connString: fmt.Sprintf("someuser:somepassword@tcp(example.com:20)/TestDB?tls=%s", customTLSKey), 127 }, 128 { 129 params: Params{ 130 db: "TestDB", 131 host: "example.com", 132 port: 20, 133 username: "someuser", 134 password: "somepassword", 135 QueryString: "tls=tlsKeyByQueryString", 136 TLS: TLS{ 137 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 138 }, 139 }, 140 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?tls=tlsKeyByQueryString", 141 }, 142 { 143 params: Params{ 144 db: "TestDB", 145 host: "example.com", 146 port: 20, 147 username: "someuser", 148 password: "somepassword", 149 TLS: TLS{ 150 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 151 ClientCert: path.Join(workingDir, "test_assets/client-cert.pem"), 152 ClientKey: path.Join(workingDir, "test_assets/client-key.pem"), 153 }, 154 }, 155 connString: fmt.Sprintf("someuser:somepassword@tcp(example.com:20)/TestDB?tls=%s", customTLSKey), 156 }, 157 { 158 params: Params{ 159 db: "TestDB", 160 host: "example.com", 161 port: 20, 162 username: "someuser", 163 password: "somepassword", 164 QueryString: "param=value", 165 TLS: TLS{ 166 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 167 ClientCert: path.Join(workingDir, "test_assets/client-cert.pem"), 168 ClientKey: path.Join(workingDir, "test_assets/client-key.pem"), 169 }, 170 }, 171 connString: fmt.Sprintf("someuser:somepassword@tcp(example.com:20)/TestDB?tls=%s¶m=value", customTLSKey), 172 }, 173 { 174 params: Params{ 175 db: "TestDB", 176 host: "example.com", 177 port: 20, 178 username: "someuser", 179 password: "somepassword", 180 QueryString: "tls=preferred", 181 TLS: TLS{ 182 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 183 ClientCert: path.Join(workingDir, "test_assets/client-cert.pem"), 184 ClientKey: path.Join(workingDir, "test_assets/client-key.pem"), 185 }, 186 }, 187 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?tls=preferred", 188 }, 189 { 190 params: Params{ 191 db: "TestDB", 192 host: "example.com", 193 port: 20, 194 username: "someuser", 195 password: "somepassword", 196 QueryString: "param=value&tls=preferred", 197 TLS: TLS{ 198 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 199 ClientCert: path.Join(workingDir, "test_assets/client-cert.pem"), 200 ClientKey: path.Join(workingDir, "test_assets/client-key.pem"), 201 }, 202 }, 203 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?param=value&tls=preferred", 204 }, 205 { 206 params: Params{ 207 db: "TestDB", 208 host: "example.com", 209 port: 20, 210 username: "someuser", 211 password: "somepassword", 212 QueryString: "param=value&tls=tlsKeyByQueryString", 213 TLS: TLS{ 214 RootCert: path.Join(workingDir, "test_assets/root-cert.pem"), 215 ClientCert: path.Join(workingDir, "test_assets/client-cert.pem"), 216 ClientKey: path.Join(workingDir, "test_assets/client-key.pem"), 217 }, 218 }, 219 connString: "someuser:somepassword@tcp(example.com:20)/TestDB?param=value&tls=tlsKeyByQueryString", 220 }, 221 } 222 223 for _, test := range tests { 224 cfg := config.DatabaseConnection{ 225 Database: test.params.db, 226 Host: test.params.host, 227 Port: test.params.port, 228 Username: test.params.username, 229 Password: test.params.password, 230 QueryString: test.params.QueryString, 231 TLS: config.TLS(test.params.TLS), 232 } 233 234 provider := MySqlDbProvider{ 235 cfg: cfg, 236 } 237 238 connString, _ := provider.ConnString() 239 assert.Equal(t, test.connString, connString, "Strings did not match") 240 } 241 }