|
4 | 4 | import java.nio.charset.StandardCharsets; |
5 | 5 | import java.nio.file.Files; |
6 | 6 | import java.nio.file.Path; |
7 | | -import java.util.ArrayList; |
8 | 7 | import java.util.List; |
9 | 8 | import java.util.Optional; |
| 9 | +import java.util.UUID; |
10 | 10 | import java.util.stream.Collectors; |
11 | 11 | import java.util.stream.Stream; |
12 | 12 |
|
@@ -449,70 +449,86 @@ public void cTemp() { |
449 | 449 | } |
450 | 450 | } |
451 | 451 |
|
452 | | - @ParameterizedTest |
453 | | - @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
454 | | - @MethodSource |
455 | | - void relativizeSymlinks(Path file, List<Path> directories, Path expected, String message) { |
456 | | - if (message.startsWith("IGNORED")) { |
457 | | - org.junit.jupiter.api.Assumptions.assumeTrue(false, message); |
458 | | - } |
459 | | - Path result = FileUtil.relativize(file, directories); |
460 | | - assertEquals(expected, result, message); |
461 | | - } |
462 | | - |
463 | 452 | /// Tests for issue <https://github.com/JabRef/jabref/issues/12995> |
464 | | - static Stream<Arguments> relativizeSymlinks() throws IOException { |
465 | | - List<Arguments> result = new ArrayList<>(); |
466 | | - |
467 | | - Path realDir = bibTempDir.resolve("realDir"); |
| 453 | + @Test |
| 454 | + @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
| 455 | + void simpleRelativizeSymlinks() throws IOException { |
| 456 | + Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID()); |
468 | 457 | Files.createDirectories(realDir); |
469 | 458 |
|
470 | | - // symlinkDir -> realDir |
471 | | - // realDir/simple.pdf |
472 | 459 | Path simpleFile = Files.createFile(realDir.resolve("simple.pdf")); |
473 | | - Path symlinkDir = bibTempDir.resolve("symlinkDir"); |
| 460 | + Path symlinkDir = bibTempDir.resolve("symlinkDir_" + UUID.randomUUID()); |
474 | 461 | Files.createSymbolicLink(symlinkDir, realDir); |
475 | | - result.add(Arguments.of(simpleFile, List.of(symlinkDir), Path.of("simple.pdf"), "Simple symlink resolves to relative")); |
476 | 462 |
|
477 | | - // chainLink1 -> chainLink2 -> chainReal |
478 | | - // chainReal/chained.pdf |
479 | | - Path chainReal = bibTempDir.resolve("chainReal"); |
| 463 | + Path result = FileUtil.relativize(simpleFile, List.of(symlinkDir)); |
| 464 | + assertEquals(Path.of("simple.pdf"), result, "Simple symlink resolves to relative"); |
| 465 | + } |
| 466 | + |
| 467 | + @Test |
| 468 | + @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
| 469 | + void chainedRelativizeSymlinks() throws IOException { |
| 470 | + Path chainReal = bibTempDir.resolve("chainReal_" + UUID.randomUUID()); |
480 | 471 | Files.createDirectories(chainReal); |
| 472 | + |
481 | 473 | Path chainedFile = Files.createFile(chainReal.resolve("chained.pdf")); |
482 | | - Path chainLink2 = bibTempDir.resolve("chainLink2"); |
| 474 | + Path chainLink2 = bibTempDir.resolve("chainLink2_" + UUID.randomUUID()); |
483 | 475 | Files.createSymbolicLink(chainLink2, chainReal); |
484 | | - Path chainLink1 = bibTempDir.resolve("chainLink1"); |
| 476 | + Path chainLink1 = bibTempDir.resolve("chainLink1_" + UUID.randomUUID()); |
485 | 477 | Files.createSymbolicLink(chainLink1, chainLink2); |
486 | | - result.add(Arguments.of(chainedFile, List.of(chainLink1), Path.of("chained.pdf"), "Chained symlink resolves to relative")); |
487 | 478 |
|
488 | | - // realDir/nestedLink -> realDir/nested |
489 | | - // realDir/nested/nested.pdf |
490 | | - Path nestedDir = realDir.resolve("nested"); |
| 479 | + Path result = FileUtil.relativize(chainedFile, List.of(chainLink1)); |
| 480 | + assertEquals(Path.of("chained.pdf"), result, "Chained symlink resolves to relative"); |
| 481 | + } |
| 482 | + |
| 483 | + @Test |
| 484 | + @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
| 485 | + void nestedRelativizeSymlinks() throws IOException { |
| 486 | + Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID()); |
| 487 | + Files.createDirectories(realDir); |
| 488 | + |
| 489 | + Path nestedDir = realDir.resolve("nested_" + UUID.randomUUID()); |
491 | 490 | Files.createDirectories(nestedDir); |
492 | 491 | Path nestedFile = Files.createFile(nestedDir.resolve("nested.pdf")); |
493 | | - Path nestedSymlink = realDir.resolve("nestedLink"); |
| 492 | + Path nestedSymlink = realDir.resolve("nestedLink_" + UUID.randomUUID()); |
494 | 493 | Files.createSymbolicLink(nestedSymlink, nestedDir); |
495 | | - result.add(Arguments.of(nestedFile, List.of(nestedSymlink), Path.of("nested.pdf"), "Nested symlink resolves to relative")); |
496 | 494 |
|
497 | | - // symlinkDir -> realDir |
498 | | - // outside.pdf |
| 495 | + Path result = FileUtil.relativize(nestedFile, List.of(nestedSymlink)); |
| 496 | + assertEquals(Path.of("nested.pdf"), result, "Nested symlink resolves to relative"); |
| 497 | + } |
| 498 | + |
| 499 | + @Test |
| 500 | + @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
| 501 | + void unrelatedFileRemainsAbsolute() throws IOException { |
| 502 | + Path realDir = bibTempDir.resolve("realDir_" + UUID.randomUUID()); |
| 503 | + Files.createDirectories(realDir); |
| 504 | + Path symlinkDir = bibTempDir.resolve("symlinkDir_" + UUID.randomUUID()); |
| 505 | + Files.createSymbolicLink(symlinkDir, realDir); |
| 506 | + |
499 | 507 | Path outsideFile = Files.createFile(bibTempDir.resolve("outside.pdf")); |
500 | | - result.add(Arguments.of(outsideFile, List.of(symlinkDir), outsideFile, "Unrelated file remains absolute")); |
501 | 508 |
|
502 | | - // symlink chain escaping base dir (ignored test case, see <https://github.com/JabRef/jabref/issues/12995#issuecomment-3065149862>) |
| 509 | + Path result = FileUtil.relativize(outsideFile, List.of(symlinkDir)); |
| 510 | + assertEquals(outsideFile, result, "Unrelated file remains absolute"); |
| 511 | + } |
| 512 | + |
| 513 | + @Test |
| 514 | + @DisabledOnOs(value = org.junit.jupiter.api.condition.OS.WINDOWS, disabledReason = "Symlink behavior unreliable on windows") |
| 515 | + void symlinkEscapeCaseIgnored() throws IOException { |
503 | 516 | Path veryPrivate = bibTempDir.resolve("veryprivate"); |
504 | 517 | Files.createDirectories(veryPrivate); |
505 | 518 | Path secretFile = Files.createFile(veryPrivate.resolve("a.pdf")); |
| 519 | + |
506 | 520 | Path expensive = bibTempDir.resolve("expensive"); |
507 | 521 | Files.createSymbolicLink(expensive, veryPrivate); |
508 | 522 | Path things = bibTempDir.resolve("things"); |
509 | 523 | Files.createSymbolicLink(things, expensive); |
| 524 | + |
510 | 525 | Path libDir = bibTempDir.resolve("lib"); |
511 | 526 | Files.createDirectories(libDir); |
512 | | - Path bibFile = Files.createFile(libDir.resolve("bib.bib")); |
513 | | - result.add(Arguments.of(secretFile, List.of(things), secretFile, "IGNORED: Symlink chain escaping base dir (#12995 comment)")); |
| 527 | + Files.createFile(libDir.resolve("bib.bib")); |
514 | 528 |
|
515 | | - return result.stream(); |
| 529 | + org.junit.jupiter.api.Assumptions.assumeTrue(false, "IGNORED: Symlink chain escaping base dir, see <https://github.com/JabRef/jabref/issues/12995#issuecomment-3065149862>"); |
| 530 | + Path result = FileUtil.relativize(secretFile, List.of(things)); |
| 531 | + assertEquals(secretFile, result); |
516 | 532 | } |
517 | 533 |
|
518 | 534 | /** |
|
0 commit comments