test/jdk/sun/security/ssl/SSLEngineImpl/CloseEngineException.java
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    43 import javax.net.ssl.SSLEngineResult.*;
    43 import javax.net.ssl.SSLEngineResult.*;
    44 import java.io.*;
    44 import java.io.*;
    45 import java.security.*;
    45 import java.security.*;
    46 import java.nio.*;
    46 import java.nio.*;
    47 
    47 
       
    48 // Note that this test case depends on JSSE provider implementation details.
    48 public class CloseEngineException {
    49 public class CloseEngineException {
    49 
    50 
    50     private static boolean debug = false;
    51     private static boolean debug = true;
    51 
    52 
    52     private SSLContext sslc;
    53     private SSLContext sslc;
    53     private SSLEngine ssle1;    // client
    54     private SSLEngine ssle1;    // client
    54     private SSLEngine ssle2;    // server
    55     private SSLEngine ssle2;    // server
    55 
    56 
    92         createBuffers();
    93         createBuffers();
    93 
    94 
    94         SSLEngineResult result1;        // ssle1's results from last operation
    95         SSLEngineResult result1;        // ssle1's results from last operation
    95         SSLEngineResult result2;        // ssle2's results from last operation
    96         SSLEngineResult result2;        // ssle2's results from last operation
    96 
    97 
    97         while (!isEngineClosed(ssle1) || !isEngineClosed(ssle2)) {
    98         while (!isEngineClosed(ssle1) && !isEngineClosed(ssle2)) {
    98 
    99 
    99             log("================");
   100             log("================");
   100 
   101 
   101             result1 = ssle1.wrap(appOut1, oneToTwo);
   102             if (!isEngineClosed(ssle1)) {
   102             result2 = ssle2.wrap(appOut2, twoToOne);
   103                 result1 = ssle1.wrap(appOut1, oneToTwo);
   103 
   104                 runDelegatedTasks(result1, ssle1);
   104             log("wrap1:  " + result1);
   105 
   105             log("oneToTwo  = " + oneToTwo);
   106                 log("wrap1:  " + result1);
   106             log("");
   107                 log("oneToTwo  = " + oneToTwo);
   107 
   108                 log("");
   108             log("wrap2:  " + result2);
   109 
   109             log("twoToOne  = " + twoToOne);
   110                 oneToTwo.flip();
   110 
   111             }
   111             runDelegatedTasks(result1, ssle1);
   112             if (!isEngineClosed(ssle2)) {
   112             runDelegatedTasks(result2, ssle2);
   113                 result2 = ssle2.wrap(appOut2, twoToOne);
   113 
   114                 runDelegatedTasks(result2, ssle2);
   114             oneToTwo.flip();
   115 
   115             twoToOne.flip();
   116                 log("wrap2:  " + result2);
       
   117                 log("twoToOne  = " + twoToOne);
       
   118 
       
   119                 twoToOne.flip();
       
   120             }
   116 
   121 
   117             log("----");
   122             log("----");
   118 
   123 
   119             result1 = ssle1.unwrap(twoToOne, appIn1);
   124             if (!isEngineClosed(ssle1) && !dataDone) {
   120             result2 = ssle2.unwrap(oneToTwo, appIn2);
   125             log("--");
   121 
   126                 result1 = ssle1.unwrap(twoToOne, appIn1);
   122             log("unwrap1: " + result1);
   127                 runDelegatedTasks(result1, ssle1);
   123             log("twoToOne  = " + twoToOne);
   128 
   124             log("");
   129                 log("unwrap1: " + result1);
   125 
   130                 log("twoToOne  = " + twoToOne);
   126             log("unwrap2: " + result2);
   131                 log("");
   127             log("oneToTwo  = " + oneToTwo);
   132 
   128 
   133                 twoToOne.compact();
   129             runDelegatedTasks(result1, ssle1);
   134             }
   130             runDelegatedTasks(result2, ssle2);
   135             if (!isEngineClosed(ssle2)) {
   131 
   136             log("---");
   132             oneToTwo.compact();
   137                 result2 = ssle2.unwrap(oneToTwo, appIn2);
   133             twoToOne.compact();
   138                 runDelegatedTasks(result2, ssle2);
       
   139 
       
   140                 log("unwrap2: " + result2);
       
   141                 log("oneToTwo  = " + oneToTwo);
       
   142 
       
   143                 oneToTwo.compact();
       
   144             }
   134 
   145 
   135             /*
   146             /*
   136              * If we've transfered all the data between app1 and app2,
   147              * If we've transfered all the data between app1 and app2,
   137              * we try to close and see what that gets us.
   148              * we try to close and see what that gets us.
   138              */
   149              */
   152                      */
   163                      */
   153                     ssle1.beginHandshake();
   164                     ssle1.beginHandshake();
   154                     throw new Exception(
   165                     throw new Exception(
   155                         "TEST FAILED:  didn't throw Exception");
   166                         "TEST FAILED:  didn't throw Exception");
   156                 } catch (SSLException e) {
   167                 } catch (SSLException e) {
   157                     System.out.println("PARTIAL PASS");
   168                     System.err.println("PARTIAL PASS");
   158                 }
   169                 }
   159             }
   170             }
   160         }
   171         }
   161 
   172 
   162         try {
   173         try {
   165              */
   176              */
   166             ssle2.beginHandshake();
   177             ssle2.beginHandshake();
   167             throw new Exception(
   178             throw new Exception(
   168                 "TEST FAILED:  didn't throw Exception");
   179                 "TEST FAILED:  didn't throw Exception");
   169         } catch (SSLException e) {
   180         } catch (SSLException e) {
   170             System.out.println("TEST PASSED");
   181             System.err.println("TEST PASSED");
   171         }
   182         }
   172     }
   183     }
   173 
   184 
   174     public static void main(String args[]) throws Exception {
   185     public static void main(String args[]) throws Exception {
   175 
   186 
   179 
   190 
   180         test.createSSLEngines();
   191         test.createSSLEngines();
   181 
   192 
   182         test.runTest();
   193         test.runTest();
   183 
   194 
   184         System.out.println("Test Passed.");
   195         System.err.println("Test Passed.");
   185     }
   196     }
   186 
   197 
   187     /*
   198     /*
   188      * **********************************************************
   199      * **********************************************************
   189      * Majority of the test case is above, below is just setup stuff
   200      * Majority of the test case is above, below is just setup stuff
   275         b.limit(b.capacity());
   286         b.limit(b.capacity());
   276     }
   287     }
   277 
   288 
   278     private static void log(String str) {
   289     private static void log(String str) {
   279         if (debug) {
   290         if (debug) {
   280             System.out.println(str);
   291             System.err.println(str);
   281         }
   292         }
   282     }
   293     }
   283 }
   294 }