| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package osfs |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"runtime" |
| 27 |
"strings" |
| 28 |
"testing" |
| 29 |
|
| 30 |
"github.com/go-git/go-billy/v5" |
| 31 |
"github.com/onsi/gomega" |
| 32 |
) |
| 33 |
|
| 34 |
func TestOpen(t *testing.T) { |
| 35 |
tests := []struct { |
| 36 |
name string |
| 37 |
filename string |
| 38 |
makeAbs bool |
| 39 |
before func(dir string) billy.Filesystem |
| 40 |
wantErr string |
| 41 |
}{ |
| 42 |
{ |
| 43 |
name: "file: rel same dir", |
| 44 |
before: func(dir string) billy.Filesystem { |
| 45 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 46 |
return newBoundOS(dir, true) |
| 47 |
}, |
| 48 |
filename: "test-file", |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "file: dot rel same dir", |
| 52 |
before: func(dir string) billy.Filesystem { |
| 53 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 54 |
return newBoundOS(dir, true) |
| 55 |
}, |
| 56 |
filename: "./test-file", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "file: dot rel same dir without deduplication", |
| 60 |
before: func(dir string) billy.Filesystem { |
| 61 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 62 |
return newBoundOS(dir, false) |
| 63 |
}, |
| 64 |
filename: "./test-file", |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "file: rel path to above cwd", |
| 68 |
before: func(dir string) billy.Filesystem { |
| 69 |
os.WriteFile(filepath.Join(dir, "rel-above-cwd"), []byte("anything"), 0o600) |
| 70 |
return newBoundOS(dir, true) |
| 71 |
}, |
| 72 |
filename: "../../rel-above-cwd", |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "file: rel path to below cwd", |
| 76 |
before: func(dir string) billy.Filesystem { |
| 77 |
os.Mkdir(filepath.Join(dir, "sub"), 0o700) |
| 78 |
os.WriteFile(filepath.Join(dir, "sub/rel-below-cwd"), []byte("anything"), 0o600) |
| 79 |
return newBoundOS(dir, true) |
| 80 |
}, |
| 81 |
filename: "sub/rel-below-cwd", |
| 82 |
}, |
| 83 |
{ |
| 84 |
name: "file: abs inside cwd", |
| 85 |
before: func(dir string) billy.Filesystem { |
| 86 |
os.WriteFile(filepath.Join(dir, "abs-test-file"), []byte("anything"), 0o600) |
| 87 |
return newBoundOS(dir, true) |
| 88 |
}, |
| 89 |
filename: "abs-test-file", |
| 90 |
makeAbs: true, |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "file: abs outside cwd", |
| 94 |
before: func(dir string) billy.Filesystem { |
| 95 |
return newBoundOS(dir, true) |
| 96 |
}, |
| 97 |
filename: "/some/path/outside/cwd", |
| 98 |
wantErr: notFoundError(), |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "symlink: same dir", |
| 102 |
before: func(dir string) billy.Filesystem { |
| 103 |
target := filepath.Join(dir, "target-file") |
| 104 |
os.WriteFile(target, []byte("anything"), 0o600) |
| 105 |
os.Symlink(target, filepath.Join(dir, "symlink")) |
| 106 |
return newBoundOS(dir, true) |
| 107 |
}, |
| 108 |
filename: "symlink", |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "symlink: rel outside cwd", |
| 112 |
before: func(dir string) billy.Filesystem { |
| 113 |
os.Symlink("../../../../../../outside/cwd", filepath.Join(dir, "symlink")) |
| 114 |
return newBoundOS(dir, true) |
| 115 |
}, |
| 116 |
filename: "symlink", |
| 117 |
makeAbs: true, |
| 118 |
wantErr: notFoundError(), |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "symlink: abs outside cwd", |
| 122 |
before: func(dir string) billy.Filesystem { |
| 123 |
os.Symlink("/some/path/outside/cwd", filepath.Join(dir, "symlink")) |
| 124 |
return newBoundOS(dir, true) |
| 125 |
}, |
| 126 |
filename: "symlink", |
| 127 |
makeAbs: true, |
| 128 |
wantErr: notFoundError(), |
| 129 |
}, |
| 130 |
} |
| 131 |
for _, tt := range tests { |
| 132 |
t.Run(tt.name, func(t *testing.T) { |
| 133 |
g := gomega.NewWithT(t) |
| 134 |
dir := t.TempDir() |
| 135 |
fs := newBoundOS(dir, true) |
| 136 |
|
| 137 |
if tt.before != nil { |
| 138 |
fs = tt.before(dir) |
| 139 |
} |
| 140 |
|
| 141 |
filename := tt.filename |
| 142 |
if tt.makeAbs { |
| 143 |
filename = filepath.Join(dir, filename) |
| 144 |
} |
| 145 |
|
| 146 |
fi, err := fs.Open(filename) |
| 147 |
if tt.wantErr != "" { |
| 148 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 149 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 150 |
g.Expect(fi).To(gomega.BeNil()) |
| 151 |
} else { |
| 152 |
g.Expect(err).To(gomega.BeNil()) |
| 153 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 154 |
g.Expect(fi.Close()).To(gomega.Succeed()) |
| 155 |
} |
| 156 |
}) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestOpenPreservesBackslashFilenamesOnNonWindows(t *testing.T) { |
| 161 |
if filepath.Separator == '\\' { |
| 162 |
t.Skip("backslash is a path separator on windows") |
| 163 |
} |
| 164 |
|
| 165 |
dir := t.TempDir() |
| 166 |
fs := newBoundOS(dir, true) |
| 167 |
|
| 168 |
tests := []struct { |
| 169 |
filename string |
| 170 |
openPath string |
| 171 |
}{ |
| 172 |
{filename: `.\test-file`, openPath: `.\test-file`}, |
| 173 |
{filename: `\test-file`, openPath: `./\test-file`}, |
| 174 |
} |
| 175 |
for _, tt := range tests { |
| 176 |
t.Run(tt.openPath, func(t *testing.T) { |
| 177 |
g := gomega.NewWithT(t) |
| 178 |
err := os.WriteFile(filepath.Join(dir, tt.filename), []byte("anything"), 0o600) |
| 179 |
g.Expect(err).To(gomega.Succeed()) |
| 180 |
|
| 181 |
fi, err := fs.Open(tt.openPath) |
| 182 |
g.Expect(err).To(gomega.BeNil()) |
| 183 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 184 |
g.Expect(fi.Close()).To(gomega.Succeed()) |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func Test_Symlink(t *testing.T) { |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
defer umask(0)() |
| 195 |
|
| 196 |
tests := []struct { |
| 197 |
name string |
| 198 |
link string |
| 199 |
target string |
| 200 |
before func(dir string) billy.Filesystem |
| 201 |
wantStatErr string |
| 202 |
}{ |
| 203 |
{ |
| 204 |
name: "link to abs valid target", |
| 205 |
link: "symlink", |
| 206 |
target: filepath.FromSlash("/etc/passwd"), |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "dot link to abs valid target", |
| 210 |
link: "./symlink", |
| 211 |
target: filepath.FromSlash("/etc/passwd"), |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "link to abs inexistent target", |
| 215 |
link: "symlink", |
| 216 |
target: filepath.FromSlash("/some/random/path"), |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "link to rel valid target", |
| 220 |
link: "symlink", |
| 221 |
target: filepath.FromSlash("../../../../../../../../../etc/passwd"), |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "link to rel inexistent target", |
| 225 |
link: "symlink", |
| 226 |
target: filepath.FromSlash("../../../some/random/path"), |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "auto create dir", |
| 230 |
link: "new-dir/symlink", |
| 231 |
target: filepath.FromSlash("../../../some/random/path"), |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "keep dir filemode if exists", |
| 235 |
link: "new-dir/symlink", |
| 236 |
before: func(dir string) billy.Filesystem { |
| 237 |
os.Mkdir(filepath.Join(dir, "new-dir"), 0o701) |
| 238 |
return newBoundOS(dir, true) |
| 239 |
}, |
| 240 |
target: filepath.FromSlash("../../../some/random/path"), |
| 241 |
}, |
| 242 |
} |
| 243 |
for _, tt := range tests { |
| 244 |
t.Run(tt.name, func(t *testing.T) { |
| 245 |
g := gomega.NewWithT(t) |
| 246 |
dir := t.TempDir() |
| 247 |
fs := newBoundOS(dir, true) |
| 248 |
|
| 249 |
if tt.before != nil { |
| 250 |
fs = tt.before(dir) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
err := os.Chdir(os.TempDir()) |
| 256 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 257 |
|
| 258 |
link := filepath.Join(dir, tt.link) |
| 259 |
|
| 260 |
diBefore, _ := os.Lstat(filepath.Dir(link)) |
| 261 |
|
| 262 |
err = fs.Symlink(tt.target, tt.link) |
| 263 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 264 |
|
| 265 |
fi, err := os.Lstat(link) |
| 266 |
if tt.wantStatErr != "" { |
| 267 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 268 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantStatErr)) |
| 269 |
} else { |
| 270 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 271 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 272 |
} |
| 273 |
|
| 274 |
got, err := os.Readlink(link) |
| 275 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 276 |
g.Expect(got).To(gomega.Equal(tt.target)) |
| 277 |
|
| 278 |
diAfter, err := os.Lstat(filepath.Dir(link)) |
| 279 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 280 |
|
| 281 |
if diBefore != nil { |
| 282 |
g.Expect(diAfter.Mode()).To(gomega.Equal(diBefore.Mode())) |
| 283 |
} |
| 284 |
}) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
func TestTempFile(t *testing.T) { |
| 289 |
g := gomega.NewWithT(t) |
| 290 |
dir := t.TempDir() |
| 291 |
fs := newBoundOS(dir, true) |
| 292 |
|
| 293 |
f, err := fs.TempFile("", "prefix") |
| 294 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 295 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 296 |
g.Expect(f.Name()).To(gomega.ContainSubstring(os.TempDir())) |
| 297 |
g.Expect(f.Close()).ToNot(gomega.HaveOccurred()) |
| 298 |
|
| 299 |
f, err = fs.TempFile("/above/cwd", "prefix") |
| 300 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 301 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 302 |
g.Expect(f.Name()).To(gomega.HavePrefix(filepath.Join(dir, "/above/cwd", "prefix"))) |
| 303 |
g.Expect(f.Close()).ToNot(gomega.HaveOccurred()) |
| 304 |
|
| 305 |
tempDir := os.TempDir() |
| 306 |
|
| 307 |
if v := filepath.VolumeName(tempDir); v != "" { |
| 308 |
tempDir = strings.TrimPrefix(tempDir, v) |
| 309 |
} |
| 310 |
|
| 311 |
f, err = fs.TempFile(tempDir, "prefix") |
| 312 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 313 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 314 |
g.Expect(f.Name()).To(gomega.HavePrefix(filepath.Join(dir, tempDir, "prefix"))) |
| 315 |
g.Expect(f.Close()).ToNot(gomega.HaveOccurred()) |
| 316 |
} |
| 317 |
|
| 318 |
func TestChroot(t *testing.T) { |
| 319 |
g := gomega.NewWithT(t) |
| 320 |
tmp := t.TempDir() |
| 321 |
fs := newBoundOS(tmp, true) |
| 322 |
|
| 323 |
f, err := fs.Chroot("test") |
| 324 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 325 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 326 |
g.Expect(f).To(gomega.BeAssignableToTypeOf(&BoundOS{})) |
| 327 |
g.Expect(f.Root()).To(gomega.Equal(filepath.Join(tmp, "test"))) |
| 328 |
} |
| 329 |
|
| 330 |
func TestRoot(t *testing.T) { |
| 331 |
g := gomega.NewWithT(t) |
| 332 |
dir := t.TempDir() |
| 333 |
fs := newBoundOS(dir, true) |
| 334 |
|
| 335 |
root := fs.Root() |
| 336 |
g.Expect(root).To(gomega.Equal(dir)) |
| 337 |
} |
| 338 |
|
| 339 |
func TestReadLink(t *testing.T) { |
| 340 |
tests := []struct { |
| 341 |
name string |
| 342 |
filename string |
| 343 |
makeAbs bool |
| 344 |
expected string |
| 345 |
makeExpectedAbs bool |
| 346 |
before func(dir string) billy.Filesystem |
| 347 |
wantErr string |
| 348 |
}{ |
| 349 |
{ |
| 350 |
name: "symlink: pointing to abs outside cwd", |
| 351 |
before: func(dir string) billy.Filesystem { |
| 352 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 353 |
return newBoundOS(dir, true) |
| 354 |
}, |
| 355 |
filename: "symlink", |
| 356 |
expected: filepath.FromSlash("/etc/passwd"), |
| 357 |
}, |
| 358 |
{ |
| 359 |
name: "file: rel pointing to abs above cwd", |
| 360 |
filename: "../../file", |
| 361 |
wantErr: "path outside base dir", |
| 362 |
}, |
| 363 |
{ |
| 364 |
name: "symlink: abs symlink pointing outside cwd", |
| 365 |
before: func(dir string) billy.Filesystem { |
| 366 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 367 |
return newBoundOS(dir, true) |
| 368 |
}, |
| 369 |
filename: "symlink", |
| 370 |
makeAbs: true, |
| 371 |
expected: filepath.FromSlash("/etc/passwd"), |
| 372 |
}, |
| 373 |
{ |
| 374 |
name: "symlink: dir pointing outside cwd", |
| 375 |
before: func(dir string) billy.Filesystem { |
| 376 |
cwd := filepath.Join(dir, "current-dir") |
| 377 |
outside := filepath.Join(dir, "outside-cwd") |
| 378 |
|
| 379 |
os.Mkdir(cwd, 0o700) |
| 380 |
os.Mkdir(outside, 0o700) |
| 381 |
|
| 382 |
os.Symlink(outside, filepath.Join(cwd, "symlink")) |
| 383 |
os.WriteFile(filepath.Join(outside, "file"), []byte("anything"), 0o600) |
| 384 |
|
| 385 |
return newBoundOS(cwd, true) |
| 386 |
}, |
| 387 |
filename: "current-dir/symlink/file", |
| 388 |
makeAbs: true, |
| 389 |
wantErr: "path outside base dir", |
| 390 |
}, |
| 391 |
{ |
| 392 |
name: "symlink: within cwd + baseDir symlink", |
| 393 |
before: func(dir string) billy.Filesystem { |
| 394 |
cwd := filepath.Join(dir, "symlink-dir") |
| 395 |
cwdAlt := filepath.Join(dir, "symlink-altdir") |
| 396 |
cwdTarget := filepath.Join(dir, "cwd-target") |
| 397 |
|
| 398 |
os.MkdirAll(cwdTarget, 0o700) |
| 399 |
|
| 400 |
os.WriteFile(filepath.Join(cwdTarget, "file"), []byte{}, 0o600) |
| 401 |
os.Symlink(cwdTarget, cwd) |
| 402 |
os.Symlink(cwdTarget, cwdAlt) |
| 403 |
os.Symlink(filepath.Join(cwdTarget, "file"), filepath.Join(cwdAlt, "symlink-file")) |
| 404 |
return newBoundOS(cwd, true) |
| 405 |
}, |
| 406 |
filename: "symlink-file", |
| 407 |
expected: filepath.Join("cwd-target/file"), |
| 408 |
makeExpectedAbs: true, |
| 409 |
}, |
| 410 |
{ |
| 411 |
name: "symlink: outside cwd + baseDir symlink", |
| 412 |
before: func(dir string) billy.Filesystem { |
| 413 |
cwd := filepath.Join(dir, "symlink-dir") |
| 414 |
outside := filepath.Join(cwd, "symlink-outside") |
| 415 |
cwdTarget := filepath.Join(dir, "cwd-target") |
| 416 |
outsideDir := filepath.Join(dir, "outside") |
| 417 |
|
| 418 |
os.Mkdir(cwdTarget, 0o700) |
| 419 |
os.Mkdir(outsideDir, 0o700) |
| 420 |
|
| 421 |
os.WriteFile(filepath.Join(cwdTarget, "file"), []byte{}, 0o600) |
| 422 |
os.Symlink(cwdTarget, cwd) |
| 423 |
os.Symlink(outsideDir, outside) |
| 424 |
os.Symlink(filepath.Join(cwdTarget, "file"), filepath.Join(outside, "symlink-file")) |
| 425 |
return newBoundOS(cwd, true) |
| 426 |
}, |
| 427 |
filename: "symlink-outside/symlink-file", |
| 428 |
wantErr: "path outside base dir", |
| 429 |
}, |
| 430 |
} |
| 431 |
for _, tt := range tests { |
| 432 |
t.Run(tt.name, func(t *testing.T) { |
| 433 |
g := gomega.NewWithT(t) |
| 434 |
dir := t.TempDir() |
| 435 |
fs := newBoundOS(dir, true) |
| 436 |
|
| 437 |
if tt.before != nil { |
| 438 |
fs = tt.before(dir) |
| 439 |
} |
| 440 |
|
| 441 |
filename := tt.filename |
| 442 |
if tt.makeAbs { |
| 443 |
filename = filepath.Join(dir, filename) |
| 444 |
} |
| 445 |
|
| 446 |
expected := tt.expected |
| 447 |
if tt.makeExpectedAbs { |
| 448 |
expected = filepath.Join(dir, expected) |
| 449 |
} |
| 450 |
|
| 451 |
got, err := fs.Readlink(filename) |
| 452 |
if tt.wantErr != "" { |
| 453 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 454 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 455 |
g.Expect(got).To(gomega.BeEmpty()) |
| 456 |
} else { |
| 457 |
g.Expect(err).To(gomega.BeNil()) |
| 458 |
g.Expect(got).To(gomega.Equal(expected)) |
| 459 |
} |
| 460 |
}) |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
func TestLstat(t *testing.T) { |
| 465 |
tests := []struct { |
| 466 |
name string |
| 467 |
filename string |
| 468 |
makeAbs bool |
| 469 |
before func(dir string) billy.Filesystem |
| 470 |
wantErr string |
| 471 |
}{ |
| 472 |
{ |
| 473 |
name: "rel symlink: pointing to abs outside cwd", |
| 474 |
before: func(dir string) billy.Filesystem { |
| 475 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 476 |
return newBoundOS(dir, true) |
| 477 |
}, |
| 478 |
filename: "symlink", |
| 479 |
}, |
| 480 |
{ |
| 481 |
name: "rel symlink: pointing to rel path above cwd", |
| 482 |
before: func(dir string) billy.Filesystem { |
| 483 |
os.Symlink("../../../../../../../../etc/passwd", filepath.Join(dir, "symlink")) |
| 484 |
return newBoundOS(dir, true) |
| 485 |
}, |
| 486 |
filename: "symlink", |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "abs symlink: pointing to abs outside cwd", |
| 490 |
before: func(dir string) billy.Filesystem { |
| 491 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 492 |
return newBoundOS(dir, true) |
| 493 |
}, |
| 494 |
filename: "symlink", |
| 495 |
makeAbs: true, |
| 496 |
}, |
| 497 |
{ |
| 498 |
name: "abs symlink: pointing to rel outside cwd", |
| 499 |
before: func(dir string) billy.Filesystem { |
| 500 |
os.Symlink("../../../../../../../../etc/passwd", filepath.Join(dir, "symlink")) |
| 501 |
return newBoundOS(dir, true) |
| 502 |
}, |
| 503 |
filename: "symlink", |
| 504 |
makeAbs: false, |
| 505 |
}, |
| 506 |
{ |
| 507 |
name: "symlink: within cwd + baseDir symlink", |
| 508 |
before: func(dir string) billy.Filesystem { |
| 509 |
cwd := filepath.Join(dir, "symlink-dir") |
| 510 |
cwdAlt := filepath.Join(dir, "symlink-altdir") |
| 511 |
cwdTarget := filepath.Join(dir, "cwd-target") |
| 512 |
|
| 513 |
os.MkdirAll(cwdTarget, 0o700) |
| 514 |
|
| 515 |
os.WriteFile(filepath.Join(cwdTarget, "file"), []byte{}, 0o600) |
| 516 |
os.Symlink(cwdTarget, cwd) |
| 517 |
os.Symlink(cwdTarget, cwdAlt) |
| 518 |
os.Symlink(filepath.Join(cwdTarget, "file"), filepath.Join(cwdAlt, "symlink-file")) |
| 519 |
return newBoundOS(cwd, true) |
| 520 |
}, |
| 521 |
filename: "symlink-file", |
| 522 |
makeAbs: false, |
| 523 |
}, |
| 524 |
{ |
| 525 |
name: "symlink: outside cwd + baseDir symlink", |
| 526 |
before: func(dir string) billy.Filesystem { |
| 527 |
cwd := filepath.Join(dir, "symlink-dir") |
| 528 |
outside := filepath.Join(cwd, "symlink-outside") |
| 529 |
cwdTarget := filepath.Join(dir, "cwd-target") |
| 530 |
outsideDir := filepath.Join(dir, "outside") |
| 531 |
|
| 532 |
os.Mkdir(cwdTarget, 0o700) |
| 533 |
os.Mkdir(outsideDir, 0o700) |
| 534 |
|
| 535 |
os.WriteFile(filepath.Join(cwdTarget, "file"), []byte{}, 0o600) |
| 536 |
os.Symlink(cwdTarget, cwd) |
| 537 |
os.Symlink(outsideDir, outside) |
| 538 |
os.Symlink(filepath.Join(cwdTarget, "file"), filepath.Join(outside, "symlink-file")) |
| 539 |
return newBoundOS(cwd, true) |
| 540 |
}, |
| 541 |
filename: "symlink-outside/symlink-file", |
| 542 |
makeAbs: false, |
| 543 |
wantErr: "path outside base dir", |
| 544 |
}, |
| 545 |
{ |
| 546 |
name: "path: rel pointing to abs above cwd", |
| 547 |
filename: "../../file", |
| 548 |
wantErr: "path outside base dir", |
| 549 |
}, |
| 550 |
{ |
| 551 |
name: "path: abs pointing outside cwd", |
| 552 |
filename: "/etc/passwd", |
| 553 |
wantErr: "path outside base dir", |
| 554 |
}, |
| 555 |
{ |
| 556 |
name: "file: rel", |
| 557 |
before: func(dir string) billy.Filesystem { |
| 558 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 559 |
return newBoundOS(dir, true) |
| 560 |
}, |
| 561 |
filename: "test-file", |
| 562 |
}, |
| 563 |
{ |
| 564 |
name: "file: abs", |
| 565 |
before: func(dir string) billy.Filesystem { |
| 566 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 567 |
return newBoundOS(dir, true) |
| 568 |
}, |
| 569 |
filename: "test-file", |
| 570 |
makeAbs: true, |
| 571 |
}, |
| 572 |
} |
| 573 |
for _, tt := range tests { |
| 574 |
t.Run(tt.name, func(t *testing.T) { |
| 575 |
g := gomega.NewWithT(t) |
| 576 |
dir := t.TempDir() |
| 577 |
fs := newBoundOS(dir, true) |
| 578 |
|
| 579 |
if tt.before != nil { |
| 580 |
fs = tt.before(dir) |
| 581 |
} |
| 582 |
|
| 583 |
filename := tt.filename |
| 584 |
if tt.makeAbs { |
| 585 |
filename = filepath.Join(dir, filename) |
| 586 |
} |
| 587 |
fi, err := fs.Lstat(filename) |
| 588 |
if tt.wantErr != "" { |
| 589 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 590 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 591 |
g.Expect(fi).To(gomega.BeNil()) |
| 592 |
} else { |
| 593 |
g.Expect(err).To(gomega.BeNil()) |
| 594 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 595 |
g.Expect(fi.Name()).To(gomega.Equal(filepath.Base(tt.filename))) |
| 596 |
} |
| 597 |
}) |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
func TestStat(t *testing.T) { |
| 602 |
tests := []struct { |
| 603 |
name string |
| 604 |
filename string |
| 605 |
makeAbs bool |
| 606 |
before func(dir string) billy.Filesystem |
| 607 |
wantErr string |
| 608 |
}{ |
| 609 |
{ |
| 610 |
name: "rel symlink: pointing to abs outside cwd", |
| 611 |
before: func(dir string) billy.Filesystem { |
| 612 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 613 |
return newBoundOS(dir, true) |
| 614 |
}, |
| 615 |
filename: "symlink", |
| 616 |
wantErr: notFoundError(), |
| 617 |
}, |
| 618 |
{ |
| 619 |
name: "rel symlink: pointing to rel path above cwd", |
| 620 |
before: func(dir string) billy.Filesystem { |
| 621 |
os.Symlink("../../../../../../../../etc/passwd", filepath.Join(dir, "symlink")) |
| 622 |
return newBoundOS(dir, true) |
| 623 |
}, |
| 624 |
filename: "symlink", |
| 625 |
wantErr: notFoundError(), |
| 626 |
}, |
| 627 |
|
| 628 |
{ |
| 629 |
name: "abs symlink: pointing to abs outside cwd", |
| 630 |
before: func(dir string) billy.Filesystem { |
| 631 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 632 |
return newBoundOS(dir, true) |
| 633 |
}, |
| 634 |
filename: "symlink", |
| 635 |
makeAbs: true, |
| 636 |
wantErr: notFoundError(), |
| 637 |
}, |
| 638 |
{ |
| 639 |
name: "abs symlink: pointing to rel outside cwd", |
| 640 |
before: func(dir string) billy.Filesystem { |
| 641 |
os.Symlink("../../../../../../../../etc/passwd", filepath.Join(dir, "symlink")) |
| 642 |
return newBoundOS(dir, true) |
| 643 |
}, |
| 644 |
filename: "symlink", |
| 645 |
makeAbs: false, |
| 646 |
wantErr: notFoundError(), |
| 647 |
}, |
| 648 |
{ |
| 649 |
name: "path: rel pointing to abs above cwd", |
| 650 |
filename: "../../file", |
| 651 |
wantErr: notFoundError(), |
| 652 |
}, |
| 653 |
{ |
| 654 |
name: "path: abs pointing outside cwd", |
| 655 |
filename: "/etc/passwd", |
| 656 |
wantErr: notFoundError(), |
| 657 |
}, |
| 658 |
{ |
| 659 |
name: "rel file", |
| 660 |
before: func(dir string) billy.Filesystem { |
| 661 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 662 |
return newBoundOS(dir, true) |
| 663 |
}, |
| 664 |
filename: "test-file", |
| 665 |
}, |
| 666 |
{ |
| 667 |
name: "abs file", |
| 668 |
before: func(dir string) billy.Filesystem { |
| 669 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 670 |
return newBoundOS(dir, true) |
| 671 |
}, |
| 672 |
filename: "test-file", |
| 673 |
makeAbs: true, |
| 674 |
}, |
| 675 |
{ |
| 676 |
name: "dot dir", |
| 677 |
filename: ".", |
| 678 |
}, |
| 679 |
} |
| 680 |
for _, tt := range tests { |
| 681 |
t.Run(tt.name, func(t *testing.T) { |
| 682 |
g := gomega.NewWithT(t) |
| 683 |
dir := t.TempDir() |
| 684 |
fs := newBoundOS(dir, true) |
| 685 |
|
| 686 |
if tt.before != nil { |
| 687 |
fs = tt.before(dir) |
| 688 |
} |
| 689 |
|
| 690 |
filename := tt.filename |
| 691 |
if tt.makeAbs { |
| 692 |
filename = filepath.Join(dir, filename) |
| 693 |
} |
| 694 |
|
| 695 |
fi, err := fs.Stat(filename) |
| 696 |
if tt.wantErr != "" { |
| 697 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 698 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 699 |
g.Expect(fi).To(gomega.BeNil()) |
| 700 |
} else { |
| 701 |
g.Expect(err).To(gomega.BeNil()) |
| 702 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 703 |
} |
| 704 |
}) |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
func TestRemove(t *testing.T) { |
| 709 |
tests := []struct { |
| 710 |
name string |
| 711 |
filename string |
| 712 |
makeAbs bool |
| 713 |
before func(dir string) billy.Filesystem |
| 714 |
wantErr string |
| 715 |
}{ |
| 716 |
{ |
| 717 |
name: "path: rel pointing outside cwd w forward slash", |
| 718 |
filename: "/some/path/outside/cwd", |
| 719 |
wantErr: notFoundError(), |
| 720 |
}, |
| 721 |
{ |
| 722 |
name: "path: rel pointing outside cwd", |
| 723 |
filename: "../../../../path/outside/cwd", |
| 724 |
wantErr: notFoundError(), |
| 725 |
}, |
| 726 |
{ |
| 727 |
name: "inexistent dir", |
| 728 |
before: func(dir string) billy.Filesystem { |
| 729 |
return newBoundOS(dir, true) |
| 730 |
}, |
| 731 |
filename: "inexistent", |
| 732 |
wantErr: notFoundError(), |
| 733 |
}, |
| 734 |
{ |
| 735 |
name: "base dir as empty path", |
| 736 |
filename: "", |
| 737 |
wantErr: "base dir cannot be removed", |
| 738 |
}, |
| 739 |
{ |
| 740 |
name: "base dir as dot", |
| 741 |
filename: ".", |
| 742 |
wantErr: "base dir cannot be removed", |
| 743 |
}, |
| 744 |
{ |
| 745 |
name: "base dir as dot slash", |
| 746 |
filename: "./", |
| 747 |
wantErr: "base dir cannot be removed", |
| 748 |
}, |
| 749 |
{ |
| 750 |
name: "base dir as absolute parent", |
| 751 |
filename: "/..", |
| 752 |
wantErr: "base dir cannot be removed", |
| 753 |
}, |
| 754 |
{ |
| 755 |
name: "base dir as absolute child parent", |
| 756 |
filename: "/foo/..", |
| 757 |
wantErr: "base dir cannot be removed", |
| 758 |
}, |
| 759 |
{ |
| 760 |
name: "same dir file", |
| 761 |
before: func(dir string) billy.Filesystem { |
| 762 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 763 |
return newBoundOS(dir, true) |
| 764 |
}, |
| 765 |
filename: "test-file", |
| 766 |
}, |
| 767 |
{ |
| 768 |
name: "symlink: same dir", |
| 769 |
before: func(dir string) billy.Filesystem { |
| 770 |
target := filepath.Join(dir, "target-file") |
| 771 |
os.WriteFile(target, []byte("anything"), 0o600) |
| 772 |
os.Symlink(target, filepath.Join(dir, "symlink")) |
| 773 |
return newBoundOS(dir, true) |
| 774 |
}, |
| 775 |
filename: "symlink", |
| 776 |
}, |
| 777 |
{ |
| 778 |
name: "rel path to file above cwd", |
| 779 |
before: func(dir string) billy.Filesystem { |
| 780 |
os.WriteFile(filepath.Join(dir, "rel-above-cwd"), []byte("anything"), 0o600) |
| 781 |
return newBoundOS(dir, true) |
| 782 |
}, |
| 783 |
filename: "../../rel-above-cwd", |
| 784 |
}, |
| 785 |
{ |
| 786 |
name: "abs file", |
| 787 |
before: func(dir string) billy.Filesystem { |
| 788 |
os.WriteFile(filepath.Join(dir, "abs-test-file"), []byte("anything"), 0o600) |
| 789 |
return newBoundOS(dir, true) |
| 790 |
}, |
| 791 |
filename: "abs-test-file", |
| 792 |
makeAbs: true, |
| 793 |
}, |
| 794 |
{ |
| 795 |
name: "abs symlink: pointing outside is forced to descend", |
| 796 |
before: func(dir string) billy.Filesystem { |
| 797 |
cwd := filepath.Join(dir, "current-dir") |
| 798 |
outsideFile := filepath.Join(dir, "outside-cwd/file") |
| 799 |
|
| 800 |
os.Mkdir(cwd, 0o700) |
| 801 |
os.MkdirAll(filepath.Dir(outsideFile), 0o700) |
| 802 |
os.WriteFile(outsideFile, []byte("anything"), 0o600) |
| 803 |
os.Symlink(outsideFile, filepath.Join(cwd, "remove-abs-symlink")) |
| 804 |
return newBoundOS(cwd, true) |
| 805 |
}, |
| 806 |
filename: "remove-abs-symlink", |
| 807 |
wantErr: notFoundError(), |
| 808 |
}, |
| 809 |
{ |
| 810 |
name: "rel symlink: pointing outside is forced to descend", |
| 811 |
before: func(dir string) billy.Filesystem { |
| 812 |
cwd := filepath.Join(dir, "current-dir") |
| 813 |
outsideFile := filepath.Join(dir, "outside-cwd", "file2") |
| 814 |
|
| 815 |
os.Mkdir(cwd, 0o700) |
| 816 |
os.MkdirAll(filepath.Dir(outsideFile), 0o700) |
| 817 |
os.WriteFile(outsideFile, []byte("anything"), 0o600) |
| 818 |
os.Symlink(filepath.Join("..", "outside-cwd", "file2"), filepath.Join(cwd, "remove-abs-symlink2")) |
| 819 |
return newBoundOS(cwd, true) |
| 820 |
}, |
| 821 |
filename: "remove-rel-symlink", |
| 822 |
wantErr: notFoundError(), |
| 823 |
}, |
| 824 |
} |
| 825 |
for _, tt := range tests { |
| 826 |
t.Run(tt.name, func(t *testing.T) { |
| 827 |
g := gomega.NewWithT(t) |
| 828 |
dir := t.TempDir() |
| 829 |
fs := newBoundOS(dir, true) |
| 830 |
|
| 831 |
if tt.before != nil { |
| 832 |
fs = tt.before(dir) |
| 833 |
} |
| 834 |
|
| 835 |
filename := tt.filename |
| 836 |
if tt.makeAbs { |
| 837 |
filename = filepath.Join(dir, filename) |
| 838 |
} |
| 839 |
|
| 840 |
err := fs.Remove(filename) |
| 841 |
if tt.wantErr != "" { |
| 842 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 843 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 844 |
} else { |
| 845 |
g.Expect(err).To(gomega.BeNil()) |
| 846 |
} |
| 847 |
}) |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
func TestRemoveAll(t *testing.T) { |
| 852 |
tests := []struct { |
| 853 |
name string |
| 854 |
filename string |
| 855 |
makeAbs bool |
| 856 |
before func(dir string) billy.Filesystem |
| 857 |
wantErr string |
| 858 |
}{ |
| 859 |
{ |
| 860 |
name: "parent with children", |
| 861 |
before: func(dir string) billy.Filesystem { |
| 862 |
os.MkdirAll(filepath.Join(dir, "parent/children"), 0o600) |
| 863 |
return newBoundOS(dir, true) |
| 864 |
}, |
| 865 |
filename: "parent", |
| 866 |
}, |
| 867 |
{ |
| 868 |
name: "inexistent dir", |
| 869 |
filename: "inexistent", |
| 870 |
}, |
| 871 |
{ |
| 872 |
name: "base dir as empty path", |
| 873 |
filename: "", |
| 874 |
wantErr: "base dir cannot be removed", |
| 875 |
}, |
| 876 |
{ |
| 877 |
name: "base dir as dot", |
| 878 |
filename: ".", |
| 879 |
wantErr: "base dir cannot be removed", |
| 880 |
}, |
| 881 |
{ |
| 882 |
name: "base dir as dot slash", |
| 883 |
filename: "./", |
| 884 |
wantErr: "base dir cannot be removed", |
| 885 |
}, |
| 886 |
{ |
| 887 |
name: "base dir as absolute parent", |
| 888 |
filename: "/..", |
| 889 |
wantErr: "base dir cannot be removed", |
| 890 |
}, |
| 891 |
{ |
| 892 |
name: "base dir as absolute child parent", |
| 893 |
filename: "/foo/..", |
| 894 |
wantErr: "base dir cannot be removed", |
| 895 |
}, |
| 896 |
{ |
| 897 |
name: "same dir file", |
| 898 |
before: func(dir string) billy.Filesystem { |
| 899 |
os.WriteFile(filepath.Join(dir, "test-file"), []byte("anything"), 0o600) |
| 900 |
return newBoundOS(dir, true) |
| 901 |
}, |
| 902 |
filename: "test-file", |
| 903 |
}, |
| 904 |
{ |
| 905 |
name: "same dir symlink", |
| 906 |
before: func(dir string) billy.Filesystem { |
| 907 |
target := filepath.Join(dir, "target-file") |
| 908 |
os.WriteFile(target, []byte("anything"), 0o600) |
| 909 |
os.Symlink(target, filepath.Join(dir, "symlink")) |
| 910 |
return newBoundOS(dir, true) |
| 911 |
}, |
| 912 |
filename: "symlink", |
| 913 |
}, |
| 914 |
{ |
| 915 |
name: "rel path to file above cwd", |
| 916 |
before: func(dir string) billy.Filesystem { |
| 917 |
os.WriteFile(filepath.Join(dir, "rel-above-cwd"), []byte("anything"), 0o600) |
| 918 |
return newBoundOS(dir, true) |
| 919 |
}, |
| 920 |
filename: "../../rel-above-cwd", |
| 921 |
}, |
| 922 |
{ |
| 923 |
name: "abs file", |
| 924 |
before: func(dir string) billy.Filesystem { |
| 925 |
os.WriteFile(filepath.Join(dir, "abs-test-file"), []byte("anything"), 0o600) |
| 926 |
return newBoundOS(dir, true) |
| 927 |
}, |
| 928 |
filename: "abs-test-file", |
| 929 |
makeAbs: true, |
| 930 |
}, |
| 931 |
{ |
| 932 |
name: "abs symlink", |
| 933 |
before: func(dir string) billy.Filesystem { |
| 934 |
os.Symlink("/etc/passwd", filepath.Join(dir, "symlink")) |
| 935 |
return newBoundOS(dir, true) |
| 936 |
}, |
| 937 |
filename: "symlink", |
| 938 |
makeAbs: true, |
| 939 |
}, |
| 940 |
} |
| 941 |
for _, tt := range tests { |
| 942 |
t.Run(tt.name, func(t *testing.T) { |
| 943 |
g := gomega.NewWithT(t) |
| 944 |
dir := t.TempDir() |
| 945 |
fs := newBoundOS(dir, true).(*BoundOS) |
| 946 |
|
| 947 |
if tt.before != nil { |
| 948 |
fs = tt.before(dir).(*BoundOS) |
| 949 |
} |
| 950 |
|
| 951 |
filename := tt.filename |
| 952 |
if tt.makeAbs { |
| 953 |
filename = filepath.Join(dir, filename) |
| 954 |
} |
| 955 |
|
| 956 |
err := fs.RemoveAll(filename) |
| 957 |
if tt.wantErr != "" { |
| 958 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 959 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 960 |
} else { |
| 961 |
g.Expect(err).To(gomega.BeNil()) |
| 962 |
} |
| 963 |
}) |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
func TestRemoveBaseDirAbsolutePath(t *testing.T) { |
| 968 |
tests := []struct { |
| 969 |
name string |
| 970 |
remove func(fs *BoundOS, path string) error |
| 971 |
}{ |
| 972 |
{ |
| 973 |
name: "remove", |
| 974 |
remove: func(fs *BoundOS, path string) error { |
| 975 |
return fs.Remove(path) |
| 976 |
}, |
| 977 |
}, |
| 978 |
{ |
| 979 |
name: "remove all", |
| 980 |
remove: func(fs *BoundOS, path string) error { |
| 981 |
return fs.RemoveAll(path) |
| 982 |
}, |
| 983 |
}, |
| 984 |
} |
| 985 |
for _, tt := range tests { |
| 986 |
t.Run(tt.name, func(t *testing.T) { |
| 987 |
g := gomega.NewWithT(t) |
| 988 |
dir := t.TempDir() |
| 989 |
fs := newBoundOS(dir, true).(*BoundOS) |
| 990 |
|
| 991 |
err := tt.remove(fs, dir) |
| 992 |
g.Expect(err).To(gomega.MatchError("base dir cannot be removed")) |
| 993 |
mustExist(dir) |
| 994 |
}) |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
func TestJoin(t *testing.T) { |
| 999 |
tests := []struct { |
| 1000 |
elems []string |
| 1001 |
wanted string |
| 1002 |
}{ |
| 1003 |
{ |
| 1004 |
elems: []string{}, |
| 1005 |
wanted: "", |
| 1006 |
}, |
| 1007 |
{ |
| 1008 |
elems: []string{"/a", "b", "c"}, |
| 1009 |
wanted: filepath.FromSlash("/a/b/c"), |
| 1010 |
}, |
| 1011 |
{ |
| 1012 |
elems: []string{"/a", "b/c"}, |
| 1013 |
wanted: filepath.FromSlash("/a/b/c"), |
| 1014 |
}, |
| 1015 |
{ |
| 1016 |
elems: []string{"/a", ""}, |
| 1017 |
wanted: filepath.FromSlash("/a"), |
| 1018 |
}, |
| 1019 |
{ |
| 1020 |
elems: []string{"/a", "/", "b"}, |
| 1021 |
wanted: filepath.FromSlash("/a/b"), |
| 1022 |
}, |
| 1023 |
} |
| 1024 |
for _, tt := range tests { |
| 1025 |
t.Run(tt.wanted, func(t *testing.T) { |
| 1026 |
g := gomega.NewWithT(t) |
| 1027 |
fs := newBoundOS(t.TempDir(), true) |
| 1028 |
|
| 1029 |
got := fs.Join(tt.elems...) |
| 1030 |
g.Expect(got).To(gomega.Equal(tt.wanted)) |
| 1031 |
}) |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
func TestAbs(t *testing.T) { |
| 1036 |
tests := []struct { |
| 1037 |
name string |
| 1038 |
cwd string |
| 1039 |
filename string |
| 1040 |
makeAbs bool |
| 1041 |
expected string |
| 1042 |
makeExpectedAbs bool |
| 1043 |
wantErr string |
| 1044 |
deduplicatePath bool |
| 1045 |
before func(dir string) |
| 1046 |
}{ |
| 1047 |
{ |
| 1048 |
name: "path: same dir rel file", |
| 1049 |
cwd: "/working/dir", |
| 1050 |
filename: "./file", |
| 1051 |
expected: filepath.FromSlash("/working/dir/file"), |
| 1052 |
}, |
| 1053 |
{ |
| 1054 |
name: "path: descending rel file", |
| 1055 |
cwd: "/working/dir", |
| 1056 |
filename: "file", |
| 1057 |
expected: filepath.FromSlash("/working/dir/file"), |
| 1058 |
}, |
| 1059 |
{ |
| 1060 |
name: "path: ascending rel file 1", |
| 1061 |
cwd: "/working/dir", |
| 1062 |
filename: "../file", |
| 1063 |
expected: filepath.FromSlash("/working/dir/file"), |
| 1064 |
}, |
| 1065 |
{ |
| 1066 |
name: "path: ascending rel file 2", |
| 1067 |
cwd: "/working/dir", |
| 1068 |
filename: "../../file", |
| 1069 |
expected: filepath.FromSlash("/working/dir/file"), |
| 1070 |
}, |
| 1071 |
{ |
| 1072 |
name: "path: ascending rel file 3", |
| 1073 |
cwd: "/working/dir", |
| 1074 |
filename: "/../../file", |
| 1075 |
expected: filepath.FromSlash("/working/dir/file"), |
| 1076 |
}, |
| 1077 |
{ |
| 1078 |
name: "path: abs file within cwd", |
| 1079 |
cwd: filepath.FromSlash("/working/dir"), |
| 1080 |
filename: filepath.FromSlash("/working/dir/abs-file"), |
| 1081 |
expected: filepath.FromSlash("/working/dir/abs-file"), |
| 1082 |
deduplicatePath: true, |
| 1083 |
}, |
| 1084 |
{ |
| 1085 |
name: "path: abs file within cwd wo deduplication", |
| 1086 |
cwd: filepath.FromSlash("/working/dir"), |
| 1087 |
filename: filepath.FromSlash("/working/dir/abs-file"), |
| 1088 |
expected: filepath.FromSlash("/working/dir/working/dir/abs-file"), |
| 1089 |
}, |
| 1090 |
{ |
| 1091 |
name: "path: abs file within cwd", |
| 1092 |
cwd: "/working/dir", |
| 1093 |
filename: "/outside/dir/abs-file", |
| 1094 |
expected: filepath.FromSlash("/working/dir/outside/dir/abs-file"), |
| 1095 |
}, |
| 1096 |
{ |
| 1097 |
name: "abs symlink: within cwd w abs descending target", |
| 1098 |
filename: "ln-cwd-cwd", |
| 1099 |
makeAbs: true, |
| 1100 |
expected: "within-cwd", |
| 1101 |
makeExpectedAbs: true, |
| 1102 |
before: func(dir string) { |
| 1103 |
os.Symlink(filepath.Join(dir, "within-cwd"), filepath.Join(dir, "ln-cwd-cwd")) |
| 1104 |
}, |
| 1105 |
deduplicatePath: true, |
| 1106 |
}, |
| 1107 |
{ |
| 1108 |
name: "abs symlink: within cwd w rel descending target", |
| 1109 |
filename: "ln-rel-cwd-cwd", |
| 1110 |
makeAbs: true, |
| 1111 |
expected: "within-cwd", |
| 1112 |
makeExpectedAbs: true, |
| 1113 |
before: func(dir string) { |
| 1114 |
os.Symlink("within-cwd", filepath.Join(dir, "ln-rel-cwd-cwd")) |
| 1115 |
}, |
| 1116 |
deduplicatePath: true, |
| 1117 |
}, |
| 1118 |
{ |
| 1119 |
name: "abs symlink: within cwd w abs ascending target", |
| 1120 |
filename: "ln-cwd-up", |
| 1121 |
makeAbs: true, |
| 1122 |
expected: "/some/outside/dir", |
| 1123 |
makeExpectedAbs: true, |
| 1124 |
before: func(dir string) { |
| 1125 |
os.Symlink("/some/outside/dir", filepath.Join(dir, "ln-cwd-up")) |
| 1126 |
}, |
| 1127 |
deduplicatePath: true, |
| 1128 |
}, |
| 1129 |
{ |
| 1130 |
name: "abs symlink: within cwd w rel ascending target", |
| 1131 |
filename: "ln-rel-cwd-up", |
| 1132 |
makeAbs: true, |
| 1133 |
expected: "outside-cwd", |
| 1134 |
makeExpectedAbs: true, |
| 1135 |
before: func(dir string) { |
| 1136 |
os.Symlink("../../outside-cwd", filepath.Join(dir, "ln-rel-cwd-up")) |
| 1137 |
}, |
| 1138 |
deduplicatePath: true, |
| 1139 |
}, |
| 1140 |
{ |
| 1141 |
name: "rel symlink: within cwd w abs descending target", |
| 1142 |
filename: "ln-cwd-cwd", |
| 1143 |
expected: "within-cwd", |
| 1144 |
makeExpectedAbs: true, |
| 1145 |
before: func(dir string) { |
| 1146 |
os.Symlink(filepath.Join(dir, "within-cwd"), filepath.Join(dir, "ln-cwd-cwd")) |
| 1147 |
}, |
| 1148 |
deduplicatePath: true, |
| 1149 |
}, |
| 1150 |
{ |
| 1151 |
name: "rel symlink: within cwd w rel descending target", |
| 1152 |
filename: "ln-rel-cwd-cwd2", |
| 1153 |
expected: "within-cwd", |
| 1154 |
makeExpectedAbs: true, |
| 1155 |
before: func(dir string) { |
| 1156 |
os.Symlink("within-cwd", filepath.Join(dir, "ln-rel-cwd-cwd2")) |
| 1157 |
}, |
| 1158 |
}, |
| 1159 |
{ |
| 1160 |
name: "rel symlink: within cwd w abs ascending target", |
| 1161 |
filename: "ln-cwd-up2", |
| 1162 |
expected: "/outside/path/up", |
| 1163 |
makeExpectedAbs: true, |
| 1164 |
before: func(dir string) { |
| 1165 |
os.Symlink("/outside/path/up", filepath.Join(dir, "ln-cwd-up2")) |
| 1166 |
}, |
| 1167 |
}, |
| 1168 |
{ |
| 1169 |
name: "rel symlink: within cwd w rel ascending target", |
| 1170 |
filename: "ln-rel-cwd-up2", |
| 1171 |
expected: "outside", |
| 1172 |
makeExpectedAbs: true, |
| 1173 |
before: func(dir string) { |
| 1174 |
os.Symlink("../../../../outside", filepath.Join(dir, "ln-rel-cwd-up2")) |
| 1175 |
}, |
| 1176 |
}, |
| 1177 |
} |
| 1178 |
for _, tt := range tests { |
| 1179 |
t.Run(tt.name, func(t *testing.T) { |
| 1180 |
g := gomega.NewWithT(t) |
| 1181 |
cwd := tt.cwd |
| 1182 |
if cwd == "" { |
| 1183 |
cwd = t.TempDir() |
| 1184 |
} |
| 1185 |
|
| 1186 |
fs := newBoundOS(cwd, tt.deduplicatePath).(*BoundOS) |
| 1187 |
if tt.before != nil { |
| 1188 |
tt.before(cwd) |
| 1189 |
} |
| 1190 |
|
| 1191 |
filename := tt.filename |
| 1192 |
if tt.makeAbs { |
| 1193 |
filename = filepath.Join(cwd, filename) |
| 1194 |
} |
| 1195 |
|
| 1196 |
expected := tt.expected |
| 1197 |
if tt.makeExpectedAbs { |
| 1198 |
expected = filepath.Join(cwd, expected) |
| 1199 |
} |
| 1200 |
|
| 1201 |
got, err := fs.abs(filename) |
| 1202 |
if tt.wantErr != "" { |
| 1203 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 1204 |
g.Expect(err.Error()).To(gomega.ContainSubstring(tt.wantErr)) |
| 1205 |
} else { |
| 1206 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1207 |
} |
| 1208 |
|
| 1209 |
g.Expect(got).To(gomega.Equal(expected)) |
| 1210 |
}) |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
func TestAbsReturnsSecureJoinError(t *testing.T) { |
| 1215 |
if runtime.GOOS == "windows" { |
| 1216 |
t.Skip("symlink creation requires additional privileges on windows") |
| 1217 |
} |
| 1218 |
|
| 1219 |
g := gomega.NewWithT(t) |
| 1220 |
dir := t.TempDir() |
| 1221 |
fs := newBoundOS(dir, true).(*BoundOS) |
| 1222 |
|
| 1223 |
err := os.Symlink("loop", filepath.Join(dir, "loop")) |
| 1224 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1225 |
|
| 1226 |
got, err := fs.abs(filepath.Join("loop", "file")) |
| 1227 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 1228 |
g.Expect(got).To(gomega.BeEmpty()) |
| 1229 |
} |
| 1230 |
|
| 1231 |
func TestReadDir(t *testing.T) { |
| 1232 |
g := gomega.NewWithT(t) |
| 1233 |
dir := t.TempDir() |
| 1234 |
fs := newBoundOS(dir, true) |
| 1235 |
|
| 1236 |
f, err := os.Create(filepath.Join(dir, "file1")) |
| 1237 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1238 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 1239 |
g.Expect(f.Close()).To(gomega.Succeed()) |
| 1240 |
|
| 1241 |
f, err = os.Create(filepath.Join(dir, "file2")) |
| 1242 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1243 |
g.Expect(f).ToNot(gomega.BeNil()) |
| 1244 |
g.Expect(f.Close()).To(gomega.Succeed()) |
| 1245 |
|
| 1246 |
dirs, err := fs.ReadDir(dir) |
| 1247 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1248 |
g.Expect(dirs).ToNot(gomega.BeNil()) |
| 1249 |
g.Expect(dirs).To(gomega.HaveLen(2)) |
| 1250 |
|
| 1251 |
dirs, err = fs.ReadDir(".") |
| 1252 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1253 |
g.Expect(dirs).ToNot(gomega.BeNil()) |
| 1254 |
g.Expect(dirs).To(gomega.HaveLen(2)) |
| 1255 |
|
| 1256 |
os.Symlink("/some/path/outside/cwd", filepath.Join(dir, "symlink")) |
| 1257 |
dirs, err = fs.ReadDir("symlink") |
| 1258 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 1259 |
g.Expect(err.Error()).To(gomega.ContainSubstring(notFoundError())) |
| 1260 |
g.Expect(dirs).To(gomega.BeNil()) |
| 1261 |
} |
| 1262 |
|
| 1263 |
func TestInsideBaseDirEval(t *testing.T) { |
| 1264 |
g := gomega.NewWithT(t) |
| 1265 |
fs := BoundOS{baseDir: "/"} |
| 1266 |
b, err := fs.insideBaseDirEval("a") |
| 1267 |
g.Expect(b).To(gomega.BeTrue()) |
| 1268 |
g.Expect(err).To(gomega.BeNil()) |
| 1269 |
} |
| 1270 |
|
| 1271 |
func TestMkdirAll(t *testing.T) { |
| 1272 |
g := gomega.NewWithT(t) |
| 1273 |
root := t.TempDir() |
| 1274 |
cwd := filepath.Join(root, "cwd") |
| 1275 |
target := "abc" |
| 1276 |
targetAbs := filepath.Join(cwd, target) |
| 1277 |
fs := newBoundOS(cwd, true) |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
err := os.Chdir(os.TempDir()) |
| 1282 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1283 |
|
| 1284 |
err = fs.MkdirAll(target, 0o700) |
| 1285 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1286 |
|
| 1287 |
fi, err := os.Stat(targetAbs) |
| 1288 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1289 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 1290 |
|
| 1291 |
err = os.Mkdir(filepath.Join(root, "outside"), 0o700) |
| 1292 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1293 |
err = os.Symlink(filepath.Join(root, "outside"), filepath.Join(cwd, "symlink")) |
| 1294 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1295 |
|
| 1296 |
err = fs.MkdirAll(filepath.Join(cwd, "symlink", "new-dir"), 0o700) |
| 1297 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
if vol := filepath.VolumeName(root); vol != "" { |
| 1302 |
root = root[len(vol):] |
| 1303 |
} |
| 1304 |
|
| 1305 |
mustExist(filepath.Join(cwd, root, "outside", "new-dir")) |
| 1306 |
} |
| 1307 |
|
| 1308 |
func TestRename(t *testing.T) { |
| 1309 |
g := gomega.NewWithT(t) |
| 1310 |
dir := t.TempDir() |
| 1311 |
fs := newBoundOS(dir, true) |
| 1312 |
|
| 1313 |
oldFile := "old-file" |
| 1314 |
newFile := filepath.Join("newdir", "newfile") |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
err := os.Chdir(os.TempDir()) |
| 1319 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1320 |
|
| 1321 |
f, err := fs.Create(oldFile) |
| 1322 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1323 |
g.Expect(f.Close()).To(gomega.Succeed()) |
| 1324 |
|
| 1325 |
err = fs.Rename(oldFile, newFile) |
| 1326 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1327 |
|
| 1328 |
fi, err := os.Stat(filepath.Join(dir, newFile)) |
| 1329 |
g.Expect(err).ToNot(gomega.HaveOccurred()) |
| 1330 |
g.Expect(fi).ToNot(gomega.BeNil()) |
| 1331 |
|
| 1332 |
err = fs.Rename(filepath.FromSlash("/tmp/outside/cwd/file1"), newFile) |
| 1333 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 1334 |
g.Expect(err.Error()).To(gomega.ContainSubstring(notFoundError())) |
| 1335 |
|
| 1336 |
err = fs.Rename(oldFile, filepath.FromSlash("/tmp/outside/cwd/file2")) |
| 1337 |
g.Expect(err).To(gomega.HaveOccurred()) |
| 1338 |
g.Expect(err.Error()).To(gomega.ContainSubstring(notFoundError())) |
| 1339 |
} |
| 1340 |
|
| 1341 |
func TestRenameBaseDir(t *testing.T) { |
| 1342 |
g := gomega.NewWithT(t) |
| 1343 |
dir := t.TempDir() |
| 1344 |
fs := newBoundOS(dir, true) |
| 1345 |
|
| 1346 |
for _, from := range []string{"", ".", "./", "/..", "/foo/..", dir} { |
| 1347 |
err := fs.Rename(from, "renamed") |
| 1348 |
g.Expect(err).To(gomega.MatchError("base dir cannot be renamed")) |
| 1349 |
mustExist(dir) |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
func mustExist(filename string) { |
| 1354 |
fi, err := os.Stat(filename) |
| 1355 |
if err != nil || fi == nil { |
| 1356 |
panic(fmt.Sprintf("file %s should exist", filename)) |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
func notFoundError() string { |
| 1361 |
switch runtime.GOOS { |
| 1362 |
case "windows": |
| 1363 |
return "The system cannot find the " |
| 1364 |
default: |
| 1365 |
return "no such file or directory" |
| 1366 |
} |
| 1367 |
} |