public void testUncheckedIOException() throws IOException { Path triggerFile = testFolder.resolve(Paths.get("dir2", "IOException")); Files.createFile(triggerFile); Path triggerDir = testFolder.resolve(Paths.get("empty", "IOException")); Files.createDirectories(triggerDir); Files.createFile(triggerDir.resolve("file")); FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance(); FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null); try { fsp.setFaultyMode(false); Path fakeRoot = fs.getRoot(); try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) { // only one file s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException")); } try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); // ordered as depth-first assertEquals(result, new String[] {"empty", "IOException", "file"}); } fsp.setFaultyMode(true); try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) { s.forEach(path -> fail("should have caused exception")); } catch (UncheckedIOException uioe) { assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); } try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to IOException"); } catch (UncheckedIOException uioe) { assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); } try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty").resolve("IOException"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to IOException"); } catch (IOException ioe) { assertTrue(ioe instanceof FaultyFileSystem.FaultyException); } catch (UncheckedIOException ex) { fail("Top level should be repored as is"); } } finally { // Cleanup if (fs != null) { fs.close(); } Files.delete(triggerFile); TestUtil.removeAll(triggerDir); } }
public void testDirectoryIteratorException() throws IOException { Path dir = testFolder.resolve("dir2"); Path trigger = dir.resolve("DirectoryIteratorException"); Files.createFile(trigger); FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance(); FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(dir, null); try { fsp.setFaultyMode(false); Path fakeRoot = fs.getRoot(); try { try (Stream<Path> s = Files.list(fakeRoot)) { s.forEach( path -> assertEquals(path.getFileName().toString(), "DirectoryIteratorException")); } } catch (UncheckedIOException uioe) { fail("Unexpected exception."); } fsp.setFaultyMode(true); try { try (DirectoryStream<Path> ds = Files.newDirectoryStream(fakeRoot)) { Iterator<Path> itor = ds.iterator(); while (itor.hasNext()) { itor.next(); } } fail("Shoule throw DirectoryIteratorException"); } catch (DirectoryIteratorException die) { } try { try (Stream<Path> s = Files.list(fakeRoot)) { s.forEach(path -> fail("should not get here")); } } catch (UncheckedIOException uioe) { assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException); } catch (DirectoryIteratorException die) { fail("Should have been converted into UncheckedIOException."); } } finally { // Cleanup if (fs != null) { fs.close(); } Files.delete(trigger); } }
public void testSecurityException() throws IOException { Path empty = testFolder.resolve("empty"); Path triggerFile = Files.createFile(empty.resolve("SecurityException")); Path sampleFile = Files.createDirectories(empty.resolve("sample")); Path dir2 = testFolder.resolve("dir2"); Path triggerDir = Files.createDirectories(dir2.resolve("SecurityException")); Files.createFile(triggerDir.resolve("fileInSE")); Path sample = Files.createFile(dir2.resolve("file")); Path triggerLink = null; Path linkTriggerDir = null; Path linkTriggerFile = null; if (supportsLinks) { Path dir = testFolder.resolve("dir"); triggerLink = Files.createSymbolicLink(dir.resolve("SecurityException"), empty); linkTriggerDir = Files.createSymbolicLink(dir.resolve("lnDirSE"), triggerDir); linkTriggerFile = Files.createSymbolicLink(dir.resolve("lnFileSE"), triggerFile); } FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance(); FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null); try { fsp.setFaultyMode(false); Path fakeRoot = fs.getRoot(); // validate setting try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"SecurityException", "sample"}); } try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"dir2", "SecurityException", "fileInSE", "file"}); } if (supportsLinks) { try (Stream<Path> s = Files.list(fakeRoot.resolve("dir"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder( result, new String[] {"d1", "f1", "lnDir2", "SecurityException", "lnDirSE", "lnFileSE"}); } } // execute test fsp.setFaultyMode(true); // ignore file cause SecurityException try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"empty", "sample"}); } // skip folder cause SecurityException try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"dir2", "file"}); } if (supportsLinks) { // not following links try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder( result, new String[] {"dir", "d1", "f1", "lnDir2", "lnDirSE", "lnFileSE"}); } // following links try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); // ?? Should fileInSE show up? // With FaultyFS, it does as no exception thrown for link to "SecurityException" with read // on "lnXxxSE" assertEqualsNoOrder( result, new String[] { "dir", "d1", "f1", "lnDir2", "file", "lnDirSE", "lnFileSE", "fileInSE" }); } } // list instead of walk try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"sample"}); } try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); assertEqualsNoOrder(result, new String[] {"file"}); } // root cause SecurityException should be reported try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2").resolve("SecurityException"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to SecurityException"); } catch (SecurityException se) { assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException); } // Walk a file cause SecurityException, we should get SE try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir").resolve("SecurityException"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to SecurityException"); } catch (SecurityException se) { assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException); } // List a file cause SecurityException, we should get SE as cannot read attribute try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2").resolve("SecurityException"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to SecurityException"); } catch (SecurityException se) { assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException); } try (Stream<Path> s = Files.list(fakeRoot.resolve("dir").resolve("SecurityException"))) { String[] result = s.map(path -> path.getFileName().toString()).toArray(String[]::new); fail("should not reach here due to SecurityException"); } catch (SecurityException se) { assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException); } } finally { // Cleanup if (fs != null) { fs.close(); } if (supportsLinks) { Files.delete(triggerLink); Files.delete(linkTriggerDir); Files.delete(linkTriggerFile); } Files.delete(triggerFile); Files.delete(sampleFile); Files.delete(sample); TestUtil.removeAll(triggerDir); } }