From e447c63fbe3190420b06b3eb2eb58db1b3901a79 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Mon, 22 Jul 2024 12:13:40 -0600 Subject: [PATCH] fix:added additional exception test Signed-off-by: Matt Peterson --- .../storage/BlockAsDirectoryTest.java | 64 +++++++++++++++---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java b/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java index f84759e4c..995064a69 100644 --- a/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java +++ b/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java @@ -149,42 +149,78 @@ public void testRemoveBlockWritePerms() throws IOException { // Change the permissions on the block node root directory removeBlockWritePerms(testConfig); BlockWriter blockWriter = new BlockAsDirWriter(JUNIT, testConfig); + + // The first BlockItem contains a header which will create a new block directory. + // This will fail here due to the lack of write permissions. Detect the exception + // thrown. assertThrows(AccessDeniedException.class, () -> blockWriter.write(blockItems.get(0))); } + @Test + public void testRemoveBlockItemWritePerms() throws IOException { + + final List blockItems = PersistTestUtils.generateBlockItems(1); + final BlockWriter blockWriter = new BlockAsDirWriter(JUNIT, testConfig); + + // Writing the header BlockItem will create the block directory. + blockWriter.write(blockItems.get(0)); + + // Change the permissions on the block node root directory + removeBlockWritePerms(1, testConfig); + + // Here, BlockItem writes won't throw an exception. + // We will rely on a different process to detect the invalid + // block and replace it. + for (int i = 2; i < blockItems.size(); i++) { + blockWriter.write(blockItems.get(1)); + } + + // Verify only the header block is on the file system + final BlockReader blockReader = new BlockAsDirReader(JUNIT, testConfig); + Optional blockOpt = blockReader.read(1); + assertFalse(blockOpt.isEmpty()); + + for (int i = 2; i < blockItems.size(); i++) { + blockOpt = blockReader.read(i); + assertTrue(blockOpt.isEmpty()); + } + } + @Test public void testConstructorWithInvalidPath() { - Map testProperties = Map.of(JUNIT, "invalid-path"); - ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build(); - Config testConfig = Config.builder(testConfigSource).build(); + final Map testProperties = Map.of(JUNIT, "invalid-path"); + final ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build(); + final Config testConfig = Config.builder(testConfigSource).build(); assertThrows(IllegalArgumentException.class, () -> new BlockAsDirWriter(JUNIT, testConfig)); } - private void removeBlockReadPerms(int blockNumber, Config config) throws IOException { - + private void removeBlockReadPerms(int blockNumber, final Config config) throws IOException { final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get()); final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber)); - - Set perms = PosixFilePermissions.fromString(NO_READ); + final Set perms = PosixFilePermissions.fromString(NO_READ); Files.setPosixFilePermissions(blockPath, perms); } - private void removeBlockWritePerms(Config config) throws IOException { - + private void removeBlockWritePerms(final Config config) throws IOException { final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get()); - - Set perms = PosixFilePermissions.fromString(NO_WRITE); + final Set perms = PosixFilePermissions.fromString(NO_WRITE); Files.setPosixFilePermissions(blockNodeRootPath, perms); } - private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config config) + private void removeBlockWritePerms(final int blockNumber, final Config config) throws IOException { + final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get()); + final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber)); + final Set perms = PosixFilePermissions.fromString(NO_WRITE); + Files.setPosixFilePermissions(blockPath, perms); + } + private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config config) + throws IOException { final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get()); final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber)); final Path blockItemPath = blockPath.resolve(blockItem + BLOCK_FILE_EXTENSION); - - Set perms = PosixFilePermissions.fromString(NO_READ); + final Set perms = PosixFilePermissions.fromString(NO_READ); Files.setPosixFilePermissions(blockItemPath, perms); } }