package fileutils // import "github.com/docker/docker/pkg/fileutils" import ( "os" "path" "path/filepath" "runtime" "testing" ) // CopyFile with invalid src func TestCopyFileWithInvalidSrc(t *testing.T) { bytes, err := CopyFile("/invalid/file/path", path.Join(t.TempDir(), "dest")) if err == nil { t.Fatal("Should have fail to copy an invalid src file") } if bytes != 0 { t.Fatal("Should have written 0 bytes") } } // CopyFile with invalid dest func TestCopyFileWithInvalidDest(t *testing.T) { tempFolder := t.TempDir() src := path.Join(tempFolder, "file") err := os.WriteFile(src, []byte("content"), 0o740) if err != nil { t.Fatal(err) } bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path")) if err == nil { t.Fatal("Should have fail to copy an invalid src file") } if bytes != 0 { t.Fatal("Should have written 0 bytes") } } // CopyFile with same src and dest func TestCopyFileWithSameSrcAndDest(t *testing.T) { file := path.Join(t.TempDir(), "file") err := os.WriteFile(file, []byte("content"), 0o740) if err != nil { t.Fatal(err) } bytes, err := CopyFile(file, file) if err != nil { t.Fatal(err) } if bytes != 0 { t.Fatal("Should have written 0 bytes as it is the same file.") } } // CopyFile with same src and dest but path is different and not clean func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) { testFolder := path.Join(t.TempDir(), "test") err := os.Mkdir(testFolder, 0o740) if err != nil { t.Fatal(err) } file := path.Join(testFolder, "file") sameFile := testFolder + "/../test/file" err = os.WriteFile(file, []byte("content"), 0o740) if err != nil { t.Fatal(err) } bytes, err := CopyFile(file, sameFile) if err != nil { t.Fatal(err) } if bytes != 0 { t.Fatal("Should have written 0 bytes as it is the same file.") } } func TestCopyFile(t *testing.T) { tempFolder := t.TempDir() src := path.Join(tempFolder, "src") dest := path.Join(tempFolder, "dest") err := os.WriteFile(src, []byte("content"), 0o777) if err != nil { t.Error(err) } err = os.WriteFile(dest, []byte("destContent"), 0o777) if err != nil { t.Error(err) } bytes, err := CopyFile(src, dest) if err != nil { t.Fatal(err) } if bytes != 7 { t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes) } actual, err := os.ReadFile(dest) if err != nil { t.Fatal(err) } if string(actual) != "content" { t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content") } } // Reading a symlink to a directory must return the directory func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) { // TODO Windows: Port this test if runtime.GOOS == "windows" { t.Skip("Needs porting to Windows") } var err error if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0o777); err != nil { t.Errorf("failed to create directory: %s", err) } if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil { t.Errorf("failed to create symlink: %s", err) } var symlinkedPath string if symlinkedPath, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil { t.Fatalf("failed to read symlink to directory: %s", err) } if symlinkedPath != "/tmp/testReadSymlinkToExistingDirectory" { t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath) } if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil { t.Errorf("failed to remove temporary directory: %s", err) } if err = os.Remove("/tmp/dirLinkTest"); err != nil { t.Errorf("failed to remove symlink: %s", err) } } // Reading a non-existing symlink must fail func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) { symLinkedPath, err := ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath") if err == nil { t.Fatalf("error expected for non-existing symlink") } if symLinkedPath != "" { t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath) } } // Reading a symlink to a file must fail func TestReadSymlinkedDirectoryToFile(t *testing.T) { // TODO Windows: Port this test if runtime.GOOS == "windows" { t.Skip("Needs porting to Windows") } var err error var file *os.File // #nosec G303 if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil { t.Fatalf("failed to create file: %s", err) } file.Close() if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil { t.Errorf("failed to create symlink: %s", err) } symlinkedPath, err := ReadSymlinkedDirectory("/tmp/fileLinkTest") if err == nil { t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed") } if symlinkedPath != "" { t.Fatalf("path should've been empty: %s", symlinkedPath) } if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil { t.Errorf("failed to remove file: %s", err) } if err = os.Remove("/tmp/fileLinkTest"); err != nil { t.Errorf("failed to remove symlink: %s", err) } } func TestCreateIfNotExistsDir(t *testing.T) { folderToCreate := filepath.Join(t.TempDir(), "tocreate") if err := CreateIfNotExists(folderToCreate, true); err != nil { t.Fatal(err) } fileinfo, err := os.Stat(folderToCreate) if err != nil { t.Fatalf("Should have create a folder, got %v", err) } if !fileinfo.IsDir() { t.Fatalf("Should have been a dir, seems it's not") } } func TestCreateIfNotExistsFile(t *testing.T) { fileToCreate := filepath.Join(t.TempDir(), "file/to/create") if err := CreateIfNotExists(fileToCreate, false); err != nil { t.Fatal(err) } fileinfo, err := os.Stat(fileToCreate) if err != nil { t.Fatalf("Should have create a file, got %v", err) } if fileinfo.IsDir() { t.Fatalf("Should have been a file, seems it's not") } }