immediately close storage opened by persistence testing
What changed, and why it matters
This is a small test-only cleanup. It adds a helper method that closes a temporary storage object after checking whether a wallet file is valid, and replaces four inline storage openings in tests with calls to that helper. There is no change to production code or user-facing behavior.
No security action needed. Treat as routine test hygiene.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.java only. It introduces isWalletValid(File, CharSequence), which constructs a Storage object, loads the wallet (encrypted or unencrypted), validates it, and then calls storage.closeAndWait() in a finally block. Four assertion sites that previously instantiated Storage inline without closing it are refactored to use this helper. This is a resource-management improvement in unit tests.
Changed components
src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.javaInspect captured patch +14 / −5
### src/test/java/com/sparrowwallet/sparrow/io/DbPersistenceTest.java
@@ -141,6 +141,15 @@ private Sha256Hash getFileHash(File file) throws Exception {
return Sha256Hash.of(Files.readAllBytes(file.toPath()));
}
+ private boolean isWalletValid(File walletFile, CharSequence password) throws Exception {
+ Storage storage = new Storage(PersistenceType.DB, walletFile);
+ try {
+ return (password == null ? storage.loadUnencryptedWallet() : storage.loadEncryptedWallet(password)).getWallet().isValid();
+ } finally {
+ storage.closeAndWait();
+ }
+ }
+
@Test
public void passwordChangeLeavesSiblingWalletUntouched() throws Exception {
Storage siblingStorage = createUnencryptedWallet("Savings.old");
@@ -153,8 +162,8 @@ public void passwordChangeLeavesSiblingWalletUntouched() throws Exception {
storage.closeAndWait();
Assertions.assertEquals(siblingHash, getFileHash(siblingFile), "sibling wallet file was rewritten by the password change");
- Assertions.assertTrue(new Storage(PersistenceType.DB, siblingFile).loadUnencryptedWallet().getWallet().isValid());
- Assertions.assertTrue(new Storage(PersistenceType.DB, storage.getWalletFile()).loadEncryptedWallet("pass").getWallet().isValid());
+ Assertions.assertTrue(isWalletValid(siblingFile, null));
+ Assertions.assertTrue(isWalletValid(storage.getWalletFile(), "pass"));
//The conversion must not leave the wallet copy or H2's temp.db behind in the wallets directory
String[] tempFiles = tempDir.toFile().list((dir, name) -> name.equals("temp.db") || name.startsWith("sparrowenc"));
@@ -172,7 +181,7 @@ public void passwordChangeAppliesToWalletNameContainingDot() throws Exception {
storage.closeAndWait();
Assertions.assertEquals(siblingHash, getFileHash(siblingStorage.getWalletFile()));
- Assertions.assertTrue(new Storage(PersistenceType.DB, storage.getWalletFile()).loadEncryptedWallet("pass").getWallet().isValid());
+ Assertions.assertTrue(isWalletValid(storage.getWalletFile(), "pass"));
}
@Test
@@ -187,7 +196,7 @@ public void walletNameContainingExtensionUsesItsOwnFile() throws Exception {
storage.closeAndWait();
Assertions.assertTrue(storage.getWalletFile().exists(), "wallet was written to a file other than the one tracked");
- Assertions.assertTrue(new Storage(PersistenceType.DB, storage.getWalletFile()).loadUnencryptedWallet().getWallet().isValid());
+ Assertions.assertTrue(isWalletValid(storage.getWalletFile(), null));
Assertions.assertEquals(otherHash, getFileHash(otherFile), "another wallet file was written by a wallet name containing the extension");
}
@@ -198,6 +207,6 @@ public void passwordRemovalDecryptsWalletFile() throws Exception {
setPassword(storage, null);
storage.closeAndWait();
- Assertions.assertTrue(new Storage(PersistenceType.DB, storage.getWalletFile()).loadUnencryptedWallet().getWallet().isValid());
+ Assertions.assertTrue(isWalletValid(storage.getWalletFile(), null));
}
}Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.