osutil_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. package osutil_test
  7. import (
  8. "os"
  9. "testing"
  10. "github.com/syncthing/syncthing/internal/osutil"
  11. )
  12. func TestInWriteableDir(t *testing.T) {
  13. err := os.RemoveAll("testdata")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer os.RemoveAll("testdata")
  18. os.Mkdir("testdata", 0700)
  19. os.Mkdir("testdata/rw", 0700)
  20. os.Mkdir("testdata/ro", 0500)
  21. create := func(name string) error {
  22. fd, err := os.Create(name)
  23. if err != nil {
  24. return err
  25. }
  26. fd.Close()
  27. return nil
  28. }
  29. // These should succeed
  30. err = osutil.InWritableDir(create, "testdata/file")
  31. if err != nil {
  32. t.Error("testdata/file:", err)
  33. }
  34. err = osutil.InWritableDir(create, "testdata/rw/foo")
  35. if err != nil {
  36. t.Error("testdata/rw/foo:", err)
  37. }
  38. err = osutil.InWritableDir(os.Remove, "testdata/rw/foo")
  39. if err != nil {
  40. t.Error("testdata/rw/foo:", err)
  41. }
  42. err = osutil.InWritableDir(create, "testdata/ro/foo")
  43. if err != nil {
  44. t.Error("testdata/ro/foo:", err)
  45. }
  46. err = osutil.InWritableDir(os.Remove, "testdata/ro/foo")
  47. if err != nil {
  48. t.Error("testdata/ro/foo:", err)
  49. }
  50. // These should not
  51. err = osutil.InWritableDir(create, "testdata/nonexistent/foo")
  52. if err == nil {
  53. t.Error("testdata/nonexistent/foo returned nil error")
  54. }
  55. err = osutil.InWritableDir(create, "testdata/file/foo")
  56. if err == nil {
  57. t.Error("testdata/file/foo returned nil error")
  58. }
  59. }