Skip to content

Commit

Permalink
tools/check: add logging and path sanitization (#36083)
Browse files Browse the repository at this point in the history
close #36082
  • Loading branch information
Zengxian Ding authored Jul 14, 2022
1 parent b4cd14d commit 704275b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
45 changes: 24 additions & 21 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"encoding/xml"
"fmt"
"io"
"log"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"sort"
Expand Down Expand Up @@ -97,7 +99,7 @@ var workDir string
func cmdList(args ...string) bool {
pkgs, err := listPackages()
if err != nil {
fmt.Println("list package error", err)
log.Println("list package error", err)
return false
}

Expand All @@ -120,12 +122,12 @@ func cmdList(args ...string) bool {

err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
log.Println("build package error", pkg, err)
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existence error", err)
log.Println("check test binary existence error", err)
return false
}
if !exist {
Expand All @@ -135,7 +137,7 @@ func cmdList(args ...string) bool {

res, err := listTestCases(pkg, nil)
if err != nil {
fmt.Println("list test cases for package error", err)
log.Println("list test cases for package error", err)
return false
}

Expand All @@ -157,15 +159,15 @@ func cmdList(args ...string) bool {
func cmdBuild(args ...string) bool {
pkgs, err := listPackages()
if err != nil {
fmt.Println("list package error", err)
log.Println("list package error", err)
return false
}

// build all packages
if len(args) == 0 {
err := buildTestBinaryMulti(pkgs)
if err != nil {
fmt.Println("build package error", pkgs, err)
log.Println("build package error", pkgs, err)
return false
}
return true
Expand All @@ -176,7 +178,7 @@ func cmdBuild(args ...string) bool {
pkg := args[0]
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
log.Println("build package error", pkg, err)
return false
}
}
Expand All @@ -196,14 +198,14 @@ func cmdRun(args ...string) bool {
if len(args) == 0 {
err := buildTestBinaryMulti(pkgs)
if err != nil {
fmt.Println("build package error", pkgs, err)
log.Println("build package error", pkgs, err)
return false
}

for _, pkg := range pkgs {
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existence error", err)
log.Println("check test binary existence error", err)
return false
}
if !exist {
Expand All @@ -213,7 +215,7 @@ func cmdRun(args ...string) bool {

tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
log.Println("list test cases error", err)
return false
}
}
Expand All @@ -224,12 +226,12 @@ func cmdRun(args ...string) bool {
pkg := args[0]
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
log.Println("build package error", pkg, err)
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existence error", err)
log.Println("check test binary existence error", err)
return false
}

Expand All @@ -239,7 +241,7 @@ func cmdRun(args ...string) bool {
}
tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
log.Println("list test cases error", err)
return false
}
}
Expand All @@ -249,12 +251,12 @@ func cmdRun(args ...string) bool {
pkg := args[0]
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
log.Println("build package error", pkg, err)
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existence error", err)
log.Println("check test binary existence error", err)
return false
}
if !exist {
Expand All @@ -264,20 +266,20 @@ func cmdRun(args ...string) bool {

tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
log.Println("list test cases error", err)
return false
}
tasks, err = filterTestCases(tasks, args[1])
if err != nil {
fmt.Println("filter test cases error", err)
log.Println("filter test cases error", err)
return false
}
}

if except != "" {
list, err := parseCaseListFromFile(except)
if err != nil {
fmt.Println("parse --except file error", err)
log.Println("parse --except file error", err)
return false
}
tmp := tasks[:0]
Expand All @@ -292,7 +294,7 @@ func cmdRun(args ...string) bool {
if only != "" {
list, err := parseCaseListFromFile(only)
if err != nil {
fmt.Println("parse --only file error", err)
log.Println("parse --only file error", err)
return false
}
tmp := tasks[:0]
Expand Down Expand Up @@ -349,7 +351,7 @@ func cmdRun(args ...string) bool {
}

func parseCaseListFromFile(fileName string) (map[string]struct{}, error) {
f, err := os.Open(fileName)
f, err := os.Open(filepath.Clean(fileName))
if err != nil {
return nil, withTrace(err)
}
Expand Down Expand Up @@ -624,7 +626,7 @@ func (b blocksByStart) Less(i, j int) bool {
func listTestCases(pkg string, tasks []task) ([]task, error) {
newCases, err := listNewTestCases(pkg)
if err != nil {
fmt.Println("list test case error", pkg, err)
log.Println("list test case error", pkg, err)
return nil, withTrace(err)
}
for _, c := range newCases {
Expand Down Expand Up @@ -818,6 +820,7 @@ func (n *numa) testCommand(pkg string, fn string) *exec.Cmd {

// session.test -test.run TestClusteredPrefixColum
args = append(args, "-test.run", "^"+fn+"$")

return exec.Command(exe, args...)
}

Expand Down
9 changes: 6 additions & 3 deletions tools/check/xprog.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
cwd := os.Args[0]
cwd = cwd[:len(cwd)-len("tools/bin/xprog")]

testBinaryPath := os.Args[1]
testBinaryPath := filepath.Clean(os.Args[1])
dir, _ := filepath.Split(testBinaryPath)

// Extract the package info from /tmp/go-build2662369829/b1382/importcfg.link
Expand All @@ -51,7 +51,7 @@ func main() {
_, file := filepath.Split(pkg)

// The path of the destination file looks like $CWD/util/topsql/topsql.test.bin
newName := filepath.Join(cwd, pkg, file+".test.bin")
newName := filepath.Clean(filepath.Join(cwd, pkg, file+".test.bin"))

if err1 := os.Rename(testBinaryPath, newName); err1 != nil {
// Rename fail, handle error like "invalid cross-device linkcd tools/check"
Expand All @@ -64,7 +64,7 @@ func main() {

func getPackageInfo(dir string) string {
// Read the /tmp/go-build2662369829/b1382/importcfg.link file to get the package information
f, err := os.Open(filepath.Join(dir, "importcfg.link"))
f, err := os.Open(filepath.Join(filepath.Clean(dir), "importcfg.link"))
if err != nil {
os.Exit(-1)
}
Expand All @@ -84,6 +84,9 @@ func getPackageInfo(dir string) string {
}

func MoveFile(sourcePath, destPath string) error {
filepath.Clean(sourcePath)
filepath.Clean(destPath)

inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("Couldn't open source file: %s", err)
Expand Down

0 comments on commit 704275b

Please sign in to comment.