Skip to content

Commit

Permalink
internal/testenv: probe for symlink on wasip1
Browse files Browse the repository at this point in the history
Certain WASI runtimes do not support generic symlinks, and
instead return permission errors when they are attempted.
Perform a runtime probe of symlink support in hasSymlink
on wasip1 to determine whether the runtime supports
generic symlinks.

Also perform the same probe on android.

For #59583

Change-Id: Iae5b704e670650d38ee350a5a98f99dcce8b5b28
Reviewed-on: https://go-review.googlesource.com/c/go/+/490115
Auto-Submit: Johan Brandhorst-Satzkorn <[email protected]>
Reviewed-by: Achille Roussel <[email protected]>
TryBot-Bypass: Johan Brandhorst-Satzkorn <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Run-TryBot: Johan Brandhorst-Satzkorn <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
  • Loading branch information
johanbrandhorst committed May 2, 2023
1 parent 4e8c6af commit 53279a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/internal/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func HasSymlink() bool {
func MustHaveSymlink(t testing.TB) {
ok, reason := hasSymlink()
if !ok {
t.Skipf("skipping test: cannot make symlinks on %s/%s%s", runtime.GOOS, runtime.GOARCH, reason)
t.Skipf("skipping test: cannot make symlinks on %s/%s: %s", runtime.GOOS, runtime.GOARCH, reason)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/internal/testenv/testenv_notunix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package testenv

import (
"errors"
"io/fs"
"os"
)

Expand All @@ -16,5 +17,5 @@ import (
var Sigquit = os.Kill

func syscallIsNotSupported(err error) bool {
return errors.Is(err, errors.ErrUnsupported)
return errors.Is(err, fs.ErrPermission) || errors.Is(err, errors.ErrUnsupported)
}
28 changes: 27 additions & 1 deletion src/internal/testenv/testenv_notwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,39 @@
package testenv

import (
"fmt"
"os"
"path/filepath"
"runtime"
)

func hasSymlink() (ok bool, reason string) {
switch runtime.GOOS {
case "android", "plan9":
case "plan9":
return false, ""
case "android", "wasip1":
// For wasip1, some runtimes forbid absolute symlinks,
// or symlinks that escape the current working directory.
// Perform a simple test to see whether the runtime
// supports symlinks or not. If we get a permission
// error, the runtime does not support symlinks.
dir, err := os.MkdirTemp("", "")
if err != nil {
return false, ""
}
defer func() {
_ = os.RemoveAll(dir)
}()
fpath := filepath.Join(dir, "testfile.txt")
if err := os.WriteFile(fpath, nil, 0644); err != nil {
return false, ""
}
if err := os.Symlink(fpath, filepath.Join(dir, "testlink")); err != nil {
if SyscallIsNotSupported(err) {
return false, fmt.Sprintf("symlinks unsupported: %s", err.Error())
}
return false, ""
}
}

return true, ""
Expand Down

0 comments on commit 53279a6

Please sign in to comment.