github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-sql-driver/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 func TestScanNullTime(t *testing.T) { 20 var scanTests = []struct { 21 in interface{} 22 error bool 23 valid bool 24 time time.Time 25 }{ 26 {tDate, false, true, tDate}, 27 {sDate, false, true, tDate}, 28 {[]byte(sDate), false, true, tDate}, 29 {tDateTime, false, true, tDateTime}, 30 {sDateTime, false, true, tDateTime}, 31 {[]byte(sDateTime), false, true, tDateTime}, 32 {tDate0, false, true, tDate0}, 33 {sDate0, false, true, tDate0}, 34 {[]byte(sDate0), false, true, tDate0}, 35 {sDateTime0, false, true, tDate0}, 36 {[]byte(sDateTime0), false, true, tDate0}, 37 {"", true, false, tDate0}, 38 {"1234", true, false, tDate0}, 39 {0, true, false, tDate0}, 40 } 41 42 var nt = NullTime{} 43 var err error 44 45 for _, tst := range scanTests { 46 err = nt.Scan(tst.in) 47 if (err != nil) != tst.error { 48 t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil)) 49 } 50 if nt.Valid != tst.valid { 51 t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid) 52 } 53 if nt.Time != tst.time { 54 t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time) 55 } 56 } 57 } 58 59 func TestLengthEncodedInteger(t *testing.T) { 60 var integerTests = []struct { 61 num uint64 62 encoded []byte 63 }{ 64 {0x0000000000000000, []byte{0x00}}, 65 {0x0000000000000012, []byte{0x12}}, 66 {0x00000000000000fa, []byte{0xfa}}, 67 {0x0000000000000100, []byte{0xfc, 0x00, 0x01}}, 68 {0x0000000000001234, []byte{0xfc, 0x34, 0x12}}, 69 {0x000000000000ffff, []byte{0xfc, 0xff, 0xff}}, 70 {0x0000000000010000, []byte{0xfd, 0x00, 0x00, 0x01}}, 71 {0x0000000000123456, []byte{0xfd, 0x56, 0x34, 0x12}}, 72 {0x0000000000ffffff, []byte{0xfd, 0xff, 0xff, 0xff}}, 73 {0x0000000001000000, []byte{0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}}, 74 {0x123456789abcdef0, []byte{0xfe, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}}, 75 {0xffffffffffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, 76 } 77 78 for _, tst := range integerTests { 79 num, isNull, numLen := readLengthEncodedInteger(tst.encoded) 80 if isNull { 81 t.Errorf("%x: expected %d, got NULL", tst.encoded, tst.num) 82 } 83 if num != tst.num { 84 t.Errorf("%x: expected %d, got %d", tst.encoded, tst.num, num) 85 } 86 if numLen != len(tst.encoded) { 87 t.Errorf("%x: expected size %d, got %d", tst.encoded, len(tst.encoded), numLen) 88 } 89 encoded := appendLengthEncodedInteger(nil, num) 90 if !bytes.Equal(encoded, tst.encoded) { 91 t.Errorf("%v: expected %x, got %x", num, tst.encoded, encoded) 92 } 93 } 94 } 95 96 func TestOldPass(t *testing.T) { 97 scramble := []byte{9, 8, 7, 6, 5, 4, 3, 2} 98 vectors := []struct { 99 pass string 100 out string 101 }{ 102 {" pass", "47575c5a435b4251"}, 103 {"pass ", "47575c5a435b4251"}, 104 {"123\t456", "575c47505b5b5559"}, 105 {"C0mpl!ca ted#PASS123", "5d5d554849584a45"}, 106 } 107 for _, tuple := range vectors { 108 ours := scrambleOldPassword(scramble, []byte(tuple.pass)) 109 if tuple.out != fmt.Sprintf("%x", ours) { 110 t.Errorf("Failed old password %q", tuple.pass) 111 } 112 } 113 } 114 115 func TestFormatBinaryDateTime(t *testing.T) { 116 rawDate := [11]byte{} 117 binary.LittleEndian.PutUint16(rawDate[:2], 1978) // years 118 rawDate[2] = 12 // months 119 rawDate[3] = 30 // days 120 rawDate[4] = 15 // hours 121 rawDate[5] = 46 // minutes 122 rawDate[6] = 23 // seconds 123 binary.LittleEndian.PutUint32(rawDate[7:], 987654) // microseconds 124 expect := func(expected string, inlen, outlen uint8) { 125 actual, _ := formatBinaryDateTime(rawDate[:inlen], outlen, false) 126 bytes, ok := actual.([]byte) 127 if !ok { 128 t.Errorf("formatBinaryDateTime must return []byte, was %T", actual) 129 } 130 if string(bytes) != expected { 131 t.Errorf( 132 "expected %q, got %q for length in %d, out %d", 133 bytes, actual, inlen, outlen, 134 ) 135 } 136 } 137 expect("0000-00-00", 0, 10) 138 expect("0000-00-00 00:00:00", 0, 19) 139 expect("1978-12-30", 4, 10) 140 expect("1978-12-30 15:46:23", 7, 19) 141 expect("1978-12-30 15:46:23.987654", 11, 26) 142 } 143 144 func TestEscapeBackslash(t *testing.T) { 145 expect := func(expected, value string) { 146 actual := string(escapeBytesBackslash([]byte{}, []byte(value))) 147 if actual != expected { 148 t.Errorf( 149 "expected %s, got %s", 150 expected, actual, 151 ) 152 } 153 154 actual = string(escapeStringBackslash([]byte{}, value)) 155 if actual != expected { 156 t.Errorf( 157 "expected %s, got %s", 158 expected, actual, 159 ) 160 } 161 } 162 163 expect("foo\\0bar", "foo\x00bar") 164 expect("foo\\nbar", "foo\nbar") 165 expect("foo\\rbar", "foo\rbar") 166 expect("foo\\Zbar", "foo\x1abar") 167 expect("foo\\\"bar", "foo\"bar") 168 expect("foo\\\\bar", "foo\\bar") 169 expect("foo\\'bar", "foo'bar") 170 } 171 172 func TestEscapeQuotes(t *testing.T) { 173 expect := func(expected, value string) { 174 actual := string(escapeBytesQuotes([]byte{}, []byte(value))) 175 if actual != expected { 176 t.Errorf( 177 "expected %s, got %s", 178 expected, actual, 179 ) 180 } 181 182 actual = string(escapeStringQuotes([]byte{}, value)) 183 if actual != expected { 184 t.Errorf( 185 "expected %s, got %s", 186 expected, actual, 187 ) 188 } 189 } 190 191 expect("foo\x00bar", "foo\x00bar") // not affected 192 expect("foo\nbar", "foo\nbar") // not affected 193 expect("foo\rbar", "foo\rbar") // not affected 194 expect("foo\x1abar", "foo\x1abar") // not affected 195 expect("foo''bar", "foo'bar") // affected 196 expect("foo\"bar", "foo\"bar") // not affected 197 }