os: in TestMkdirStickyUmask, create a non-sticky directory as a control

Fixes #62684.

Change-Id: If7afa811526973671d83e21440cbbc1a7b2120d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/529115
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Bryan C. Mills 2023-09-18 11:48:15 -04:00 committed by Gopher Robot
parent a815078683
commit 15e5e35be5
1 changed files with 20 additions and 2 deletions

View File

@ -257,8 +257,23 @@ func TestMkdirStickyUmask(t *testing.T) {
const umask = 0077
dir := newDir("TestMkdirStickyUmask", t)
defer RemoveAll(dir)
oldUmask := syscall.Umask(umask)
defer syscall.Umask(oldUmask)
// 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 create a non-sticky directory as a “control case” to compare against our
// sticky-bit “experiment”.
control := filepath.Join(dir, "control")
if err := Mkdir(control, 0755); err != nil {
t.Fatal(err)
}
cfi, err := Stat(control)
if err != nil {
t.Fatal(err)
}
p := filepath.Join(dir, "dir1")
if err := Mkdir(p, ModeSticky|0755); err != nil {
t.Fatal(err)
@ -267,8 +282,11 @@ func TestMkdirStickyUmask(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if mode := fi.Mode(); (mode&umask) != 0 || (mode&^ModePerm) != (ModeDir|ModeSticky) {
t.Errorf("unexpected mode %s", mode)
got := fi.Mode()
want := cfi.Mode() | ModeSticky
if got != want {
t.Errorf("Mkdir(_, ModeSticky|0755) created dir with mode %v; want %v", got, want)
}
}