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