1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-17 18:02:05 +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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -40,6 +41,17 @@ public class TestUtils {
return dir; 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) { private static String randomString(int length) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) { 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.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import com.ginsberg.junit.exit.FailOnSystemExit; import com.ginsberg.junit.exit.FailOnSystemExit;
@ -27,6 +24,7 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.cli.PGPainlessCLI; import org.pgpainless.cli.PGPainlessCLI;
import org.pgpainless.cli.TestUtils;
import org.pgpainless.key.info.KeyInfo; import org.pgpainless.key.info.KeyInfo;
import org.pgpainless.key.info.KeyRingInfo; import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.UnlockSecretKey; import org.pgpainless.key.protection.UnlockSecretKey;
@ -61,14 +59,8 @@ public class GenerateCertCmdTest {
PrintStream orig = System.out; PrintStream orig = System.out;
try { try {
// Write password to file // Write password to file
Path tempDir = Files.createTempDirectory("genkey"); File tempDir = TestUtils.createTempDirectory();
File passwordFile = new File(tempDir.toFile(), "password"); File passwordFile = TestUtils.writeTempFile(tempDir, "sw0rdf1sh".getBytes(StandardCharsets.UTF_8));
passwordFile.createNewFile();
passwordFile.deleteOnExit();
FileOutputStream fileOut = new FileOutputStream(passwordFile);
fileOut.write("sw0rdf1sh".getBytes(StandardCharsets.UTF_8));
fileOut.flush();
fileOut.close();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out)); System.setOut(new PrintStream(out));