dsn_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, AllowNativePasswords: true},
  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, AllowNativePasswords: true, 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, AllowNativePasswords: true, 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, AllowNativePasswords: true},
  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, AllowNativePasswords: true, 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, AllowNativePasswords: true, 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, AllowNativePasswords: true, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxAllowedPacket: 16777216},
  41. }, {
  42. "user:password@/dbname?allowNativePasswords=false",
  43. &Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: false},
  44. }, {
  45. "user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local",
  46. &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, AllowNativePasswords: true},
  47. }, {
  48. "/dbname",
  49. &Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  50. }, {
  51. "@/",
  52. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  53. }, {
  54. "/",
  55. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  56. }, {
  57. "",
  58. &Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  59. }, {
  60. "user:p@/ssword@/",
  61. &Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  62. }, {
  63. "unix/?arg=%2Fsome%2Fpath.ext",
  64. &Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
  65. }}
  66. func TestDSNParser(t *testing.T) {
  67. for i, tst := range testDSNs {
  68. cfg, err := ParseDSN(tst.in)
  69. if err != nil {
  70. t.Error(err.Error())
  71. }
  72. // pointer not static
  73. cfg.tls = nil
  74. if !reflect.DeepEqual(cfg, tst.out) {
  75. t.Errorf("%d. ParseDSN(%q) mismatch:\ngot %+v\nwant %+v", i, tst.in, cfg, tst.out)
  76. }
  77. }
  78. }
  79. func TestDSNParserInvalid(t *testing.T) {
  80. var invalidDSNs = []string{
  81. "@net(addr/", // no closing brace
  82. "@tcp(/", // no closing brace
  83. "tcp(/", // no closing brace
  84. "(/", // no closing brace
  85. "net(addr)//", // unescaped
  86. "User:pass@tcp(1.2.3.4:3306)", // no trailing slash
  87. //"/dbname?arg=/some/unescaped/path",
  88. }
  89. for i, tst := range invalidDSNs {
  90. if _, err := ParseDSN(tst); err == nil {
  91. t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
  92. }
  93. }
  94. }
  95. func TestDSNReformat(t *testing.T) {
  96. for i, tst := range testDSNs {
  97. dsn1 := tst.in
  98. cfg1, err := ParseDSN(dsn1)
  99. if err != nil {
  100. t.Error(err.Error())
  101. continue
  102. }
  103. cfg1.tls = nil // pointer not static
  104. res1 := fmt.Sprintf("%+v", cfg1)
  105. dsn2 := cfg1.FormatDSN()
  106. cfg2, err := ParseDSN(dsn2)
  107. if err != nil {
  108. t.Error(err.Error())
  109. continue
  110. }
  111. cfg2.tls = nil // pointer not static
  112. res2 := fmt.Sprintf("%+v", cfg2)
  113. if res1 != res2 {
  114. t.Errorf("%d. %q does not match %q", i, res2, res1)
  115. }
  116. }
  117. }
  118. func TestDSNWithCustomTLS(t *testing.T) {
  119. baseDSN := "User:password@tcp(localhost:5555)/dbname?tls="
  120. tlsCfg := tls.Config{}
  121. RegisterTLSConfig("utils_test", &tlsCfg)
  122. // Custom TLS is missing
  123. tst := baseDSN + "invalid_tls"
  124. cfg, err := ParseDSN(tst)
  125. if err == nil {
  126. t.Errorf("invalid custom TLS in DSN (%s) but did not error. Got config: %#v", tst, cfg)
  127. }
  128. tst = baseDSN + "utils_test"
  129. // Custom TLS with a server name
  130. name := "foohost"
  131. tlsCfg.ServerName = name
  132. cfg, err = ParseDSN(tst)
  133. if err != nil {
  134. t.Error(err.Error())
  135. } else if cfg.tls.ServerName != name {
  136. t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, tst)
  137. }
  138. // Custom TLS without a server name
  139. name = "localhost"
  140. tlsCfg.ServerName = ""
  141. cfg, err = ParseDSN(tst)
  142. if err != nil {
  143. t.Error(err.Error())
  144. } else if cfg.tls.ServerName != name {
  145. t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
  146. } else if tlsCfg.ServerName != "" {
  147. t.Errorf("tlsCfg was mutated ServerName (%s) should be empty parsing DSN (%s).", name, tst)
  148. }
  149. DeregisterTLSConfig("utils_test")
  150. }
  151. func TestDSNWithCustomTLSQueryEscape(t *testing.T) {
  152. const configKey = "&%!:"
  153. dsn := "User:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
  154. name := "foohost"
  155. tlsCfg := tls.Config{ServerName: name}
  156. RegisterTLSConfig(configKey, &tlsCfg)
  157. cfg, err := ParseDSN(dsn)
  158. if err != nil {
  159. t.Error(err.Error())
  160. } else if cfg.tls.ServerName != name {
  161. t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
  162. }
  163. }
  164. func TestDSNUnsafeCollation(t *testing.T) {
  165. _, err := ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
  166. if err != errInvalidDSNUnsafeCollation {
  167. t.Errorf("expected %v, got %v", errInvalidDSNUnsafeCollation, err)
  168. }
  169. _, err = ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=false")
  170. if err != nil {
  171. t.Errorf("expected %v, got %v", nil, err)
  172. }
  173. _, err = ParseDSN("/dbname?collation=gbk_chinese_ci")
  174. if err != nil {
  175. t.Errorf("expected %v, got %v", nil, err)
  176. }
  177. _, err = ParseDSN("/dbname?collation=ascii_bin&interpolateParams=true")
  178. if err != nil {
  179. t.Errorf("expected %v, got %v", nil, err)
  180. }
  181. _, err = ParseDSN("/dbname?collation=latin1_german1_ci&interpolateParams=true")
  182. if err != nil {
  183. t.Errorf("expected %v, got %v", nil, err)
  184. }
  185. _, err = ParseDSN("/dbname?collation=utf8_general_ci&interpolateParams=true")
  186. if err != nil {
  187. t.Errorf("expected %v, got %v", nil, err)
  188. }
  189. _, err = ParseDSN("/dbname?collation=utf8mb4_general_ci&interpolateParams=true")
  190. if err != nil {
  191. t.Errorf("expected %v, got %v", nil, err)
  192. }
  193. }
  194. func TestParamsAreSorted(t *testing.T) {
  195. expected := "/dbname?interpolateParams=true&foobar=baz&quux=loo"
  196. dsn := &Config{
  197. DBName: "dbname",
  198. InterpolateParams: true,
  199. AllowNativePasswords: true,
  200. Params: map[string]string{
  201. "quux": "loo",
  202. "foobar": "baz",
  203. },
  204. }
  205. actual := dsn.FormatDSN()
  206. if actual != expected {
  207. t.Errorf("generic Config.Params were not sorted: want %#v, got %#v", expected, actual)
  208. }
  209. }
  210. func BenchmarkParseDSN(b *testing.B) {
  211. b.ReportAllocs()
  212. for i := 0; i < b.N; i++ {
  213. for _, tst := range testDSNs {
  214. if _, err := ParseDSN(tst.in); err != nil {
  215. b.Error(err.Error())
  216. }
  217. }
  218. }
  219. }