-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.sh
executable file
·167 lines (140 loc) · 3.7 KB
/
test.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env bash
set -e
if [ "$#" -ne 1 ]; then
echo 'Usage: ./auth_test.sh SERVER_CTL_FILE' 1>&2
echo 1>&2
echo 'Examples:' 1>&2
echo ' ./auth_test.sh ./homebrew.sh' 1>&2
echo ' ./auth_test.sh ./docker.sh' 1>&2
exit 1
fi
if ! [ -x "$(command -v mysql)" ]; then
echo mysql client is not installed 1>&2
exit 1
fi
source $1
plugins=(mysql_native sha256 caching_sha2)
tmp=$(mktemp -d)
mycnf_backup=$tmp/my.cnf
cp $mycnf $mycnf_backup
creds=$tmp/creds.cnf
cat > $creds << EOF
[client]
protocol = tcp
host = $host
port = $port
user = $user
password = $password
EOF
on_exit(){
exit_code=$?
echo +++ deleting users before exit
if running; then
for plugin in ${plugins[@]}; do
execute "DROP USER IF EXISTS '${plugin}_user'@'%'"
done
fi
set -ex
cp $mycnf_backup $mycnf
cleanup
rm -rf cov.txt
rm -rf $tmp
if [ $exit_code -eq 0 ]; then
echo 'script succeeded' >&2
else
echo "script failed at line $BASH_LINENO (exit code $exit_code)" >&2
fi
}
trap on_exit EXIT
running() {
mysql --defaults-extra-file=$creds --execute=exit > /dev/null 2>&1
}
start() {
echo +++ starting mysql
start_mysql
while ! running; do
echo mysql is not up yet
sleep 3
done
echo mysql is up
}
stop() {
echo +++ stopping mysql
stop_mysql
while running; do
echo mystil is not down yet
sleep 3
done
echo mysql is down
}
restart() {
if running; then
stop
fi
start
}
execute() {
sql=$1
echo +++ $sql
mysql --defaults-extra-file=$creds --execute="$sql"
}
create_users() {
for plugin in ${plugins[@]}; do
pass=$(pwgen $plugin)
execute "DROP USER IF EXISTS '${plugin}_user'@'%'"
execute "CREATE USER '${plugin}_user'@'%' IDENTIFIED WITH ${plugin}_password BY '$pass'"
done
}
run_tests() {
for defplugin in ${plugins[@]}; do
echo +++ set default_authentication_plugin=${defplugin}_password
cp $mycnf_backup $mycnf
echo default_authentication_plugin=${defplugin}_password >> $mycnf
restart
for plugin in ${plugins[@]}; do
pass=$(pwgen $plugin)
if [ -z "$sock" ]; then
echo +++ skipped unix transport
else
echo +++ testing ${plugin} with default=$defplugin transport=unix
go test -v -coverprofile cov.txt -mysql unix:$sock,user=${plugin}_user,password=$pass -run TestRemote_Authenticate
cat cov.txt >> coverage.txt
fi
echo +++ testing ${plugin} with default=$defplugin transport=tcp
go test -v -coverprofile cov.txt -mysql tcp:$host:$port,user=${plugin}_user,password=$pass -run TestRemote_Authenticate
cat cov.txt >> coverage.txt
echo +++ testing ${plugin} with default=$defplugin transport=ssl
go test -v -coverprofile cov.txt -mysql tcp:$host:$port,user=${plugin}_user,password=$pass,ssl -run TestRemote_Authenticate
cat cov.txt >> coverage.txt
done
done
}
rm -rf cov.txt coverage.txt
if ! running; then
start
fi
echo '+++ testing with short password (less than 20 chars)'
pwgen() {
plugin=$1
echo -n ${plugin}_secret
}
create_users
run_tests
echo +++ testing with empty password
pwgen() {
echo -n
}
create_users
run_tests
echo '+++ testing with long password (greater than 20 chars)'
pwgen() {
plugin=$1
echo -n ${plugin}_really_very_long_password
}
create_users
run_tests
echo '+++ run remaining tests'
restart
execute "CREATE DATABASE binlog"
go test -v -coverprofile cov.txt -mysql tcp:$host:$port,ssl,user=$user,password=$password,db=binlog
cat cov.txt >> coverage.txt