22
33import java .io .ByteArrayInputStream ;
44import java .io .ByteArrayOutputStream ;
5+ import java .io .IOException ;
6+ import java .io .InputStream ;
57import java .security .SecureRandom ;
68import java .security .Security ;
9+ import java .util .Calendar ;
710import java .util .Date ;
811import java .util .Iterator ;
12+ import java .util .TimeZone ;
913
1014import org .bouncycastle .bcpg .AEADAlgorithmTags ;
1115import org .bouncycastle .bcpg .ArmoredInputStream ;
2529import org .bouncycastle .crypto .params .X25519KeyGenerationParameters ;
2630import org .bouncycastle .jce .provider .BouncyCastleProvider ;
2731import org .bouncycastle .openpgp .PGPEncryptedDataList ;
32+ import org .bouncycastle .openpgp .PGPLiteralData ;
2833import org .bouncycastle .openpgp .PGPObjectFactory ;
2934import org .bouncycastle .openpgp .PGPPBEEncryptedData ;
3035import org .bouncycastle .openpgp .PGPPublicKey ;
3136import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
3237import org .bouncycastle .openpgp .operator .bc .BcPGPKeyPair ;
38+ import org .bouncycastle .openpgp .operator .jcajce .JcaKeyFingerprintCalculator ;
3339import org .bouncycastle .util .Arrays ;
40+ import org .bouncycastle .util .Pack ;
3441import org .bouncycastle .util .Strings ;
3542import org .bouncycastle .util .test .SimpleTest ;
3643
3744public class BcpgGeneralTest
3845 extends SimpleTest
3946{
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+
4061 public static void main (String [] args )
4162 {
4263 Security .addProvider (new BouncyCastleProvider ());
@@ -54,13 +75,69 @@ public String getName()
5475 public void performTest ()
5576 throws Exception
5677 {
78+ testReadTime ();
79+ testReadTime2 ();
5780 //testS2K();
5881 testExceptions ();
5982 testECDHPublicBCPGKey ();
6083 // Tests for PreferredAEADCiphersuites
6184 testPreferredAEADCiphersuites ();
6285 }
6386
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+
93+ // StreamUtil.readTime
94+ static long readTime (BCPGInputStream in )
95+ throws IOException
96+ {
97+ return ((long )read4OctetLength (in ) & 0xFFFFFFFFL ) * 1000L ;
98+ }
99+
100+ public void testReadTime ()
101+ throws IOException
102+ {
103+ Calendar calendar = Calendar .getInstance ();
104+ calendar .set (2074 , Calendar .JANUARY , 1 , 0 , 0 , 0 );
105+ calendar .set (Calendar .MILLISECOND , 0 );
106+
107+ Date tmp = calendar .getTime ();
108+ long time = tmp .getTime () / 1000L * 1000L ;
109+ byte [] date = Pack .intToBigEndian ((int )(time / 1000L ));
110+
111+ ByteArrayInputStream bs = new ByteArrayInputStream (date );
112+ BCPGInputStream stream = new BCPGInputStream (bs );
113+ long rlt = readTime (stream );
114+ isTrue (rlt == time );
115+
116+ time = Long .MAX_VALUE / 1000L * 1000L ;
117+ date = Pack .intToBigEndian ((int )(time / 1000L ));
118+ bs = new ByteArrayInputStream (date );
119+ stream = new BCPGInputStream (bs );
120+ rlt = readTime (stream );
121+ byte [] date2 = Pack .intToBigEndian ((int )(rlt / 1000L ));
122+ isTrue (Arrays .areEqual (date , date2 ));
123+ }
124+
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+
64141 public void testPreferredAEADCiphersuites ()
65142 throws Exception
66143 {
0 commit comments