ProvidenceConfigUtil { static Path resolveFile(Path reference, String path) throws IOException { if (reference == null) { Path file = canonicalFileLocation(Paths.get(path)); if (Files.exists(file)) { if (Files.isRegularFile(file)) { return file; } throw new FileNotFoundException(path + " is a directory, expected file"); } throw new FileNotFoundException("File " + path + " not found"); } else if (path.startsWith("/")) { throw new FileNotFoundException("Absolute path includes not allowed: " + path); } else { reference = readCanonicalPath(reference); if (!Files.isDirectory(reference)) { reference = reference.getParent(); } Path file = canonicalFileLocation(reference.resolve(path)); if (Files.exists(file)) { if (Files.isRegularFile(file)) { return file; } throw new FileNotFoundException(path + " is a directory, expected file"); } throw new FileNotFoundException("Included file " + path + " not found"); } } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); static final String UNDEFINED; }
@Test public void testResolveFile() throws IOException { File test = tmp.newFolder("test"); File other = tmp.newFolder("other"); File f1_1 = new File(test, "test.cfg"); File f1_2 = new File(test, "same.cfg"); File f2_1 = new File(other, "other.cfg"); File f2_2 = tmp.newFile("third.cfg"); writeContentTo("a", f1_1); writeContentTo("a", f1_2); writeContentTo("a", f2_1); assertEquals(f1_1.getCanonicalFile().toPath(), resolveFile(null, tmp.getRoot() + "/test/test.cfg").toAbsolutePath()); assertEquals(f1_2.getCanonicalFile().toPath(), resolveFile(f1_1.toPath(), "same.cfg").toAbsolutePath()); assertEquals(f2_1.getCanonicalFile().toPath(), resolveFile(f1_1.toPath(), "../other/other.cfg").toAbsolutePath()); assertEquals(f2_2.getCanonicalFile().toPath(), resolveFile(f1_1.toPath(), "../third.cfg").toAbsolutePath()); assertFileNotResolved(f1_1, "../", "../ is a directory, expected file"); assertFileNotResolved(f1_1, "../fourth.cfg", "Included file ../fourth.cfg not found"); assertFileNotResolved(f1_1, "fourth.cfg", "Included file fourth.cfg not found"); assertFileNotResolved(f1_1, "/fourth.cfg", "Absolute path includes not allowed: /fourth.cfg"); assertFileNotResolved(f1_1, "other/fourth.cfg", "Included file other/fourth.cfg not found"); assertFileNotResolved(f1_1, "../other", "../other is a directory, expected file"); assertFileNotResolved(f1_1, "other", "Included file other not found"); assertFileNotResolved(f1_1, "../../../../../../../../other", "Parent of root does not exist!"); assertFileNotResolved(null, "../", "../ is a directory, expected file"); assertFileNotResolved(null, "../fourth.cfg", "File ../fourth.cfg not found"); assertFileNotResolved(null, "fourth.cfg", "File fourth.cfg not found"); assertFileNotResolved(null, "/fourth.cfg", "File /fourth.cfg not found"); assertFileNotResolved(null, "other/fourth.cfg", "File other/fourth.cfg not found"); assertFileNotResolved(null, "../other", "File ../other not found"); assertFileNotResolved(null, "other", "File other not found"); }