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 ;
912
3134import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
3235import org .bouncycastle .openpgp .operator .bc .BcPGPKeyPair ;
3336import org .bouncycastle .util .Arrays ;
37+ import org .bouncycastle .util .Pack ;
3438import org .bouncycastle .util .Strings ;
3539import org .bouncycastle .util .test .SimpleTest ;
3640
@@ -54,13 +58,46 @@ public String getName()
5458 public void performTest ()
5559 throws Exception
5660 {
61+ testReadTime ();
5762 //testS2K();
5863 testExceptions ();
5964 testECDHPublicBCPGKey ();
6065 // Tests for PreferredAEADCiphersuites
6166 testPreferredAEADCiphersuites ();
6267 }
6368
69+ // StreamUtil.readTime
70+ static long readTime (BCPGInputStream in )
71+ throws IOException
72+ {
73+ return (((long ) in .read () << 24 | in .read () << 16 | in .read () << 8 | in .read ()) & 0xFFFFFFFFL ) * 1000L ;
74+ }
75+
76+ public void testReadTime ()
77+ throws IOException
78+ {
79+ Calendar calendar = Calendar .getInstance ();
80+ calendar .set (2074 , Calendar .JANUARY , 1 , 0 , 0 , 0 );
81+ calendar .set (Calendar .MILLISECOND , 0 );
82+
83+ Date tmp = calendar .getTime ();
84+ long time = tmp .getTime () / 1000L * 1000L ;
85+ byte [] date = Pack .intToBigEndian ((int )(time / 1000L ));
86+
87+ ByteArrayInputStream bs = new ByteArrayInputStream (date );
88+ BCPGInputStream stream = new BCPGInputStream (bs );
89+ long rlt = readTime (stream );
90+ isTrue (rlt == time );
91+
92+ time = Long .MAX_VALUE / 1000L * 1000L ;
93+ date = Pack .intToBigEndian ((int )(time / 1000L ));
94+ bs = new ByteArrayInputStream (date );
95+ stream = new BCPGInputStream (bs );
96+ rlt = readTime (stream );
97+ byte [] date2 = Pack .intToBigEndian ((int )(rlt / 1000L ));
98+ isTrue (Arrays .areEqual (date , date2 ));
99+ }
100+
64101 public void testPreferredAEADCiphersuites ()
65102 throws Exception
66103 {
0 commit comments