Simplify the Ghostryde API

First step of RDE encoding refactoring.

Creates a single InputStream (OutputStream) to decode (encode) Ghostryde files.
This replaces the 3 InputStreams (OutputStreams) that were needed before.

Also removes a lot of classes, and removes the "injection" of the Ghostryde
class. It's an encoding, there's no point in injecting it.

Finally, removed the buffer-size configuration and replaced with a static final
const value. It's just a buffer size - it doesn't actually affect much. There
are much more "important" fields that weren't configured (such as the
compression algorithm and whether or not to do integrity checks)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202319102
This commit is contained in:
guyben 2018-06-27 09:10:19 -07:00 committed by Ben McIlwain
parent bee3d6a5a4
commit 6ff48b7dae
15 changed files with 347 additions and 589 deletions

View file

@ -19,15 +19,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import google.registry.keyring.api.Keyring;
import google.registry.rde.Ghostryde;
import google.registry.rde.Ghostryde.DecodeResult;
import google.registry.testing.BouncyCastleProviderRule;
import google.registry.testing.FakeKeyringModule;
import google.registry.testing.InjectRule;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -35,7 +32,6 @@ import org.junit.Test;
/** Unit tests for {@link GhostrydeCommand}. */
public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
private static final DateTime MODIFIED_TIME = DateTime.parse("1984-12-18T04:20:00Z");
private static final byte[] SONG_BY_CHRISTINA_ROSSETTI = (""
+ "When I am dead, my dearest, \n"
+ " Sing no sad songs for me; \n"
@ -66,7 +62,6 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
@Before
public void before() {
keyring = new FakeKeyringModule().get();
command.ghostryde = new Ghostryde(1024);
command.rdeStagingDecryptionKey = keyring::getRdeStagingDecryptionKey;
command.rdeStagingEncryptionKey = keyring::getRdeStagingEncryptionKey;
}
@ -76,14 +71,10 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString());
Path outFile = Paths.get(tmpDir.newFile().toString());
Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI);
Files.setLastModifiedTime(inFile, FileTime.fromMillis(MODIFIED_TIME.getMillis()));
runCommand("--encrypt", "--input=" + inFile, "--output=" + outFile);
DecodeResult decoded = Ghostryde.decode(
Files.readAllBytes(outFile),
keyring.getRdeStagingDecryptionKey());
assertThat(decoded.getData()).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getName()).isEqualTo("atrain.txt");
assertThat(decoded.getModified()).isEqualTo(MODIFIED_TIME);
byte[] decoded =
Ghostryde.decode(Files.readAllBytes(outFile), keyring.getRdeStagingDecryptionKey());
assertThat(decoded).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
}
@Test
@ -91,45 +82,34 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString());
Path outDir = Paths.get(tmpDir.newFolder().toString());
Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI);
Files.setLastModifiedTime(inFile, FileTime.fromMillis(MODIFIED_TIME.getMillis()));
runCommand("--encrypt", "--input=" + inFile, "--output=" + outDir);
Path lenOutFile = outDir.resolve("atrain.txt.length");
assertThat(Ghostryde.readLength(Files.newInputStream(lenOutFile)))
.isEqualTo(SONG_BY_CHRISTINA_ROSSETTI.length);
Path outFile = outDir.resolve("atrain.txt.ghostryde");
DecodeResult decoded = Ghostryde.decode(
Files.readAllBytes(outFile),
keyring.getRdeStagingDecryptionKey());
assertThat(decoded.getData()).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getName()).isEqualTo("atrain.txt");
assertThat(decoded.getModified()).isEqualTo(MODIFIED_TIME);
byte[] decoded =
Ghostryde.decode(Files.readAllBytes(outFile), keyring.getRdeStagingDecryptionKey());
assertThat(decoded).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
}
@Test
public void testDecrypt_outputIsAFile_writesToFile() throws Exception {
Path inFile = Paths.get(tmpDir.newFile().toString());
Path outFile = Paths.get(tmpDir.newFile().toString());
Files.write(inFile, Ghostryde.encode(
SONG_BY_CHRISTINA_ROSSETTI,
keyring.getRdeStagingEncryptionKey(),
"atrain.txt",
MODIFIED_TIME));
Files.write(
inFile, Ghostryde.encode(SONG_BY_CHRISTINA_ROSSETTI, keyring.getRdeStagingEncryptionKey()));
runCommand("--decrypt", "--input=" + inFile, "--output=" + outFile);
assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(Files.getLastModifiedTime(outFile))
.isEqualTo(FileTime.fromMillis(MODIFIED_TIME.getMillis()));
}
@Test
public void testDecrypt_outputIsADirectory_writesToFileFromInnerName() throws Exception {
Path inFile = Paths.get(tmpDir.newFile().toString());
public void testDecrypt_outputIsADirectory_AppendsDecryptExtension() throws Exception {
Path inFile = Paths.get(tmpDir.newFolder().toString()).resolve("atrain.ghostryde");
Path outDir = Paths.get(tmpDir.newFolder().toString());
Files.write(inFile, Ghostryde.encode(
SONG_BY_CHRISTINA_ROSSETTI,
keyring.getRdeStagingEncryptionKey(),
"atrain.txt",
MODIFIED_TIME));
Files.write(
inFile, Ghostryde.encode(SONG_BY_CHRISTINA_ROSSETTI, keyring.getRdeStagingEncryptionKey()));
runCommand("--decrypt", "--input=" + inFile, "--output=" + outDir);
Path outFile = outDir.resolve("atrain.txt");
Path outFile = outDir.resolve("atrain.ghostryde.decrypt");
assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(Files.getLastModifiedTime(outFile))
.isEqualTo(FileTime.fromMillis(MODIFIED_TIME.getMillis()));
}
}