osutil_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package osutil_test
  16. import (
  17. "os"
  18. "testing"
  19. "github.com/syncthing/syncthing/internal/osutil"
  20. )
  21. func TestInWriteableDir(t *testing.T) {
  22. err := os.RemoveAll("testdata")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. defer os.RemoveAll("testdata")
  27. os.Mkdir("testdata", 0700)
  28. os.Mkdir("testdata/rw", 0700)
  29. os.Mkdir("testdata/ro", 0500)
  30. create := func(name string) error {
  31. fd, err := os.Create(name)
  32. if err != nil {
  33. return err
  34. }
  35. fd.Close()
  36. return nil
  37. }
  38. // These should succeed
  39. err = osutil.InWritableDir(create, "testdata/file")
  40. if err != nil {
  41. t.Error("testdata/file:", err)
  42. }
  43. err = osutil.InWritableDir(create, "testdata/rw/foo")
  44. if err != nil {
  45. t.Error("testdata/rw/foo:", err)
  46. }
  47. err = osutil.InWritableDir(os.Remove, "testdata/rw/foo")
  48. if err != nil {
  49. t.Error("testdata/rw/foo:", err)
  50. }
  51. err = osutil.InWritableDir(create, "testdata/ro/foo")
  52. if err != nil {
  53. t.Error("testdata/ro/foo:", err)
  54. }
  55. err = osutil.InWritableDir(os.Remove, "testdata/ro/foo")
  56. if err != nil {
  57. t.Error("testdata/ro/foo:", err)
  58. }
  59. // These should not
  60. err = osutil.InWritableDir(create, "testdata/nonexistent/foo")
  61. if err == nil {
  62. t.Error("testdata/nonexistent/foo returned nil error")
  63. }
  64. err = osutil.InWritableDir(create, "testdata/file/foo")
  65. if err == nil {
  66. t.Error("testdata/file/foo returned nil error")
  67. }
  68. }