Skip to content

Commit e447c63

Browse files
fix:added additional exception test
Signed-off-by: Matt Peterson <[email protected]>
1 parent cf15057 commit e447c63

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirectoryTest.java

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,42 +149,78 @@ public void testRemoveBlockWritePerms() throws IOException {
149149
// Change the permissions on the block node root directory
150150
removeBlockWritePerms(testConfig);
151151
BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
152+
153+
// The first BlockItem contains a header which will create a new block directory.
154+
// This will fail here due to the lack of write permissions. Detect the exception
155+
// thrown.
152156
assertThrows(AccessDeniedException.class, () -> blockWriter.write(blockItems.get(0)));
153157
}
154158

159+
@Test
160+
public void testRemoveBlockItemWritePerms() throws IOException {
161+
162+
final List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
163+
final BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
164+
165+
// Writing the header BlockItem will create the block directory.
166+
blockWriter.write(blockItems.get(0));
167+
168+
// Change the permissions on the block node root directory
169+
removeBlockWritePerms(1, testConfig);
170+
171+
// Here, BlockItem writes won't throw an exception.
172+
// We will rely on a different process to detect the invalid
173+
// block and replace it.
174+
for (int i = 2; i < blockItems.size(); i++) {
175+
blockWriter.write(blockItems.get(1));
176+
}
177+
178+
// Verify only the header block is on the file system
179+
final BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
180+
Optional<Block> blockOpt = blockReader.read(1);
181+
assertFalse(blockOpt.isEmpty());
182+
183+
for (int i = 2; i < blockItems.size(); i++) {
184+
blockOpt = blockReader.read(i);
185+
assertTrue(blockOpt.isEmpty());
186+
}
187+
}
188+
155189
@Test
156190
public void testConstructorWithInvalidPath() {
157-
Map<String, String> testProperties = Map.of(JUNIT, "invalid-path");
158-
ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build();
159-
Config testConfig = Config.builder(testConfigSource).build();
191+
final Map<String, String> testProperties = Map.of(JUNIT, "invalid-path");
192+
final ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build();
193+
final Config testConfig = Config.builder(testConfigSource).build();
160194
assertThrows(IllegalArgumentException.class, () -> new BlockAsDirWriter(JUNIT, testConfig));
161195
}
162196

163-
private void removeBlockReadPerms(int blockNumber, Config config) throws IOException {
164-
197+
private void removeBlockReadPerms(int blockNumber, final Config config) throws IOException {
165198
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
166199
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
167-
168-
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
200+
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
169201
Files.setPosixFilePermissions(blockPath, perms);
170202
}
171203

172-
private void removeBlockWritePerms(Config config) throws IOException {
173-
204+
private void removeBlockWritePerms(final Config config) throws IOException {
174205
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
175-
176-
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_WRITE);
206+
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_WRITE);
177207
Files.setPosixFilePermissions(blockNodeRootPath, perms);
178208
}
179209

180-
private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config config)
210+
private void removeBlockWritePerms(final int blockNumber, final Config config)
181211
throws IOException {
212+
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
213+
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
214+
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_WRITE);
215+
Files.setPosixFilePermissions(blockPath, perms);
216+
}
182217

218+
private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config config)
219+
throws IOException {
183220
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
184221
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
185222
final Path blockItemPath = blockPath.resolve(blockItem + BLOCK_FILE_EXTENSION);
186-
187-
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
223+
final Set<PosixFilePermission> perms = PosixFilePermissions.fromString(NO_READ);
188224
Files.setPosixFilePermissions(blockItemPath, perms);
189225
}
190226
}

0 commit comments

Comments
 (0)