fileutils_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package fileutils // import "github.com/docker/docker/pkg/fileutils"
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. "runtime"
  7. "testing"
  8. )
  9. // CopyFile with invalid src
  10. func TestCopyFileWithInvalidSrc(t *testing.T) {
  11. bytes, err := CopyFile("/invalid/file/path", path.Join(t.TempDir(), "dest"))
  12. if err == nil {
  13. t.Fatal("Should have fail to copy an invalid src file")
  14. }
  15. if bytes != 0 {
  16. t.Fatal("Should have written 0 bytes")
  17. }
  18. }
  19. // CopyFile with invalid dest
  20. func TestCopyFileWithInvalidDest(t *testing.T) {
  21. tempFolder := t.TempDir()
  22. src := path.Join(tempFolder, "file")
  23. err := os.WriteFile(src, []byte("content"), 0o740)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
  28. if err == nil {
  29. t.Fatal("Should have fail to copy an invalid src file")
  30. }
  31. if bytes != 0 {
  32. t.Fatal("Should have written 0 bytes")
  33. }
  34. }
  35. // CopyFile with same src and dest
  36. func TestCopyFileWithSameSrcAndDest(t *testing.T) {
  37. file := path.Join(t.TempDir(), "file")
  38. err := os.WriteFile(file, []byte("content"), 0o740)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. bytes, err := CopyFile(file, file)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if bytes != 0 {
  47. t.Fatal("Should have written 0 bytes as it is the same file.")
  48. }
  49. }
  50. // CopyFile with same src and dest but path is different and not clean
  51. func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
  52. testFolder := path.Join(t.TempDir(), "test")
  53. err := os.Mkdir(testFolder, 0o740)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. file := path.Join(testFolder, "file")
  58. sameFile := testFolder + "/../test/file"
  59. err = os.WriteFile(file, []byte("content"), 0o740)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. bytes, err := CopyFile(file, sameFile)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if bytes != 0 {
  68. t.Fatal("Should have written 0 bytes as it is the same file.")
  69. }
  70. }
  71. func TestCopyFile(t *testing.T) {
  72. tempFolder := t.TempDir()
  73. src := path.Join(tempFolder, "src")
  74. dest := path.Join(tempFolder, "dest")
  75. err := os.WriteFile(src, []byte("content"), 0o777)
  76. if err != nil {
  77. t.Error(err)
  78. }
  79. err = os.WriteFile(dest, []byte("destContent"), 0o777)
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. bytes, err := CopyFile(src, dest)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if bytes != 7 {
  88. t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
  89. }
  90. actual, err := os.ReadFile(dest)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if string(actual) != "content" {
  95. t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
  96. }
  97. }
  98. // Reading a symlink to a directory must return the directory
  99. func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
  100. // TODO Windows: Port this test
  101. if runtime.GOOS == "windows" {
  102. t.Skip("Needs porting to Windows")
  103. }
  104. var err error
  105. if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0o777); err != nil {
  106. t.Errorf("failed to create directory: %s", err)
  107. }
  108. if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
  109. t.Errorf("failed to create symlink: %s", err)
  110. }
  111. var symlinkedPath string
  112. if symlinkedPath, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
  113. t.Fatalf("failed to read symlink to directory: %s", err)
  114. }
  115. if symlinkedPath != "/tmp/testReadSymlinkToExistingDirectory" {
  116. t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath)
  117. }
  118. if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
  119. t.Errorf("failed to remove temporary directory: %s", err)
  120. }
  121. if err = os.Remove("/tmp/dirLinkTest"); err != nil {
  122. t.Errorf("failed to remove symlink: %s", err)
  123. }
  124. }
  125. // Reading a non-existing symlink must fail
  126. func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
  127. symLinkedPath, err := ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath")
  128. if err == nil {
  129. t.Fatalf("error expected for non-existing symlink")
  130. }
  131. if symLinkedPath != "" {
  132. t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath)
  133. }
  134. }
  135. // Reading a symlink to a file must fail
  136. func TestReadSymlinkedDirectoryToFile(t *testing.T) {
  137. // TODO Windows: Port this test
  138. if runtime.GOOS == "windows" {
  139. t.Skip("Needs porting to Windows")
  140. }
  141. var err error
  142. var file *os.File
  143. // #nosec G303
  144. if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
  145. t.Fatalf("failed to create file: %s", err)
  146. }
  147. file.Close()
  148. if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
  149. t.Errorf("failed to create symlink: %s", err)
  150. }
  151. symlinkedPath, err := ReadSymlinkedDirectory("/tmp/fileLinkTest")
  152. if err == nil {
  153. t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
  154. }
  155. if symlinkedPath != "" {
  156. t.Fatalf("path should've been empty: %s", symlinkedPath)
  157. }
  158. if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
  159. t.Errorf("failed to remove file: %s", err)
  160. }
  161. if err = os.Remove("/tmp/fileLinkTest"); err != nil {
  162. t.Errorf("failed to remove symlink: %s", err)
  163. }
  164. }
  165. func TestCreateIfNotExistsDir(t *testing.T) {
  166. folderToCreate := filepath.Join(t.TempDir(), "tocreate")
  167. if err := CreateIfNotExists(folderToCreate, true); err != nil {
  168. t.Fatal(err)
  169. }
  170. fileinfo, err := os.Stat(folderToCreate)
  171. if err != nil {
  172. t.Fatalf("Should have create a folder, got %v", err)
  173. }
  174. if !fileinfo.IsDir() {
  175. t.Fatalf("Should have been a dir, seems it's not")
  176. }
  177. }
  178. func TestCreateIfNotExistsFile(t *testing.T) {
  179. fileToCreate := filepath.Join(t.TempDir(), "file/to/create")
  180. if err := CreateIfNotExists(fileToCreate, false); err != nil {
  181. t.Fatal(err)
  182. }
  183. fileinfo, err := os.Stat(fileToCreate)
  184. if err != nil {
  185. t.Fatalf("Should have create a file, got %v", err)
  186. }
  187. if fileinfo.IsDir() {
  188. t.Fatalf("Should have been a file, seems it's not")
  189. }
  190. }