From ee804df4e9fce47ff92a39524646c4aba0a90ddc Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 27 Oct 2022 20:10:48 +0700 Subject: DateTimeUtilities improvements: - DateTimeToUnixMs converts to UTC - UnixMsToDateTime checks input range --- crypto/test/src/openpgp/test/DSA2Test.cs | 10 +++++----- crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs | 9 +++++---- crypto/test/src/openpgp/test/PGPDSATest.cs | 17 ++++++++++------- crypto/test/src/openpgp/test/PGPPBETest.cs | 16 ++++++++-------- crypto/test/src/openpgp/test/PGPRSATest.cs | 18 ++++++++++-------- crypto/test/src/openpgp/test/PGPSignatureTest.cs | 4 ++-- 6 files changed, 40 insertions(+), 34 deletions(-) (limited to 'crypto/test/src/openpgp') diff --git a/crypto/test/src/openpgp/test/DSA2Test.cs b/crypto/test/src/openpgp/test/DSA2Test.cs index 883ae317f..507afceae 100644 --- a/crypto/test/src/openpgp/test/DSA2Test.cs +++ b/crypto/test/src/openpgp/test/DSA2Test.cs @@ -4,6 +4,7 @@ using System.Text; using NUnit.Framework; +using Org.BouncyCastle.Utilities.Date; using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests @@ -126,16 +127,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); -// Date testDate = new Date((System.currentTimeMillis() / 1000) * 1000); - DateTime testDate = new DateTime( - (DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond); + DateTime modificationTime = DateTimeUtilities.UnixMsToDateTime( + DateTimeUtilities.CurrentUnixMs() / 1000 * 1000); Stream lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - testDate); + modificationTime); int ch; while ((ch = testIn.ReadByte()) >= 0) @@ -156,7 +156,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests Assert.AreEqual(PublicKeyAlgorithmTag.Dsa, ops.KeyAlgorithm); PgpLiteralData p2 = (PgpLiteralData)pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDate)) + if (!p2.ModificationTime.Equals(modificationTime)) { Assert.Fail("Modification time not preserved"); } diff --git a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs index 1b1f5b6db..07c4d7eb6 100644 --- a/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs +++ b/crypto/test/src/openpgp/test/PGPDSAElGamalTest.cs @@ -141,13 +141,14 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime testDateTime = new DateTime(1973, 7, 27); - Stream lOut = lGen.Open( + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + + Stream lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - testDateTime); + modificationTime); int ch; while ((ch = testIn.ReadByte()) >= 0) @@ -176,7 +177,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpOnePassSignature ops = p1[0]; PgpLiteralData p2 = (PgpLiteralData)pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } diff --git a/crypto/test/src/openpgp/test/PGPDSATest.cs b/crypto/test/src/openpgp/test/PGPDSATest.cs index d5ef8b5e9..70aca8e1f 100644 --- a/crypto/test/src/openpgp/test/PGPDSATest.cs +++ b/crypto/test/src/openpgp/test/PGPDSATest.cs @@ -305,13 +305,14 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime testDateTime = new DateTime(1973, 7, 27); + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + Stream lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - testDateTime); + modificationTime); int ch; while ((ch = testIn.ReadByte()) >= 0) @@ -335,7 +336,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpOnePassSignature ops = p1[0]; PgpLiteralData p2 = (PgpLiteralData) pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } @@ -429,13 +430,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bcOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime testDateTime = new DateTime(1973, 7, 27); - Stream lOut = lGen.Open( + + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + + Stream lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Text, "_CONSOLE", dataBytes.Length, - testDateTime); + modificationTime); while ((ch = testIn.ReadByte()) >= 0) { @@ -463,7 +466,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops = p1[0]; p2 = (PgpLiteralData) pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } diff --git a/crypto/test/src/openpgp/test/PGPPBETest.cs b/crypto/test/src/openpgp/test/PGPPBETest.cs index df40e1ef2..6b3718ecc 100644 --- a/crypto/test/src/openpgp/test/PGPPBETest.cs +++ b/crypto/test/src/openpgp/test/PGPPBETest.cs @@ -16,7 +16,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests public class PgpPbeTest : SimpleTest { - private static readonly DateTime TestDateTime = new DateTime(2003, 8, 29, 23, 35, 11, 0); + private static readonly DateTime ModificationTime = new DateTime(2003, 8, 29, 23, 35, 11, DateTimeKind.Utc); private static readonly byte[] enc1 = Base64.Decode( "jA0EAwMC5M5wWBP2HBZgySvUwWFAmMRLn7dWiZN6AkQMvpE3b6qwN3SSun7zInw2" @@ -66,9 +66,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests Fail("wrong filename in packet"); } - if (!ld.ModificationTime.Equals(TestDateTime)) + if (!ld.ModificationTime.Equals(ModificationTime)) { - Fail("wrong modification time in packet: " + ld.ModificationTime + " vs " + TestDateTime); + Fail("wrong modification time in packet: " + ld.ModificationTime + " vs " + ModificationTime); } Stream unc = ld.GetInputStream(); @@ -104,9 +104,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests { Fail("wrong filename in packet"); } - if (!ld.ModificationTime.Equals(TestDateTime)) + if (!ld.ModificationTime.Equals(ModificationTime)) { - Fail("wrong modification time in packet: " + ld.ModificationTime.Ticks + " " + TestDateTime.Ticks); + Fail("wrong modification time in packet: " + ld.ModificationTime.Ticks + " " + ModificationTime.Ticks); } Stream unc = ld.GetInputStream(); @@ -154,7 +154,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralData.Binary, PgpLiteralData.Console, text.Length, - TestDateTime); + ModificationTime); ldOut.Write(text, 0, text.Length); ldOut.Close(); @@ -222,7 +222,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests new UncloseableStream(comOut), PgpLiteralData.Binary, PgpLiteralData.Console, - TestDateTime, + ModificationTime, new byte[16]); ldOut.Write(test, 0, test.Length); @@ -317,7 +317,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralData.Binary, PgpLiteralData.Console, msg.Length, - TestDateTime); + ModificationTime); ldOut.Write(msg, 0, msg.Length); diff --git a/crypto/test/src/openpgp/test/PGPRSATest.cs b/crypto/test/src/openpgp/test/PGPRSATest.cs index 9d1dedd95..fa573de65 100644 --- a/crypto/test/src/openpgp/test/PGPRSATest.cs +++ b/crypto/test/src/openpgp/test/PGPRSATest.cs @@ -967,9 +967,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime testDateTime = new DateTime(1973, 7, 27); + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); Stream lOut = lGen.Open(new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", - dataBytes.Length, testDateTime); + dataBytes.Length, modificationTime); // TODO Need a stream object to automatically call Update? // (via ISigner implementation of PgpSignatureGenerator) @@ -999,7 +999,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops = p1[0]; p2 = (PgpLiteralData)pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } @@ -1045,7 +1045,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - testDateTime); + modificationTime); // TODO Need a stream object to automatically call Update? // (via ISigner implementation of PgpSignatureGenerator) @@ -1075,7 +1075,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests ops = p1[0]; p2 = (PgpLiteralData)pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } @@ -1139,13 +1139,15 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests sGen.GenerateOnePassVersion(false).Encode(bcOut); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); - DateTime testDateTime = new DateTime(1973, 7, 27); + + DateTime modificationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); + Stream lOut = lGen.Open( new UncloseableStream(bcOut), PgpLiteralData.Binary, "_CONSOLE", dataBytes.Length, - testDateTime); + modificationTime); // TODO Need a stream object to automatically call Update? // (via ISigner implementation of PgpSignatureGenerator) @@ -1176,7 +1178,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpOnePassSignature ops = p1[0]; PgpLiteralData p2 = (PgpLiteralData)pgpFact.NextPgpObject(); - if (!p2.ModificationTime.Equals(testDateTime)) + if (!p2.ModificationTime.Equals(modificationTime)) { Fail("Modification time not preserved"); } diff --git a/crypto/test/src/openpgp/test/PGPSignatureTest.cs b/crypto/test/src/openpgp/test/PGPSignatureTest.cs index e43eb5d8a..ea9df9567 100644 --- a/crypto/test/src/openpgp/test/PGPSignatureTest.cs +++ b/crypto/test/src/openpgp/test/PGPSignatureTest.cs @@ -551,7 +551,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests hashedGen = new PgpSignatureSubpacketGenerator(); - DateTime creationTime = new DateTime(1973, 7, 27); + DateTime creationTime = new DateTime(1973, 7, 27, 0, 0, 0, DateTimeKind.Utc); hashedGen.SetSignatureCreationTime(false, creationTime); sGen.SetHashedSubpackets(hashedGen.Generate()); @@ -997,7 +997,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests PgpSignature sig = sGen.Generate(); - if (sig.CreationTime == DateTimeUtilities.UnixMsToDateTime(0)) + if (DateTimeUtilities.DateTimeToUnixMs(sig.CreationTime) == 0) { Fail("creation time not set in v3 signature"); } -- cgit 1.4.1