archive_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package chrootarchive // import "github.com/docker/docker/pkg/chrootarchive"
  2. import (
  3. "bytes"
  4. "fmt"
  5. "hash/crc32"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/pkg/archive"
  14. "github.com/docker/docker/pkg/idtools"
  15. "gotest.tools/v3/skip"
  16. )
  17. var chrootArchiver = NewArchiver(idtools.IdentityMapping{})
  18. func TarUntar(src, dst string) error {
  19. return chrootArchiver.TarUntar(src, dst)
  20. }
  21. func CopyFileWithTar(src, dst string) (err error) {
  22. return chrootArchiver.CopyFileWithTar(src, dst)
  23. }
  24. func UntarPath(src, dst string) error {
  25. return chrootArchiver.UntarPath(src, dst)
  26. }
  27. func CopyWithTar(src, dst string) error {
  28. return chrootArchiver.CopyWithTar(src, dst)
  29. }
  30. func TestChrootTarUntar(t *testing.T) {
  31. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  32. tmpdir := t.TempDir()
  33. src := filepath.Join(tmpdir, "src")
  34. if err := os.Mkdir(src, 0o700); err != nil {
  35. t.Fatal(err)
  36. }
  37. if err := os.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0o644); err != nil {
  38. t.Fatal(err)
  39. }
  40. if err := os.WriteFile(filepath.Join(src, "lolo"), []byte("hello lolo"), 0o644); err != nil {
  41. t.Fatal(err)
  42. }
  43. stream, err := archive.Tar(src, archive.Uncompressed)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. dest := filepath.Join(tmpdir, "dest")
  48. if err := os.Mkdir(dest, 0o700); err != nil {
  49. t.Fatal(err)
  50. }
  51. if err := Untar(stream, dest, &archive.TarOptions{ExcludePatterns: []string{"lolo"}}); err != nil {
  52. t.Fatal(err)
  53. }
  54. }
  55. // gh#10426: Verify the fix for having a huge excludes list (like on `docker load` with large # of
  56. // local images)
  57. func TestChrootUntarWithHugeExcludesList(t *testing.T) {
  58. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  59. tmpdir := t.TempDir()
  60. src := filepath.Join(tmpdir, "src")
  61. if err := os.Mkdir(src, 0o700); err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := os.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0o644); err != nil {
  65. t.Fatal(err)
  66. }
  67. stream, err := archive.Tar(src, archive.Uncompressed)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. dest := filepath.Join(tmpdir, "dest")
  72. if err := os.Mkdir(dest, 0o700); err != nil {
  73. t.Fatal(err)
  74. }
  75. options := &archive.TarOptions{}
  76. // 65534 entries of 64-byte strings ~= 4MB of environment space which should overflow
  77. // on most systems when passed via environment or command line arguments
  78. excludes := make([]string, 65534)
  79. var i rune
  80. for i = 0; i < 65534; i++ {
  81. excludes[i] = strings.Repeat(string(i), 64)
  82. }
  83. options.ExcludePatterns = excludes
  84. if err := Untar(stream, dest, options); err != nil {
  85. t.Fatal(err)
  86. }
  87. }
  88. func TestChrootUntarEmptyArchive(t *testing.T) {
  89. if err := Untar(nil, t.TempDir(), nil); err == nil {
  90. t.Fatal("expected error on empty archive")
  91. }
  92. }
  93. func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks bool) (int, error) {
  94. fileData := []byte("fooo")
  95. for n := 0; n < numberOfFiles; n++ {
  96. fileName := fmt.Sprintf("file-%d", n)
  97. if err := os.WriteFile(filepath.Join(targetPath, fileName), fileData, 0o700); err != nil {
  98. return 0, err
  99. }
  100. if makeSymLinks {
  101. if err := os.Symlink(filepath.Join(targetPath, fileName), filepath.Join(targetPath, fileName+"-link")); err != nil {
  102. return 0, err
  103. }
  104. }
  105. }
  106. totalSize := numberOfFiles * len(fileData)
  107. return totalSize, nil
  108. }
  109. func getHash(filename string) (uint32, error) {
  110. stream, err := os.ReadFile(filename)
  111. if err != nil {
  112. return 0, err
  113. }
  114. hash := crc32.NewIEEE()
  115. hash.Write(stream)
  116. return hash.Sum32(), nil
  117. }
  118. func compareDirectories(src string, dest string) error {
  119. changes, err := archive.ChangesDirs(dest, src)
  120. if err != nil {
  121. return err
  122. }
  123. if len(changes) > 0 {
  124. return fmt.Errorf("Unexpected differences after untar: %v", changes)
  125. }
  126. return nil
  127. }
  128. func compareFiles(src string, dest string) error {
  129. srcHash, err := getHash(src)
  130. if err != nil {
  131. return err
  132. }
  133. destHash, err := getHash(dest)
  134. if err != nil {
  135. return err
  136. }
  137. if srcHash != destHash {
  138. return fmt.Errorf("%s is different from %s", src, dest)
  139. }
  140. return nil
  141. }
  142. func TestChrootTarUntarWithSymlink(t *testing.T) {
  143. skip.If(t, runtime.GOOS == "windows", "FIXME: figure out why this is failing")
  144. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  145. tmpdir := t.TempDir()
  146. src := filepath.Join(tmpdir, "src")
  147. if err := os.Mkdir(src, 0o700); err != nil {
  148. t.Fatal(err)
  149. }
  150. if _, err := prepareSourceDirectory(10, src, false); err != nil {
  151. t.Fatal(err)
  152. }
  153. dest := filepath.Join(tmpdir, "dest")
  154. if err := TarUntar(src, dest); err != nil {
  155. t.Fatal(err)
  156. }
  157. if err := compareDirectories(src, dest); err != nil {
  158. t.Fatal(err)
  159. }
  160. }
  161. func TestChrootCopyWithTar(t *testing.T) {
  162. skip.If(t, runtime.GOOS == "windows", "FIXME: figure out why this is failing")
  163. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  164. tmpdir := t.TempDir()
  165. src := filepath.Join(tmpdir, "src")
  166. if err := os.Mkdir(src, 0o700); err != nil {
  167. t.Fatal(err)
  168. }
  169. if _, err := prepareSourceDirectory(10, src, true); err != nil {
  170. t.Fatal(err)
  171. }
  172. // Copy directory
  173. dest := filepath.Join(tmpdir, "dest")
  174. if err := CopyWithTar(src, dest); err != nil {
  175. t.Fatal(err)
  176. }
  177. if err := compareDirectories(src, dest); err != nil {
  178. t.Fatal(err)
  179. }
  180. // Copy file
  181. srcfile := filepath.Join(src, "file-1")
  182. dest = filepath.Join(tmpdir, "destFile")
  183. destfile := filepath.Join(dest, "file-1")
  184. if err := CopyWithTar(srcfile, destfile); err != nil {
  185. t.Fatal(err)
  186. }
  187. if err := compareFiles(srcfile, destfile); err != nil {
  188. t.Fatal(err)
  189. }
  190. // Copy symbolic link
  191. srcLinkfile := filepath.Join(src, "file-1-link")
  192. dest = filepath.Join(tmpdir, "destSymlink")
  193. destLinkfile := filepath.Join(dest, "file-1-link")
  194. if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
  195. t.Fatal(err)
  196. }
  197. if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
  198. t.Fatal(err)
  199. }
  200. }
  201. func TestChrootCopyFileWithTar(t *testing.T) {
  202. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  203. tmpdir := t.TempDir()
  204. src := filepath.Join(tmpdir, "src")
  205. if err := os.Mkdir(src, 0o700); err != nil {
  206. t.Fatal(err)
  207. }
  208. if _, err := prepareSourceDirectory(10, src, true); err != nil {
  209. t.Fatal(err)
  210. }
  211. // Copy directory
  212. dest := filepath.Join(tmpdir, "dest")
  213. if err := CopyFileWithTar(src, dest); err == nil {
  214. t.Fatal("Expected error on copying directory")
  215. }
  216. // Copy file
  217. srcfile := filepath.Join(src, "file-1")
  218. dest = filepath.Join(tmpdir, "destFile")
  219. destfile := filepath.Join(dest, "file-1")
  220. if err := CopyFileWithTar(srcfile, destfile); err != nil {
  221. t.Fatal(err)
  222. }
  223. if err := compareFiles(srcfile, destfile); err != nil {
  224. t.Fatal(err)
  225. }
  226. // Copy symbolic link
  227. srcLinkfile := filepath.Join(src, "file-1-link")
  228. dest = filepath.Join(tmpdir, "destSymlink")
  229. destLinkfile := filepath.Join(dest, "file-1-link")
  230. if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
  231. t.Fatal(err)
  232. }
  233. if err := compareFiles(srcLinkfile, destLinkfile); err != nil {
  234. t.Fatal(err)
  235. }
  236. }
  237. func TestChrootUntarPath(t *testing.T) {
  238. skip.If(t, runtime.GOOS == "windows", "FIXME: figure out why this is failing")
  239. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  240. tmpdir := t.TempDir()
  241. src := filepath.Join(tmpdir, "src")
  242. if err := os.Mkdir(src, 0o700); err != nil {
  243. t.Fatal(err)
  244. }
  245. if _, err := prepareSourceDirectory(10, src, false); err != nil {
  246. t.Fatal(err)
  247. }
  248. dest := filepath.Join(tmpdir, "dest")
  249. // Untar a directory
  250. if err := UntarPath(src, dest); err == nil {
  251. t.Fatal("Expected error on untaring a directory")
  252. }
  253. // Untar a tar file
  254. stream, err := archive.Tar(src, archive.Uncompressed)
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. buf := new(bytes.Buffer)
  259. buf.ReadFrom(stream)
  260. tarfile := filepath.Join(tmpdir, "src.tar")
  261. if err := os.WriteFile(tarfile, buf.Bytes(), 0o644); err != nil {
  262. t.Fatal(err)
  263. }
  264. if err := UntarPath(tarfile, dest); err != nil {
  265. t.Fatal(err)
  266. }
  267. if err := compareDirectories(src, dest); err != nil {
  268. t.Fatal(err)
  269. }
  270. }
  271. type slowEmptyTarReader struct {
  272. size int
  273. offset int
  274. chunkSize int
  275. }
  276. // Read is a slow reader of an empty tar (like the output of "tar c --files-from /dev/null")
  277. func (s *slowEmptyTarReader) Read(p []byte) (int, error) {
  278. time.Sleep(100 * time.Millisecond)
  279. count := s.chunkSize
  280. if len(p) < s.chunkSize {
  281. count = len(p)
  282. }
  283. for i := 0; i < count; i++ {
  284. p[i] = 0
  285. }
  286. s.offset += count
  287. if s.offset > s.size {
  288. return count, io.EOF
  289. }
  290. return count, nil
  291. }
  292. func TestChrootUntarEmptyArchiveFromSlowReader(t *testing.T) {
  293. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  294. tmpdir := t.TempDir()
  295. dest := filepath.Join(tmpdir, "dest")
  296. if err := os.Mkdir(dest, 0o700); err != nil {
  297. t.Fatal(err)
  298. }
  299. stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
  300. if err := Untar(stream, dest, nil); err != nil {
  301. t.Fatal(err)
  302. }
  303. }
  304. func TestChrootApplyEmptyArchiveFromSlowReader(t *testing.T) {
  305. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  306. tmpdir := t.TempDir()
  307. dest := filepath.Join(tmpdir, "dest")
  308. if err := os.Mkdir(dest, 0o700); err != nil {
  309. t.Fatal(err)
  310. }
  311. stream := &slowEmptyTarReader{size: 10240, chunkSize: 1024}
  312. if _, err := ApplyLayer(dest, stream); err != nil {
  313. t.Fatal(err)
  314. }
  315. }
  316. func TestChrootApplyDotDotFile(t *testing.T) {
  317. skip.If(t, os.Getuid() != 0, "skipping test that requires root")
  318. tmpdir := t.TempDir()
  319. src := filepath.Join(tmpdir, "src")
  320. if err := os.Mkdir(src, 0o700); err != nil {
  321. t.Fatal(err)
  322. }
  323. if err := os.WriteFile(filepath.Join(src, "..gitme"), []byte(""), 0o644); err != nil {
  324. t.Fatal(err)
  325. }
  326. stream, err := archive.Tar(src, archive.Uncompressed)
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. dest := filepath.Join(tmpdir, "dest")
  331. if err := os.Mkdir(dest, 0o700); err != nil {
  332. t.Fatal(err)
  333. }
  334. if _, err := ApplyLayer(dest, stream); err != nil {
  335. t.Fatal(err)
  336. }
  337. }