Skip to content

Commit 27a1049

Browse files
author
James Cor
committed
merge with main
2 parents 129227e + 5425a89 commit 27a1049

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+3247
-1322
lines changed

enginetest/engine_only_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ func TestTriggerViewWarning(t *testing.T) {
659659
assert.NoError(t, err)
660660

661661
ctx := harness.NewContext()
662+
ctx.SetCurrentDatabase("mydb")
662663
enginetest.CreateNewConnectionForServerEngine(ctx, e)
663664

664665
enginetest.TestQueryWithContext(t, ctx, e, harness, "insert into mytable values (4, 'fourth row')", []sql.UntypedSqlRow{{types.NewOkResult(1)}}, nil, nil, nil)
@@ -1003,6 +1004,7 @@ func TestAlterTableWithBadSchema(t *testing.T) {
10031004
for _, tt := range tests {
10041005
t.Run(tt.name, func(t *testing.T) {
10051006
ctx := harness.NewContext()
1007+
ctx.SetCurrentDatabase("mydb")
10061008
_, iter, _, err := engine.Query(ctx, tt.q)
10071009
// errors should be analyze time, not execution time
10081010
if tt.err {

enginetest/enginetests.go

Lines changed: 150 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package enginetest
1616

1717
import (
1818
"context"
19+
"crypto/tls"
1920
dsql "database/sql"
2021
"fmt"
2122
"io"
@@ -313,6 +314,51 @@ func TestInfoSchema(t *testing.T, h Harness) {
313314
}, nil, nil, nil)
314315
})
315316

317+
t.Run("information_schema.processlist projection case", func(t *testing.T) {
318+
e := mustNewEngine(t, h)
319+
defer e.Close()
320+
321+
if IsServerEngine(e) {
322+
t.Skip("skipping for server engine as the processlist returned from server differs")
323+
}
324+
p := sqle.NewProcessList()
325+
p.AddConnection(1, "localhost")
326+
327+
ctx := NewContext(h)
328+
ctx.Session.SetClient(sql.Client{Address: "localhost", User: "root"})
329+
ctx.Session.SetConnectionId(1)
330+
ctx.ProcessList = p
331+
ctx.SetCurrentDatabase("")
332+
333+
p.ConnectionReady(ctx.Session)
334+
335+
ctx, err := p.BeginQuery(ctx, "SELECT foo")
336+
require.NoError(t, err)
337+
338+
p.AddConnection(2, "otherhost")
339+
sess2 := sql.NewBaseSessionWithClientServer("localhost", sql.Client{Address: "otherhost", User: "root"}, 2)
340+
sess2.SetCurrentDatabase("otherdb")
341+
p.ConnectionReady(sess2)
342+
ctx2 := sql.NewContext(context.Background(), sql.WithPid(2), sql.WithSession(sess2))
343+
ctx2, err = p.BeginQuery(ctx2, "SELECT bar")
344+
require.NoError(t, err)
345+
p.EndQuery(ctx2)
346+
347+
TestQueryWithContext(t, ctx, e, h,
348+
"SELECT id, uSeR, hOST FROM information_schema.processlist ORDER BY id",
349+
[]sql.Row{
350+
{uint64(1), "root", "localhost"},
351+
{uint64(2), "root", "otherhost"},
352+
},
353+
sql.Schema{
354+
{Name: "id", Type: types.Uint64},
355+
{Name: "uSeR", Type: types.MustCreateString(sqltypes.VarChar, 96, sql.Collation_Information_Schema_Default)},
356+
{Name: "hOST", Type: types.MustCreateString(sqltypes.VarChar, 783, sql.Collation_Information_Schema_Default)},
357+
},
358+
nil, nil,
359+
)
360+
})
361+
316362
for _, tt := range queries.SkippedInfoSchemaQueries {
317363
t.Run(tt.Query, func(t *testing.T) {
318364
t.Skip()
@@ -972,44 +1018,50 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
9721018
})
9731019

9741020
tests := []struct {
975-
file string
976-
query string
977-
exp string
978-
err *errors.Kind
979-
skip bool
1021+
file string
1022+
query string
1023+
exp string
1024+
expRows []sql.Row
1025+
err *errors.Kind
1026+
skip bool
9801027
}{
9811028
{
982-
file: "outfile.txt",
983-
query: "select * from mytable into outfile 'outfile.txt';",
1029+
file: "outfile.txt",
1030+
query: "select * from mytable into outfile 'outfile.txt';",
1031+
expRows: []sql.Row{{types.NewOkResult(3)}},
9841032
exp: "" +
9851033
"1\tfirst row\n" +
9861034
"2\tsecond row\n" +
9871035
"3\tthird row\n",
9881036
},
9891037
{
990-
file: "dumpfile.txt",
991-
query: "select * from mytable limit 1 into dumpfile 'dumpfile.txt';",
992-
exp: "1first row",
1038+
file: "dumpfile.txt",
1039+
query: "select * from mytable limit 1 into dumpfile 'dumpfile.txt';",
1040+
expRows: []sql.Row{{types.NewOkResult(1)}},
1041+
exp: "1first row",
9931042
},
9941043
{
995-
file: "outfile.txt",
996-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',';",
1044+
file: "outfile.txt",
1045+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',';",
1046+
expRows: []sql.Row{{types.NewOkResult(3)}},
9971047
exp: "" +
9981048
"1,first row\n" +
9991049
"2,second row\n" +
10001050
"3,third row\n",
10011051
},
10021052
{
1003-
file: "outfile.txt",
1004-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by '$$';",
1053+
file: "outfile.txt",
1054+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by '$$';",
1055+
expRows: []sql.Row{{types.NewOkResult(3)}},
10051056
exp: "" +
10061057
"1$$first row\n" +
10071058
"2$$second row\n" +
10081059
"3$$third row\n",
10091060
},
10101061
{
1011-
file: "outfile.txt",
1012-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' optionally enclosed by '\"';",
1062+
file: "outfile.txt",
1063+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' optionally enclosed by '\"';",
1064+
expRows: []sql.Row{{types.NewOkResult(3)}},
10131065
exp: "" +
10141066
"1,\"first row\"\n" +
10151067
"2,\"second row\"\n" +
@@ -1026,56 +1078,63 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
10261078
err: sql.ErrUnexpectedSeparator,
10271079
},
10281080
{
1029-
file: "outfile.txt",
1030-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' enclosed by '\"';",
1081+
file: "outfile.txt",
1082+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' enclosed by '\"';",
1083+
expRows: []sql.Row{{types.NewOkResult(3)}},
10311084
exp: "" +
10321085
"\"1\",\"first row\"\n" +
10331086
"\"2\",\"second row\"\n" +
10341087
"\"3\",\"third row\"\n",
10351088
},
10361089
{
1037-
file: "outfile.txt",
1038-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by ';';",
1090+
file: "outfile.txt",
1091+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by ';';",
1092+
expRows: []sql.Row{{types.NewOkResult(3)}},
10391093
exp: "" +
10401094
"1,first row;" +
10411095
"2,second row;" +
10421096
"3,third row;",
10431097
},
10441098
{
1045-
file: "outfile.txt",
1046-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by 'r';",
1099+
file: "outfile.txt",
1100+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by 'r';",
1101+
expRows: []sql.Row{{types.NewOkResult(3)}},
10471102
exp: "" +
10481103
"1,fi\\rst \\rowr" +
10491104
"2,second \\rowr" +
10501105
"3,thi\\rd \\rowr",
10511106
},
10521107
{
1053-
file: "outfile.txt",
1054-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines starting by 'r';",
1108+
file: "outfile.txt",
1109+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines starting by 'r';",
1110+
expRows: []sql.Row{{types.NewOkResult(3)}},
10551111
exp: "" +
10561112
"r1,first row\n" +
10571113
"r2,second row\n" +
10581114
"r3,third row\n",
10591115
},
10601116
{
1061-
file: "outfile.txt",
1062-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by '';",
1117+
file: "outfile.txt",
1118+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by '';",
1119+
expRows: []sql.Row{{types.NewOkResult(3)}},
10631120
exp: "" +
10641121
"1\tfirst row\n" +
10651122
"2\tsecond row\n" +
10661123
"3\tthird row\n",
10671124
},
10681125
{
1069-
file: "outfile.txt",
1070-
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by '';",
1126+
file: "outfile.txt",
1127+
query: "select * from mytable into outfile 'outfile.txt' fields terminated by ',' lines terminated by '';",
1128+
expRows: []sql.Row{{types.NewOkResult(3)}},
10711129
exp: "" +
10721130
"1,first row" +
10731131
"2,second row" +
10741132
"3,third row",
10751133
},
10761134
{
1077-
file: "outfile.txt",
1078-
query: "select * from niltable into outfile 'outfile.txt';",
1135+
file: "outfile.txt",
1136+
query: "select * from niltable into outfile 'outfile.txt';",
1137+
expRows: []sql.Row{{types.NewOkResult(6)}},
10791138
exp: "1\t\\N\t\\N\t\\N\n" +
10801139
"2\t2\t1\t\\N\n" +
10811140
"3\t\\N\t0\t\\N\n" +
@@ -1084,8 +1143,9 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
10841143
"6\t6\t0\t6\n",
10851144
},
10861145
{
1087-
file: "outfile.txt",
1088-
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' enclosed by '\"';",
1146+
file: "outfile.txt",
1147+
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' enclosed by '\"';",
1148+
expRows: []sql.Row{{types.NewOkResult(6)}},
10891149
exp: "\"1\",\\N,\\N,\\N\n" +
10901150
"\"2\",\"2\",\"1\",\\N\n" +
10911151
"\"3\",\\N,\"0\",\\N\n" +
@@ -1094,8 +1154,9 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
10941154
"\"6\",\"6\",\"0\",\"6\"\n",
10951155
},
10961156
{
1097-
file: "outfile.txt",
1098-
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' escaped by '$';",
1157+
file: "outfile.txt",
1158+
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' escaped by '$';",
1159+
expRows: []sql.Row{{types.NewOkResult(6)}},
10991160
exp: "1,$N,$N,$N\n" +
11001161
"2,2,1,$N\n" +
11011162
"3,$N,0,$N\n" +
@@ -1104,8 +1165,9 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
11041165
"6,6,0,6\n",
11051166
},
11061167
{
1107-
file: "outfile.txt",
1108-
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' escaped by '';",
1168+
file: "outfile.txt",
1169+
query: "select * from niltable into outfile 'outfile.txt' fields terminated by ',' escaped by '';",
1170+
expRows: []sql.Row{{types.NewOkResult(6)}},
11091171
exp: "1,NULL,NULL,NULL\n" +
11101172
"2,2,1,NULL\n" +
11111173
"3,NULL,0,NULL\n" +
@@ -1114,8 +1176,9 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
11141176
"6,6,0,6\n",
11151177
},
11161178
{
1117-
file: "./subdir/outfile.txt",
1118-
query: "select * from mytable into outfile './subdir/outfile.txt';",
1179+
file: "./subdir/outfile.txt",
1180+
query: "select * from mytable into outfile './subdir/outfile.txt';",
1181+
expRows: []sql.Row{{types.NewOkResult(3)}},
11191182
exp: "" +
11201183
"1\tfirst row\n" +
11211184
"2\tsecond row\n" +
@@ -1153,7 +1216,11 @@ func TestSelectIntoFile(t *testing.T, harness Harness) {
11531216
}
11541217
// in case there are any residual files from previous runs
11551218
os.Remove(tt.file)
1156-
TestQueryWithContext(t, ctx, e, harness, tt.query, nil, nil, nil, nil)
1219+
var expected = tt.expRows
1220+
if IsServerEngine(e) {
1221+
expected = nil
1222+
}
1223+
TestQueryWithContext(t, ctx, e, harness, tt.query, expected, types.OkResultSchema, nil, nil)
11571224
res, err := os.ReadFile(tt.file)
11581225
require.NoError(t, err)
11591226
require.Equal(t, tt.exp, string(res))
@@ -1944,11 +2011,19 @@ func TestUserAuthentication(t *testing.T, h Harness) {
19442011
User: "root",
19452012
Address: "localhost",
19462013
})
2014+
2015+
tlsCert, err := tls.LoadX509KeyPair("./testdata/selfsigned_cert.pem", "./testdata/selfsigned_key.pem")
2016+
require.NoError(t, err)
2017+
tlsConfig := tls.Config{
2018+
Certificates: []tls.Certificate{tlsCert},
2019+
}
2020+
19472021
serverConfig := server.Config{
1948-
Protocol: "tcp",
1949-
Address: fmt.Sprintf("localhost:%d", port),
1950-
MaxConnections: 1000,
1951-
AllowClearTextWithoutTLS: true,
2022+
Protocol: "tcp",
2023+
Address: fmt.Sprintf("localhost:%d", port),
2024+
MaxConnections: 1000,
2025+
TLSConfig: &tlsConfig,
2026+
RequireSecureTransport: true,
19522027
}
19532028

19542029
e := mustNewEngine(t, clientHarness)
@@ -1989,24 +2064,37 @@ func TestUserAuthentication(t *testing.T, h Harness) {
19892064
}()
19902065

19912066
for _, assertion := range script.Assertions {
1992-
conn, err := dbr.Open("mysql", fmt.Sprintf("%s:%s@tcp(localhost:%d)/?allowCleartextPasswords=true",
1993-
assertion.Username, assertion.Password, port), nil)
1994-
require.NoError(t, err)
1995-
r, err := conn.Query(assertion.Query)
1996-
if assertion.ExpectedErr || len(assertion.ExpectedErrStr) > 0 || assertion.ExpectedErrKind != nil {
1997-
if !assert.Error(t, err) {
1998-
require.NoError(t, r.Close())
1999-
} else if len(assertion.ExpectedErrStr) > 0 {
2000-
assert.Equal(t, assertion.ExpectedErrStr, err.Error())
2001-
} else if assertion.ExpectedErrKind != nil {
2002-
assert.True(t, assertion.ExpectedErrKind.Is(err))
2003-
}
2004-
} else {
2005-
if assert.NoError(t, err) {
2006-
require.NoError(t, r.Close())
2067+
t.Run(assertion.Query, func(t *testing.T) {
2068+
conn, err := dbr.Open("mysql", fmt.Sprintf("%s:%s@tcp(localhost:%d)/?allowCleartextPasswords=true&tls=skip-verify",
2069+
assertion.Username, assertion.Password, port), nil)
2070+
require.NoError(t, err)
2071+
r, err := conn.Query(assertion.Query)
2072+
if assertion.ExpectedErr || len(assertion.ExpectedErrStr) > 0 || assertion.ExpectedErrKind != nil {
2073+
if !assert.Error(t, err) {
2074+
require.NoError(t, r.Close())
2075+
} else if len(assertion.ExpectedErrStr) > 0 {
2076+
assert.Equal(t, assertion.ExpectedErrStr, err.Error())
2077+
} else if assertion.ExpectedErrKind != nil {
2078+
assert.True(t, assertion.ExpectedErrKind.Is(err))
2079+
}
2080+
} else {
2081+
if assert.NoError(t, err) {
2082+
require.NoError(t, r.Close())
2083+
}
2084+
if assertion.ExpectedAuthPlugin != "" {
2085+
// NOTE: This query works as long as there is only one account configured for the current user
2086+
r, err := conn.Query("SELECT plugin FROM mysql.user WHERE user=SUBSTRING_INDEX(USER(),'@',1);")
2087+
require.NoError(t, err)
2088+
require.True(t, r.Next())
2089+
var authPlugin string
2090+
err = r.Scan(&authPlugin)
2091+
require.False(t, r.Next())
2092+
require.NoError(t, err)
2093+
require.Equal(t, assertion.ExpectedAuthPlugin, authPlugin)
2094+
}
20072095
}
2008-
}
2009-
require.NoError(t, conn.Close())
2096+
require.NoError(t, conn.Close())
2097+
})
20102098
}
20112099
})
20122100
}
@@ -5655,6 +5743,7 @@ func TestTypesOverWire(t *testing.T, harness ClientHarness, sessionBuilder serve
56555743
require.NoError(t, err)
56565744
expectedRowSet := script.Results[queryIdx]
56575745
expectedRowIdx := 0
5746+
buf := sql.NewByteBuffer(1000)
56585747
var engineRow sql.Row
56595748
for engineRow, err = engineIter.Next(ctx); err == nil; engineRow, err = engineIter.Next(ctx) {
56605749
if !assert.True(t, r.Next()) {
@@ -5672,7 +5761,7 @@ func TestTypesOverWire(t *testing.T, harness ClientHarness, sessionBuilder serve
56725761
break
56735762
}
56745763
expectedEngineRow := make([]*string, engineRow.Len())
5675-
row, err := server.RowToSQL(ctx, sch, engineRow, nil)
5764+
row, err := server.RowToSQL(ctx, sch, engineRow, nil, buf)
56765765
if !assert.NoError(t, err) {
56775766
break
56785767
}

0 commit comments

Comments
 (0)