]> source.dussan.org Git - poi.git/commitdiff
Calling ping is not a good idea as it runs endlessly on some platforms, do a Java...
authorDominik Stadler <centic@apache.org>
Fri, 7 Nov 2014 13:05:57 +0000 (13:05 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 7 Nov 2014 13:05:57 +0000 (13:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1637370 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSignatureInfo.java

index 3628ac75b455bf86378d5251794b37164d41d35c..34c8d59ef1c39ed9cbf498cad374b16105774a1a 100644 (file)
    ================================================================= */ \r
 package org.apache.poi.poifs.crypt;\r
 \r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertFalse;\r
-import static org.junit.Assert.assertNotNull;\r
-import static org.junit.Assert.assertTrue;\r
+import static org.junit.Assert.*;\r
 \r
 import java.io.File;\r
 import java.io.FileInputStream;\r
@@ -35,6 +32,9 @@ import java.io.IOException;
 import java.io.InputStream;\r
 import java.lang.reflect.Method;\r
 import java.net.ConnectException;\r
+import java.net.HttpURLConnection;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
 import java.security.Key;\r
 import java.security.KeyPair;\r
 import java.security.KeyStore;\r
@@ -298,9 +298,9 @@ public class TestSignatureInfo {
         signatureConfig.addSignatureFacet(new XAdESSignatureFacet());\r
         signatureConfig.addSignatureFacet(new XAdESXLSignatureFacet());\r
         \r
-        // check for internet\r
-        Process p1 = Runtime.getRuntime().exec("ping www.google.com");\r
-        boolean mockTsp = (p1.waitFor() == 1);\r
+        // check for internet, no error means it works\r
+        boolean mockTsp = (getAccessError("http://timestamp.comodoca.com/rfc3161", true, 10000) == null);\r
+        \r
         // http://timestamping.edelweb.fr/service/tsp\r
         // http://tsa.belgium.be/connect\r
         // http://timestamp.comodoca.com/authenticode\r
@@ -401,6 +401,52 @@ public class TestSignatureInfo {
         pkg.close();\r
     }\r
 \r
+    public static String getAccessError(String destinationUrl, boolean fireRequest, int timeout) {\r
+        URL url;\r
+        try {\r
+            url = new URL(destinationUrl);\r
+        } catch (MalformedURLException e) {\r
+            throw new IllegalArgumentException("Invalid destination URL", e);\r
+        }\r
+\r
+        HttpURLConnection conn = null;\r
+        try {\r
+            conn = (HttpURLConnection) url.openConnection();\r
+\r
+            // set specified timeout if non-zero\r
+            if(timeout != 0) {\r
+                conn.setConnectTimeout(timeout);\r
+                conn.setReadTimeout(timeout);\r
+            }\r
+\r
+            conn.setDoOutput(false);\r
+            conn.setDoInput(true);\r
+\r
+            /* if connecting is not possible this will throw a connection refused exception */\r
+            conn.connect();\r
+\r
+            if (fireRequest) {\r
+                InputStream is = null;\r
+                try {\r
+                    is = conn.getInputStream();\r
+                } finally {\r
+                    IOUtils.closeQuietly(is);\r
+                }\r
+\r
+            }\r
+            /* if connecting is possible we return true here */\r
+            return null;\r
+\r
+        } catch (IOException e) {\r
+            /* exception is thrown -> server not available */\r
+            return e.getClass().getName() + ": " + e.getMessage();\r
+        } finally {\r
+            if (conn != null) {\r
+                conn.disconnect();\r
+            }\r
+        }\r
+    }\r
+    \r
     @Test\r
     public void testCertChain() throws Exception {\r
         KeyStore keystore = KeyStore.getInstance("PKCS12");\r