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