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. //+build !windows
  7. // (No syscall.Umask or the equivalent on Windows)
  8. package osutil
  9. import (
  10. "io/ioutil"
  11. "os"
  12. "syscall"
  13. "testing"
  14. )
  15. func TestTempFilePermissions(t *testing.T) {
  16. // Set a zero umask, so any files created will have the permission bits
  17. // asked for in the create call and nothing less.
  18. oldMask := syscall.Umask(0)
  19. defer syscall.Umask(oldMask)
  20. fd, err := ioutil.TempFile("", "test")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. info, err := fd.Stat()
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. defer os.Remove(fd.Name())
  29. defer fd.Close()
  30. // The temp file should have 0600 permissions at the most, or we have a
  31. // security problem in CreateAtomic.
  32. t.Logf("Got 0%03o", info.Mode())
  33. if info.Mode()&^0600 != 0 {
  34. t.Errorf("Permission 0%03o is too generous", info.Mode())
  35. }
  36. }