Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro committed Sep 27, 2023
1 parent d03f362 commit 46972e1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ type dbSnap struct {
chunk map[string]*chunk
}

func recoveryMysqlPwd(addr string) string {
colonIndex := strings.Index(addr, ":")
atIndex := strings.LastIndex(addr, "@")
pwd := addr[colonIndex+1 : atIndex]
if parse, err := url.Parse("mysql://root:" + pwd + "@127.0.0.1"); err == nil {
if originPwd, ok := parse.User.Password(); ok {
addr = fmt.Sprintf("%s:%s%s", addr[:colonIndex], originPwd, addr[atIndex:])
}
}
return addr
}

func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
var searchPath string
if driver == "postgres" {
Expand All @@ -218,17 +230,7 @@ func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {

// escaping is not necessary for mysql password https://github.com/go-sql-driver/mysql#password
if driver == "mysql" {
colonIndex := strings.Index(addr, ":")
atIndex := strings.LastIndex(addr, "@")
pwd := addr[colonIndex+1 : atIndex]
parse, err := url.Parse("mysql://root:" + pwd + "@127.0.0.1")
if err != nil {
return nil, fmt.Errorf("parse url %s failed: %s", addr, err)
}
originPwd, ok := parse.User.Password()
if ok {
addr = fmt.Sprintf("%s:%s%s", addr[:colonIndex], originPwd, addr[atIndex:])
}
addr = recoveryMysqlPwd(addr)
}

engine, err := xorm.NewEngine(driver, addr)
Expand Down
47 changes: 47 additions & 0 deletions pkg/meta/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,50 @@ func TestPostgreSQLClientWithSearchPath(t *testing.T) { //skip mutate
t.Fatalf("TestPostgreSQLClientWithSearchPath error: %s", err)
}
}

func TestRecoveryMysqlPwd(t *testing.T) { //skip mutate
testCase := []struct {
addr string
expect string
}{
// no special char
{"root:password@localhost:3306/db1",
"root:password@localhost:3306/db1",
},

// set from env @
{"root:pass%40word@localhost:3306/db1",
"root:pass@word@localhost:3306/db1",
},

// direct pass special char @
{"root:pass@word@localhost:3306/db1",
"root:pass@word@localhost:3306/db1",
},

// set from env |
{"root:pass%7Cword@localhost:3306/db1",
"root:pass|word@localhost:3306/db1",
},

// direct pass special char |
{"root:pass|word@localhost:3306/db1",
"root:pass|word@localhost:3306/db1",
},

// set from env :
{"root:pass%3Aword@localhost:3306/db1",
"root:pass:word@localhost:3306/db1",
},

// direct pass special char :
{"root:pass:word@localhost:3306/db1",
"root:pass:word@localhost:3306/db1",
},
}
for _, tc := range testCase {
if got := recoveryMysqlPwd(tc.addr); got != tc.expect {
t.Fatalf("recoveryMysqlPwd error: expect %s but got %s", tc.expect, got)
}
}
}

0 comments on commit 46972e1

Please sign in to comment.