dsn_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "crypto/tls"
  11. "fmt"
  12. "net/url"
  13. "reflect"
  14. "testing"
  15. "time"
  16. )
  17. var testDSNs = []struct {
  18. in string
  19. out *Config
  20. }{{
  21. "username:password@protocol(address)/dbname?param=value",
  22. &Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC},
  23. }, {
  24. "username:password@protocol(address)/dbname?param=value&columnsWithAlias=true",
  25. &Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, ColumnsWithAlias: true},
  26. }, {
  27. "username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true",
  28. &Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, ColumnsWithAlias: true, MultiStatements: true},
  29. }, {
  30. "user@unix(/path/to/socket)/dbname?charset=utf8",
  31. &Config{User: "user", Net: "unix", Addr: "/path/to/socket", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC},
  32. }, {
  33. "user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true",
  34. &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, TLSConfig: "true"},
  35. }, {
  36. "user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
  37. &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8mb4,utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, TLSConfig: "skip-verify"},
  38. }, {
  39. "user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxAllowedPacket=16777216",
  40. &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxAllowedPacket: 16777216},
  41. }, {
  42. "user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local",
  43. &Config{User: "user", Passwd: "p@ss(word)", Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:80", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.Local},
  44. }, {
  45. "/dbname",
  46. &Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC},
  47. }, {
  48. "@/",
  49. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
  50. }, {
  51. "/",
  52. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
  53. }, {
  54. "",
  55. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
  56. }, {
  57. "user:p@/ssword@/",
  58. &Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC},
  59. }, {
  60. "unix/?arg=%2Fsome%2Fpath.ext",
  61. &Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC},
  62. }}
  63. func TestDSNParser(t *testing.T) {
  64. for i, tst := range testDSNs {
  65. cfg, err := ParseDSN(tst.in)
  66. if err != nil {
  67. t.Error(err.Error())
  68. }
  69. // pointer not static
  70. cfg.tls = nil
  71. if !reflect.DeepEqual(cfg, tst.out) {
  72. t.Errorf("%d. ParseDSN(%q) mismatch:\ngot %+v\nwant %+v", i, tst.in, cfg, tst.out)
  73. }
  74. }
  75. }
  76. func TestDSNParserInvalid(t *testing.T) {
  77. var invalidDSNs = []string{
  78. "@net(addr/", // no closing brace
  79. "@tcp(/", // no closing brace
  80. "tcp(/", // no closing brace
  81. "(/", // no closing brace
  82. "net(addr)//", // unescaped
  83. "User:pass@tcp(1.2.3.4:3306)", // no trailing slash
  84. //"/dbname?arg=/some/unescaped/path",
  85. }
  86. for i, tst := range invalidDSNs {
  87. if _, err := ParseDSN(tst); err == nil {
  88. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  89. }
  90. }
  91. }
  92. func TestDSNReformat(t *testing.T) {
  93. for i, tst := range testDSNs {
  94. dsn1 := tst.in
  95. cfg1, err := ParseDSN(dsn1)
  96. if err != nil {
  97. t.Error(err.Error())
  98. continue
  99. }
  100. cfg1.tls = nil // pointer not static
  101. res1 := fmt.Sprintf("%+v", cfg1)
  102. dsn2 := cfg1.FormatDSN()
  103. cfg2, err := ParseDSN(dsn2)
  104. if err != nil {
  105. t.Error(err.Error())
  106. continue
  107. }
  108. cfg2.tls = nil // pointer not static
  109. res2 := fmt.Sprintf("%+v", cfg2)
  110. if res1 != res2 {
  111. t.Errorf("%d. %q does not match %q", i, res2, res1)
  112. }
  113. }
  114. }
  115. func TestDSNWithCustomTLS(t *testing.T) {
  116. baseDSN := "User:password@tcp(localhost:5555)/dbname?tls="
  117. tlsCfg := tls.Config{}
  118. RegisterTLSConfig("utils_test", &tlsCfg)
  119. // Custom TLS is missing
  120. tst := baseDSN + "invalid_tls"
  121. cfg, err := ParseDSN(tst)
  122. if err == nil {
  123. t.Errorf("invalid custom TLS in DSN (%s) but did not error. Got config: %#v", tst, cfg)
  124. }
  125. tst = baseDSN + "utils_test"
  126. // Custom TLS with a server name
  127. name := "foohost"
  128. tlsCfg.ServerName = name
  129. cfg, err = ParseDSN(tst)
  130. if err != nil {
  131. t.Error(err.Error())
  132. } else if cfg.tls.ServerName != name {
  133. t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, tst)
  134. }
  135. // Custom TLS without a server name
  136. name = "localhost"
  137. tlsCfg.ServerName = ""
  138. cfg, err = ParseDSN(tst)
  139. if err != nil {
  140. t.Error(err.Error())
  141. } else if cfg.tls.ServerName != name {
  142. t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
  143. }
  144. DeregisterTLSConfig("utils_test")
  145. }
  146. func TestDSNWithCustomTLSQueryEscape(t *testing.T) {
  147. const configKey = "&%!:"
  148. dsn := "User:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
  149. name := "foohost"
  150. tlsCfg := tls.Config{ServerName: name}
  151. RegisterTLSConfig(configKey, &tlsCfg)
  152. cfg, err := ParseDSN(dsn)
  153. if err != nil {
  154. t.Error(err.Error())
  155. } else if cfg.tls.ServerName != name {
  156. t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
  157. }
  158. }
  159. func TestDSNUnsafeCollation(t *testing.T) {
  160. _, err := ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
  161. if err != errInvalidDSNUnsafeCollation {
  162. t.Errorf("expected %v, got %v", errInvalidDSNUnsafeCollation, err)
  163. }
  164. _, err = ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=false")
  165. if err != nil {
  166. t.Errorf("expected %v, got %v", nil, err)
  167. }
  168. _, err = ParseDSN("/dbname?collation=gbk_chinese_ci")
  169. if err != nil {
  170. t.Errorf("expected %v, got %v", nil, err)
  171. }
  172. _, err = ParseDSN("/dbname?collation=ascii_bin&interpolateParams=true")
  173. if err != nil {
  174. t.Errorf("expected %v, got %v", nil, err)
  175. }
  176. _, err = ParseDSN("/dbname?collation=latin1_german1_ci&interpolateParams=true")
  177. if err != nil {
  178. t.Errorf("expected %v, got %v", nil, err)
  179. }
  180. _, err = ParseDSN("/dbname?collation=utf8_general_ci&interpolateParams=true")
  181. if err != nil {
  182. t.Errorf("expected %v, got %v", nil, err)
  183. }
  184. _, err = ParseDSN("/dbname?collation=utf8mb4_general_ci&interpolateParams=true")
  185. if err != nil {
  186. t.Errorf("expected %v, got %v", nil, err)
  187. }
  188. }
  189. func BenchmarkParseDSN(b *testing.B) {
  190. b.ReportAllocs()
  191. for i := 0; i < b.N; i++ {
  192. for _, tst := range testDSNs {
  193. if _, err := ParseDSN(tst.in); err != nil {
  194. b.Error(err.Error())
  195. }
  196. }
  197. }
  198. }