1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-22 19:08:00 +01:00

Create test util to write data to temp file

This commit is contained in:
Paul Schaub 2022-06-11 11:55:58 +02:00
parent 7074ff5f2f
commit 3f16c54867
2 changed files with 15 additions and 11 deletions

View file

@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@ -40,6 +41,17 @@ public class TestUtils {
return dir;
}
public static File writeTempFile(File tempDir, byte[] value) throws IOException {
File tempFile = new File(tempDir, randomString(10));
tempFile.createNewFile();
tempFile.deleteOnExit();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
fileOutputStream.write(value);
fileOutputStream.flush();
fileOutputStream.close();
return tempFile;
}
private static String randomString(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {

View file

@ -11,12 +11,9 @@ import static org.pgpainless.cli.TestUtils.ARMOR_PRIVATE_KEY_HEADER_BYTES;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import com.ginsberg.junit.exit.FailOnSystemExit;
@ -27,6 +24,7 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.cli.PGPainlessCLI;
import org.pgpainless.cli.TestUtils;
import org.pgpainless.key.info.KeyInfo;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.UnlockSecretKey;
@ -61,14 +59,8 @@ public class GenerateCertCmdTest {
PrintStream orig = System.out;
try {
// Write password to file
Path tempDir = Files.createTempDirectory("genkey");
File passwordFile = new File(tempDir.toFile(), "password");
passwordFile.createNewFile();
passwordFile.deleteOnExit();
FileOutputStream fileOut = new FileOutputStream(passwordFile);
fileOut.write("sw0rdf1sh".getBytes(StandardCharsets.UTF_8));
fileOut.flush();
fileOut.close();
File tempDir = TestUtils.createTempDirectory();
File passwordFile = TestUtils.writeTempFile(tempDir, "sw0rdf1sh".getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));