Path: blob/master/test/jdk/javax/net/ssl/SSLEngine/LargePacket.java
41152 views
/*1* Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30*31* @bug 638845632* @summary Need adjustable TLS max record size for interoperability33* with non-compliant34* @run main/othervm LargePacket35*36* @author Xuelei Fan37*/3839import javax.net.ssl.*;40import java.nio.channels.*;41import java.net.*;4243public class LargePacket extends SSLEngineService {4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Should we run the client or server in a separate thread?53* Both sides can throw exceptions, but do you have a preference54* as to which side should be the main thread.55*/56static boolean separateServerThread = true;5758// Is the server ready to serve?59volatile static boolean serverReady = false;6061/*62* Turn on SSL debugging?63*/64static boolean debug = false;6566/*67* Define the server side of the test.68*69* If the server prematurely exits, serverReady will be set to true70* to avoid infinite hangs.71*/72void doServerSide() throws Exception {73// create SSLEngine.74SSLEngine ssle = createSSLEngine(false);7576// Create a server socket channel.77InetSocketAddress isa =78new InetSocketAddress(InetAddress.getLocalHost(), serverPort);79ServerSocketChannel ssc = ServerSocketChannel.open();80ssc.socket().bind(isa);81serverPort = ssc.socket().getLocalPort();8283// Signal Client, we're ready for his connect.84serverReady = true;8586// Accept a socket channel.87SocketChannel sc = ssc.accept();8889// Complete connection.90while (!sc.finishConnect()) {91// waiting for the connection completed.92}9394// handshaking95handshaking(ssle, sc, null);9697// receive application data98receive(ssle, sc);99100// send out application data101deliver(ssle, sc);102103// close the socket channel.104sc.close();105ssc.close();106}107108/*109* Define the client side of the test.110*111* If the server prematurely exits, serverReady will be set to true112* to avoid infinite hangs.113*/114void doClientSide() throws Exception {115// create SSLEngine.116SSLEngine ssle = createSSLEngine(true);117118/*119* Wait for server to get started.120*/121while (!serverReady) {122Thread.sleep(50);123}124125// Create a non-blocking socket channel.126SocketChannel sc = SocketChannel.open();127sc.configureBlocking(false);128InetSocketAddress isa =129new InetSocketAddress(InetAddress.getLocalHost(), serverPort);130sc.connect(isa);131132// Complete connection.133while (!sc.finishConnect() ) {134// waiting for the connection completed.135}136137// handshaking138handshaking(ssle, sc, null);139140// send out application data141deliver(ssle, sc);142143// receive application data144receive(ssle, sc);145146// close the socket channel.147sc.close();148}149150/*151* =============================================================152* The remainder is just support stuff153*/154volatile Exception serverException = null;155volatile Exception clientException = null;156157// use any free port by default158volatile int serverPort = 0;159160public static void main(String args[]) throws Exception {161if (debug)162System.setProperty("javax.net.debug", "all");163164new LargePacket();165}166167Thread clientThread = null;168Thread serverThread = null;169170/*171* Primary constructor, used to drive remainder of the test.172*173* Fork off the other side, then do your work.174*/175LargePacket() throws Exception {176super("../etc");177178if (separateServerThread) {179startServer(true);180startClient(false);181} else {182startClient(true);183startServer(false);184}185186/*187* Wait for other side to close down.188*/189if (separateServerThread) {190serverThread.join();191} else {192clientThread.join();193}194195/*196* When we get here, the test is pretty much over.197*198* If the main thread excepted, that propagates back199* immediately. If the other thread threw an exception, we200* should report back.201*/202if (serverException != null) {203System.out.print("Server Exception:");204throw serverException;205}206if (clientException != null) {207System.out.print("Client Exception:");208throw clientException;209}210}211212void startServer(boolean newThread) throws Exception {213if (newThread) {214serverThread = new Thread() {215public void run() {216try {217doServerSide();218} catch (Exception e) {219/*220* Our server thread just died.221*222* Release the client, if not active already...223*/224System.err.println("Server died...");225System.err.println(e);226serverReady = true;227serverException = e;228}229}230};231serverThread.start();232} else {233doServerSide();234}235}236237void startClient(boolean newThread) throws Exception {238if (newThread) {239clientThread = new Thread() {240public void run() {241try {242doClientSide();243} catch (Exception e) {244/*245* Our client thread just died.246*/247System.err.println("Client died...");248clientException = e;249}250}251};252clientThread.start();253} else {254doClientSide();255}256}257}258259260