Skip to content

Commit

Permalink
Use TemporaryFolder in RadixKeyStoreTest (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasGasior1 authored Oct 3, 2023
2 parents 65a072c + 4823a66 commit 7343e0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
15 changes: 7 additions & 8 deletions common/src/test/java/com/radixdlt/crypto/RadixKeyStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@
import java.util.stream.IntStream;
import javax.crypto.SecretKey;
import org.bouncycastle.jcajce.PKCS12Key;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class RadixKeyStoreTest {
private static final String TEST_SECRET = "secret";
private static final String TEST_KS_FILENAME = "testfile.ks";
private static final char[] DEFAULT_TEST_PASSWORD = "radix".toCharArray();

@Rule public TemporaryFolder folder = new TemporaryFolder();

/** Test method for {@link RadixKeyStore#fromFile(java.io.File, char[], boolean)}. */
@Test
public void testFromFileCreate() throws IOException, KeyStoreException {
Expand Down Expand Up @@ -188,14 +192,9 @@ public void testReadKeyPairSuccess()

try (RadixKeyStore ks = RadixKeyStore.fromFile(file, storePassword, true)) {
assertTrue(file.exists());

ks.writeKeyPair(keyPairName, originalKeypair);
}

final File renamedFile = new File(TEST_KS_FILENAME);
file.renameTo(renamedFile);

try (RadixKeyStore ks = RadixKeyStore.fromFile(renamedFile, storePassword, false)) {
try (RadixKeyStore ks = RadixKeyStore.fromFile(file, storePassword, false)) {
var loadedKeypair = ks.readKeyPair(keyPairName, false);
assertThat(loadedKeypair).isEqualTo(originalKeypair);
}
Expand Down Expand Up @@ -327,8 +326,8 @@ public void test_read_various_key_stores() throws Exception {
}
}

private static File newFile(String filename) throws IOException {
File file = new File(filename);
private File newFile(String filename) throws IOException {
File file = folder.newFile(filename);
if (!Files.deleteIfExists(file.toPath())) {
// In this case we are fine if "file" does not exist and wasn't deleted.
}
Expand Down
20 changes: 12 additions & 8 deletions core/src/test/java/com/radixdlt/RadixNodeModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
import com.radixdlt.networks.Network;
import com.radixdlt.utils.properties.RuntimeProperties;
import java.io.File;
import java.io.IOException;
import org.apache.commons.cli.ParseException;
import org.assertj.core.util.Files;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -152,20 +152,24 @@ private RuntimeProperties createDefaultProperties() {
throw new RuntimeException(e);
}
doReturn("127.0.0.1").when(properties).get(eq("host.ip"), anyString());
var keyStore = new File("nonesuch.ks");
Files.delete(keyStore);
generateKeystore(keyStore);

doReturn("nonesuch.ks").when(properties).get(eq("node.key.path"), anyString());
return properties;
try {
final var keyStoreFile = new File(folder.newFolder(), "test-keystore.ks");
generateKeystore(keyStoreFile);
doReturn(keyStoreFile.getAbsolutePath())
.when(properties)
.get(eq("node.key.path"), anyString());
return properties;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void generateKeystore(File keyStore) {
try {
RadixKeyStore.fromFile(keyStore, "".toCharArray(), true)
.writeKeyPair("node", ECKeyPair.generateNew());
} catch (Exception e) {
throw new IllegalStateException("Unable to create keystore");
throw new IllegalStateException("Unable to create keystore", e);
}
}
}

0 comments on commit 7343e0a

Please sign in to comment.