mirror of
https://github.com/google/nomulus
synced 2026-09-18 22:14:23 +00:00
Set clock precision to milliseconds for Datetime->Instant migration (#2999)
Our existing precision is milliseconds so we want to stick with that for Instants. If we want to increase the precision globally after that we can do so all in one go post-migration, but for now, it would be a bad thing to have mixed precision going on just depending on whether a class happens to be migrated yet or not. This PR also migrates all existing DateTime.nowUtc() calls to use the Clock interface, so that when they are migrated they will get the benefit of this precision-setting as well. BUG= http://b/496985355
This commit is contained in:
@@ -324,12 +324,12 @@ public final class PosixTarHeader {
|
||||
private final byte[] header = new byte[HEADER_LENGTH];
|
||||
private boolean hasName = false;
|
||||
private boolean hasSize = false;
|
||||
private boolean hasMtime = false;
|
||||
|
||||
public Builder() {
|
||||
setMode(DEFAULT_MODE);
|
||||
setUid(DEFAULT_UID);
|
||||
setGid(DEFAULT_GID);
|
||||
setMtime(DateTime.now(UTC));
|
||||
setType(DEFAULT_TYPE);
|
||||
setMagic();
|
||||
setVersion();
|
||||
@@ -418,6 +418,7 @@ public final class PosixTarHeader {
|
||||
public Builder setMtime(DateTime mtime) {
|
||||
checkNotNull(mtime, "mtime");
|
||||
setField("mtime", 136, 12, String.format("%011o", mtime.getMillis() / MILLIS_PER_SECOND));
|
||||
hasMtime = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -483,8 +484,10 @@ public final class PosixTarHeader {
|
||||
public PosixTarHeader build() {
|
||||
checkState(hasName, "name not set");
|
||||
checkState(hasSize, "size not set");
|
||||
checkState(hasMtime, "mtime not set");
|
||||
hasName = false;
|
||||
hasSize = false;
|
||||
hasMtime = false;
|
||||
setChksum(); // Calculate the checksum last.
|
||||
return new PosixTarHeader(header.clone());
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
package google.registry.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.math.BigInteger;
|
||||
@@ -42,8 +41,6 @@ import org.joda.time.DateTime;
|
||||
public class SelfSignedCaCertificate {
|
||||
|
||||
private static final String DEFAULT_ISSUER_FQDN = "registry-test";
|
||||
private static final DateTime DEFAULT_NOT_BEFORE = DateTime.now(UTC).minusHours(1);
|
||||
private static final DateTime DEFAULT_NOT_AFTER = DateTime.now(UTC).plusDays(1);
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider();
|
||||
@@ -68,13 +65,16 @@ public class SelfSignedCaCertificate {
|
||||
return cert;
|
||||
}
|
||||
|
||||
public static SelfSignedCaCertificate create() throws Exception {
|
||||
public static SelfSignedCaCertificate create(Clock clock) throws Exception {
|
||||
return create(
|
||||
keyGen.generateKeyPair(), DEFAULT_ISSUER_FQDN, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
|
||||
keyGen.generateKeyPair(),
|
||||
DEFAULT_ISSUER_FQDN,
|
||||
clock.nowUtc().minusHours(1),
|
||||
clock.nowUtc().plusDays(1));
|
||||
}
|
||||
|
||||
public static SelfSignedCaCertificate create(String fqdn) throws Exception {
|
||||
return create(fqdn, DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER);
|
||||
public static SelfSignedCaCertificate create(String fqdn, Clock clock) throws Exception {
|
||||
return create(fqdn, clock.nowUtc().minusHours(1), clock.nowUtc().plusDays(1));
|
||||
}
|
||||
|
||||
public static SelfSignedCaCertificate create(String fqdn, DateTime from, DateTime to)
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -199,7 +200,11 @@ class PosixTarHeaderTest {
|
||||
@Test
|
||||
void testBadChecksum() {
|
||||
PosixTarHeader header =
|
||||
new PosixTarHeader.Builder().setName("(◕‿◕).txt").setSize(31337).build();
|
||||
new PosixTarHeader.Builder()
|
||||
.setName("(◕‿◕).txt")
|
||||
.setSize(31337)
|
||||
.setMtime(DateTime.now(DateTimeZone.UTC))
|
||||
.build();
|
||||
byte[] bytes = header.getBytes();
|
||||
bytes[150] = '0';
|
||||
bytes[151] = '0';
|
||||
@@ -234,6 +239,7 @@ class PosixTarHeaderTest {
|
||||
new PosixTarHeader.Builder()
|
||||
.setName("(•︵•).txt") // Awwwww! It looks so sad...
|
||||
.setSize(123)
|
||||
.setMtime(DateTime.now(DateTimeZone.UTC))
|
||||
.build())
|
||||
.testEquals();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user