github.com/rumpl/bof@v23.0.0-rc.2+incompatible/pkg/fileutils/fileutils_test.go (about)

     1  package fileutils // import "github.com/docker/docker/pkg/fileutils"
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  )
    10  
    11  // CopyFile with invalid src
    12  func TestCopyFileWithInvalidSrc(t *testing.T) {
    13  	bytes, err := CopyFile("/invalid/file/path", path.Join(t.TempDir(), "dest"))
    14  	if err == nil {
    15  		t.Fatal("Should have fail to copy an invalid src file")
    16  	}
    17  	if bytes != 0 {
    18  		t.Fatal("Should have written 0 bytes")
    19  	}
    20  }
    21  
    22  // CopyFile with invalid dest
    23  func TestCopyFileWithInvalidDest(t *testing.T) {
    24  	tempFolder := t.TempDir()
    25  	src := path.Join(tempFolder, "file")
    26  	err := os.WriteFile(src, []byte("content"), 0o740)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
    31  	if err == nil {
    32  		t.Fatal("Should have fail to copy an invalid src file")
    33  	}
    34  	if bytes != 0 {
    35  		t.Fatal("Should have written 0 bytes")
    36  	}
    37  }
    38  
    39  // CopyFile with same src and dest
    40  func TestCopyFileWithSameSrcAndDest(t *testing.T) {
    41  	file := path.Join(t.TempDir(), "file")
    42  	err := os.WriteFile(file, []byte("content"), 0o740)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	bytes, err := CopyFile(file, file)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if bytes != 0 {
    51  		t.Fatal("Should have written 0 bytes as it is the same file.")
    52  	}
    53  }
    54  
    55  // CopyFile with same src and dest but path is different and not clean
    56  func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
    57  	testFolder := path.Join(t.TempDir(), "test")
    58  	err := os.Mkdir(testFolder, 0o740)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	file := path.Join(testFolder, "file")
    63  	sameFile := testFolder + "/../test/file"
    64  	err = os.WriteFile(file, []byte("content"), 0o740)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	bytes, err := CopyFile(file, sameFile)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if bytes != 0 {
    73  		t.Fatal("Should have written 0 bytes as it is the same file.")
    74  	}
    75  }
    76  
    77  func TestCopyFile(t *testing.T) {
    78  	tempFolder := t.TempDir()
    79  	src := path.Join(tempFolder, "src")
    80  	dest := path.Join(tempFolder, "dest")
    81  	err := os.WriteFile(src, []byte("content"), 0o777)
    82  	if err != nil {
    83  		t.Error(err)
    84  	}
    85  	err = os.WriteFile(dest, []byte("destContent"), 0o777)
    86  	if err != nil {
    87  		t.Error(err)
    88  	}
    89  	bytes, err := CopyFile(src, dest)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if bytes != 7 {
    94  		t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
    95  	}
    96  	actual, err := os.ReadFile(dest)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	if string(actual) != "content" {
   101  		t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
   102  	}
   103  }
   104  
   105  // Reading a symlink to a directory must return the directory
   106  func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
   107  	// TODO Windows: Port this test
   108  	if runtime.GOOS == "windows" {
   109  		t.Skip("Needs porting to Windows")
   110  	}
   111  	var err error
   112  	if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0o777); err != nil {
   113  		t.Errorf("failed to create directory: %s", err)
   114  	}
   115  
   116  	if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
   117  		t.Errorf("failed to create symlink: %s", err)
   118  	}
   119  
   120  	var symlinkedPath string
   121  	if symlinkedPath, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
   122  		t.Fatalf("failed to read symlink to directory: %s", err)
   123  	}
   124  
   125  	if symlinkedPath != "/tmp/testReadSymlinkToExistingDirectory" {
   126  		t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath)
   127  	}
   128  
   129  	if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
   130  		t.Errorf("failed to remove temporary directory: %s", err)
   131  	}
   132  
   133  	if err = os.Remove("/tmp/dirLinkTest"); err != nil {
   134  		t.Errorf("failed to remove symlink: %s", err)
   135  	}
   136  }
   137  
   138  // Reading a non-existing symlink must fail
   139  func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
   140  	symLinkedPath, err := ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath")
   141  	if err == nil {
   142  		t.Fatalf("error expected for non-existing symlink")
   143  	}
   144  
   145  	if symLinkedPath != "" {
   146  		t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath)
   147  	}
   148  }
   149  
   150  // Reading a symlink to a file must fail
   151  func TestReadSymlinkedDirectoryToFile(t *testing.T) {
   152  	// TODO Windows: Port this test
   153  	if runtime.GOOS == "windows" {
   154  		t.Skip("Needs porting to Windows")
   155  	}
   156  	var err error
   157  	var file *os.File
   158  
   159  	// #nosec G303
   160  	if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
   161  		t.Fatalf("failed to create file: %s", err)
   162  	}
   163  
   164  	file.Close()
   165  
   166  	if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
   167  		t.Errorf("failed to create symlink: %s", err)
   168  	}
   169  
   170  	symlinkedPath, err := ReadSymlinkedDirectory("/tmp/fileLinkTest")
   171  	if err == nil {
   172  		t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
   173  	}
   174  
   175  	if symlinkedPath != "" {
   176  		t.Fatalf("path should've been empty: %s", symlinkedPath)
   177  	}
   178  
   179  	if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
   180  		t.Errorf("failed to remove file: %s", err)
   181  	}
   182  
   183  	if err = os.Remove("/tmp/fileLinkTest"); err != nil {
   184  		t.Errorf("failed to remove symlink: %s", err)
   185  	}
   186  }
   187  
   188  func TestCreateIfNotExistsDir(t *testing.T) {
   189  	folderToCreate := filepath.Join(t.TempDir(), "tocreate")
   190  
   191  	if err := CreateIfNotExists(folderToCreate, true); err != nil {
   192  		t.Fatal(err)
   193  	}
   194  	fileinfo, err := os.Stat(folderToCreate)
   195  	if err != nil {
   196  		t.Fatalf("Should have create a folder, got %v", err)
   197  	}
   198  
   199  	if !fileinfo.IsDir() {
   200  		t.Fatalf("Should have been a dir, seems it's not")
   201  	}
   202  }
   203  
   204  func TestCreateIfNotExistsFile(t *testing.T) {
   205  	fileToCreate := filepath.Join(t.TempDir(), "file/to/create")
   206  
   207  	if err := CreateIfNotExists(fileToCreate, false); err != nil {
   208  		t.Fatal(err)
   209  	}
   210  	fileinfo, err := os.Stat(fileToCreate)
   211  	if err != nil {
   212  		t.Fatalf("Should have create a file, got %v", err)
   213  	}
   214  
   215  	if fileinfo.IsDir() {
   216  		t.Fatalf("Should have been a file, seems it's not")
   217  	}
   218  }