github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/mysql/utils_test.go (about) 1 // Go MySQL Driver - A MySQL-Driver for Go's database/sql package 2 // 3 // Copyright 2013 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 9 package mysql 10 11 import ( 12 "bytes" 13 "encoding/binary" 14 "fmt" 15 "testing" 16 "time" 17 ) 18 19 var testDSNs = []struct { 20 in string 21 out string 22 loc *time.Location 23 }{ 24 {"username:password@protocol(address)/dbname?param=value", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 25 {"user@unix(/path/to/socket)/dbname?charset=utf8", "&{user:user passwd: net:unix addr:/path/to/socket dbname:dbname params:map[charset:utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 26 {"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true", "&{user:user passwd:password net:tcp addr:localhost:5555 dbname:dbname params:map[charset:utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 27 {"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify", "&{user:user passwd:password net:tcp addr:localhost:5555 dbname:dbname params:map[charset:utf8mb4,utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 28 {"user:password@/dbname?loc=UTC&timeout=30s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci", "&{user:user passwd:password net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p tls:<nil> timeout:30000000000 collation:224 allowAllFiles:true allowOldPasswords:true clientFoundRows:true}", time.UTC}, 29 {"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local", "&{user:user passwd:p@ss(word) net:tcp addr:[de:ad:be:ef::ca:fe]:80 dbname:dbname params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.Local}, 30 {"/dbname", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 31 {"@/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 32 {"/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 33 {"", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 34 {"user:p@/ssword@/", "&{user:user passwd:p@/ssword net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 35 {"unix/?arg=%2Fsome%2Fpath.ext", "&{user: passwd: net:unix addr:/tmp/mysql.sock dbname: params:map[arg:/some/path.ext] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC}, 36 } 37 38 func TestDSNParser(t *testing.T) { 39 var cfg *config 40 var err error 41 var res string 42 43 for i, tst := range testDSNs { 44 cfg, err = parseDSN(tst.in) 45 if err != nil { 46 t.Error(err.Error()) 47 } 48 49 // pointer not static 50 cfg.tls = nil 51 52 res = fmt.Sprintf("%+v", cfg) 53 if res != fmt.Sprintf(tst.out, tst.loc) { 54 t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc)) 55 } 56 } 57 } 58 59 func TestDSNParserInvalid(t *testing.T) { 60 var invalidDSNs = []string{ 61 "@net(addr/", // no closing brace 62 "@tcp(/", // no closing brace 63 "tcp(/", // no closing brace 64 "(/", // no closing brace 65 "net(addr)//", // unescaped 66 "user:pass@tcp(1.2.3.4:3306)", // no trailing slash 67 //"/dbname?arg=/some/unescaped/path", 68 } 69 70 for i, tst := range invalidDSNs { 71 if _, err := parseDSN(tst); err == nil { 72 t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst) 73 } 74 } 75 } 76 77 func BenchmarkParseDSN(b *testing.B) { 78 b.ReportAllocs() 79 80 for i := 0; i < b.N; i++ { 81 for _, tst := range testDSNs { 82 if _, err := parseDSN(tst.in); err != nil { 83 b.Error(err.Error()) 84 } 85 } 86 } 87 } 88 89 func TestScanNullTime(t *testing.T) { 90 var scanTests = []struct { 91 in interface{} 92 error bool 93 valid bool 94 time time.Time 95 }{ 96 {tDate, false, true, tDate}, 97 {sDate, false, true, tDate}, 98 {[]byte(sDate), false, true, tDate}, 99 {tDateTime, false, true, tDateTime}, 100 {sDateTime, false, true, tDateTime}, 101 {[]byte(sDateTime), false, true, tDateTime}, 102 {tDate0, false, true, tDate0}, 103 {sDate0, false, true, tDate0}, 104 {[]byte(sDate0), false, true, tDate0}, 105 {sDateTime0, false, true, tDate0}, 106 {[]byte(sDateTime0), false, true, tDate0}, 107 {"", true, false, tDate0}, 108 {"1234", true, false, tDate0}, 109 {0, true, false, tDate0}, 110 } 111 112 var nt = NullTime{} 113 var err error 114 115 for _, tst := range scanTests { 116 err = nt.Scan(tst.in) 117 if (err != nil) != tst.error { 118 t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil)) 119 } 120 if nt.Valid != tst.valid { 121 t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid) 122 } 123 if nt.Time != tst.time { 124 t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time) 125 } 126 } 127 } 128 129 func TestLengthEncodedInteger(t *testing.T) { 130 var integerTests = []struct { 131 num uint64 132 encoded []byte 133 }{ 134 {0x0000000000000000, []byte{0x00}}, 135 {0x0000000000000012, []byte{0x12}}, 136 {0x00000000000000fa, []byte{0xfa}}, 137 {0x0000000000000100, []byte{0xfc, 0x00, 0x01}}, 138 {0x0000000000001234, []byte{0xfc, 0x34, 0x12}}, 139 {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}}, 140 {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}}, 141 {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}}, 142 {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}}, 143 {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}}, 144 {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}}, 145 {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 146 } 147 148 for _, tst := range integerTests { 149 num, isNull, numLen := readLengthEncodedInteger(tst.encoded) 150 if isNull { 151 t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num) 152 } 153 if num != tst.num { 154 t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num) 155 } 156 if numLen != len(tst.encoded) { 157 t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen) 158 } 159 encoded := appendLengthEncodedInteger(nil, num) 160 if !bytes.Equal(encoded, tst.encoded) { 161 t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded) 162 } 163 } 164 } 165 166 func TestOldPass(t *testing.T) { 167 scramble := []byte{9, 8, 7, 6, 5, 4, 3, 2} 168 vectors := []struct { 169 pass string 170 out string 171 }{ 172 {" pass", "47575c5a435b4251"}, 173 {"pass ", "47575c5a435b4251"}, 174 {"123\t456", "575c47505b5b5559"}, 175 {"C0mpl!ca ted#PASS123", "5d5d554849584a45"}, 176 } 177 for _, tuple := range vectors { 178 ours := scrambleOldPassword(scramble, []byte(tuple.pass)) 179 if tuple.out != fmt.Sprintf("%x", ours) { 180 t.Errorf("Failed old password %q", tuple.pass) 181 } 182 } 183 } 184 185 func TestFormatBinaryDateTime(t *testing.T) { 186 rawDate := [11]byte{} 187 binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years 188 rawDate[2] = 12 // months 189 rawDate[3] = 30 // days 190 rawDate[4] = 15 // hours 191 rawDate[5] = 46 // minutes 192 rawDate[6] = 23 // seconds 193 binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds 194 expect := func(expected string, length int, withTime bool) { 195 actual, _ := formatBinaryDateTime(rawDate[:length], withTime) 196 bytes, ok := actual.([]byte) 197 if !ok { 198 t.Errorf("formatBinaryDateTime must return []byte, was %T", actual) 199 } 200 if string(bytes) != expected { 201 t.Errorf( 202 "expected %q, got %q for length %d, withTime %v", 203 bytes, actual, length, withTime, 204 ) 205 } 206 } 207 expect("0000-00-00", 0, false) 208 expect("0000-00-00 00:00:00", 0, true) 209 expect("1978-12-30", 4, false) 210 expect("1978-12-30 15:46:23", 7, true) 211 expect("1978-12-30 15:46:23.987654", 11, true) 212 }