copy_unix_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. //go:build !windows
  2. // +build !windows
  3. // TODO Windows: Some of these tests may be salvageable and portable to Windows.
  4. package archive // import "github.com/docker/docker/pkg/archive"
  5. import (
  6. "bytes"
  7. "crypto/sha256"
  8. "encoding/hex"
  9. "fmt"
  10. "io"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "testing"
  15. "gotest.tools/v3/assert"
  16. )
  17. func removeAllPaths(paths ...string) {
  18. for _, path := range paths {
  19. os.RemoveAll(path)
  20. }
  21. }
  22. func getTestTempDirs(t *testing.T) (tmpDirA, tmpDirB string) {
  23. var err error
  24. tmpDirA, err = os.MkdirTemp("", "archive-copy-test")
  25. assert.NilError(t, err)
  26. tmpDirB, err = os.MkdirTemp("", "archive-copy-test")
  27. assert.NilError(t, err)
  28. return
  29. }
  30. func isNotDir(err error) bool {
  31. return strings.Contains(err.Error(), "not a directory")
  32. }
  33. func joinTrailingSep(pathElements ...string) string {
  34. joined := filepath.Join(pathElements...)
  35. return fmt.Sprintf("%s%c", joined, filepath.Separator)
  36. }
  37. func fileContentsEqual(t *testing.T, filenameA, filenameB string) (err error) {
  38. t.Logf("checking for equal file contents: %q and %q\n", filenameA, filenameB)
  39. fileA, err := os.Open(filenameA)
  40. if err != nil {
  41. return
  42. }
  43. defer fileA.Close()
  44. fileB, err := os.Open(filenameB)
  45. if err != nil {
  46. return
  47. }
  48. defer fileB.Close()
  49. hasher := sha256.New()
  50. if _, err = io.Copy(hasher, fileA); err != nil {
  51. return
  52. }
  53. hashA := hasher.Sum(nil)
  54. hasher.Reset()
  55. if _, err = io.Copy(hasher, fileB); err != nil {
  56. return
  57. }
  58. hashB := hasher.Sum(nil)
  59. if !bytes.Equal(hashA, hashB) {
  60. err = fmt.Errorf("file content hashes not equal - expected %s, got %s", hex.EncodeToString(hashA), hex.EncodeToString(hashB))
  61. }
  62. return
  63. }
  64. func dirContentsEqual(t *testing.T, newDir, oldDir string) (err error) {
  65. t.Logf("checking for equal directory contents: %q and %q\n", newDir, oldDir)
  66. var changes []Change
  67. if changes, err = ChangesDirs(newDir, oldDir); err != nil {
  68. return
  69. }
  70. if len(changes) != 0 {
  71. err = fmt.Errorf("expected no changes between directories, but got: %v", changes)
  72. }
  73. return
  74. }
  75. func logDirContents(t *testing.T, dirPath string) {
  76. logWalkedPaths := filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
  77. if err != nil {
  78. t.Errorf("stat error for path %q: %s", path, err)
  79. return nil
  80. }
  81. if info.IsDir() {
  82. path = joinTrailingSep(path)
  83. }
  84. t.Logf("\t%s", path)
  85. return nil
  86. })
  87. t.Logf("logging directory contents: %q", dirPath)
  88. err := filepath.Walk(dirPath, logWalkedPaths)
  89. assert.NilError(t, err)
  90. }
  91. func testCopyHelper(t *testing.T, srcPath, dstPath string) (err error) {
  92. t.Logf("copying from %q to %q (not follow symbol link)", srcPath, dstPath)
  93. return CopyResource(srcPath, dstPath, false)
  94. }
  95. func testCopyHelperFSym(t *testing.T, srcPath, dstPath string) (err error) {
  96. t.Logf("copying from %q to %q (follow symbol link)", srcPath, dstPath)
  97. return CopyResource(srcPath, dstPath, true)
  98. }
  99. // Basic assumptions about SRC and DST:
  100. // 1. SRC must exist.
  101. // 2. If SRC ends with a trailing separator, it must be a directory.
  102. // 3. DST parent directory must exist.
  103. // 4. If DST exists as a file, it must not end with a trailing separator.
  104. // First get these easy error cases out of the way.
  105. // Test for error when SRC does not exist.
  106. func TestCopyErrSrcNotExists(t *testing.T) {
  107. tmpDirA, tmpDirB := getTestTempDirs(t)
  108. defer removeAllPaths(tmpDirA, tmpDirB)
  109. if _, err := CopyInfoSourcePath(filepath.Join(tmpDirA, "file1"), false); !os.IsNotExist(err) {
  110. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  111. }
  112. }
  113. // Test for error when SRC ends in a trailing
  114. // path separator but it exists as a file.
  115. func TestCopyErrSrcNotDir(t *testing.T) {
  116. tmpDirA, tmpDirB := getTestTempDirs(t)
  117. defer removeAllPaths(tmpDirA, tmpDirB)
  118. // Load A with some sample files and directories.
  119. createSampleDir(t, tmpDirA)
  120. if _, err := CopyInfoSourcePath(joinTrailingSep(tmpDirA, "file1"), false); !isNotDir(err) {
  121. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  122. }
  123. }
  124. // Test for error when SRC is a valid file or directory,
  125. // but the DST parent directory does not exist.
  126. func TestCopyErrDstParentNotExists(t *testing.T) {
  127. tmpDirA, tmpDirB := getTestTempDirs(t)
  128. defer removeAllPaths(tmpDirA, tmpDirB)
  129. // Load A with some sample files and directories.
  130. createSampleDir(t, tmpDirA)
  131. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  132. // Try with a file source.
  133. content, err := TarResource(srcInfo)
  134. if err != nil {
  135. t.Fatalf("unexpected error %T: %s", err, err)
  136. }
  137. defer content.Close()
  138. // Copy to a file whose parent does not exist.
  139. if err = CopyTo(content, srcInfo, filepath.Join(tmpDirB, "fakeParentDir", "file1")); err == nil {
  140. t.Fatal("expected IsNotExist error, but got nil instead")
  141. }
  142. if !os.IsNotExist(err) {
  143. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  144. }
  145. // Try with a directory source.
  146. srcInfo = CopyInfo{Path: filepath.Join(tmpDirA, "dir1"), Exists: true, IsDir: true}
  147. content, err = TarResource(srcInfo)
  148. if err != nil {
  149. t.Fatalf("unexpected error %T: %s", err, err)
  150. }
  151. defer content.Close()
  152. // Copy to a directory whose parent does not exist.
  153. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "fakeParentDir", "fakeDstDir")); err == nil {
  154. t.Fatal("expected IsNotExist error, but got nil instead")
  155. }
  156. if !os.IsNotExist(err) {
  157. t.Fatalf("expected IsNotExist error, but got %T: %s", err, err)
  158. }
  159. }
  160. // Test for error when DST ends in a trailing
  161. // path separator but exists as a file.
  162. func TestCopyErrDstNotDir(t *testing.T) {
  163. tmpDirA, tmpDirB := getTestTempDirs(t)
  164. defer removeAllPaths(tmpDirA, tmpDirB)
  165. // Load A and B with some sample files and directories.
  166. createSampleDir(t, tmpDirA)
  167. createSampleDir(t, tmpDirB)
  168. // Try with a file source.
  169. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  170. content, err := TarResource(srcInfo)
  171. if err != nil {
  172. t.Fatalf("unexpected error %T: %s", err, err)
  173. }
  174. defer content.Close()
  175. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "file1")); err == nil {
  176. t.Fatal("expected IsNotDir error, but got nil instead")
  177. }
  178. if !isNotDir(err) {
  179. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  180. }
  181. // Try with a directory source.
  182. srcInfo = CopyInfo{Path: filepath.Join(tmpDirA, "dir1"), Exists: true, IsDir: true}
  183. content, err = TarResource(srcInfo)
  184. if err != nil {
  185. t.Fatalf("unexpected error %T: %s", err, err)
  186. }
  187. defer content.Close()
  188. if err = CopyTo(content, srcInfo, joinTrailingSep(tmpDirB, "file1")); err == nil {
  189. t.Fatal("expected IsNotDir error, but got nil instead")
  190. }
  191. if !isNotDir(err) {
  192. t.Fatalf("expected IsNotDir error, but got %T: %s", err, err)
  193. }
  194. }
  195. // Test to check if CopyTo works with a long (>100 characters) destination file name.
  196. // This is a regression (see https://github.com/docker/for-linux/issues/484).
  197. func TestCopyLongDstFilename(t *testing.T) {
  198. const longName = "a_very_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_long_filename_that_is_101_characters"
  199. tmpDirA, tmpDirB := getTestTempDirs(t)
  200. defer removeAllPaths(tmpDirA, tmpDirB)
  201. // Load A with some sample files and directories.
  202. createSampleDir(t, tmpDirA)
  203. srcInfo := CopyInfo{Path: filepath.Join(tmpDirA, "file1"), Exists: true, IsDir: false}
  204. content, err := TarResource(srcInfo)
  205. if err != nil {
  206. t.Fatalf("unexpected error %T: %s", err, err)
  207. }
  208. defer content.Close()
  209. err = CopyTo(content, srcInfo, filepath.Join(tmpDirB, longName))
  210. if err != nil {
  211. t.Fatalf("unexpected error %T: %s", err, err)
  212. }
  213. }
  214. // Possibilities are reduced to the remaining 10 cases:
  215. //
  216. // case | srcIsDir | onlyDirContents | dstExists | dstIsDir | dstTrSep | action
  217. // ===================================================================================================
  218. // A | no | - | no | - | no | create file
  219. // B | no | - | no | - | yes | error
  220. // C | no | - | yes | no | - | overwrite file
  221. // D | no | - | yes | yes | - | create file in dst dir
  222. // E | yes | no | no | - | - | create dir, copy contents
  223. // F | yes | no | yes | no | - | error
  224. // G | yes | no | yes | yes | - | copy dir and contents
  225. // H | yes | yes | no | - | - | create dir, copy contents
  226. // I | yes | yes | yes | no | - | error
  227. // J | yes | yes | yes | yes | - | copy dir contents
  228. //
  229. // A. SRC specifies a file and DST (no trailing path separator) doesn't exist.
  230. //
  231. // This should create a file with the name DST and copy the contents of the source
  232. // file into it.
  233. func TestCopyCaseA(t *testing.T) {
  234. tmpDirA, tmpDirB := getTestTempDirs(t)
  235. defer removeAllPaths(tmpDirA, tmpDirB)
  236. // Load A with some sample files and directories.
  237. createSampleDir(t, tmpDirA)
  238. srcPath := filepath.Join(tmpDirA, "file1")
  239. dstPath := filepath.Join(tmpDirB, "itWorks.txt")
  240. var err error
  241. if err = testCopyHelper(t, srcPath, dstPath); err != nil {
  242. t.Fatalf("unexpected error %T: %s", err, err)
  243. }
  244. err = fileContentsEqual(t, srcPath, dstPath)
  245. assert.NilError(t, err)
  246. os.Remove(dstPath)
  247. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  248. symlinkPath1 := filepath.Join(tmpDirA, "symlink4")
  249. linkTarget := filepath.Join(tmpDirA, "file1")
  250. if err = testCopyHelperFSym(t, symlinkPath, dstPath); err != nil {
  251. t.Fatalf("unexpected error %T: %s", err, err)
  252. }
  253. err = fileContentsEqual(t, linkTarget, dstPath)
  254. assert.NilError(t, err)
  255. os.Remove(dstPath)
  256. if err = testCopyHelperFSym(t, symlinkPath1, dstPath); err != nil {
  257. t.Fatalf("unexpected error %T: %s", err, err)
  258. }
  259. err = fileContentsEqual(t, linkTarget, dstPath)
  260. assert.NilError(t, err)
  261. }
  262. // B. SRC specifies a file and DST (with trailing path separator) doesn't exist.
  263. //
  264. // This should cause an error because the copy operation cannot create a directory
  265. // when copying a single file.
  266. func TestCopyCaseB(t *testing.T) {
  267. tmpDirA, tmpDirB := getTestTempDirs(t)
  268. defer removeAllPaths(tmpDirA, tmpDirB)
  269. // Load A with some sample files and directories.
  270. createSampleDir(t, tmpDirA)
  271. srcPath := filepath.Join(tmpDirA, "file1")
  272. dstDir := joinTrailingSep(tmpDirB, "testDir")
  273. var err error
  274. if err = testCopyHelper(t, srcPath, dstDir); err == nil {
  275. t.Fatal("expected ErrDirNotExists error, but got nil instead")
  276. }
  277. if err != ErrDirNotExists {
  278. t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
  279. }
  280. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  281. if err = testCopyHelperFSym(t, symlinkPath, dstDir); err == nil {
  282. t.Fatal("expected ErrDirNotExists error, but got nil instead")
  283. }
  284. if err != ErrDirNotExists {
  285. t.Fatalf("expected ErrDirNotExists error, but got %T: %s", err, err)
  286. }
  287. }
  288. // C. SRC specifies a file and DST exists as a file.
  289. //
  290. // This should overwrite the file at DST with the contents of the source file.
  291. func TestCopyCaseC(t *testing.T) {
  292. tmpDirA, tmpDirB := getTestTempDirs(t)
  293. defer removeAllPaths(tmpDirA, tmpDirB)
  294. // Load A and B with some sample files and directories.
  295. createSampleDir(t, tmpDirA)
  296. createSampleDir(t, tmpDirB)
  297. srcPath := filepath.Join(tmpDirA, "file1")
  298. dstPath := filepath.Join(tmpDirB, "file2")
  299. var err error
  300. // Ensure they start out different.
  301. if err = fileContentsEqual(t, srcPath, dstPath); err == nil {
  302. t.Fatal("expected different file contents")
  303. }
  304. if err = testCopyHelper(t, srcPath, dstPath); err != nil {
  305. t.Fatalf("unexpected error %T: %s", err, err)
  306. }
  307. err = fileContentsEqual(t, srcPath, dstPath)
  308. assert.NilError(t, err)
  309. }
  310. // C. Symbol link following version: SRC specifies a file and DST exists as a file.
  311. //
  312. // This should overwrite the file at DST with the contents of the source file.
  313. func TestCopyCaseCFSym(t *testing.T) {
  314. tmpDirA, tmpDirB := getTestTempDirs(t)
  315. defer removeAllPaths(tmpDirA, tmpDirB)
  316. // Load A and B with some sample files and directories.
  317. createSampleDir(t, tmpDirA)
  318. createSampleDir(t, tmpDirB)
  319. symlinkPathBad := filepath.Join(tmpDirA, "symlink1")
  320. symlinkPath := filepath.Join(tmpDirA, "symlink3")
  321. linkTarget := filepath.Join(tmpDirA, "file1")
  322. dstPath := filepath.Join(tmpDirB, "file2")
  323. var err error
  324. // first to test broken link
  325. if err = testCopyHelperFSym(t, symlinkPathBad, dstPath); err == nil {
  326. t.Fatalf("unexpected error %T: %s", err, err)
  327. }
  328. // test symbol link -> symbol link -> target
  329. // Ensure they start out different.
  330. if err = fileContentsEqual(t, linkTarget, dstPath); err == nil {
  331. t.Fatal("expected different file contents")
  332. }
  333. if err = testCopyHelperFSym(t, symlinkPath, dstPath); err != nil {
  334. t.Fatalf("unexpected error %T: %s", err, err)
  335. }
  336. err = fileContentsEqual(t, linkTarget, dstPath)
  337. assert.NilError(t, err)
  338. }
  339. // D. SRC specifies a file and DST exists as a directory.
  340. //
  341. // This should place a copy of the source file inside it using the basename from
  342. // SRC. Ensure this works whether DST has a trailing path separator or not.
  343. func TestCopyCaseD(t *testing.T) {
  344. tmpDirA, tmpDirB := getTestTempDirs(t)
  345. defer removeAllPaths(tmpDirA, tmpDirB)
  346. // Load A and B with some sample files and directories.
  347. createSampleDir(t, tmpDirA)
  348. createSampleDir(t, tmpDirB)
  349. srcPath := filepath.Join(tmpDirA, "file1")
  350. dstDir := filepath.Join(tmpDirB, "dir1")
  351. dstPath := filepath.Join(dstDir, "file1")
  352. var err error
  353. // Ensure that dstPath doesn't exist.
  354. if _, err = os.Stat(dstPath); !os.IsNotExist(err) {
  355. t.Fatalf("did not expect dstPath %q to exist", dstPath)
  356. }
  357. if err = testCopyHelper(t, srcPath, dstDir); err != nil {
  358. t.Fatalf("unexpected error %T: %s", err, err)
  359. }
  360. err = fileContentsEqual(t, srcPath, dstPath)
  361. assert.NilError(t, err)
  362. // Now try again but using a trailing path separator for dstDir.
  363. if err = os.RemoveAll(dstDir); err != nil {
  364. t.Fatalf("unable to remove dstDir: %s", err)
  365. }
  366. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  367. t.Fatalf("unable to make dstDir: %s", err)
  368. }
  369. dstDir = joinTrailingSep(tmpDirB, "dir1")
  370. if err = testCopyHelper(t, srcPath, dstDir); err != nil {
  371. t.Fatalf("unexpected error %T: %s", err, err)
  372. }
  373. err = fileContentsEqual(t, srcPath, dstPath)
  374. assert.NilError(t, err)
  375. }
  376. // D. Symbol link following version: SRC specifies a file and DST exists as a directory.
  377. //
  378. // This should place a copy of the source file inside it using the basename from
  379. // SRC. Ensure this works whether DST has a trailing path separator or not.
  380. func TestCopyCaseDFSym(t *testing.T) {
  381. tmpDirA, tmpDirB := getTestTempDirs(t)
  382. defer removeAllPaths(tmpDirA, tmpDirB)
  383. // Load A and B with some sample files and directories.
  384. createSampleDir(t, tmpDirA)
  385. createSampleDir(t, tmpDirB)
  386. srcPath := filepath.Join(tmpDirA, "symlink4")
  387. linkTarget := filepath.Join(tmpDirA, "file1")
  388. dstDir := filepath.Join(tmpDirB, "dir1")
  389. dstPath := filepath.Join(dstDir, "symlink4")
  390. var err error
  391. // Ensure that dstPath doesn't exist.
  392. if _, err = os.Stat(dstPath); !os.IsNotExist(err) {
  393. t.Fatalf("did not expect dstPath %q to exist", dstPath)
  394. }
  395. if err = testCopyHelperFSym(t, srcPath, dstDir); err != nil {
  396. t.Fatalf("unexpected error %T: %s", err, err)
  397. }
  398. err = fileContentsEqual(t, linkTarget, dstPath)
  399. assert.NilError(t, err)
  400. // Now try again but using a trailing path separator for dstDir.
  401. if err = os.RemoveAll(dstDir); err != nil {
  402. t.Fatalf("unable to remove dstDir: %s", err)
  403. }
  404. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  405. t.Fatalf("unable to make dstDir: %s", err)
  406. }
  407. dstDir = joinTrailingSep(tmpDirB, "dir1")
  408. if err = testCopyHelperFSym(t, srcPath, dstDir); err != nil {
  409. t.Fatalf("unexpected error %T: %s", err, err)
  410. }
  411. err = fileContentsEqual(t, linkTarget, dstPath)
  412. assert.NilError(t, err)
  413. }
  414. // E. SRC specifies a directory and DST does not exist.
  415. //
  416. // This should create a directory at DST and copy the contents of the SRC directory
  417. // into the DST directory. Ensure this works whether DST has a trailing path
  418. // separator or not.
  419. func TestCopyCaseE(t *testing.T) {
  420. tmpDirA, tmpDirB := getTestTempDirs(t)
  421. defer removeAllPaths(tmpDirA, tmpDirB)
  422. // Load A with some sample files and directories.
  423. createSampleDir(t, tmpDirA)
  424. srcDir := filepath.Join(tmpDirA, "dir1")
  425. dstDir := filepath.Join(tmpDirB, "testDir")
  426. var err error
  427. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  428. t.Fatalf("unexpected error %T: %s", err, err)
  429. }
  430. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  431. t.Log("dir contents not equal")
  432. logDirContents(t, tmpDirA)
  433. logDirContents(t, tmpDirB)
  434. t.Fatal(err)
  435. }
  436. // Now try again but using a trailing path separator for dstDir.
  437. if err = os.RemoveAll(dstDir); err != nil {
  438. t.Fatalf("unable to remove dstDir: %s", err)
  439. }
  440. dstDir = joinTrailingSep(tmpDirB, "testDir")
  441. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  442. t.Fatalf("unexpected error %T: %s", err, err)
  443. }
  444. err = dirContentsEqual(t, dstDir, srcDir)
  445. assert.NilError(t, err)
  446. }
  447. // E. Symbol link following version: SRC specifies a directory and DST does not exist.
  448. //
  449. // This should create a directory at DST and copy the contents of the SRC directory
  450. // into the DST directory. Ensure this works whether DST has a trailing path
  451. // separator or not.
  452. func TestCopyCaseEFSym(t *testing.T) {
  453. tmpDirA, tmpDirB := getTestTempDirs(t)
  454. defer removeAllPaths(tmpDirA, tmpDirB)
  455. // Load A with some sample files and directories.
  456. createSampleDir(t, tmpDirA)
  457. srcDir := filepath.Join(tmpDirA, "dirSymlink")
  458. linkTarget := filepath.Join(tmpDirA, "dir1")
  459. dstDir := filepath.Join(tmpDirB, "testDir")
  460. var err error
  461. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  462. t.Fatalf("unexpected error %T: %s", err, err)
  463. }
  464. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  465. t.Log("dir contents not equal")
  466. logDirContents(t, tmpDirA)
  467. logDirContents(t, tmpDirB)
  468. t.Fatal(err)
  469. }
  470. // Now try again but using a trailing path separator for dstDir.
  471. if err = os.RemoveAll(dstDir); err != nil {
  472. t.Fatalf("unable to remove dstDir: %s", err)
  473. }
  474. dstDir = joinTrailingSep(tmpDirB, "testDir")
  475. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  476. t.Fatalf("unexpected error %T: %s", err, err)
  477. }
  478. err = dirContentsEqual(t, dstDir, linkTarget)
  479. assert.NilError(t, err)
  480. }
  481. // F. SRC specifies a directory and DST exists as a file.
  482. //
  483. // This should cause an error as it is not possible to overwrite a file with a
  484. // directory.
  485. func TestCopyCaseF(t *testing.T) {
  486. tmpDirA, tmpDirB := getTestTempDirs(t)
  487. defer removeAllPaths(tmpDirA, tmpDirB)
  488. // Load A and B with some sample files and directories.
  489. createSampleDir(t, tmpDirA)
  490. createSampleDir(t, tmpDirB)
  491. srcDir := filepath.Join(tmpDirA, "dir1")
  492. symSrcDir := filepath.Join(tmpDirA, "dirSymlink")
  493. dstFile := filepath.Join(tmpDirB, "file1")
  494. var err error
  495. if err = testCopyHelper(t, srcDir, dstFile); err == nil {
  496. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  497. }
  498. if err != ErrCannotCopyDir {
  499. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  500. }
  501. // now test with symbol link
  502. if err = testCopyHelperFSym(t, symSrcDir, dstFile); err == nil {
  503. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  504. }
  505. if err != ErrCannotCopyDir {
  506. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  507. }
  508. }
  509. // G. SRC specifies a directory and DST exists as a directory.
  510. //
  511. // This should copy the SRC directory and all its contents to the DST directory.
  512. // Ensure this works whether DST has a trailing path separator or not.
  513. func TestCopyCaseG(t *testing.T) {
  514. tmpDirA, tmpDirB := getTestTempDirs(t)
  515. defer removeAllPaths(tmpDirA, tmpDirB)
  516. // Load A and B with some sample files and directories.
  517. createSampleDir(t, tmpDirA)
  518. createSampleDir(t, tmpDirB)
  519. srcDir := filepath.Join(tmpDirA, "dir1")
  520. dstDir := filepath.Join(tmpDirB, "dir2")
  521. resultDir := filepath.Join(dstDir, "dir1")
  522. var err error
  523. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  524. t.Fatalf("unexpected error %T: %s", err, err)
  525. }
  526. err = dirContentsEqual(t, resultDir, srcDir)
  527. assert.NilError(t, err)
  528. // Now try again but using a trailing path separator for dstDir.
  529. if err = os.RemoveAll(dstDir); err != nil {
  530. t.Fatalf("unable to remove dstDir: %s", err)
  531. }
  532. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  533. t.Fatalf("unable to make dstDir: %s", err)
  534. }
  535. dstDir = joinTrailingSep(tmpDirB, "dir2")
  536. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  537. t.Fatalf("unexpected error %T: %s", err, err)
  538. }
  539. err = dirContentsEqual(t, resultDir, srcDir)
  540. assert.NilError(t, err)
  541. }
  542. // G. Symbol link version: SRC specifies a directory and DST exists as a directory.
  543. //
  544. // This should copy the SRC directory and all its contents to the DST directory.
  545. // Ensure this works whether DST has a trailing path separator or not.
  546. func TestCopyCaseGFSym(t *testing.T) {
  547. tmpDirA, tmpDirB := getTestTempDirs(t)
  548. defer removeAllPaths(tmpDirA, tmpDirB)
  549. // Load A and B with some sample files and directories.
  550. createSampleDir(t, tmpDirA)
  551. createSampleDir(t, tmpDirB)
  552. srcDir := filepath.Join(tmpDirA, "dirSymlink")
  553. linkTarget := filepath.Join(tmpDirA, "dir1")
  554. dstDir := filepath.Join(tmpDirB, "dir2")
  555. resultDir := filepath.Join(dstDir, "dirSymlink")
  556. var err error
  557. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  558. t.Fatalf("unexpected error %T: %s", err, err)
  559. }
  560. err = dirContentsEqual(t, resultDir, linkTarget)
  561. assert.NilError(t, err)
  562. // Now try again but using a trailing path separator for dstDir.
  563. if err = os.RemoveAll(dstDir); err != nil {
  564. t.Fatalf("unable to remove dstDir: %s", err)
  565. }
  566. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  567. t.Fatalf("unable to make dstDir: %s", err)
  568. }
  569. dstDir = joinTrailingSep(tmpDirB, "dir2")
  570. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  571. t.Fatalf("unexpected error %T: %s", err, err)
  572. }
  573. err = dirContentsEqual(t, resultDir, linkTarget)
  574. assert.NilError(t, err)
  575. }
  576. // H. SRC specifies a directory's contents only and DST does not exist.
  577. //
  578. // This should create a directory at DST and copy the contents of the SRC
  579. // directory (but not the directory itself) into the DST directory. Ensure
  580. // this works whether DST has a trailing path separator or not.
  581. func TestCopyCaseH(t *testing.T) {
  582. tmpDirA, tmpDirB := getTestTempDirs(t)
  583. defer removeAllPaths(tmpDirA, tmpDirB)
  584. // Load A with some sample files and directories.
  585. createSampleDir(t, tmpDirA)
  586. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  587. dstDir := filepath.Join(tmpDirB, "testDir")
  588. var err error
  589. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  590. t.Fatalf("unexpected error %T: %s", err, err)
  591. }
  592. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  593. t.Log("dir contents not equal")
  594. logDirContents(t, tmpDirA)
  595. logDirContents(t, tmpDirB)
  596. t.Fatal(err)
  597. }
  598. // Now try again but using a trailing path separator for dstDir.
  599. if err = os.RemoveAll(dstDir); err != nil {
  600. t.Fatalf("unable to remove dstDir: %s", err)
  601. }
  602. dstDir = joinTrailingSep(tmpDirB, "testDir")
  603. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  604. t.Fatalf("unexpected error %T: %s", err, err)
  605. }
  606. if err = dirContentsEqual(t, dstDir, srcDir); err != nil {
  607. t.Log("dir contents not equal")
  608. logDirContents(t, tmpDirA)
  609. logDirContents(t, tmpDirB)
  610. t.Fatal(err)
  611. }
  612. }
  613. // H. Symbol link following version: SRC specifies a directory's contents only and DST does not exist.
  614. //
  615. // This should create a directory at DST and copy the contents of the SRC
  616. // directory (but not the directory itself) into the DST directory. Ensure
  617. // this works whether DST has a trailing path separator or not.
  618. func TestCopyCaseHFSym(t *testing.T) {
  619. tmpDirA, tmpDirB := getTestTempDirs(t)
  620. defer removeAllPaths(tmpDirA, tmpDirB)
  621. // Load A with some sample files and directories.
  622. createSampleDir(t, tmpDirA)
  623. srcDir := joinTrailingSep(tmpDirA, "dirSymlink") + "."
  624. linkTarget := filepath.Join(tmpDirA, "dir1")
  625. dstDir := filepath.Join(tmpDirB, "testDir")
  626. var err error
  627. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  628. t.Fatalf("unexpected error %T: %s", err, err)
  629. }
  630. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  631. t.Log("dir contents not equal")
  632. logDirContents(t, tmpDirA)
  633. logDirContents(t, tmpDirB)
  634. t.Fatal(err)
  635. }
  636. // Now try again but using a trailing path separator for dstDir.
  637. if err = os.RemoveAll(dstDir); err != nil {
  638. t.Fatalf("unable to remove dstDir: %s", err)
  639. }
  640. dstDir = joinTrailingSep(tmpDirB, "testDir")
  641. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  642. t.Fatalf("unexpected error %T: %s", err, err)
  643. }
  644. if err = dirContentsEqual(t, dstDir, linkTarget); err != nil {
  645. t.Log("dir contents not equal")
  646. logDirContents(t, tmpDirA)
  647. logDirContents(t, tmpDirB)
  648. t.Fatal(err)
  649. }
  650. }
  651. // I. SRC specifies a directory's contents only and DST exists as a file.
  652. //
  653. // This should cause an error as it is not possible to overwrite a file with a
  654. // directory.
  655. func TestCopyCaseI(t *testing.T) {
  656. tmpDirA, tmpDirB := getTestTempDirs(t)
  657. defer removeAllPaths(tmpDirA, tmpDirB)
  658. // Load A and B with some sample files and directories.
  659. createSampleDir(t, tmpDirA)
  660. createSampleDir(t, tmpDirB)
  661. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  662. symSrcDir := filepath.Join(tmpDirB, "dirSymlink")
  663. dstFile := filepath.Join(tmpDirB, "file1")
  664. var err error
  665. if err = testCopyHelper(t, srcDir, dstFile); err == nil {
  666. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  667. }
  668. if err != ErrCannotCopyDir {
  669. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  670. }
  671. // now try with symbol link of dir
  672. if err = testCopyHelperFSym(t, symSrcDir, dstFile); err == nil {
  673. t.Fatal("expected ErrCannotCopyDir error, but got nil instead")
  674. }
  675. if err != ErrCannotCopyDir {
  676. t.Fatalf("expected ErrCannotCopyDir error, but got %T: %s", err, err)
  677. }
  678. }
  679. // J. SRC specifies a directory's contents only and DST exists as a directory.
  680. //
  681. // This should copy the contents of the SRC directory (but not the directory
  682. // itself) into the DST directory. Ensure this works whether DST has a
  683. // trailing path separator or not.
  684. func TestCopyCaseJ(t *testing.T) {
  685. tmpDirA, tmpDirB := getTestTempDirs(t)
  686. defer removeAllPaths(tmpDirA, tmpDirB)
  687. // Load A and B with some sample files and directories.
  688. createSampleDir(t, tmpDirA)
  689. createSampleDir(t, tmpDirB)
  690. srcDir := joinTrailingSep(tmpDirA, "dir1") + "."
  691. dstDir := filepath.Join(tmpDirB, "dir5")
  692. var err error
  693. // first to create an empty dir
  694. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  695. t.Fatalf("unable to make dstDir: %s", err)
  696. }
  697. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  698. t.Fatalf("unexpected error %T: %s", err, err)
  699. }
  700. err = dirContentsEqual(t, dstDir, srcDir)
  701. assert.NilError(t, err)
  702. // Now try again but using a trailing path separator for dstDir.
  703. if err = os.RemoveAll(dstDir); err != nil {
  704. t.Fatalf("unable to remove dstDir: %s", err)
  705. }
  706. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  707. t.Fatalf("unable to make dstDir: %s", err)
  708. }
  709. dstDir = joinTrailingSep(tmpDirB, "dir5")
  710. if err = testCopyHelper(t, srcDir, dstDir); err != nil {
  711. t.Fatalf("unexpected error %T: %s", err, err)
  712. }
  713. err = dirContentsEqual(t, dstDir, srcDir)
  714. assert.NilError(t, err)
  715. }
  716. // J. Symbol link following version: SRC specifies a directory's contents only and DST exists as a directory.
  717. //
  718. // This should copy the contents of the SRC directory (but not the directory
  719. // itself) into the DST directory. Ensure this works whether DST has a
  720. // trailing path separator or not.
  721. func TestCopyCaseJFSym(t *testing.T) {
  722. tmpDirA, tmpDirB := getTestTempDirs(t)
  723. defer removeAllPaths(tmpDirA, tmpDirB)
  724. // Load A and B with some sample files and directories.
  725. createSampleDir(t, tmpDirA)
  726. createSampleDir(t, tmpDirB)
  727. srcDir := joinTrailingSep(tmpDirA, "dirSymlink") + "."
  728. linkTarget := filepath.Join(tmpDirA, "dir1")
  729. dstDir := filepath.Join(tmpDirB, "dir5")
  730. var err error
  731. // first to create an empty dir
  732. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  733. t.Fatalf("unable to make dstDir: %s", err)
  734. }
  735. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  736. t.Fatalf("unexpected error %T: %s", err, err)
  737. }
  738. err = dirContentsEqual(t, dstDir, linkTarget)
  739. assert.NilError(t, err)
  740. // Now try again but using a trailing path separator for dstDir.
  741. if err = os.RemoveAll(dstDir); err != nil {
  742. t.Fatalf("unable to remove dstDir: %s", err)
  743. }
  744. if err = os.MkdirAll(dstDir, os.FileMode(0755)); err != nil {
  745. t.Fatalf("unable to make dstDir: %s", err)
  746. }
  747. dstDir = joinTrailingSep(tmpDirB, "dir5")
  748. if err = testCopyHelperFSym(t, srcDir, dstDir); err != nil {
  749. t.Fatalf("unexpected error %T: %s", err, err)
  750. }
  751. err = dirContentsEqual(t, dstDir, linkTarget)
  752. assert.NilError(t, err)
  753. }