-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcaching_sha2_cache_test.go
238 lines (200 loc) · 5.91 KB
/
caching_sha2_cache_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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package server
import (
"database/sql"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/test_util"
"github.com/go-mysql-org/go-mysql/test_util/test_keys"
)
// test caching for 'caching_sha2_password'
// NOTE the idea here is to plugin a throttled credential provider so that the first connection (cache miss) will take longer time
// than the second connection (cache hit). Remember to set the password for MySQL user otherwise it won't cache empty password.
func TestCachingSha2Cache(t *testing.T) {
remoteProvider := &RemoteThrottleProvider{
InMemoryProvider: NewInMemoryProvider(),
}
remoteProvider.AddUser(*testUser, *testPassword)
cacheServer := NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.PubPem, tlsConf)
// no TLS
suite.Run(t, &cacheTestSuite{
server: cacheServer,
credProvider: remoteProvider,
tlsPara: "false",
})
}
func TestCachingSha2CacheTLS(t *testing.T) {
remoteProvider := &RemoteThrottleProvider{
InMemoryProvider: NewInMemoryProvider(),
}
remoteProvider.AddUser(*testUser, *testPassword)
cacheServer := NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.PubPem, tlsConf)
// TLS
suite.Run(t, &cacheTestSuite{
server: cacheServer,
credProvider: remoteProvider,
tlsPara: "skip-verify",
})
}
type RemoteThrottleProvider struct {
*InMemoryProvider
getCredCallCount atomic.Int64
}
func (m *RemoteThrottleProvider) GetCredential(username string) (password string, found bool, err error) {
m.getCredCallCount.Add(1)
return m.InMemoryProvider.GetCredential(username)
}
type cacheTestSuite struct {
suite.Suite
server *Server
serverAddr string
credProvider CredentialProvider
tlsPara string
db *sql.DB
l net.Listener
}
func (s *cacheTestSuite) SetupSuite() {
s.serverAddr = fmt.Sprintf("%s:%s", *test_util.MysqlFakeHost, *test_util.MysqlFakePort)
var err error
s.l, err = net.Listen("tcp", s.serverAddr)
require.NoError(s.T(), err)
go s.onAccept()
time.Sleep(30 * time.Millisecond)
}
func (s *cacheTestSuite) TearDownSuite() {
if s.l != nil {
s.l.Close()
}
}
func (s *cacheTestSuite) onAccept() {
for {
conn, err := s.l.Accept()
if err != nil {
return
}
go s.onConn(conn)
}
}
func (s *cacheTestSuite) onConn(conn net.Conn) {
// co, err := NewConn(conn, *testUser, *testPassword, &testHandler{s})
co, err := NewCustomizedConn(conn, s.server, s.credProvider, &testCacheHandler{s})
require.NoError(s.T(), err)
for {
err = co.HandleCommand()
if err != nil {
return
}
}
}
func (s *cacheTestSuite) runSelect() {
var a int64
var b string
err := s.db.QueryRow("SELECT a, b FROM tbl WHERE id=1").Scan(&a, &b)
require.NoError(s.T(), err)
require.Equal(s.T(), int64(1), a)
require.Equal(s.T(), "hello world", b)
}
func (s *cacheTestSuite) TestCache() {
// first connection
var err error
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s", *testUser, *testPassword, s.serverAddr, *testDB, s.tlsPara))
require.NoError(s.T(), err)
s.db.SetMaxIdleConns(4)
s.runSelect()
got := s.credProvider.(*RemoteThrottleProvider).getCredCallCount.Load()
require.Equal(s.T(), int64(1), got)
if s.db != nil {
s.db.Close()
}
// second connection
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s", *testUser, *testPassword, s.serverAddr, *testDB, s.tlsPara))
require.NoError(s.T(), err)
s.db.SetMaxIdleConns(4)
s.runSelect()
got = s.credProvider.(*RemoteThrottleProvider).getCredCallCount.Load()
require.Equal(s.T(), int64(1), got)
if s.db != nil {
s.db.Close()
}
s.server.cacheShaPassword = &sync.Map{}
}
type testCacheHandler struct {
s *cacheTestSuite
}
func (h *testCacheHandler) UseDB(dbName string) error {
return nil
}
func (h *testCacheHandler) handleQuery(query string, binary bool) (*mysql.Result, error) {
ss := strings.Split(query, " ")
switch strings.ToLower(ss[0]) {
case "select":
var r *mysql.Resultset
var err error
// for handle go mysql driver select @@max_allowed_packet
if strings.Contains(strings.ToLower(query), "max_allowed_packet") {
r, err = mysql.BuildSimpleResultset([]string{"@@max_allowed_packet"}, [][]interface{}{
{mysql.MaxPayloadLen},
}, binary)
} else {
r, err = mysql.BuildSimpleResultset([]string{"a", "b"}, [][]interface{}{
{1, "hello world"},
}, binary)
}
if err != nil {
return nil, errors.Trace(err)
} else {
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 0,
AffectedRows: 0,
Resultset: r,
}, nil
}
case "insert":
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 1,
AffectedRows: 0,
Resultset: nil,
}, nil
case "delete", "update", "replace":
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 0,
AffectedRows: 1,
Resultset: nil,
}, nil
default:
return nil, fmt.Errorf("invalid query %s", query)
}
}
func (h *testCacheHandler) HandleQuery(query string) (*mysql.Result, error) {
return h.handleQuery(query, false)
}
func (h *testCacheHandler) HandleFieldList(table string, fieldWildcard string) ([]*mysql.Field, error) {
return nil, nil
}
func (h *testCacheHandler) HandleStmtPrepare(sql string) (params int, columns int, ctx interface{}, err error) {
return 0, 0, nil, nil
}
func (h *testCacheHandler) HandleStmtClose(context interface{}) error {
return nil
}
func (h *testCacheHandler) HandleStmtExecute(ctx interface{}, query string, args []interface{}) (*mysql.Result, error) {
return h.handleQuery(query, true)
}
func (h *testCacheHandler) HandleOtherCommand(cmd byte, data []byte) error {
return mysql.NewError(mysql.ER_UNKNOWN_ERROR, fmt.Sprintf("command %d is not supported now", cmd))
}