archive_unix_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // +build !windows
  2. package archive
  3. import (
  4. "bytes"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "syscall"
  12. "testing"
  13. "github.com/docker/docker/pkg/system"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. "golang.org/x/sys/unix"
  17. )
  18. func TestCanonicalTarNameForPath(t *testing.T) {
  19. cases := []struct{ in, expected string }{
  20. {"foo", "foo"},
  21. {"foo/bar", "foo/bar"},
  22. {"foo/dir/", "foo/dir/"},
  23. }
  24. for _, v := range cases {
  25. if out, err := CanonicalTarNameForPath(v.in); err != nil {
  26. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  27. } else if out != v.expected {
  28. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  29. }
  30. }
  31. }
  32. func TestCanonicalTarName(t *testing.T) {
  33. cases := []struct {
  34. in string
  35. isDir bool
  36. expected string
  37. }{
  38. {"foo", false, "foo"},
  39. {"foo", true, "foo/"},
  40. {"foo/bar", false, "foo/bar"},
  41. {"foo/bar", true, "foo/bar/"},
  42. }
  43. for _, v := range cases {
  44. if out, err := canonicalTarName(v.in, v.isDir); err != nil {
  45. t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
  46. } else if out != v.expected {
  47. t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
  48. }
  49. }
  50. }
  51. func TestChmodTarEntry(t *testing.T) {
  52. cases := []struct {
  53. in, expected os.FileMode
  54. }{
  55. {0000, 0000},
  56. {0777, 0777},
  57. {0644, 0644},
  58. {0755, 0755},
  59. {0444, 0444},
  60. }
  61. for _, v := range cases {
  62. if out := chmodTarEntry(v.in); out != v.expected {
  63. t.Fatalf("wrong chmod. expected:%v got:%v", v.expected, out)
  64. }
  65. }
  66. }
  67. func TestTarWithHardLink(t *testing.T) {
  68. origin, err := ioutil.TempDir("", "docker-test-tar-hardlink")
  69. require.NoError(t, err)
  70. defer os.RemoveAll(origin)
  71. err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
  72. require.NoError(t, err)
  73. err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2"))
  74. require.NoError(t, err)
  75. var i1, i2 uint64
  76. i1, err = getNlink(filepath.Join(origin, "1"))
  77. require.NoError(t, err)
  78. // sanity check that we can hardlink
  79. if i1 != 2 {
  80. t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1)
  81. }
  82. dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest")
  83. require.NoError(t, err)
  84. defer os.RemoveAll(dest)
  85. // we'll do this in two steps to separate failure
  86. fh, err := Tar(origin, Uncompressed)
  87. require.NoError(t, err)
  88. // ensure we can read the whole thing with no error, before writing back out
  89. buf, err := ioutil.ReadAll(fh)
  90. require.NoError(t, err)
  91. bRdr := bytes.NewReader(buf)
  92. err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed})
  93. require.NoError(t, err)
  94. i1, err = getInode(filepath.Join(dest, "1"))
  95. require.NoError(t, err)
  96. i2, err = getInode(filepath.Join(dest, "2"))
  97. require.NoError(t, err)
  98. assert.Equal(t, i1, i2)
  99. }
  100. func TestTarWithHardLinkAndRebase(t *testing.T) {
  101. tmpDir, err := ioutil.TempDir("", "docker-test-tar-hardlink-rebase")
  102. require.NoError(t, err)
  103. defer os.RemoveAll(tmpDir)
  104. origin := filepath.Join(tmpDir, "origin")
  105. err = os.Mkdir(origin, 0700)
  106. require.NoError(t, err)
  107. err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
  108. require.NoError(t, err)
  109. err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2"))
  110. require.NoError(t, err)
  111. var i1, i2 uint64
  112. i1, err = getNlink(filepath.Join(origin, "1"))
  113. require.NoError(t, err)
  114. // sanity check that we can hardlink
  115. if i1 != 2 {
  116. t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1)
  117. }
  118. dest := filepath.Join(tmpDir, "dest")
  119. bRdr, err := TarResourceRebase(origin, "origin")
  120. require.NoError(t, err)
  121. dstDir, srcBase := SplitPathDirEntry(origin)
  122. _, dstBase := SplitPathDirEntry(dest)
  123. content := RebaseArchiveEntries(bRdr, srcBase, dstBase)
  124. err = Untar(content, dstDir, &TarOptions{Compression: Uncompressed, NoLchown: true, NoOverwriteDirNonDir: true})
  125. require.NoError(t, err)
  126. i1, err = getInode(filepath.Join(dest, "1"))
  127. require.NoError(t, err)
  128. i2, err = getInode(filepath.Join(dest, "2"))
  129. require.NoError(t, err)
  130. assert.Equal(t, i1, i2)
  131. }
  132. func getNlink(path string) (uint64, error) {
  133. stat, err := os.Stat(path)
  134. if err != nil {
  135. return 0, err
  136. }
  137. statT, ok := stat.Sys().(*syscall.Stat_t)
  138. if !ok {
  139. return 0, fmt.Errorf("expected type *syscall.Stat_t, got %t", stat.Sys())
  140. }
  141. // We need this conversion on ARM64
  142. return uint64(statT.Nlink), nil
  143. }
  144. func getInode(path string) (uint64, error) {
  145. stat, err := os.Stat(path)
  146. if err != nil {
  147. return 0, err
  148. }
  149. statT, ok := stat.Sys().(*syscall.Stat_t)
  150. if !ok {
  151. return 0, fmt.Errorf("expected type *syscall.Stat_t, got %t", stat.Sys())
  152. }
  153. return statT.Ino, nil
  154. }
  155. func TestTarWithBlockCharFifo(t *testing.T) {
  156. origin, err := ioutil.TempDir("", "docker-test-tar-hardlink")
  157. require.NoError(t, err)
  158. defer os.RemoveAll(origin)
  159. err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
  160. require.NoError(t, err)
  161. err = system.Mknod(filepath.Join(origin, "2"), unix.S_IFBLK, int(system.Mkdev(int64(12), int64(5))))
  162. require.NoError(t, err)
  163. err = system.Mknod(filepath.Join(origin, "3"), unix.S_IFCHR, int(system.Mkdev(int64(12), int64(5))))
  164. require.NoError(t, err)
  165. err = system.Mknod(filepath.Join(origin, "4"), unix.S_IFIFO, int(system.Mkdev(int64(12), int64(5))))
  166. require.NoError(t, err)
  167. dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest")
  168. require.NoError(t, err)
  169. defer os.RemoveAll(dest)
  170. // we'll do this in two steps to separate failure
  171. fh, err := Tar(origin, Uncompressed)
  172. require.NoError(t, err)
  173. // ensure we can read the whole thing with no error, before writing back out
  174. buf, err := ioutil.ReadAll(fh)
  175. require.NoError(t, err)
  176. bRdr := bytes.NewReader(buf)
  177. err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed})
  178. require.NoError(t, err)
  179. changes, err := ChangesDirs(origin, dest)
  180. require.NoError(t, err)
  181. if len(changes) > 0 {
  182. t.Fatalf("Tar with special device (block, char, fifo) should keep them (recreate them when untar) : %v", changes)
  183. }
  184. }
  185. // TestTarUntarWithXattr is Unix as Lsetxattr is not supported on Windows
  186. func TestTarUntarWithXattr(t *testing.T) {
  187. if runtime.GOOS == "solaris" {
  188. t.Skip()
  189. }
  190. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  191. require.NoError(t, err)
  192. defer os.RemoveAll(origin)
  193. err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700)
  194. require.NoError(t, err)
  195. err = ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700)
  196. require.NoError(t, err)
  197. err = ioutil.WriteFile(filepath.Join(origin, "3"), []byte("will be ignored"), 0700)
  198. require.NoError(t, err)
  199. err = system.Lsetxattr(filepath.Join(origin, "2"), "security.capability", []byte{0x00}, 0)
  200. require.NoError(t, err)
  201. for _, c := range []Compression{
  202. Uncompressed,
  203. Gzip,
  204. } {
  205. changes, err := tarUntar(t, origin, &TarOptions{
  206. Compression: c,
  207. ExcludePatterns: []string{"3"},
  208. })
  209. if err != nil {
  210. t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
  211. }
  212. if len(changes) != 1 || changes[0].Path != "/3" {
  213. t.Fatalf("Unexpected differences after tarUntar: %v", changes)
  214. }
  215. capability, _ := system.Lgetxattr(filepath.Join(origin, "2"), "security.capability")
  216. if capability == nil && capability[0] != 0x00 {
  217. t.Fatalf("Untar should have kept the 'security.capability' xattr.")
  218. }
  219. }
  220. }
  221. func TestCopyInfoDestinationPathSymlink(t *testing.T) {
  222. tmpDir, _ := getTestTempDirs(t)
  223. defer removeAllPaths(tmpDir)
  224. root := strings.TrimRight(tmpDir, "/") + "/"
  225. type FileTestData struct {
  226. resource FileData
  227. file string
  228. expected CopyInfo
  229. }
  230. testData := []FileTestData{
  231. //Create a directory: /tmp/archive-copy-test*/dir1
  232. //Test will "copy" file1 to dir1
  233. {resource: FileData{filetype: Dir, path: "dir1", permissions: 0740}, file: "file1", expected: CopyInfo{Path: root + "dir1/file1", Exists: false, IsDir: false}},
  234. //Create a symlink directory to dir1: /tmp/archive-copy-test*/dirSymlink -> dir1
  235. //Test will "copy" file2 to dirSymlink
  236. {resource: FileData{filetype: Symlink, path: "dirSymlink", contents: root + "dir1", permissions: 0600}, file: "file2", expected: CopyInfo{Path: root + "dirSymlink/file2", Exists: false, IsDir: false}},
  237. //Create a file in tmp directory: /tmp/archive-copy-test*/file1
  238. //Test to cover when the full file path already exists.
  239. {resource: FileData{filetype: Regular, path: "file1", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "file1", Exists: true}},
  240. //Create a directory: /tmp/archive-copy*/dir2
  241. //Test to cover when the full directory path already exists
  242. {resource: FileData{filetype: Dir, path: "dir2", permissions: 0740}, file: "", expected: CopyInfo{Path: root + "dir2", Exists: true, IsDir: true}},
  243. //Create a symlink to a non-existent target: /tmp/archive-copy*/symlink1 -> noSuchTarget
  244. //Negative test to cover symlinking to a target that does not exit
  245. {resource: FileData{filetype: Symlink, path: "symlink1", contents: "noSuchTarget", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "noSuchTarget", Exists: false}},
  246. //Create a file in tmp directory for next test: /tmp/existingfile
  247. {resource: FileData{filetype: Regular, path: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}},
  248. //Create a symlink to an existing file: /tmp/archive-copy*/symlink2 -> /tmp/existingfile
  249. //Test to cover when the parent directory of a new file is a symlink
  250. {resource: FileData{filetype: Symlink, path: "symlink2", contents: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}},
  251. }
  252. var dirs []FileData
  253. for _, data := range testData {
  254. dirs = append(dirs, data.resource)
  255. }
  256. provisionSampleDir(t, tmpDir, dirs)
  257. for _, info := range testData {
  258. p := filepath.Join(tmpDir, info.resource.path, info.file)
  259. ci, err := CopyInfoDestinationPath(p)
  260. assert.NoError(t, err)
  261. assert.Equal(t, info.expected, ci)
  262. }
  263. }