google-nomulus/javatests/google/registry/rde/RydeTarTest.java
guyben 801c8efbc1 Move the RDE TAR file encoding to a dedicated file
The "tar file encoding" saves the file + metadata (filename and modification) in a "tar" format that is required in the RDE spec, even though it only contains a single file.

This is only relevant for RyDE, and not for Ghostryde. In fact, the only reason Ghostryde exists is to not have the TAR layer.

Currently we only encrypt RyDE, so we only need the TAR encoding. We plan to add decryption ability so we can test files we sent to IronMountain if there's a problem - so we will need TAR decoding for that.

The new file - RydeTar.java - has both encoding and decoding. We keep the format used for all other Input/OutputStreams for consistency, even though in this case it could be a private part of the RyDE encoder / decoder.

This is one of a series of CLs - each merging a single "part" of the encoding.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=208056757
2018-08-10 13:46:48 -04:00

54 lines
2 KiB
Java

// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.rde;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.io.ByteStreams;
import google.registry.testing.ShardableTestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.joda.time.DateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public final class RydeTarTest extends ShardableTestCase {
@Test
public void testWriteRead() throws Exception {
byte[] expectedContent = "Testing 1, 2, 3".getBytes(UTF_8);
String expectedFilename = "myFile.xml";
DateTime expectedModified = DateTime.parse("2015-12-25T06:30:00.000Z");
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (OutputStream writer =
RydeTar.openTarWriter(output, expectedContent.length, expectedFilename, expectedModified)) {
writer.write(expectedContent);
}
byte[] tar = output.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(tar);
try (RydeTar.TarInputStream reader = RydeTar.openTarReader(input)) {
assertThat(reader.getFilename()).isEqualTo(expectedFilename);
assertThat(reader.getModified()).isEqualTo(expectedModified);
assertThat(ByteStreams.toByteArray(reader)).isEqualTo(expectedContent);
}
}
}