You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2023/06/05 02:40:17 UTC

svn commit: r1910231 - in /pdfbox/trunk/examples: pom.xml src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Author: tilman
Date: Mon Jun  5 02:40:17 2023
New Revision: 1910231

URL: http://svn.apache.org/viewvc?rev=1910231&view=rev
Log:
PDFBOX-5619: check whether local time is out of sync

Modified:
    pdfbox/trunk/examples/pom.xml
    pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java

Modified: pdfbox/trunk/examples/pom.xml
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/pom.xml?rev=1910231&r1=1910230&r2=1910231&view=diff
==============================================================================
--- pdfbox/trunk/examples/pom.xml (original)
+++ pdfbox/trunk/examples/pom.xml Mon Jun  5 02:40:17 2023
@@ -96,6 +96,12 @@
         <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>commons-net</groupId>
+      <artifactId>commons-net</artifactId>
+      <version>3.9.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
         <groupId>org.apache.logging.log4j</groupId>
         <artifactId>log4j-core</artifactId>
         <version>${log4j2.version}</version>

Modified: pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java?rev=1910231&r1=1910230&r2=1910231&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java (original)
+++ pdfbox/trunk/examples/src/test/java/org/apache/pdfbox/examples/pdmodel/TestCreateSignature.java Mon Jun  5 02:40:17 2023
@@ -36,6 +36,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.InetAddress;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -52,6 +53,7 @@ import java.security.cert.CertificateFac
 import java.security.cert.X509CRL;
 import java.security.cert.X509Certificate;
 import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
@@ -59,6 +61,8 @@ import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import org.apache.commons.net.ntp.NTPUDPClient;
+import org.apache.commons.net.ntp.TimeInfo;
 
 import org.apache.pdfbox.Loader;
 import org.apache.pdfbox.cos.COSArray;
@@ -168,6 +172,28 @@ class TestCreateSignature
     }
 
     /**
+     * Test whether local machine has the correct time. When this happens, other tests often fail
+     * with "OCSP answer is too old".
+     */
+    @Test
+    void testTimeDifference() throws IOException
+    {
+        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
+        
+        Date localTime = new Date();
+
+        // https://stackoverflow.com/questions/4442192/
+        NTPUDPClient timeClient = new NTPUDPClient();
+        InetAddress inetAddress = InetAddress.getByName("time.nist.gov");
+        TimeInfo timeInfo = timeClient.getTime(inetAddress);
+        long returnTime = timeInfo.getReturnTime();
+        System.out.println("NTP   time: " + sdf.format(new Date(returnTime)));
+        System.out.println("Local time: " + sdf.format(localTime));
+        long diff = Math.abs(localTime.getTime() - returnTime) / 1000;
+        assertTrue(diff < 15, "Local time is off by more than " + diff + " seconds");
+    }
+
+    /**
      * Signs a PDF using the "adbe.pkcs7.detached" SubFilter with the SHA-256 digest.
      *
      * @throws IOException