@@ -39,8 +39,8 @@ var testDSNs = []struct {
39
39
"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify" ,
40
40
& Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "localhost:5555" , DBName : "dbname" , Params : map [string ]string {"charset" : "utf8mb4,utf8" }, Collation : "utf8mb4_general_ci" , Loc : time .UTC , MaxAllowedPacket : defaultMaxAllowedPacket , AllowNativePasswords : true , TLSConfig : "skip-verify" },
41
41
}, {
42
- "user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxAllowedPacket=16777216" ,
43
- & Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "127.0.0.1:3306" , DBName : "dbname" , Collation : "utf8mb4_unicode_ci" , Loc : time .UTC , AllowNativePasswords : true , Timeout : 30 * time .Second , ReadTimeout : time .Second , WriteTimeout : time .Second , AllowAllFiles : true , AllowOldPasswords : true , ClientFoundRows : true , MaxAllowedPacket : 16777216 },
42
+ "user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxAllowedPacket=16777216&tls=false&allowCleartextPasswords=true&parseTime=true&rejectReadOnly=true " ,
43
+ & Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "127.0.0.1:3306" , DBName : "dbname" , Collation : "utf8mb4_unicode_ci" , Loc : time .UTC , TLSConfig : "false" , AllowCleartextPasswords : true , AllowNativePasswords : true , Timeout : 30 * time .Second , ReadTimeout : time .Second , WriteTimeout : time .Second , AllowAllFiles : true , AllowOldPasswords : true , ClientFoundRows : true , MaxAllowedPacket : 16777216 , ParseTime : true , RejectReadOnly : true },
44
44
}, {
45
45
"user:password@/dbname?allowNativePasswords=false&maxAllowedPacket=0" ,
46
46
& Config {User : "user" , Passwd : "password" , Net : "tcp" , Addr : "127.0.0.1:3306" , DBName : "dbname" , Collation : "utf8mb4_general_ci" , Loc : time .UTC , MaxAllowedPacket : 0 , AllowNativePasswords : false },
@@ -358,6 +358,50 @@ func TestCloneConfig(t *testing.T) {
358
358
}
359
359
}
360
360
361
+ func TestNormalizeTLSConfig (t * testing.T ) {
362
+ tt := []struct {
363
+ tlsConfig string
364
+ want * tls.Config
365
+ }{
366
+ {"" , nil },
367
+ {"false" , nil },
368
+ {"true" , & tls.Config {ServerName : "myserver" }},
369
+ {"skip-verify" , & tls.Config {InsecureSkipVerify : true }},
370
+ {"preferred" , & tls.Config {InsecureSkipVerify : true }},
371
+ {"test_tls_config" , & tls.Config {ServerName : "myServerName" }},
372
+ }
373
+
374
+ RegisterTLSConfig ("test_tls_config" , & tls.Config {ServerName : "myServerName" })
375
+ defer func () { DeregisterTLSConfig ("test_tls_config" ) }()
376
+
377
+ for _ , tc := range tt {
378
+ t .Run (tc .tlsConfig , func (t * testing.T ) {
379
+ cfg := & Config {
380
+ Addr : "myserver:3306" ,
381
+ TLSConfig : tc .tlsConfig ,
382
+ }
383
+
384
+ cfg .normalize ()
385
+
386
+ if cfg .tls == nil {
387
+ if tc .want != nil {
388
+ t .Fatal ("wanted a tls config but got nil instead" )
389
+ }
390
+ return
391
+ }
392
+
393
+ if cfg .tls .ServerName != tc .want .ServerName {
394
+ t .Errorf ("tls.ServerName doesn't match (want: '%s', got: '%s')" ,
395
+ tc .want .ServerName , cfg .tls .ServerName )
396
+ }
397
+ if cfg .tls .InsecureSkipVerify != tc .want .InsecureSkipVerify {
398
+ t .Errorf ("tls.InsecureSkipVerify doesn't match (want: %T, got :%T)" ,
399
+ tc .want .InsecureSkipVerify , cfg .tls .InsecureSkipVerify )
400
+ }
401
+ })
402
+ }
403
+ }
404
+
361
405
func BenchmarkParseDSN (b * testing.B ) {
362
406
b .ReportAllocs ()
363
407
0 commit comments