Skip to content

Commit 13d0d41

Browse files
author
gefeili
committed
Add test for readtime that is related to LiteralDataPacket, PGPLiteralData, PGPObjectFactory and ArmoredInputStream.
1 parent 8c92657 commit 13d0d41

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

pg/src/test/java/org/bouncycastle/openpgp/test/BcpgGeneralTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Calendar;
1010
import java.util.Date;
1111
import java.util.Iterator;
12+
import java.util.TimeZone;
1213

1314
import org.bouncycastle.bcpg.AEADAlgorithmTags;
1415
import org.bouncycastle.bcpg.ArmoredInputStream;
@@ -28,11 +29,13 @@
2829
import org.bouncycastle.crypto.params.X25519KeyGenerationParameters;
2930
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3031
import org.bouncycastle.openpgp.PGPEncryptedDataList;
32+
import org.bouncycastle.openpgp.PGPLiteralData;
3133
import org.bouncycastle.openpgp.PGPObjectFactory;
3234
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
3335
import org.bouncycastle.openpgp.PGPPublicKey;
3436
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
3537
import org.bouncycastle.openpgp.operator.bc.BcPGPKeyPair;
38+
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
3639
import org.bouncycastle.util.Arrays;
3740
import org.bouncycastle.util.Pack;
3841
import org.bouncycastle.util.Strings;
@@ -41,6 +44,20 @@
4144
public class BcpgGeneralTest
4245
extends SimpleTest
4346
{
47+
/*
48+
* Format: Binary data
49+
Filename: "hello.txt"
50+
Timestamp: 2104-06-26 14:42:55 UTC
51+
Content: "Hello, world!\n"
52+
* */
53+
byte[] message = Strings.toUTF8ByteArray("-----BEGIN PGP MESSAGE-----\n" +
54+
"\n" +
55+
"yx1iCWhlbGxvLnR4dPz1TW9IZWxsbywgd29ybGQhCg==\n" +
56+
"=3swl\n" +
57+
"-----END PGP MESSAGE-----");
58+
59+
60+
4461
public static void main(String[] args)
4562
{
4663
Security.addProvider(new BouncyCastleProvider());
@@ -59,18 +76,25 @@ public void performTest()
5976
throws Exception
6077
{
6178
testReadTime();
79+
testReadTime2();
6280
//testS2K();
6381
testExceptions();
6482
testECDHPublicBCPGKey();
6583
// Tests for PreferredAEADCiphersuites
6684
testPreferredAEADCiphersuites();
6785
}
6886

87+
static int read4OctetLength(InputStream in)
88+
throws IOException
89+
{
90+
return (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
91+
}
92+
6993
// StreamUtil.readTime
7094
static long readTime(BCPGInputStream in)
7195
throws IOException
7296
{
73-
return (((long) in.read() << 24 | in.read() << 16 | in.read() << 8 | in.read()) & 0xFFFFFFFFL) * 1000L;
97+
return ((long)read4OctetLength(in) & 0xFFFFFFFFL) * 1000L;
7498
}
7599

76100
public void testReadTime()
@@ -98,6 +122,22 @@ public void testReadTime()
98122
isTrue(Arrays.areEqual(date, date2));
99123
}
100124

125+
public void testReadTime2()
126+
throws Exception
127+
{
128+
PGPObjectFactory pgpObjectFactoryOfTestFile = new PGPObjectFactory(
129+
new ArmoredInputStream(new ByteArrayInputStream(message)), new JcaKeyFingerprintCalculator());
130+
PGPLiteralData ld = (PGPLiteralData)pgpObjectFactoryOfTestFile.nextObject();
131+
Date date = ld.getModificationTime();
132+
133+
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
134+
calendar.set(2104, Calendar.JUNE, 26, 14, 42, 55);
135+
calendar.set(Calendar.MILLISECOND, 0);
136+
Date expected = calendar.getTime();
137+
138+
isTrue(date.equals(expected));
139+
}
140+
101141
public void testPreferredAEADCiphersuites()
102142
throws Exception
103143
{

0 commit comments

Comments
 (0)