osutil_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "testing"
  14. "github.com/syncthing/syncthing/lib/osutil"
  15. )
  16. func TestInWriteableDir(t *testing.T) {
  17. err := os.RemoveAll("testdata")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. defer os.RemoveAll("testdata")
  22. os.Mkdir("testdata", 0700)
  23. os.Mkdir("testdata/rw", 0700)
  24. os.Mkdir("testdata/ro", 0500)
  25. create := func(name string) error {
  26. fd, err := os.Create(name)
  27. if err != nil {
  28. return err
  29. }
  30. fd.Close()
  31. return nil
  32. }
  33. // These should succeed
  34. err = osutil.InWritableDir(create, "testdata/file")
  35. if err != nil {
  36. t.Error("testdata/file:", err)
  37. }
  38. err = osutil.InWritableDir(create, "testdata/rw/foo")
  39. if err != nil {
  40. t.Error("testdata/rw/foo:", err)
  41. }
  42. err = osutil.InWritableDir(os.Remove, "testdata/rw/foo")
  43. if err != nil {
  44. t.Error("testdata/rw/foo:", err)
  45. }
  46. err = osutil.InWritableDir(create, "testdata/ro/foo")
  47. if err != nil {
  48. t.Error("testdata/ro/foo:", err)
  49. }
  50. err = osutil.InWritableDir(os.Remove, "testdata/ro/foo")
  51. if err != nil {
  52. t.Error("testdata/ro/foo:", err)
  53. }
  54. // These should not
  55. err = osutil.InWritableDir(create, "testdata/nonexistent/foo")
  56. if err == nil {
  57. t.Error("testdata/nonexistent/foo returned nil error")
  58. }
  59. err = osutil.InWritableDir(create, "testdata/file/foo")
  60. if err == nil {
  61. t.Error("testdata/file/foo returned nil error")
  62. }
  63. }
  64. func TestInWritableDirWindowsRemove(t *testing.T) {
  65. if runtime.GOOS != "windows" {
  66. t.Skipf("Tests not required")
  67. return
  68. }
  69. err := os.RemoveAll("testdata")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer os.RemoveAll("testdata")
  74. create := func(name string) error {
  75. fd, err := os.Create(name)
  76. if err != nil {
  77. return err
  78. }
  79. fd.Close()
  80. return nil
  81. }
  82. os.Mkdir("testdata", 0700)
  83. os.Mkdir("testdata/windows", 0500)
  84. os.Mkdir("testdata/windows/ro", 0500)
  85. create("testdata/windows/ro/readonly")
  86. os.Chmod("testdata/windows/ro/readonly", 0500)
  87. for _, path := range []string{"testdata/windows/ro/readonly", "testdata/windows/ro", "testdata/windows"} {
  88. err := os.Remove(path)
  89. if err == nil {
  90. t.Errorf("Expected error %s", path)
  91. }
  92. }
  93. for _, path := range []string{"testdata/windows/ro/readonly", "testdata/windows/ro", "testdata/windows"} {
  94. err := osutil.InWritableDir(osutil.Remove, path)
  95. if err != nil {
  96. t.Errorf("Unexpected error %s: %s", path, err)
  97. }
  98. }
  99. }
  100. func TestInWritableDirWindowsRename(t *testing.T) {
  101. if runtime.GOOS != "windows" {
  102. t.Skipf("Tests not required")
  103. return
  104. }
  105. err := os.RemoveAll("testdata")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. defer os.RemoveAll("testdata")
  110. create := func(name string) error {
  111. fd, err := os.Create(name)
  112. if err != nil {
  113. return err
  114. }
  115. fd.Close()
  116. return nil
  117. }
  118. os.Mkdir("testdata", 0700)
  119. os.Mkdir("testdata/windows", 0500)
  120. os.Mkdir("testdata/windows/ro", 0500)
  121. create("testdata/windows/ro/readonly")
  122. os.Chmod("testdata/windows/ro/readonly", 0500)
  123. for _, path := range []string{"testdata/windows/ro/readonly", "testdata/windows/ro", "testdata/windows"} {
  124. err := os.Rename(path, path+"new")
  125. if err == nil {
  126. t.Skipf("seem like this test doesn't work here")
  127. return
  128. }
  129. }
  130. rename := func(path string) error {
  131. return osutil.Rename(path, path+"new")
  132. }
  133. for _, path := range []string{"testdata/windows/ro/readonly", "testdata/windows/ro", "testdata/windows"} {
  134. err := osutil.InWritableDir(rename, path)
  135. if err != nil {
  136. t.Errorf("Unexpected error %s: %s", path, err)
  137. }
  138. _, err = os.Stat(path + "new")
  139. if err != nil {
  140. t.Errorf("Unexpected error %s: %s", path, err)
  141. }
  142. }
  143. }
  144. func TestDiskUsage(t *testing.T) {
  145. free, err := osutil.DiskFreePercentage(".")
  146. if err != nil {
  147. if runtime.GOOS == "netbsd" ||
  148. runtime.GOOS == "openbsd" ||
  149. runtime.GOOS == "solaris" {
  150. t.Skip()
  151. }
  152. t.Errorf("Unexpected error: %s", err)
  153. }
  154. if free < 1 {
  155. t.Error("Disk is full?", free)
  156. }
  157. }
  158. func TestCaseSensitiveStat(t *testing.T) {
  159. switch runtime.GOOS {
  160. case "windows", "darwin":
  161. break // We can test!
  162. default:
  163. t.Skip("Cannot test on this platform")
  164. return
  165. }
  166. dir, err := ioutil.TempDir("", "TestCaseSensitiveStat")
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. defer os.RemoveAll(dir)
  171. if err := ioutil.WriteFile(filepath.Join(dir, "File"), []byte("data"), 0644); err != nil {
  172. t.Fatal(err)
  173. }
  174. if _, err := os.Lstat(filepath.Join(dir, "File")); err != nil {
  175. // Standard Lstat should report the file exists
  176. t.Fatal("Unexpected error:", err)
  177. }
  178. if _, err := os.Lstat(filepath.Join(dir, "fILE")); err != nil {
  179. // ... also with the incorrect case spelling
  180. t.Fatal("Unexpected error:", err)
  181. }
  182. // Create the case sensitive stat:er. We stress it a little by giving it a
  183. // base path with an intentionally incorrect casing.
  184. css := osutil.NewCachedCaseSensitiveStat(strings.ToUpper(dir))
  185. if _, err := css.Lstat(filepath.Join(dir, "File")); err != nil {
  186. // Our Lstat should report the file exists
  187. t.Fatal("Unexpected error:", err)
  188. }
  189. if _, err := css.Lstat(filepath.Join(dir, "fILE")); err == nil || !os.IsNotExist(err) {
  190. // ... but with the incorrect case we should get ErrNotExist
  191. t.Fatal("Unexpected non-IsNotExist error:", err)
  192. }
  193. // Now do the same tests for a file in a case-sensitive directory.
  194. if err := os.Mkdir(filepath.Join(dir, "Dir"), 0755); err != nil {
  195. t.Fatal(err)
  196. }
  197. if err := ioutil.WriteFile(filepath.Join(dir, "Dir/File"), []byte("data"), 0644); err != nil {
  198. t.Fatal(err)
  199. }
  200. if _, err := os.Lstat(filepath.Join(dir, "Dir/File")); err != nil {
  201. // Standard Lstat should report the file exists
  202. t.Fatal("Unexpected error:", err)
  203. }
  204. if _, err := os.Lstat(filepath.Join(dir, "dIR/File")); err != nil {
  205. // ... also with the incorrect case spelling
  206. t.Fatal("Unexpected error:", err)
  207. }
  208. // Recreate the case sensitive stat:er. We stress it a little by giving it a
  209. // base path with an intentionally incorrect casing.
  210. css = osutil.NewCachedCaseSensitiveStat(strings.ToLower(dir))
  211. if _, err := css.Lstat(filepath.Join(dir, "Dir/File")); err != nil {
  212. // Our Lstat should report the file exists
  213. t.Fatal("Unexpected error:", err)
  214. }
  215. if _, err := css.Lstat(filepath.Join(dir, "dIR/File")); err == nil || !os.IsNotExist(err) {
  216. // ... but with the incorrect case we should get ErrNotExist
  217. t.Fatal("Unexpected non-IsNotExist error:", err)
  218. }
  219. }