archive_test.go 9.7 KB

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