Package org.apache.harmony.xnet.tests.javax.net.ssl

Source Code of org.apache.harmony.xnet.tests.javax.net.ssl.HandshakeCompletedEventTest

/*
*  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.harmony.xnet.tests.javax.net.ssl;

import java.io.IOException;
import java.net.ServerSocket;
import java.security.cert.Certificate;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import junit.framework.TestCase;


/**
* Tests for <code>HandshakeCompletedEvent</code> class constructors and methods.
*
*/
public class HandshakeCompletedEventTest extends TestCase {

    int port;

    ServerSocket ss;

    SSLSocket soc;

    boolean noFreePort = false;
    boolean noSocket = false;

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
        try {
            ss = new ServerSocket(0);
            port = ss.getLocalPort();
        } catch (Exception e) {
            e.printStackTrace();
            noFreePort = true;
            return;
        }
        try {
            soc = (SSLSocket) sf.createSocket("localhost", port);
        } catch (IOException e) {
            noSocket = true;
        }

    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
        if (ss != null) {
            ss.close();
        }
        if (soc != null) {
            soc.close();
        }
    }

    public final void testGetCipherSuite() {
        if (noFreePort || noSocket) {
            return;
        }
        SSLSession ses = new MySSLSession();

        HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses);
        String name = event.getCipherSuite();
        String name_ses = ses.getCipherSuite();
        if (name == null && name_ses != null) {
            fail("incorrect null CipherCuite");
        }
        if (!name.equals(name_ses)) {
            fail("incorrect CipherCuite");
        }
    }

    public final void testGetLocalCertificates() {
      if (noFreePort || noSocket) {
            return;
        }
      SSLSession ses = new MySSLSession();
        HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses);

        Certificate[] certs = event.getLocalCertificates();
        Certificate[] ses_certs = ses.getLocalCertificates();
        if (certs == null && ses_certs == null) {
            return;
        }
        if (certs == null || ses_certs == null) {
            fail("incorrect LocalCertificates");
        }
        for (int i = 0; i < certs.length; i++) {
            if (certs[i] != ses_certs[i]) {
                fail("incorrect LocalCertificates");
            }
        }
    }

    public final void testGetPeerCertificates() {
      if (noFreePort || noSocket) {
            return;
        }
      SSLSession ses = new MySSLSession();
        HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses);
        try {
            event.getPeerCertificates();
            fail("No excpected SSLPeerUnverifiedException");
        } catch (SSLPeerUnverifiedException e) {
        }
    }

    public final void testGetPeerCertificateChain() {
      if (noFreePort || noSocket) {
            return;
        }
      SSLSession ses = new MySSLSession();
        HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses);
        try {
            event.getPeerCertificateChain();
            fail("No excpected SSLPeerUnverifiedException");
        } catch (SSLPeerUnverifiedException e) {
        }
    }

    public final void testHandshakeCompletedEvent() {
      if (noFreePort || noSocket) {
            return;
        }
      SSLSession ses = new MySSLSession();
        HandshakeCompletedEvent event = new HandshakeCompletedEvent(soc, ses);
        if (!ses.equals(event.getSession())) {
            fail("incorrect session");
        }
        if (!soc.equals(event.getSocket())) {
            fail("incorrect socket");
        }
    }

}
TOP

Related Classes of org.apache.harmony.xnet.tests.javax.net.ssl.HandshakeCompletedEventTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.