]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.16] testing: drop unusual characters from TempDir directory name
authorTobias Klauser <tklauser@distanz.ch>
Tue, 8 Jun 2021 13:33:54 +0000 (15:33 +0200)
committerDmitri Shuralyov <dmitshur@golang.org>
Wed, 26 Jan 2022 18:34:58 +0000 (18:34 +0000)
Only use safe characters of the test name for the os.MkdirTemp pattern.
This currently includes the alphanumeric characters and ASCII
punctuation characters known not to interact with globs.

For #46624
Fixes #50645

Change-Id: I402c34775b943fed9b97963c52f79245cc16dc1d
Reviewed-on: https://go-review.googlesource.com/c/go/+/326010
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
(cherry picked from commit 97cee43c93cfccded197cd281f0a5885cdb605b4)
Reviewed-on: https://go-review.googlesource.com/c/go/+/378914
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
src/testing/testing.go
src/testing/testing_test.go

index dec39d24da27b7c7d4987b8393da78c58fe57c45..a53d5745cb2b77dabf5f22a0e66a588053f5b679 100644 (file)
@@ -251,6 +251,8 @@ import (
        "sync"
        "sync/atomic"
        "time"
+       "unicode"
+       "unicode/utf8"
 )
 
 var initRan bool
@@ -906,11 +908,6 @@ func (c *common) Cleanup(f func()) {
        c.cleanups = append(c.cleanups, fn)
 }
 
-var tempDirReplacer struct {
-       sync.Once
-       r *strings.Replacer
-}
-
 // TempDir returns a temporary directory for the test to use.
 // The directory is automatically removed by Cleanup when the test and
 // all its subtests complete.
@@ -934,13 +931,26 @@ func (c *common) TempDir() string {
        if nonExistent {
                c.Helper()
 
-               // os.MkdirTemp doesn't like path separators in its pattern,
-               // so mangle the name to accommodate subtests.
-               tempDirReplacer.Do(func() {
-                       tempDirReplacer.r = strings.NewReplacer("/", "_", "\\", "_", ":", "_")
-               })
-               pattern := tempDirReplacer.r.Replace(c.Name())
-
+               // Drop unusual characters (such as path separators or
+               // characters interacting with globs) from the directory name to
+               // avoid surprising os.MkdirTemp behavior.
+               mapper := func(r rune) rune {
+                       if r < utf8.RuneSelf {
+                               const allowed = "!#$%&()+,-.=@^_{}~ "
+                               if '0' <= r && r <= '9' ||
+                                       'a' <= r && r <= 'z' ||
+                                       'A' <= r && r <= 'Z' {
+                                       return r
+                               }
+                               if strings.ContainsRune(allowed, r) {
+                                       return r
+                               }
+                       } else if unicode.IsLetter(r) || unicode.IsNumber(r) {
+                               return r
+                       }
+                       return -1
+               }
+               pattern := strings.Map(mapper, c.Name())
                c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
                if c.tempDirErr == nil {
                        c.Cleanup(func() {
index 0f096980ca4561798f227f567161cdc19d97d23a..42058ae449c30ff0fa816f383e2781096ec0f861 100644 (file)
@@ -58,6 +58,9 @@ func TestTempDir(t *testing.T) {
        t.Run("test:subtest", testTempDir)
        t.Run("test/..", testTempDir)
        t.Run("../test", testTempDir)
+       t.Run("test[]", testTempDir)
+       t.Run("test*", testTempDir)
+       t.Run("äöüéè", testTempDir)
 }
 
 func testTempDir(t *testing.T) {
@@ -74,7 +77,7 @@ func testTempDir(t *testing.T) {
                        if err != nil {
                                t.Fatal(err)
                        }
-                       t.Errorf("directory %q stil exists: %v, isDir=%v", dir, fi, fi.IsDir())
+                       t.Errorf("directory %q still exists: %v, isDir=%v", dir, fi, fi.IsDir())
                default:
                        if !t.Failed() {
                                t.Fatal("never received dir channel")
@@ -108,4 +111,9 @@ func testTempDir(t *testing.T) {
        if len(files) > 0 {
                t.Errorf("unexpected %d files in TempDir: %v", len(files), files)
        }
+
+       glob := filepath.Join(dir, "*.txt")
+       if _, err := filepath.Glob(glob); err != nil {
+               t.Error(err)
+       }
 }