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