Skip to content

Commit

Permalink
cmd/go: in TestGoBuildUmask, create a file using os.WriteFile as a co…
Browse files Browse the repository at this point in the history
…ntrol

Fixes #62724.
Updates #17909.

Change-Id: Ib2e9abec4fb88f418c4251ece7fcdef315190835
Reviewed-on: https://go-review.googlesource.com/c/go/+/529495
Auto-Submit: Bryan Mills <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Sep 19, 2023
1 parent 3fb86fb commit 203c69a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/cmd/go/go_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,39 @@ func TestGoBuildUmask(t *testing.T) {
// Do not use tg.parallel; avoid other tests seeing umask manipulation.
mask := syscall.Umask(0077) // prohibit low bits
defer syscall.Umask(mask)

tg := testgo(t)
defer tg.cleanup()
tg.tempFile("x.go", `package main; func main() {}`)
// Make sure artifact will be output to /tmp/... in case the user
// has POSIX acl's on their go source tree.
// See issue 17909.

// We have set a umask, but if the parent directory happens to have a default
// ACL, the umask may be ignored. To prevent spurious failures from an ACL,
// we compare the file created by "go build" against a file written explicitly
// by os.WriteFile.
//
// (See https://go.dev/issue/62724, https://go.dev/issue/17909.)
control := tg.path("control")
tg.creatingTemp(control)
if err := os.WriteFile(control, []byte("#!/bin/sh\nexit 0"), 0777); err != nil {
t.Fatal(err)
}
cfi, err := os.Stat(control)
if err != nil {
t.Fatal(err)
}

exe := tg.path("x")
tg.creatingTemp(exe)
tg.run("build", "-o", exe, tg.path("x.go"))
fi, err := os.Stat(exe)
if err != nil {
t.Fatal(err)
}
if mode := fi.Mode(); mode&0077 != 0 {
t.Fatalf("wrote x with mode=%v, wanted no 0077 bits", mode)
got, want := fi.Mode(), cfi.Mode()
if got == want {
t.Logf("wrote x with mode %v", got)
} else {
t.Fatalf("wrote x with mode %v, wanted no 0077 bits (%v)", got, want)
}
}

Expand Down

0 comments on commit 203c69a

Please sign in to comment.