Don't use Files.copy() when decrypting to stdout

Files.copy() attempts to delete the file if it already exists, which obviously
won't work very well for /dev/stdout.  Instead copy directly from the decoder
to standard output.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228384575
This commit is contained in:
mmuller 2019-01-08 13:06:24 -08:00 committed by Shicong Huang
parent 04a495bc99
commit 9e0b675275
5 changed files with 47 additions and 3 deletions

View file

@ -22,9 +22,12 @@ import google.registry.rde.Ghostryde;
import google.registry.testing.BouncyCastleProviderRule;
import google.registry.testing.FakeKeyringModule;
import google.registry.testing.InjectRule;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -58,12 +61,19 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule();
private Keyring keyring;
private PrintStream orgStdout;
@Before
public void before() {
keyring = new FakeKeyringModule().get();
command.rdeStagingDecryptionKey = keyring::getRdeStagingDecryptionKey;
command.rdeStagingEncryptionKey = keyring::getRdeStagingEncryptionKey;
orgStdout = System.out;
}
@After
public void after() {
System.setOut(orgStdout);
}
@Test
@ -112,4 +122,15 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
Path outFile = outDir.resolve("atrain.ghostryde.decrypt");
assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
}
@Test
public void testDecrypt_outputIsStdOut() throws Exception {
Path inFile = Paths.get(tmpDir.newFolder().toString()).resolve("atrain.ghostryde");
Files.write(
inFile, Ghostryde.encode(SONG_BY_CHRISTINA_ROSSETTI, keyring.getRdeStagingEncryptionKey()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
runCommand("--decrypt", "--input=" + inFile);
assertThat(out.toByteArray()).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
}
}