< prev index next >

test/jdk/javax/net/ssl/DTLS/DTLSWontNegotiateV10.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -19,17 +19,17 @@
   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  
+ import jdk.test.lib.process.ProcessTools;
  import jdk.test.lib.security.SecurityUtils;
  
  import javax.net.ssl.*;
  import java.io.IOException;
  import java.net.*;
  import java.nio.ByteBuffer;
- import java.nio.file.Path;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicInteger;
  
  /*

@@ -46,10 +46,12 @@
      private static final int MTU = 1024;
      private static final String DTLSV_1_0 = "DTLSv1.0";
      private static final String DTLS = "DTLS";
      private static final String DTLSV_1_2 = "DTLSv1.2";
  
+     private static final int READ_TIMEOUT_SECS = Integer.getInteger("readtimeout", 30);
+ 
      public static void main(String[] args) throws Exception {
          if (args[0].equals(DTLSV_1_0)) {
              SecurityUtils.removeFromDisabledTlsAlgs(DTLSV_1_0);
          }
  

@@ -61,24 +63,46 @@
              }
  
          } else {
              // server process
              // args: protocol
-             try (DTLSServer server = new DTLSServer(args[0])) {
-                 List<String> command = List.of(
-                         Path.of(System.getProperty("java.home"), "bin", "java").toString(),
-                         "DTLSWontNegotiateV10",
-                         // if server is "DTLS" then the client should be v1.0 and vice versa
-                         args[0].equals(DTLS) ? DTLSV_1_0 : DTLS,
-                         Integer.toString(server.getListeningPortNumber())
-                 );
- 
-                 ProcessBuilder builder = new ProcessBuilder(command);
-                 Process p = builder.inheritIO().start();
-                 server.run();
-                 p.destroy();
-                 System.out.println("Success: DTLSv1.0 connection was not established.");
+             final int totalAttempts = 5;
+             int tries;
+             for (tries = 0 ; tries < totalAttempts ; ++tries) {
+                 try {
+                     System.out.printf("Starting server %d/%d attempts%n", tries+1, totalAttempts);
+                     runServer(args[0]);
+                     break;
+                 } catch (SocketTimeoutException exc) {
+                     System.out.println("The server timed-out waiting for packets from the client.");
+                 }
+             }
+             if (tries == totalAttempts) {
+                 throw new RuntimeException("The server/client communications timed-out after " + totalAttempts + " tries.");
+             }
+         }
+     }
+ 
+     private static void runServer(String protocol) throws Exception {
+         // args: protocol
+         Process clientProcess = null;
+         try (DTLSServer server = new DTLSServer(protocol)) {
+             List<String> command = List.of(
+                     "DTLSWontNegotiateV10",
+                     // if server is "DTLS" then the client should be v1.0 and vice versa
+                     protocol.equals(DTLS) ? DTLSV_1_0 : DTLS,
+                     Integer.toString(server.getListeningPortNumber())
+             );
+ 
+             ProcessBuilder builder = ProcessTools.createTestJavaProcessBuilder(command);
+             clientProcess = builder.inheritIO().start();
+             server.run();
+             System.out.println("Success: DTLSv1.0 connection was not established.");
+ 
+         } finally {
+             if (clientProcess != null) {
+                 clientProcess.destroy();
              }
          }
      }
  
      private static class DTLSClient extends DTLSEndpoint {

@@ -87,10 +111,13 @@
          private final DatagramSocket socket = new DatagramSocket();
  
          public DTLSClient(String protocol, int portNumber) throws Exception {
              super(true, protocol);
              remotePort = portNumber;
+             socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
+             log("Client listening on port " + socket.getLocalPort()
+                     + ". Sending data to server port " + remotePort);
              log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
          }
  
          @Override
          public void run() throws Exception {

@@ -285,10 +312,12 @@
          private final AtomicInteger portNumber = new AtomicInteger(0);
          private final DatagramSocket socket = new DatagramSocket(0);
  
          public DTLSServer(String protocol) throws Exception {
              super(false, protocol);
+             socket.setSoTimeout(READ_TIMEOUT_SECS * 1000);
+             log("Server listening on port: " + socket.getLocalPort());
              log("Enabled protocols: " + String.join(" ", engine.getEnabledProtocols()));
          }
  
          @Override
          public void run() throws Exception {
< prev index next >