atomic_unix_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build !windows
  7. // +build !windows
  8. // (No syscall.Umask or the equivalent on Windows)
  9. package osutil
  10. import (
  11. "io/ioutil"
  12. "os"
  13. "syscall"
  14. "testing"
  15. )
  16. func TestTempFilePermissions(t *testing.T) {
  17. // Set a zero umask, so any files created will have the permission bits
  18. // asked for in the create call and nothing less.
  19. oldMask := syscall.Umask(0)
  20. defer syscall.Umask(oldMask)
  21. fd, err := ioutil.TempFile("", "test")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. info, err := fd.Stat()
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer os.Remove(fd.Name())
  30. defer fd.Close()
  31. // The temp file should have 0600 permissions at the most, or we have a
  32. // security problem in CreateAtomic.
  33. t.Logf("Got 0%03o", info.Mode())
  34. if info.Mode()&^0600 != 0 {
  35. t.Errorf("Permission 0%03o is too generous", info.Mode())
  36. }
  37. }