Path: blob/master/test/jdk/javax/net/ssl/FixingJavadocs/SSLSocketInherit.java
41153 views
/*1* Copyright (c) 2001, 2011, 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* @test25* @bug 438788226* @summary Need to revisit the javadocs for JSSE, especially the27* promoted classes. This test checks to see if the settings28* on the server sockets get propagated to the sockets.29* @run main/othervm SSLSocketInherit30*31* SunJSSE does not support dynamic system properties, no way to re-use32* system properties in samevm/agentvm mode.33* @author Brad Wetmore34*/3536import java.net.*;37import javax.net.ssl.*;3839public class SSLSocketInherit {40String pathToStores = "../etc";41static String keyStoreFile = "keystore";42static String trustStoreFile = "truststore";43static String passwd = "passphrase";4445volatile int serverPort = 0;4647/*48* Let's just create silly sockets to do a basic connection,49* that's all we really need.50*/51Thread forkClient() {52Thread clientThread = new Thread() {53public void run() {54try {55new Socket("localhost", serverPort);56} catch (Exception e) {57// ignore for now...58}59}60};61clientThread.start();62return clientThread;63}6465SSLSocketInherit() throws Exception {66Exception exc = null;6768String keyFilename =69System.getProperty("test.src", "./") + "/" + pathToStores +70"/" + keyStoreFile;71String trustFilename =72System.getProperty("test.src", "./") + "/" + pathToStores +73"/" + trustStoreFile;7475System.setProperty("javax.net.ssl.keyStore", keyFilename);76System.setProperty("javax.net.ssl.keyStorePassword", passwd);77System.setProperty("javax.net.ssl.trustStore", trustFilename);78System.setProperty("javax.net.ssl.trustStorePassword", passwd);7980SSLServerSocketFactory sslssf =81(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();82SSLServerSocket sslss =83(SSLServerSocket) sslssf.createServerSocket(0);84serverPort = sslss.getLocalPort();8586Thread client = forkClient();8788String [] ciphers =89new String [] { "SSL_RSA_WITH_DES_CBC_SHA" };9091String [] protocols =92new String [] { "SSLv3" };9394sslss.setEnabledCipherSuites(ciphers);95sslss.setEnabledProtocols(protocols);96sslss.setNeedClientAuth(true);97sslss.setUseClientMode(true);98sslss.setEnableSessionCreation(true);99100SSLSocket ssls = (SSLSocket) sslss.accept();101102if (((ciphers = ssls.getEnabledCipherSuites()) == null) ||103(ciphers.length != 1) ||104(ciphers[0].compareToIgnoreCase(105"SSL_RSA_WITH_DES_CBC_SHA") != 0)) {106exc = new Exception("problem with get/setEnabledCipherSuites()");107}108109if (((protocols = ssls.getEnabledProtocols()) == null) ||110(protocols.length != 1) ||111(protocols[0].compareToIgnoreCase(112"SSLv3") != 0)) {113exc = new Exception("problem with get/setEnabledProtocols()");114}115116if (ssls.getNeedClientAuth() != true) {117exc = new Exception("problem with get/setNeedClientAuth()");118}119120if (ssls.getUseClientMode() != true) {121exc = new Exception("problem with get/setUseClientMode()");122}123124client.join();125126if (exc != null) {127throw exc;128}129130System.out.println("First SSLSocket inherited right info");131132/*133* Try it again.134*/135client = forkClient();136137ciphers = new String [] { "SSL_DH_anon_WITH_DES_CBC_SHA" };138protocols = new String [] { "TLSv1" };139140sslss.setEnabledCipherSuites(ciphers);141sslss.setEnabledProtocols(protocols);142sslss.setWantClientAuth(true);143sslss.setUseClientMode(false);144sslss.setEnableSessionCreation(false);145146ssls = (SSLSocket) sslss.accept();147148if (((ciphers = ssls.getEnabledCipherSuites()) == null) ||149(ciphers.length != 1) ||150(ciphers[0].compareToIgnoreCase(151"SSL_DH_anon_WITH_DES_CBC_SHA") != 0)) {152exc = new Exception("problem with get/setEnabledCipherSuites()");153}154155if (((protocols = ssls.getEnabledProtocols()) == null) ||156(protocols.length != 1) ||157(protocols[0].compareToIgnoreCase(158"TLSv1") != 0)) {159exc = new Exception("problem with get/setEnabledProtocols()");160}161162if (ssls.getWantClientAuth() != true) {163exc = new Exception("problem with get/setWantClientAuth()");164}165166if (ssls.getUseClientMode() != false) {167exc = new Exception("problem with get/setUseClientMode()");168}169170client.join();171172if (exc != null) {173throw exc;174}175176System.out.println("Second SSLSocket inherited right info");177178/*179* Lastly, try to set some wild suites, and make sure we180* catch it.181*/182183ciphers = sslss.getSupportedCipherSuites();184ciphers[1] = "this isn't a cipher suite";185186try {187sslss.setEnabledCipherSuites(ciphers);188throw new Exception(189"server socket setEnabledCipherSuites didn't throw Exception");190} catch (IllegalArgumentException e) {191System.out.println("Caught proper Exception on server socket");192}193194try {195ssls.setEnabledCipherSuites(ciphers);196throw new Exception(197"socket setEnabledCipherSuites didn't throw Exception");198} catch (IllegalArgumentException e) {199System.out.println("Caught proper Exception on socket");200}201202try {203ssls.setEnabledProtocols(null);204throw new Exception(205"socket setEnabledProtocols null didn't throw Exception");206} catch (IllegalArgumentException e) {207System.out.println("Caught proper Exception on socket");208}209210try {211sslss.setEnabledProtocols(null);212throw new Exception(213"server socket setEnabledProtocols null "+214"didn't throw Exception");215} catch (IllegalArgumentException e) {216System.out.println("Caught proper Exception on server socket");217}218219try {220ssls.setEnabledCipherSuites(null);221throw new Exception(222"socket setEnabledCipherSuites null didn't throw Exception");223} catch (IllegalArgumentException e) {224System.out.println("Caught proper Exception on socket");225}226227try {228sslss.setEnabledCipherSuites(null);229throw new Exception(230"server socket setEnabledCipherSuites null "+231"didn't throw Exception");232} catch (IllegalArgumentException e) {233System.out.println("Caught proper Exception on server socket");234}235236System.out.println("All tests PASS!");237238}239240public static void main(String args[]) throws Exception {241new SSLSocketInherit();242}243}244245246