From 407db8a0e3cf85d3e4c352dd2760eff4d1ce19d2 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Fri, 28 Dec 2018 19:06:23 +0800 Subject: [PATCH] test cases --- CHANGES.md | 4 +- cmd/soar/soar_test.go | 2 + common/config.go | 1 + common/config_test.go | 1 + common/testdata/TestParseDSN.golden | 80 +++++++++++++++++---------- database/testdata/TestTrace.golden | 5 +- database/trace_test.go | 15 +++-- env/env_test.go | 2 + env/testdata/TestNewVirtualEnv.golden | 2 + 9 files changed, 74 insertions(+), 38 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 138246c..d4b1ad5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,12 +8,14 @@ - command line dsn args support '@', '/', ':' in password - add new heuristic rule RES.009, "SELECT * FROM tbl WHERE col = col = 'abc'" - add new heuristic rule RuleColumnNotAllowType COL.018 +- add string escape function for security +- fix #122 single table select * don't auto-complete table name +- fix #171 support socket access type - fix #58 sampling not deal with NULL able string - fix #172 compatible with mysql 5.1, which explain has no Index_Comment column - fix #163 column.Tp may be nil, which may raise panic - fix #151 bit type not config as int, when two columns compare will give ARG.003 suggestion. - - ## 2018-11 - add all third-party lib into vendor diff --git a/cmd/soar/soar_test.go b/cmd/soar/soar_test.go index f8aa494..573a6a3 100644 --- a/cmd/soar/soar_test.go +++ b/cmd/soar/soar_test.go @@ -47,6 +47,8 @@ func Test_Main(_ *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) common.Config.OnlineDSN.Disable = true common.Config.LogLevel = 0 + common.Config.Query = "select * syntaxError" + main() common.Config.Query = "select * from film;alter table city add index idx_country_id(country_id);" main() common.Log.Debug("Exiting function: %s", common.GetFunctionName()) diff --git a/common/config.go b/common/config.go index f933d27..9370f10 100644 --- a/common/config.go +++ b/common/config.go @@ -426,6 +426,7 @@ func parseDSN(odbc string, d *Dsn) *Dsn { func ParseDSN(odbc string, d *Dsn) *Dsn { cfg, err := mysql.ParseDSN(odbc) if err != nil { + Log.Warn("go-sql-driver/mysql.ParseDSN Error: %s, DSN: %s, try to use old version parseDSN", err.Error(), odbc) return parseDSN(odbc, d) } return newDSN(cfg) diff --git a/common/config_test.go b/common/config_test.go index 1dead0b..3f2cb49 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -94,6 +94,7 @@ func TestParseDSN(t *testing.T) { "user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped", "user:password@/dbname", "user:password@/", + "user:password@tcp(localhost:3307)/database?charset=utf8&timeout=5s", } err := GoldenDiff(func() { diff --git a/common/testdata/TestParseDSN.golden b/common/testdata/TestParseDSN.golden index fb2701d..dcf8184 100644 --- a/common/testdata/TestParseDSN.golden +++ b/common/testdata/TestParseDSN.golden @@ -17,7 +17,7 @@ WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@hostname:3307/database @@ -39,7 +39,7 @@ user:password@hostname:3307/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@hostname:3307/database?charset=utf8 @@ -61,7 +61,7 @@ user:password@hostname:3307/database?charset=utf8 WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@hostname:3307 @@ -83,7 +83,7 @@ user:password@hostname:3307 WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@hostname:/database @@ -105,7 +105,7 @@ user:password@hostname:/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@:3307/database @@ -127,7 +127,7 @@ user:password@:3307/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user@hostname/database @@ -149,7 +149,7 @@ user@hostname/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:pwd:pwd@pwd/pwd@hostname/database @@ -171,7 +171,7 @@ user:pwd:pwd@pwd/pwd@hostname/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@ @@ -193,7 +193,7 @@ user:password@ WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } hostname:3307/database @@ -215,7 +215,7 @@ hostname:3307/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } @hostname:3307/database @@ -237,7 +237,7 @@ hostname:3307/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } @hostname @@ -259,7 +259,7 @@ hostname:3307/database WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } hostname @@ -281,7 +281,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } @/database @@ -303,7 +303,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } @hostname:3307 @@ -325,7 +325,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } @:3307/database @@ -347,7 +347,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } :3307/database @@ -369,7 +369,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } /database @@ -391,7 +391,7 @@ hostname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user@unix(/path/to/socket)/dbname @@ -413,7 +413,7 @@ user@unix(/path/to/socket)/dbname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local @@ -435,7 +435,7 @@ root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true @@ -457,7 +457,7 @@ user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@/dbname?sql_mode=TRADITIONAL @@ -479,7 +479,7 @@ user:password@/dbname?sql_mode=TRADITIONAL WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci @@ -501,7 +501,7 @@ user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_ WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } id:password@tcp(your-amazonaws-uri.com:3306)/dbname @@ -523,7 +523,7 @@ id:password@tcp(your-amazonaws-uri.com:3306)/dbname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user@cloudsql(project-id:instance-name)/dbname @@ -545,7 +545,7 @@ user@cloudsql(project-id:instance-name)/dbname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user@cloudsql(project-id:regionname:instance-name)/dbname @@ -567,7 +567,7 @@ user@cloudsql(project-id:regionname:instance-name)/dbname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped @@ -589,7 +589,7 @@ user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@/dbname @@ -611,7 +611,7 @@ user:password@/dbname WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, Version: 99999, } user:password@/ @@ -633,6 +633,28 @@ user:password@/ WriteTimeout: 0, AllowNativePasswords: true, AllowOldPasswords: false, - Disable: true, + Disable: false, + Version: 99999, +} +user:password@tcp(localhost:3307)/database?charset=utf8&timeout=5s +&common.Dsn{ + User: "user", + Password: "password", + Net: "tcp", + Addr: "localhost:3307", + Schema: "database", + Charset: "utf8", + Collation: "utf8_general_ci", + Loc: "UTC", + TLS: "", + ServerPubKey: "", + MaxAllowedPacket: 4194304, + Params: {"charset":"utf8"}, + Timeout: 5, + ReadTimeout: 0, + WriteTimeout: 0, + AllowNativePasswords: true, + AllowOldPasswords: false, + Disable: false, Version: 99999, } diff --git a/database/testdata/TestTrace.golden b/database/testdata/TestTrace.golden index b766610..8353131 100644 --- a/database/testdata/TestTrace.golden +++ b/database/testdata/TestTrace.golden @@ -1,3 +1,4 @@ -[]database.TraceRow{ +select 1 []database.TraceRow{ {Query:"explain select 1", Trace:"{\n \"steps\": [\n {\n \"join_preparation\": {\n \"select#\": 1,\n \"steps\": [\n {\n \"expanded_query\": \"/* select#1 */ select 1 AS `1`\"\n }\n ]\n }\n },\n {\n \"join_optimization\": {\n \"select#\": 1,\n \"steps\": [\n ]\n }\n },\n {\n \"join_explain\": {\n \"select#\": 1,\n \"steps\": [\n ]\n }\n }\n ]\n}", MissingBytesBeyondMaxMemSize:0, InsufficientPrivileges:0}, -} +} nil +explain select 1 []database.TraceRow(nil) &errors.errorString{s:"no need trace"} diff --git a/database/trace_test.go b/database/trace_test.go index 887ee4c..9eabece 100644 --- a/database/trace_test.go +++ b/database/trace_test.go @@ -26,13 +26,16 @@ import ( func TestTrace(t *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) - res, err := connTest.Trace("select 1") - if err != nil { - t.Error(err) + sqls := []string{ + "select 1", + "explain select 1", + "show create table film", } - - err = common.GoldenDiff(func() { - pretty.Println(res) + err := common.GoldenDiff(func() { + for _, sql := range sqls { + res, err := connTest.Trace(sql) + pretty.Println(sql, res, err) + } }, t.Name(), update) if err != nil { t.Error(err) diff --git a/env/env_test.go b/env/env_test.go index d245428..e7e5ece 100644 --- a/env/env_test.go +++ b/env/env_test.go @@ -61,6 +61,8 @@ func TestMain(m *testing.M) { func TestNewVirtualEnv(t *testing.T) { common.Log.Debug("Entering function: %s", common.GetFunctionName()) testSQL := []string{ + "use sakila", + "select frm syntaxError", "create table t(id int,c1 varchar(20),PRIMARY KEY (id));", "alter table t add index `idx_c1`(c1);", "select * from city where country_id = 44;", diff --git a/env/testdata/TestNewVirtualEnv.golden b/env/testdata/TestNewVirtualEnv.golden index 63b6946..8e561b3 100644 --- a/env/testdata/TestNewVirtualEnv.golden +++ b/env/testdata/TestNewVirtualEnv.golden @@ -1,3 +1,5 @@ +use sakila OK +select frm syntaxError OK create table t(id int,c1 varchar(20),PRIMARY KEY (id)); OK alter table t add index `idx_c1`(c1); OK select * from city where country_id = 44; OK -- GitLab