0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.xnet.provider.jsse;
0019:
0020: import java.io.IOException;
0021: import java.net.InetAddress;
0022: import java.net.InetSocketAddress;
0023: import java.net.ServerSocket;
0024: import java.net.Socket;
0025: import javax.net.ssl.HandshakeCompletedEvent;
0026: import javax.net.ssl.HandshakeCompletedListener;
0027: import javax.net.ssl.SSLSession;
0028: import javax.net.ssl.SSLSocket;
0029:
0030: import junit.framework.Test;
0031: import junit.framework.TestCase;
0032: import junit.framework.TestSuite;
0033:
0034: /**
0035: * SSLSocketImplTest test
0036: */
0037: public class SSLSocketImplTest extends TestCase {
0038:
0039: // turn on/off the debug logging
0040: private static boolean doLog = false;
0041:
0042: /**
0043: * Sets up the test case.
0044: */
0045: public void setUp() throws Exception {
0046: if (doLog) {
0047: System.out.println("");
0048: System.out.println("========================");
0049: System.out.println("====== Running the test: " + getName());
0050: }
0051: }
0052:
0053: private SSLSocket createSSLSocket() throws Exception {
0054: return new SSLSocketImpl(JSSETestData.getSSLParameters());
0055: }
0056:
0057: private SSLSocket createSSLSocket(int port) throws Exception {
0058: return new SSLSocketImpl("localhost", port, JSSETestData
0059: .getSSLParameters());
0060: }
0061:
0062: /**
0063: * SSLSocketImpl(SSLParameters sslParameters) method testing.
0064: */
0065: public void testSSLSocketImpl1() throws Exception {
0066: Server server = null;
0067: SSLSocket socket = null;
0068: try {
0069: server = new Server();
0070:
0071: socket = new SSLSocketImpl(JSSETestData.getSSLParameters());
0072: socket.connect(new InetSocketAddress("localhost", server
0073: .getPort()));
0074: ((SSLSocketImpl) socket).init();
0075: socket.setUseClientMode(true);
0076:
0077: server.start();
0078: final SSLSocket s = socket;
0079: Thread thread = new Thread() {
0080: public void run() {
0081: try {
0082: s.startHandshake();
0083: } catch (Exception e) {
0084: }
0085: }
0086: };
0087:
0088: thread.start();
0089:
0090: int timeout = 10; // wait no more than 10*500 ms for handshake
0091: while (!server.handshakeStarted()) {
0092: // wait for handshake start
0093: try {
0094: Thread.sleep(500);
0095: } catch (Exception e) {
0096: }
0097: timeout--;
0098: if (timeout < 0) {
0099: try {
0100: server.close();
0101: } catch (IOException ex) {
0102: }
0103: try {
0104: socket.close();
0105: } catch (IOException ex) {
0106: }
0107: fail("Handshake was not started");
0108: }
0109: }
0110: } finally {
0111: if (server != null) {
0112: try {
0113: server.close();
0114: } catch (IOException ex) {
0115: }
0116: }
0117: if (socket != null) {
0118: try {
0119: socket.close();
0120: } catch (IOException ex) {
0121: }
0122: }
0123: }
0124: }
0125:
0126: /**
0127: * SSLSocketImpl(String host, int port, SSLParameters sslParameters) method
0128: * testing.
0129: */
0130: public void testSSLSocketImpl2() throws Exception {
0131: Server server = null;
0132: SSLSocket socket = null;
0133: try {
0134: server = new Server();
0135:
0136: socket = new SSLSocketImpl("localhost", server.getPort(),
0137: JSSETestData.getSSLParameters());
0138: socket.setUseClientMode(true);
0139:
0140: server.start();
0141: final SSLSocket s = socket;
0142: Thread thread = new Thread() {
0143: public void run() {
0144: try {
0145: s.startHandshake();
0146: } catch (Exception e) {
0147: }
0148: }
0149: };
0150:
0151: thread.start();
0152:
0153: int timeout = 10; // wait no more than 5 seconds for handshake
0154: while (!server.handshakeStarted()) {
0155: // wait for handshake start
0156: try {
0157: Thread.sleep(500);
0158: } catch (Exception e) {
0159: }
0160: timeout--;
0161: if (timeout < 0) {
0162: try {
0163: server.close();
0164: } catch (IOException ex) {
0165: }
0166: try {
0167: socket.close();
0168: } catch (IOException ex) {
0169: }
0170: fail("Handshake was not started");
0171: }
0172: }
0173: } finally {
0174: if (server != null) {
0175: try {
0176: server.close();
0177: } catch (IOException ex) {
0178: }
0179: }
0180: if (socket != null) {
0181: try {
0182: socket.close();
0183: } catch (IOException ex) {
0184: }
0185: }
0186: }
0187: }
0188:
0189: /**
0190: * SSLSocketImpl(String host, int port, InetAddress localHost, int
0191: * localPort, SSLParameters sslParameters) method testing.
0192: */
0193: public void testSSLSocketImpl3() throws Exception {
0194: Server server = null;
0195: SSLSocket socket = null;
0196: try {
0197: server = new Server();
0198:
0199: socket = new SSLSocketImpl("localhost", server.getPort(),
0200: InetAddress.getByName("localhost"), 0, JSSETestData
0201: .getSSLParameters());
0202: socket.setUseClientMode(true);
0203:
0204: server.start();
0205: final SSLSocket s = socket;
0206: Thread thread = new Thread() {
0207: public void run() {
0208: try {
0209: s.startHandshake();
0210: } catch (Exception e) {
0211: }
0212: }
0213: };
0214:
0215: thread.start();
0216:
0217: int timeout = 10; // wait no more than 5 seconds for handshake
0218: while (!server.handshakeStarted()) {
0219: // wait for handshake start
0220: try {
0221: Thread.sleep(500);
0222: } catch (Exception e) {
0223: }
0224: timeout--;
0225: if (timeout < 0) {
0226: try {
0227: server.close();
0228: } catch (IOException ex) {
0229: }
0230: try {
0231: socket.close();
0232: } catch (IOException ex) {
0233: }
0234: fail("Handshake was not started");
0235: }
0236: }
0237: } finally {
0238: if (server != null) {
0239: try {
0240: server.close();
0241: } catch (IOException ex) {
0242: }
0243: }
0244: if (socket != null) {
0245: try {
0246: socket.close();
0247: } catch (IOException ex) {
0248: }
0249: }
0250: }
0251: }
0252:
0253: /**
0254: * SSLSocketImpl(InetAddress host, int port, SSLParameters sslParameters)
0255: * method testing.
0256: */
0257: public void testSSLSocketImpl4() throws Exception {
0258: Server server = null;
0259: SSLSocket socket = null;
0260: try {
0261: server = new Server();
0262:
0263: socket = new SSLSocketImpl(InetAddress
0264: .getByName("localhost"), server.getPort(),
0265: JSSETestData.getSSLParameters());
0266: socket.setUseClientMode(true);
0267:
0268: server.start();
0269: final SSLSocket s = socket;
0270: Thread thread = new Thread() {
0271: public void run() {
0272: try {
0273: s.startHandshake();
0274: } catch (Exception e) {
0275: }
0276: }
0277: };
0278:
0279: thread.start();
0280:
0281: int timeout = 10; // wait no more than 5 seconds for handshake
0282: while (!server.handshakeStarted()) {
0283: // wait for handshake start
0284: try {
0285: Thread.sleep(500);
0286: } catch (Exception e) {
0287: }
0288: timeout--;
0289: if (timeout < 0) {
0290: try {
0291: server.close();
0292: } catch (IOException ex) {
0293: }
0294: try {
0295: socket.close();
0296: } catch (IOException ex) {
0297: }
0298: fail("Handshake was not started");
0299: }
0300: }
0301: } finally {
0302: if (server != null) {
0303: try {
0304: server.close();
0305: } catch (IOException ex) {
0306: }
0307: }
0308: if (socket != null) {
0309: try {
0310: socket.close();
0311: } catch (IOException ex) {
0312: }
0313: }
0314: }
0315: }
0316:
0317: /**
0318: * SSLSocketImpl(InetAddress address, int port, InetAddress localAddress,
0319: * int localPort, SSLParameters sslParameters) method testing.
0320: */
0321: public void testSSLSocketImpl5() throws Exception {
0322: Server server = null;
0323: SSLSocket socket = null;
0324: try {
0325: server = new Server();
0326:
0327: socket = new SSLSocketImpl(InetAddress
0328: .getByName("localhost"), server.getPort(),
0329: InetAddress.getByName("localhost"), 0, JSSETestData
0330: .getSSLParameters());
0331: socket.setUseClientMode(true);
0332:
0333: server.start();
0334: final SSLSocket s = socket;
0335: Thread thread = new Thread() {
0336: public void run() {
0337: try {
0338: s.startHandshake();
0339: } catch (Exception e) {
0340: }
0341: }
0342: };
0343:
0344: thread.start();
0345:
0346: int timeout = 10; // wait no more than 5 seconds for handshake
0347: while (!server.handshakeStarted()) {
0348: // wait for handshake start
0349: try {
0350: Thread.sleep(500);
0351: } catch (Exception e) {
0352: }
0353: timeout--;
0354: if (timeout < 0) {
0355: try {
0356: server.close();
0357: } catch (IOException ex) {
0358: }
0359: try {
0360: socket.close();
0361: } catch (IOException ex) {
0362: }
0363: fail("Handshake was not started");
0364: }
0365: }
0366: } finally {
0367: if (server != null) {
0368: try {
0369: server.close();
0370: } catch (IOException ex) {
0371: }
0372: }
0373: if (socket != null) {
0374: try {
0375: socket.close();
0376: } catch (IOException ex) {
0377: }
0378: }
0379: }
0380: }
0381:
0382: /**
0383: * getSupportedCipherSuites() method testing.
0384: */
0385: public void testGetSupportedCipherSuites() throws Exception {
0386: SSLSocket socket = createSSLSocket();
0387: String[] supported = socket.getSupportedCipherSuites();
0388: assertNotNull(supported);
0389: supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
0390: supported = socket.getEnabledCipherSuites();
0391: for (int i = 0; i < supported.length; i++) {
0392: if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
0393: fail("Modification of the returned result "
0394: + "causes the modification of the internal state");
0395: }
0396: }
0397: }
0398:
0399: /**
0400: * getEnabledCipherSuites() method testing.
0401: */
0402: public void testGetEnabledCipherSuites() throws Exception {
0403: SSLSocket socket = createSSLSocket();
0404: String[] enabled = socket.getEnabledCipherSuites();
0405: assertNotNull(enabled);
0406: String[] supported = socket.getSupportedCipherSuites();
0407: for (int i = 0; i < enabled.length; i++) {
0408: found: {
0409: for (int j = 0; j < supported.length; j++) {
0410: if (enabled[i].equals(supported[j])) {
0411: break found;
0412: }
0413: }
0414: fail("Enabled suite does not belong to the set "
0415: + "of supported cipher suites: " + enabled[i]);
0416: }
0417: }
0418: socket.setEnabledCipherSuites(supported);
0419: for (int i = 0; i < supported.length; i++) {
0420: enabled = new String[supported.length - i];
0421: System.arraycopy(supported, 0, enabled, 0, supported.length
0422: - i);
0423: socket.setEnabledCipherSuites(enabled);
0424: String[] result = socket.getEnabledCipherSuites();
0425: if (result.length != enabled.length) {
0426: fail("Returned result does not correspond to expected.");
0427: }
0428: for (int k = 0; k < result.length; k++) {
0429: found: {
0430: for (int n = 0; n < enabled.length; n++) {
0431: if (result[k].equals(enabled[n])) {
0432: break found;
0433: }
0434: }
0435: if (result.length != enabled.length) {
0436: fail("Returned result does not correspond "
0437: + "to expected.");
0438: }
0439: }
0440: }
0441: }
0442: }
0443:
0444: /**
0445: * setEnabledCipherSuites(String[] suites) method testing.
0446: */
0447: public void testSetEnabledCipherSuites() throws Exception {
0448: SSLSocket socket = createSSLSocket();
0449: String[] enabled = socket.getEnabledCipherSuites();
0450: assertNotNull(enabled);
0451: String[] supported = socket.getSupportedCipherSuites();
0452: for (int i = 0; i < enabled.length; i++) {
0453: found: {
0454: for (int j = 0; j < supported.length; j++) {
0455: if (enabled[i].equals(supported[j])) {
0456: break found;
0457: }
0458: }
0459: fail("Enabled suite does not belong to the set "
0460: + "of supported cipher suites: " + enabled[i]);
0461: }
0462: }
0463: socket.setEnabledCipherSuites(supported);
0464: socket.setEnabledCipherSuites(enabled);
0465: socket.setEnabledCipherSuites(supported);
0466: String[] more_than_supported = new String[supported.length + 1];
0467: for (int i = 0; i < supported.length + 1; i++) {
0468: more_than_supported[i] = "NOT_SUPPORTED_CIPHER_SUITE";
0469: System.arraycopy(supported, 0, more_than_supported, 0, i);
0470: System.arraycopy(supported, i, more_than_supported, i + 1,
0471: supported.length - i);
0472: try {
0473: socket.setEnabledCipherSuites(more_than_supported);
0474: fail("Expected IllegalArgumentException was not thrown");
0475: } catch (IllegalArgumentException e) {
0476: }
0477: }
0478: enabled = socket.getEnabledCipherSuites();
0479: enabled[0] = "NOT_SUPPORTED_CIPHER_SUITE";
0480: enabled = socket.getEnabledCipherSuites();
0481: for (int i = 0; i < enabled.length; i++) {
0482: if ("NOT_SUPPORTED_CIPHER_SUITE".equals(enabled[i])) {
0483: fail("Modification of the returned result "
0484: + "causes the modification of the internal state");
0485: }
0486: }
0487: }
0488:
0489: /**
0490: * getSupportedProtocols() method testing.
0491: */
0492: public void testGetSupportedProtocols() throws Exception {
0493: SSLSocket socket = createSSLSocket();
0494: String[] supported = socket.getSupportedProtocols();
0495: assertNotNull(supported);
0496: assertFalse(supported.length == 0);
0497: supported[0] = "NOT_SUPPORTED_PROTOCOL";
0498: supported = socket.getSupportedProtocols();
0499: for (int i = 0; i < supported.length; i++) {
0500: if ("NOT_SUPPORTED_PROTOCOL".equals(supported[i])) {
0501: fail("Modification of the returned result "
0502: + "causes the modification of the internal state");
0503: }
0504: }
0505: }
0506:
0507: /**
0508: * getEnabledProtocols() method testing.
0509: */
0510: public void testGetEnabledProtocols() throws Exception {
0511: SSLSocket socket = createSSLSocket();
0512: String[] enabled = socket.getEnabledProtocols();
0513: assertNotNull(enabled);
0514: String[] supported = socket.getSupportedProtocols();
0515: for (int i = 0; i < enabled.length; i++) {
0516: found: {
0517: for (int j = 0; j < supported.length; j++) {
0518: if (enabled[i].equals(supported[j])) {
0519: break found;
0520: }
0521: }
0522: fail("Enabled protocol does not belong to the set "
0523: + "of supported protocols: " + enabled[i]);
0524: }
0525: }
0526: socket.setEnabledProtocols(supported);
0527: for (int i = 0; i < supported.length; i++) {
0528: enabled = new String[supported.length - i];
0529: System.arraycopy(supported, i, enabled, 0, supported.length
0530: - i);
0531: socket.setEnabledProtocols(enabled);
0532: String[] result = socket.getEnabledProtocols();
0533: if (result.length != enabled.length) {
0534: fail("Returned result does not correspond to expected.");
0535: }
0536: for (int k = 0; k < result.length; k++) {
0537: found: {
0538: for (int n = 0; n < enabled.length; n++) {
0539: if (result[k].equals(enabled[n])) {
0540: break found;
0541: }
0542: }
0543: if (result.length != enabled.length) {
0544: fail("Returned result does not correspond "
0545: + "to expected.");
0546: }
0547: }
0548: }
0549: }
0550: }
0551:
0552: /**
0553: * setEnabledProtocols(String[] protocols) method testing.
0554: */
0555: public void testSetEnabledProtocols() throws Exception {
0556: SSLSocket socket = createSSLSocket();
0557: String[] enabled = socket.getEnabledProtocols();
0558: assertNotNull(enabled);
0559: String[] supported = socket.getSupportedProtocols();
0560: for (int i = 0; i < enabled.length; i++) {
0561: //System.out.println("Checking of "+enabled[i]);
0562: found: {
0563: for (int j = 0; j < supported.length; j++) {
0564: if (enabled[i].equals(supported[j])) {
0565: break found;
0566: }
0567: }
0568: fail("Enabled suite does not belong to the set "
0569: + "of supported cipher suites: " + enabled[i]);
0570: }
0571: }
0572: socket.setEnabledProtocols(supported);
0573: socket.setEnabledProtocols(enabled);
0574: socket.setEnabledProtocols(supported);
0575: String[] more_than_supported = new String[supported.length + 1];
0576: for (int i = 0; i < supported.length + 1; i++) {
0577: more_than_supported[i] = "NOT_SUPPORTED_PROTOCOL";
0578: System.arraycopy(supported, 0, more_than_supported, 0, i);
0579: System.arraycopy(supported, i, more_than_supported, i + 1,
0580: supported.length - i);
0581: try {
0582: socket.setEnabledProtocols(more_than_supported);
0583: fail("Expected IllegalArgumentException was not thrown");
0584: } catch (IllegalArgumentException e) {
0585: }
0586: }
0587: enabled = socket.getEnabledProtocols();
0588: enabled[0] = "NOT_SUPPORTED_PROTOCOL";
0589: enabled = socket.getEnabledProtocols();
0590: for (int i = 0; i < enabled.length; i++) {
0591: if ("NOT_SUPPORTED_PROTOCOL".equals(enabled[i])) {
0592: fail("Modification of the returned result "
0593: + "causes the modification of the internal state");
0594: }
0595: }
0596: }
0597:
0598: private static class Server extends Thread {
0599:
0600: private final ServerSocket server;
0601: private boolean closed;
0602: private boolean handshake_started = false;
0603: private Socket endpoint = null;
0604:
0605: public Server() throws IOException {
0606: super ();
0607: server = new ServerSocket(0);
0608: server.setSoTimeout(1000);
0609: }
0610:
0611: public int getPort() {
0612: return server.getLocalPort();
0613: }
0614:
0615: public void run() {
0616: while (!closed) {
0617: try {
0618: if (doLog) {
0619: System.out.print(".");
0620: }
0621: if (!handshake_started) {
0622: endpoint = server.accept();
0623: endpoint.getInputStream().read();
0624: handshake_started = true;
0625: }
0626: Thread.sleep(1000);
0627: } catch (Exception e) {
0628: e.printStackTrace();
0629: }
0630: }
0631: if (endpoint != null) {
0632: try {
0633: endpoint.close();
0634: } catch (IOException e) {
0635: }
0636: }
0637: }
0638:
0639: public boolean handshakeStarted() {
0640: return handshake_started;
0641: }
0642:
0643: public void close() throws IOException {
0644: closed = true;
0645: server.close();
0646: }
0647:
0648: };
0649:
0650: /**
0651: * setUseClientMode(boolean mode) method testing.
0652: * getUseClientMode() method testing.
0653: */
0654: public void testSetGetUseClientMode() throws Exception {
0655: Server server = null;
0656: SSLSocket socket = null;
0657: try {
0658: server = new Server();
0659:
0660: socket = createSSLSocket(server.getPort());
0661:
0662: socket.setUseClientMode(false);
0663: assertFalse("Result does not correspond to expected",
0664: socket.getUseClientMode());
0665: socket.setUseClientMode(true);
0666: assertTrue("Result does not correspond to expected", socket
0667: .getUseClientMode());
0668:
0669: server.start();
0670: final SSLSocket s = socket;
0671: new Thread() {
0672: public void run() {
0673: try {
0674: s.startHandshake();
0675: } catch (IOException e) {
0676: //e.printStackTrace();
0677: }
0678: }
0679: }.start();
0680:
0681: while (!server.handshakeStarted()) {
0682: // wait for handshake start
0683: try {
0684: Thread.sleep(500);
0685: } catch (Exception e) {
0686: }
0687: }
0688:
0689: try {
0690: socket.setUseClientMode(false);
0691: server.close();
0692: socket.close();
0693: fail("Expected IllegalArgumentException was not thrown");
0694: } catch (IllegalArgumentException e) {
0695: }
0696: } finally {
0697: if (server != null) {
0698: try {
0699: server.close();
0700: } catch (IOException ex) {
0701: }
0702: }
0703: if (socket != null) {
0704: try {
0705: socket.close();
0706: } catch (IOException ex) {
0707: }
0708: }
0709: }
0710: }
0711:
0712: /**
0713: * setNeedClientAuth(boolean need) method testing.
0714: * getNeedClientAuth() method testing.
0715: */
0716: public void testSetGetNeedClientAuth() throws Exception {
0717: SSLSocket socket = createSSLSocket();
0718:
0719: socket.setWantClientAuth(true);
0720: socket.setNeedClientAuth(false);
0721: assertFalse("Result does not correspond to expected", socket
0722: .getNeedClientAuth());
0723: assertFalse("Socket did not reset its want client auth state",
0724: socket.getWantClientAuth());
0725: socket.setWantClientAuth(true);
0726: socket.setNeedClientAuth(true);
0727: assertTrue("Result does not correspond to expected", socket
0728: .getNeedClientAuth());
0729: assertFalse("Socket did not reset its want client auth state",
0730: socket.getWantClientAuth());
0731: }
0732:
0733: /**
0734: * setWantClientAuth(boolean want) method testing.
0735: * getWantClientAuth() method testing.
0736: */
0737: public void testSetGetWantClientAuth() throws Exception {
0738: SSLSocket socket = createSSLSocket();
0739:
0740: socket.setNeedClientAuth(true);
0741: socket.setWantClientAuth(false);
0742: assertFalse("Result does not correspond to expected", socket
0743: .getWantClientAuth());
0744: assertFalse("Socket did not reset its want client auth state",
0745: socket.getNeedClientAuth());
0746: socket.setNeedClientAuth(true);
0747: socket.setWantClientAuth(true);
0748: assertTrue("Result does not correspond to expected", socket
0749: .getWantClientAuth());
0750: assertFalse("Socket did not reset its want client auth state",
0751: socket.getNeedClientAuth());
0752: }
0753:
0754: /**
0755: * setEnableSessionCreation(boolean flag) method testing.
0756: * getEnableSessionCreation() method testing.
0757: */
0758: public void testSetGetEnableSessionCreation() throws Exception {
0759: SSLSocket socket = createSSLSocket();
0760:
0761: socket.setEnableSessionCreation(false);
0762: assertFalse("Result does not correspond to expected", socket
0763: .getEnableSessionCreation());
0764: socket.setEnableSessionCreation(true);
0765: assertTrue("Result does not correspond to expected", socket
0766: .getEnableSessionCreation());
0767: }
0768:
0769: /**
0770: * getSession() method testing.
0771: */
0772: public void testGetSession() throws Exception {
0773: Server server = null;
0774: SSLSocket socket = null;
0775: try {
0776: server = new Server();
0777:
0778: socket = createSSLSocket(server.getPort());
0779: socket.setUseClientMode(true);
0780:
0781: server.start();
0782: final SSLSocket s = socket;
0783: final SSLSession[] session = new SSLSession[1];
0784: Thread thread = new Thread() {
0785: public void run() {
0786: try {
0787: session[0] = s.getSession();
0788: } catch (Exception e) {
0789: e.printStackTrace();
0790: }
0791: }
0792: };
0793:
0794: thread.start();
0795:
0796: int timeout = 10; // wait no more than 5 seconds for handshake
0797: while (!server.handshakeStarted()) {
0798: // wait for handshake start
0799: try {
0800: Thread.sleep(500);
0801: } catch (Exception e) {
0802: }
0803: timeout--;
0804: if (timeout < 0) {
0805: try {
0806: server.close();
0807: } catch (IOException ex) {
0808: }
0809: try {
0810: socket.close();
0811: } catch (IOException ex) {
0812: }
0813: fail("getSession method did not start a handshake");
0814: }
0815: }
0816:
0817: server.close(); // makes error during the handshake
0818: thread.join();
0819: if ((session[0] == null)
0820: || (!session[0].getCipherSuite().endsWith(
0821: "_NULL_WITH_NULL_NULL"))) {
0822: fail("Returned session is null "
0823: + "or not TLS_NULL_WITH_NULL_NULL");
0824: }
0825: } finally {
0826: if (server != null) {
0827: try {
0828: server.close();
0829: } catch (IOException ex) {
0830: }
0831: }
0832: if (socket != null) {
0833: try {
0834: socket.close();
0835: } catch (IOException ex) {
0836: }
0837: }
0838: }
0839: }
0840:
0841: /**
0842: * addHandshakeCompletedListener( HandshakeCompletedListener listener)
0843: * method testing.
0844: * removeHandshakeCompletedListener( HandshakeCompletedListener listener)
0845: * method testing.
0846: */
0847: public void testAddRemoveHandshakeCompletedListener()
0848: throws Exception {
0849: HandshakeCompletedListener listener = new HandshakeCompletedListener() {
0850: public void handshakeCompleted(HandshakeCompletedEvent event) {
0851: }
0852: };
0853: SSLSocket socket = createSSLSocket();
0854: socket.addHandshakeCompletedListener(listener);
0855: try {
0856: socket.addHandshakeCompletedListener(null);
0857: fail("Expected IllegalArgumentException was not thrown.");
0858: } catch (IllegalArgumentException e) {
0859: }
0860: try {
0861: socket.removeHandshakeCompletedListener(null);
0862: fail("Expected IllegalArgumentException was not thrown.");
0863: } catch (IllegalArgumentException e) {
0864: }
0865: try {
0866: socket
0867: .removeHandshakeCompletedListener(new HandshakeCompletedListener() {
0868: public void handshakeCompleted(
0869: HandshakeCompletedEvent event) {
0870: }
0871: });
0872: fail("Expected IllegalArgumentException was not thrown.");
0873: } catch (IllegalArgumentException e) {
0874: }
0875: try {
0876: socket.removeHandshakeCompletedListener(listener);
0877: } catch (IllegalArgumentException e) {
0878: fail("Unxpected IllegalArgumentException was thrown.");
0879: }
0880: }
0881:
0882: /**
0883: * startHandshake() method testing.
0884: */
0885: public void testStartHandshake() throws Exception {
0886: Server server = null;
0887: SSLSocket socket = null;
0888: try {
0889: server = new Server();
0890:
0891: socket = createSSLSocket(server.getPort());
0892: socket.setUseClientMode(true);
0893:
0894: server.start();
0895: final SSLSocket s = socket;
0896: final Exception[] exception = new Exception[1];
0897: Thread thread = new Thread() {
0898: public void run() {
0899: try {
0900: s.startHandshake();
0901: } catch (Exception e) {
0902: exception[0] = e;
0903: }
0904: }
0905: };
0906:
0907: thread.start();
0908:
0909: int timeout = 10; // wait no more than 5 seconds for handshake
0910: while (!server.handshakeStarted()) {
0911: // wait for handshake start
0912: try {
0913: Thread.sleep(500);
0914: } catch (Exception e) {
0915: }
0916: timeout--;
0917: if (timeout < 0) {
0918: fail("Handshake was not started");
0919: }
0920: }
0921:
0922: server.close(); // makes error during the handshake
0923: thread.join();
0924: if (exception[0] == null) {
0925: fail("Expected IOException was not thrown");
0926: }
0927: } finally {
0928: if (server != null) {
0929: try {
0930: server.close();
0931: } catch (IOException ex) {
0932: }
0933: }
0934: if (socket != null) {
0935: try {
0936: socket.close();
0937: } catch (IOException ex) {
0938: }
0939: }
0940: }
0941: }
0942:
0943: /**
0944: * getInputStream() method testing.
0945: */
0946: public void testGetInputStream() throws Exception {
0947: Server server = null;
0948: SSLSocket socket = null;
0949: try {
0950: server = new Server();
0951:
0952: socket = createSSLSocket(server.getPort());
0953: socket.setUseClientMode(true);
0954:
0955: server.start();
0956: final SSLSocket s = socket;
0957: Thread thread = new Thread() {
0958: public void run() {
0959: try {
0960: s.getInputStream().read(); // should start handshake
0961: } catch (Exception e) {
0962: }
0963: }
0964: };
0965:
0966: thread.start();
0967:
0968: int timeout = 10; // wait no more than 5 seconds for handshake
0969: while (!server.handshakeStarted()) {
0970: // wait for handshake start
0971: try {
0972: Thread.sleep(500);
0973: } catch (Exception e) {
0974: }
0975: timeout--;
0976: if (timeout < 0) {
0977: try {
0978: server.close();
0979: } catch (IOException ex) {
0980: }
0981: try {
0982: socket.close();
0983: } catch (IOException ex) {
0984: }
0985: fail("Handshake was not started");
0986: }
0987: }
0988: } finally {
0989: if (server != null) {
0990: try {
0991: server.close();
0992: } catch (IOException ex) {
0993: }
0994: }
0995: if (socket != null) {
0996: try {
0997: socket.close();
0998: } catch (IOException ex) {
0999: }
1000: }
1001: }
1002: }
1003:
1004: /**
1005: * getOutputStream() method testing.
1006: */
1007: public void testGetOutputStream() throws Exception {
1008: Server server = null;
1009: SSLSocket socket = null;
1010: try {
1011: server = new Server();
1012:
1013: socket = createSSLSocket(server.getPort());
1014: socket.setUseClientMode(true);
1015:
1016: server.start();
1017: final SSLSocket s = socket;
1018: Thread thread = new Thread() {
1019: public void run() {
1020: try {
1021: s.getOutputStream().write(0); // should start handshake
1022: } catch (Exception e) {
1023: }
1024: }
1025: };
1026:
1027: thread.start();
1028:
1029: int timeout = 10; // wait no more than 5 seconds for handshake
1030: while (!server.handshakeStarted()) {
1031: // wait for handshake start
1032: try {
1033: Thread.sleep(500);
1034: } catch (Exception e) {
1035: }
1036: timeout--;
1037: if (timeout < 0) {
1038: try {
1039: server.close();
1040: } catch (IOException ex) {
1041: }
1042: try {
1043: socket.close();
1044: } catch (IOException ex) {
1045: }
1046: fail("Handshake was not started");
1047: }
1048: }
1049: } finally {
1050: if (server != null) {
1051: try {
1052: server.close();
1053: } catch (IOException ex) {
1054: }
1055: }
1056: if (socket != null) {
1057: try {
1058: socket.close();
1059: } catch (IOException ex) {
1060: }
1061: }
1062: }
1063: }
1064:
1065: /**
1066: * sendUrgentData(int data) method testing.
1067: */
1068: public void testSendUrgentData() {
1069: Server server = null;
1070: SSLSocket socket = null;
1071: try {
1072: server = new Server();
1073: socket = createSSLSocket(server.getPort());
1074:
1075: socket.sendUrgentData(0);
1076: fail("Expected exception was not thrown");
1077: } catch (Exception e) {
1078: if (doLog) {
1079: System.out.println("Trowed exception: "
1080: + e.getMessage());
1081: }
1082: } finally {
1083: if (server != null) {
1084: try {
1085: server.close();
1086: } catch (IOException ex) {
1087: }
1088: }
1089: if (socket != null) {
1090: try {
1091: socket.close();
1092: } catch (IOException ex) {
1093: }
1094: }
1095: }
1096: }
1097:
1098: /**
1099: * setOOBInline(boolean on) method testing.
1100: */
1101: public void testSetOOBInline() {
1102: Server server = null;
1103: SSLSocket socket = null;
1104: try {
1105: server = new Server();
1106: socket = createSSLSocket(server.getPort());
1107:
1108: socket.setOOBInline(true);
1109: fail("Expected exception was not thrown");
1110: } catch (Exception e) {
1111: if (doLog) {
1112: System.out.println("Trowed exception: "
1113: + e.getMessage());
1114: }
1115: } finally {
1116: if (server != null) {
1117: try {
1118: server.close();
1119: } catch (IOException ex) {
1120: }
1121: }
1122: if (socket != null) {
1123: try {
1124: socket.close();
1125: } catch (IOException ex) {
1126: }
1127: }
1128: }
1129: }
1130:
1131: /**
1132: * shutdownOutput() method testing.
1133: */
1134: public void testShutdownOutput() {
1135: Server server = null;
1136: SSLSocket socket = null;
1137: try {
1138: server = new Server();
1139: socket = createSSLSocket(server.getPort());
1140:
1141: socket.shutdownOutput();
1142: fail("Expected exception was not thrown");
1143: } catch (Exception e) {
1144: if (doLog) {
1145: System.out.println("Trowed exception: "
1146: + e.getMessage());
1147: }
1148: } finally {
1149: if (server != null) {
1150: try {
1151: server.close();
1152: } catch (IOException ex) {
1153: }
1154: }
1155: if (socket != null) {
1156: try {
1157: socket.close();
1158: } catch (IOException ex) {
1159: }
1160: }
1161: }
1162: }
1163:
1164: /**
1165: * shutdownInput() method testing.
1166: */
1167: public void testShutdownInput() {
1168: Server server = null;
1169: SSLSocket socket = null;
1170: try {
1171: server = new Server();
1172: socket = createSSLSocket(server.getPort());
1173:
1174: socket.shutdownInput();
1175: fail("Expected exception was not thrown");
1176: } catch (Exception e) {
1177: if (doLog) {
1178: System.out.println("Trowed exception: "
1179: + e.getMessage());
1180: }
1181: } finally {
1182: if (server != null) {
1183: try {
1184: server.close();
1185: } catch (IOException ex) {
1186: }
1187: }
1188: if (socket != null) {
1189: try {
1190: socket.close();
1191: } catch (IOException ex) {
1192: }
1193: }
1194: }
1195: }
1196:
1197: /**
1198: * toString() method testing.
1199: */
1200: public void testToString() throws Exception {
1201: SSLSocket socket = createSSLSocket();
1202: assertNotNull("String representation is null", socket
1203: .toString());
1204: }
1205:
1206: public static Test suite() {
1207: return new TestSuite(SSLSocketImplTest.class);
1208: }
1209:
1210: public static void main(String[] args) {
1211: junit.textui.TestRunner.run(suite());
1212: }
1213: }
|