< prev index next >

test/javax/net/ssl/templates/SSLTest.java

Print this page




  77      */
  78     private volatile int serverPort;
  79 
  80     private volatile Exception serverException;
  81     private volatile Exception clientException;
  82 
  83     private Thread clientThread;
  84     private Thread serverThread;
  85 
  86     private Peer serverPeer;
  87     private Peer clientPeer;
  88 
  89     private Application serverApplication;
  90     private Application clientApplication;
  91 
  92     private SSLContext context;
  93 
  94     /*
  95      * Is the server ready to serve?
  96      */
  97     private final CountDownLatch serverCondition = new CountDownLatch(1);
  98 
  99     /*
 100      * Is the client ready to handshake?
 101      */
 102     private final CountDownLatch clientCondition = new CountDownLatch(1);










 103 
 104     /*
 105      * Public API.
 106      */
 107 
 108     public static interface Peer {
 109         void run(SSLTest test) throws Exception;
 110     }
 111 
 112     public static interface Application {
 113         void run(SSLSocket socket, SSLTest test) throws Exception;
 114     }
 115 
 116     public static void debug() {
 117         debug("ssl");
 118     }
 119 
 120     public static void debug(String mode) {
 121         System.setProperty("javax.net.debug", mode);
 122     }


 145             Arrays.stream(errors).forEach(e -> e.printStackTrace(System.out));
 146         }
 147     }
 148 
 149     public static KeyStore loadJksKeyStore(String filename, String password)
 150             throws Exception {
 151 
 152         return loadKeyStore(filename, password, "JKS");
 153     }
 154 
 155     public static KeyStore loadKeyStore(String filename, String password,
 156             String type) throws Exception {
 157 
 158         KeyStore keystore = KeyStore.getInstance(type);
 159         try (FileInputStream fis = new FileInputStream(filename)) {
 160             keystore.load(fis, password.toCharArray());
 161         }
 162         return keystore;
 163     }
 164 



















 165     public SSLTest setSeparateServerThread(boolean separateServerThread) {
 166         this.separateServerThread = separateServerThread;
 167         return this;
 168     }
 169 
 170     public SSLTest setServerPort(int serverPort) {
 171         this.serverPort = serverPort;
 172         return this;
 173     }
 174 
 175     public int getServerPort() {
 176         return serverPort;
 177     }
 178 
 179     public SSLTest setSSLContext(SSLContext context) {
 180         this.context = context;
 181         return this;
 182     }
 183 
 184     public SSLContext getSSLContext() {
 185         return context;
 186     }
 187 
 188     public SSLServerSocketFactory getSSLServerSocketFactory() {
 189         if (context != null) {
 190             return context.getServerSocketFactory();
 191         }
 192 
 193         return (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
 194     }
 195 
 196     public SSLSocketFactory getSSLSocketFactory() {
 197         if (context != null) {
 198             return context.getSocketFactory();
 199         }
 200 
 201         return (SSLSocketFactory) SSLSocketFactory.getDefault();
 202     }
 203 
 204     public void signalServerReady() {
 205         serverCondition.countDown();




 206     }
 207 
 208     public boolean waitForClientSignal(long timeout, TimeUnit unit)
 209             throws InterruptedException {
 210 
 211         return clientCondition.await(timeout, unit);
 212     }
 213 
 214     public boolean waitForClientSignal() throws InterruptedException {
 215         return waitForClientSignal(CLIENT_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 216     }
 217 










 218     public void signalClientReady() {
 219         clientCondition.countDown();




 220     }
 221 
 222     public boolean waitForServerSignal(long timeout, TimeUnit unit)
 223             throws InterruptedException {
 224 
 225         return serverCondition.await(timeout, unit);
 226     }
 227 
 228     public boolean waitForServerSignal() throws InterruptedException {
 229         return waitForServerSignal(SERVER_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 230     }
 231 










 232     public SSLTest setServerPeer(Peer serverPeer) {
 233         this.serverPeer = serverPeer;
 234         return this;
 235     }
 236 
 237     public Peer getServerPeer() {
 238         return serverPeer;
 239     }
 240 
 241     public SSLTest setServerApplication(Application serverApplication) {
 242         this.serverApplication = serverApplication;
 243         return this;
 244     }
 245 
 246     public Application getServerApplication() {
 247         return serverApplication;
 248     }
 249 
 250     public SSLTest setClientPeer(Peer clientPeer) {
 251         this.clientPeer = clientPeer;


 293      */
 294 
 295 
 296     /*
 297      * Define the server side of the test.
 298      */
 299     private static void doServerSide(SSLTest test) throws Exception {
 300         SSLServerSocket sslServerSocket;
 301 
 302         // kick start the server side service
 303         SSLServerSocketFactory sslssf = test.getSSLServerSocketFactory();
 304         sslServerSocket = (SSLServerSocket)sslssf.createServerSocket(FREE_PORT);
 305 
 306         test.setServerPort(sslServerSocket.getLocalPort());
 307         print("Server is listening on port " + test.getServerPort());
 308 
 309         // Signal the client, the server is ready to accept connection.
 310         test.signalServerReady();
 311 
 312         // Try to accept a connection in 30 seconds.
 313         SSLSocket sslSocket;
 314         try {
 315             sslServerSocket.setSoTimeout(SERVER_TIMEOUT);
 316             sslSocket = (SSLSocket) sslServerSocket.accept();
 317             print("Server accepted connection");
 318         } catch (SocketTimeoutException ste) {
 319             sslServerSocket.close();
 320 
 321             // Ignore the test case if no connection within 30 seconds.
 322             print("No incoming client connection in 30 seconds. "
 323                     + "Ignore in server side.", ste);
 324             return;
 325         }

 326 
 327         // handle the connection
 328         try {
 329             // Is it the expected client connection?
 330             //
 331             // Naughty test cases or third party routines may try to
 332             // connection to this server port unintentionally.  In
 333             // order to mitigate the impact of unexpected client
 334             // connections and avoid intermittent failure, it should
 335             // be checked that the accepted connection is really linked
 336             // to the expected client.
 337             boolean clientIsReady = test.waitForClientSignal();
 338 
 339             if (clientIsReady) {
 340                 // Run the application in server side.
 341                 print("Run server application");
 342                 test.getServerApplication().run(sslSocket, test);
 343             } else {    // Otherwise, ignore
 344                 // We don't actually care about plain socket connections
 345                 // for TLS communication testing generally.  Just ignore
 346                 // the test if the accepted connection is not linked to
 347                 // the expected client or the client connection timeout
 348                 // in 30 seconds.
 349                 print("The client is not the expected one or timeout. "
 350                         + "Ignore in server side.");
 351             }
 352         } finally {
 353             sslSocket.close();
 354             sslServerSocket.close();
 355         }


 356     }
 357 
 358     /*
 359      * Define the server side application of the test for the specified socket.
 360      */
 361     private static void runServerApplication(SSLSocket socket)
 362             throws Exception {
 363 
 364         // here comes the test logic
 365         InputStream sslIS = socket.getInputStream();
 366         OutputStream sslOS = socket.getOutputStream();
 367 
 368         sslIS.read();
 369         sslOS.write(85);
 370         sslOS.flush();
 371     }
 372 
 373     /*
 374      * Define the client side of the test.
 375      */


 402                 // established.
 403                 print("Cannot make a connection in 15 seconds. "
 404                         + "Ignore in client side.", ioe);
 405                 return;
 406             }
 407 
 408             // OK, here the client and server get connected.
 409 
 410             // Signal the server, the client is ready to communicate.
 411             test.signalClientReady();
 412 
 413             // There is still a chance in theory that the server thread may
 414             // wait client-ready timeout and then quit.  The chance should
 415             // be really rare so we don't consider it until it becomes a
 416             // real problem.
 417 
 418             // Run the application in client side.
 419             print("Run client application");
 420             test.getClientApplication().run(sslSocket, test);
 421         }


 422     }
 423 
 424     /*
 425      * Define the client side application of the test for the specified socket.
 426      */
 427     private static void runClientApplication(SSLSocket socket)
 428             throws Exception {
 429 
 430         InputStream sslIS = socket.getInputStream();
 431         OutputStream sslOS = socket.getOutputStream();
 432 
 433         sslOS.write(280);
 434         sslOS.flush();
 435         sslIS.read();
 436     }
 437 
 438     private void startServer(boolean newThread, SSLTest test) throws Exception {
 439         if (newThread) {
 440             serverThread = new Thread() {
 441                 @Override




  77      */
  78     private volatile int serverPort;
  79 
  80     private volatile Exception serverException;
  81     private volatile Exception clientException;
  82 
  83     private Thread clientThread;
  84     private Thread serverThread;
  85 
  86     private Peer serverPeer;
  87     private Peer clientPeer;
  88 
  89     private Application serverApplication;
  90     private Application clientApplication;
  91 
  92     private SSLContext context;
  93 
  94     /*
  95      * Is the server ready to serve?
  96      */
  97     private final CountDownLatch serverReadyCondition = new CountDownLatch(1);
  98 
  99     /*
 100      * Is the client ready to handshake?
 101      */
 102     private final CountDownLatch clientReadyCondition = new CountDownLatch(1);
 103 
 104     /*
 105      * Is the server done?
 106      */
 107     private final CountDownLatch serverDoneCondition = new CountDownLatch(1);
 108 
 109     /*
 110      * Is the client done?
 111      */
 112     private final CountDownLatch clientDoneCondition = new CountDownLatch(1);
 113 
 114     /*
 115      * Public API.
 116      */
 117 
 118     public static interface Peer {
 119         void run(SSLTest test) throws Exception;
 120     }
 121 
 122     public static interface Application {
 123         void run(SSLSocket socket, SSLTest test) throws Exception;
 124     }
 125 
 126     public static void debug() {
 127         debug("ssl");
 128     }
 129 
 130     public static void debug(String mode) {
 131         System.setProperty("javax.net.debug", mode);
 132     }


 155             Arrays.stream(errors).forEach(e -> e.printStackTrace(System.out));
 156         }
 157     }
 158 
 159     public static KeyStore loadJksKeyStore(String filename, String password)
 160             throws Exception {
 161 
 162         return loadKeyStore(filename, password, "JKS");
 163     }
 164 
 165     public static KeyStore loadKeyStore(String filename, String password,
 166             String type) throws Exception {
 167 
 168         KeyStore keystore = KeyStore.getInstance(type);
 169         try (FileInputStream fis = new FileInputStream(filename)) {
 170             keystore.load(fis, password.toCharArray());
 171         }
 172         return keystore;
 173     }
 174 
 175     // Try to accept a connection in 30 seconds.
 176     public static SSLSocket accept(SSLServerSocket sslServerSocket)
 177             throws IOException {
 178 
 179         return accept(sslServerSocket, SERVER_TIMEOUT);
 180     }
 181 
 182     public static SSLSocket accept(SSLServerSocket sslServerSocket, int timeout)
 183             throws IOException {
 184 
 185         try {
 186             sslServerSocket.setSoTimeout(timeout);
 187             return (SSLSocket) sslServerSocket.accept();
 188         } catch (SocketTimeoutException ste) {
 189             sslServerSocket.close();
 190             return null;
 191         }
 192     }
 193 
 194     public SSLTest setSeparateServerThread(boolean separateServerThread) {
 195         this.separateServerThread = separateServerThread;
 196         return this;
 197     }
 198 
 199     public SSLTest setServerPort(int serverPort) {
 200         this.serverPort = serverPort;
 201         return this;
 202     }
 203 
 204     public int getServerPort() {
 205         return serverPort;
 206     }
 207 
 208     public SSLTest setSSLContext(SSLContext context) {
 209         this.context = context;
 210         return this;
 211     }
 212 
 213     public SSLContext getSSLContext() {
 214         return context;
 215     }
 216 
 217     public SSLServerSocketFactory getSSLServerSocketFactory() {
 218         if (context != null) {
 219             return context.getServerSocketFactory();
 220         }
 221 
 222         return (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
 223     }
 224 
 225     public SSLSocketFactory getSSLSocketFactory() {
 226         if (context != null) {
 227             return context.getSocketFactory();
 228         }
 229 
 230         return (SSLSocketFactory) SSLSocketFactory.getDefault();
 231     }
 232 
 233     public void signalServerReady() {
 234         serverReadyCondition.countDown();
 235     }
 236 
 237     public void signalServerDone() {
 238         serverDoneCondition.countDown();
 239     }
 240 
 241     public boolean waitForClientSignal(long timeout, TimeUnit unit)
 242             throws InterruptedException {
 243 
 244         return clientReadyCondition.await(timeout, unit);
 245     }
 246 
 247     public boolean waitForClientSignal() throws InterruptedException {
 248         return waitForClientSignal(CLIENT_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 249     }
 250 
 251     public boolean waitForClientDone(long timeout, TimeUnit unit)
 252             throws InterruptedException {
 253 
 254         return clientDoneCondition.await(timeout, unit);
 255     }
 256 
 257     public boolean waitForClientDone() throws InterruptedException {
 258         return waitForClientDone(CLIENT_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 259     }
 260 
 261     public void signalClientReady() {
 262         clientReadyCondition.countDown();
 263     }
 264 
 265     public void signalClientDone() {
 266         clientDoneCondition.countDown();
 267     }
 268 
 269     public boolean waitForServerSignal(long timeout, TimeUnit unit)
 270             throws InterruptedException {
 271 
 272         return serverReadyCondition.await(timeout, unit);
 273     }
 274 
 275     public boolean waitForServerSignal() throws InterruptedException {
 276         return waitForServerSignal(SERVER_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 277     }
 278 
 279     public boolean waitForServerDone(long timeout, TimeUnit unit)
 280             throws InterruptedException {
 281 
 282         return serverDoneCondition.await(timeout, unit);
 283     }
 284 
 285     public boolean waitForServerDone() throws InterruptedException {
 286         return waitForServerDone(SERVER_SIGNAL_TIMEOUT, TimeUnit.SECONDS);
 287     }
 288 
 289     public SSLTest setServerPeer(Peer serverPeer) {
 290         this.serverPeer = serverPeer;
 291         return this;
 292     }
 293 
 294     public Peer getServerPeer() {
 295         return serverPeer;
 296     }
 297 
 298     public SSLTest setServerApplication(Application serverApplication) {
 299         this.serverApplication = serverApplication;
 300         return this;
 301     }
 302 
 303     public Application getServerApplication() {
 304         return serverApplication;
 305     }
 306 
 307     public SSLTest setClientPeer(Peer clientPeer) {
 308         this.clientPeer = clientPeer;


 350      */
 351 
 352 
 353     /*
 354      * Define the server side of the test.
 355      */
 356     private static void doServerSide(SSLTest test) throws Exception {
 357         SSLServerSocket sslServerSocket;
 358 
 359         // kick start the server side service
 360         SSLServerSocketFactory sslssf = test.getSSLServerSocketFactory();
 361         sslServerSocket = (SSLServerSocket)sslssf.createServerSocket(FREE_PORT);
 362 
 363         test.setServerPort(sslServerSocket.getLocalPort());
 364         print("Server is listening on port " + test.getServerPort());
 365 
 366         // Signal the client, the server is ready to accept connection.
 367         test.signalServerReady();
 368 
 369         // Try to accept a connection in 30 seconds.
 370         SSLSocket sslSocket = accept(sslServerSocket);
 371         if (sslSocket == null) {






 372             // Ignore the test case if no connection within 30 seconds.
 373             print("No incoming client connection in 30 seconds. "
 374                     + "Ignore in server side.");
 375             return;
 376         }
 377         print("Server accepted connection");
 378 
 379         // handle the connection
 380         try {
 381             // Is it the expected client connection?
 382             //
 383             // Naughty test cases or third party routines may try to
 384             // connection to this server port unintentionally.  In
 385             // order to mitigate the impact of unexpected client
 386             // connections and avoid intermittent failure, it should
 387             // be checked that the accepted connection is really linked
 388             // to the expected client.
 389             boolean clientIsReady = test.waitForClientSignal();
 390 
 391             if (clientIsReady) {
 392                 // Run the application in server side.
 393                 print("Run server application");
 394                 test.getServerApplication().run(sslSocket, test);
 395             } else {    // Otherwise, ignore
 396                 // We don't actually care about plain socket connections
 397                 // for TLS communication testing generally.  Just ignore
 398                 // the test if the accepted connection is not linked to
 399                 // the expected client or the client connection timeout
 400                 // in 30 seconds.
 401                 print("The client is not the expected one or timeout. "
 402                         + "Ignore in server side.");
 403             }
 404         } finally {
 405             sslSocket.close();
 406             sslServerSocket.close();
 407         }
 408 
 409         test.signalServerDone();
 410     }
 411 
 412     /*
 413      * Define the server side application of the test for the specified socket.
 414      */
 415     private static void runServerApplication(SSLSocket socket)
 416             throws Exception {
 417 
 418         // here comes the test logic
 419         InputStream sslIS = socket.getInputStream();
 420         OutputStream sslOS = socket.getOutputStream();
 421 
 422         sslIS.read();
 423         sslOS.write(85);
 424         sslOS.flush();
 425     }
 426 
 427     /*
 428      * Define the client side of the test.
 429      */


 456                 // established.
 457                 print("Cannot make a connection in 15 seconds. "
 458                         + "Ignore in client side.", ioe);
 459                 return;
 460             }
 461 
 462             // OK, here the client and server get connected.
 463 
 464             // Signal the server, the client is ready to communicate.
 465             test.signalClientReady();
 466 
 467             // There is still a chance in theory that the server thread may
 468             // wait client-ready timeout and then quit.  The chance should
 469             // be really rare so we don't consider it until it becomes a
 470             // real problem.
 471 
 472             // Run the application in client side.
 473             print("Run client application");
 474             test.getClientApplication().run(sslSocket, test);
 475         }
 476 
 477         test.signalClientDone();
 478     }
 479 
 480     /*
 481      * Define the client side application of the test for the specified socket.
 482      */
 483     private static void runClientApplication(SSLSocket socket)
 484             throws Exception {
 485 
 486         InputStream sslIS = socket.getInputStream();
 487         OutputStream sslOS = socket.getOutputStream();
 488 
 489         sslOS.write(280);
 490         sslOS.flush();
 491         sslIS.read();
 492     }
 493 
 494     private void startServer(boolean newThread, SSLTest test) throws Exception {
 495         if (newThread) {
 496             serverThread = new Thread() {
 497                 @Override


< prev index next >