-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_test.go
83 lines (77 loc) · 1.81 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package binlog
import (
"flag"
"fmt"
"net/url"
"os"
"strings"
"testing"
"time"
)
func TestRemote_Authenticate(t *testing.T) {
if *mysql == "" {
t.Skip(skipReason)
}
r, err := Dial(network, address, 5*time.Second)
if err != nil {
t.Fatal(err)
}
defer r.Close()
if ssl {
if !r.IsSSLSupported() {
t.Fatal("server does not support ssl")
}
if err := r.UpgradeSSL(nil); err != nil {
t.Fatal(err)
}
}
err = r.Authenticate(user, passwd)
t.Log("authFlow:", r.authFlow)
if err != nil {
t.Fatal(err)
}
if _, err = r.queryRows("show databases"); err != nil {
t.Fatal(err)
}
}
// test flags ---
var (
mysql = flag.String("mysql", "", "mysql server used for testing")
network, address string
user, passwd string
db = "binlog"
ssl bool
driverURL string
skipReason = `SKIPPED: pass -mysql flag to run this test
example: go test -mysql tcp:localhost:3306,ssl,user=root,password=password,db=binlog
`
)
func TestMain(m *testing.M) {
flag.Parse()
if *mysql != "" {
colon := strings.IndexByte(*mysql, ':')
network, address = (*mysql)[:colon], (*mysql)[colon+1:]
tok := strings.Split(address, ",")
address = tok[0]
for _, t := range tok[1:] {
switch {
case t == "ssl":
ssl = true
case strings.HasPrefix(t, "user="):
user = strings.TrimPrefix(t, "user=")
case strings.HasPrefix(t, "password="):
passwd = strings.TrimPrefix(t, "password=")
case strings.HasPrefix(t, "db="):
db = strings.TrimPrefix(t, "db=")
}
}
tls := "false"
if ssl {
tls = "skip-verify"
}
timezone := url.QueryEscape(time.Now().Format("'-07:00'"))
sqlmode := url.QueryEscape("''")
driverURL = fmt.Sprintf("%s:%s@%s(%s)/%s?tls=%v&time_zone=%s&sql_mode=%s", user, passwd, network, address, db, tls, timezone, sqlmode)
}
os.Exit(m.Run())
}