osutil_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 https://mozilla.org/MPL/2.0/.
  6. package osutil_test
  7. import (
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/build"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/osutil"
  16. )
  17. func TestIsDeleted(t *testing.T) {
  18. type tc struct {
  19. path string
  20. isDel bool
  21. }
  22. cases := []tc{
  23. {"del", true},
  24. {"del.file", false},
  25. {filepath.Join("del", "del"), true},
  26. {"file", false},
  27. {"linkToFile", false},
  28. {"linkToDel", false},
  29. {"linkToDir", false},
  30. {filepath.Join("linkToDir", "file"), true},
  31. {filepath.Join("file", "behindFile"), true},
  32. {"dir", false},
  33. {"dir.file", false},
  34. {filepath.Join("dir", "file"), false},
  35. {filepath.Join("dir", "del"), true},
  36. {filepath.Join("dir", "del", "del"), true},
  37. {filepath.Join("del", "del", "del"), true},
  38. }
  39. testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
  40. testFs.MkdirAll("dir", 0777)
  41. for _, f := range []string{"file", "del.file", "dir.file", filepath.Join("dir", "file")} {
  42. fd, err := testFs.Create(f)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. fd.Close()
  47. }
  48. if !build.IsWindows {
  49. // Can't create unreadable dir on windows
  50. testFs.MkdirAll("inacc", 0777)
  51. if err := testFs.Chmod("inacc", 0000); err == nil {
  52. if _, err := testFs.Lstat(filepath.Join("inacc", "file")); fs.IsPermission(err) {
  53. // May fail e.g. if tests are run as root -> just skip
  54. cases = append(cases, tc{"inacc", false}, tc{filepath.Join("inacc", "file"), false})
  55. }
  56. }
  57. }
  58. for _, n := range []string{"Dir", "File", "Del"} {
  59. if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, strings.ToLower(n), "linkTo"+n); err != nil {
  60. if build.IsWindows {
  61. t.Skip("Symlinks aren't working")
  62. }
  63. t.Fatal(err)
  64. }
  65. }
  66. for _, c := range cases {
  67. if osutil.IsDeleted(testFs, c.path) != c.isDel {
  68. t.Errorf("IsDeleted(%v) != %v", c.path, c.isDel)
  69. }
  70. }
  71. testFs.Chmod("inacc", 0777)
  72. os.RemoveAll("testdata")
  73. }
  74. func TestRenameOrCopy(t *testing.T) {
  75. sameFs := fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir())
  76. tests := []struct {
  77. src fs.Filesystem
  78. dst fs.Filesystem
  79. file string
  80. }{
  81. {
  82. src: sameFs,
  83. dst: sameFs,
  84. file: "file",
  85. },
  86. {
  87. src: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
  88. dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
  89. file: "file",
  90. },
  91. {
  92. src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`),
  93. dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
  94. file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`),
  95. },
  96. }
  97. for _, test := range tests {
  98. content := test.src.URI()
  99. if _, err := test.src.Lstat(test.file); err != nil {
  100. if !fs.IsNotExist(err) {
  101. t.Fatal(err)
  102. }
  103. if fd, err := test.src.Create(test.file); err != nil {
  104. t.Fatal(err)
  105. } else {
  106. if _, err := fd.Write([]byte(test.src.URI())); err != nil {
  107. t.Fatal(err)
  108. }
  109. _ = fd.Close()
  110. }
  111. } else {
  112. fd, err := test.src.Open(test.file)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. buf, err := io.ReadAll(fd)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. _ = fd.Close()
  121. content = string(buf)
  122. }
  123. err := osutil.RenameOrCopy(fs.CopyRangeMethodStandard, test.src, test.dst, test.file, "new")
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if fd, err := test.dst.Open("new"); err != nil {
  128. t.Fatal(err)
  129. } else {
  130. t.Cleanup(func() {
  131. _ = fd.Close()
  132. })
  133. if buf, err := io.ReadAll(fd); err != nil {
  134. t.Fatal(err)
  135. } else if string(buf) != content {
  136. t.Fatalf("expected %s got %s", content, string(buf))
  137. }
  138. }
  139. }
  140. }