Skip to content

Commit

Permalink
fix:added additional exception test
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Jul 22, 2024
1 parent cf15057 commit e447c63
Showing 1 changed file with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,42 +149,78 @@ public void testRemoveBlockWritePerms() throws IOException {
// Change the permissions on the block node root directory
removeBlockWritePerms(testConfig);
BlockWriter<BlockItem> 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<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
final BlockWriter<BlockItem> 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<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
Optional<Block> 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<String, String> testProperties = Map.of(JUNIT, "invalid-path");
ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build();
Config testConfig = Config.builder(testConfigSource).build();
final Map<String, String> 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<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
final Set<PosixFilePermission> 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<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_WRITE);
final Set<PosixFilePermission> 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<PosixFilePermission> 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<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
Files.setPosixFilePermissions(blockItemPath, perms);
}
}

0 comments on commit e447c63

Please sign in to comment.