Skip to content

Commit

Permalink
Issue checkstyle#12238: Unable to persist cache file if target direct…
Browse files Browse the repository at this point in the history
…ory is a symbolic link
  • Loading branch information
piyush kumar sadangi authored and rnveach committed Jan 1, 2024
1 parent 86b9a28 commit 2e99389
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,21 @@ public void load() throws IOException {
*/
public void persist() throws IOException {
final Path path = Paths.get(fileName);
final Path directory = path.getParent();
Path directory = path.getParent();

if (directory != null) {
if (Files.isSymbolicLink(directory)) {
final Path actualDir = directory.toRealPath();

if (Files.isDirectory(actualDir)) {
directory = actualDir;
}
else {
throw new IOException(
"Resolved symbolic link " + directory
+ " is not a directory.");
}
}
Files.createDirectories(directory);
}
try (OutputStream out = Files.newOutputStream(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,97 @@ public void testPathToCacheContainsOnlyFileName() throws IOException {

// no exception expected
cache.persist();

assertWithMessage("Cache file does not exist")
.that(Files.exists(filePath))
.isTrue();
Files.delete(filePath);
}

@Test
public void testPersistWithSymbolicLinkToDirectory() throws IOException {
final Path tempDirectory = Files.createTempDirectory("tempDir");
final Path symbolicLinkDirectory = Files.createTempDirectory("symbolicLinkDir")
.resolve("symbolicLink");
Files.createSymbolicLink(symbolicLinkDirectory, tempDirectory);

final Configuration config = new DefaultConfiguration("myName");
final String cacheFilePath = symbolicLinkDirectory.resolve("cache.temp").toString();
final PropertyCacheFile cache = new PropertyCacheFile(config, cacheFilePath);

cache.persist();

final Path expectedFilePath = tempDirectory.resolve("cache.temp");
assertWithMessage("Cache file should be created in the actual directory")
.that(Files.exists(expectedFilePath))
.isTrue();
}

@Test
public void testSymbolicLinkResolution() throws IOException {
final Path tempDirectory = Files.createTempDirectory("tempDir");
final Path symbolicLinkDirectory = Files.createTempDirectory("symbolicLinkDir")
.resolve("symbolicLink");
Files.createSymbolicLink(symbolicLinkDirectory, tempDirectory);

final Configuration config = new DefaultConfiguration("myName");
final String cacheFilePath = symbolicLinkDirectory.resolve("cache.temp").toString();
final PropertyCacheFile cache = new PropertyCacheFile(config, cacheFilePath);

cache.persist();

final Path expectedFilePath = tempDirectory.resolve("cache.temp");
assertWithMessage(
"Cache file should be created in the actual directory.")
.that(Files.exists(expectedFilePath))
.isTrue();
}

@Test
public void testSymbolicLinkToNonDirectory() throws IOException {
final Path tempFile = Files.createTempFile("tempFile", null);
final Path symbolicLinkDirectory = Files.createTempDirectory("symbolicLinkDir");
final Path symbolicLink = symbolicLinkDirectory.resolve("symbolicLink");
Files.createSymbolicLink(symbolicLink, tempFile);

final Configuration config = new DefaultConfiguration("myName");
final String cacheFilePath = symbolicLink.resolve("cache.temp").toString();
final PropertyCacheFile cache = new PropertyCacheFile(config, cacheFilePath);

final IOException thrown = assertThrows(IOException.class, cache::persist);

final String expectedMessage = "Resolved symbolic link " + symbolicLink
+ " is not a directory.";

assertWithMessage(
"Expected IOException when symbolicLink is not a directory")
.that(thrown.getMessage())
.contains(expectedMessage);
}

@Test
public void testMultipleSymbolicLinkResolution() throws IOException {
final Path actualDirectory = Files.createTempDirectory("actualDir");
final Path firstSymbolicLink = Files.createTempDirectory("firstLinkDir")
.resolve("firstLink");
Files.createSymbolicLink(firstSymbolicLink, actualDirectory);

final Path secondSymbolicLink = Files.createTempDirectory("secondLinkDir")
.resolve("secondLink");
Files.createSymbolicLink(secondSymbolicLink, firstSymbolicLink);

final Configuration config = new DefaultConfiguration("myName");
final String cacheFilePath = secondSymbolicLink.resolve("cache.temp").toString();
final PropertyCacheFile cache = new PropertyCacheFile(config, cacheFilePath);

cache.persist();

final Path expectedFilePath = actualDirectory.resolve("cache.temp");
assertWithMessage("Cache file should be created in the final actual directory")
.that(Files.exists(expectedFilePath))
.isTrue();
}

@Test
public void testChangeInConfig() throws Exception {
final DefaultConfiguration config = new DefaultConfiguration("myConfig");
Expand Down

0 comments on commit 2e99389

Please sign in to comment.