Home | History | Annotate | Download | only in support
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.xnet.tests.support;
     19 
     20 import java.nio.ByteBuffer;
     21 import java.security.KeyManagementException;
     22 import java.security.SecureRandom;
     23 
     24 import javax.net.ssl.KeyManager;
     25 import javax.net.ssl.SSLContextSpi;
     26 import javax.net.ssl.SSLEngine;
     27 import javax.net.ssl.SSLEngineResult;
     28 import javax.net.ssl.SSLException;
     29 import javax.net.ssl.SSLSession;
     30 import javax.net.ssl.SSLSessionContext;
     31 import javax.net.ssl.SSLServerSocketFactory;
     32 import javax.net.ssl.SSLSocketFactory;
     33 import javax.net.ssl.TrustManager;
     34 
     35 /**
     36  * Additional class for verification of SSLContextSpi and SSLContext
     37  * functionality
     38  *
     39  */
     40 
     41 public class MySSLContextSpi extends SSLContextSpi {
     42     private boolean init = false;
     43     protected void engineInit(KeyManager[] km, TrustManager[] tm,
     44             SecureRandom sr) throws KeyManagementException {
     45         if (sr == null) {
     46             throw new KeyManagementException(
     47                     "secureRandom is null");
     48         }
     49         init = true;
     50     }
     51 
     52     protected SSLSocketFactory engineGetSocketFactory() {
     53         if (!init) {
     54             throw new RuntimeException("Not initialiazed");
     55         }
     56         return null;
     57     }
     58 
     59     protected SSLServerSocketFactory engineGetServerSocketFactory() {
     60         if (!init) {
     61             throw new RuntimeException("Not initialiazed");
     62         }
     63         return null;
     64     }
     65 
     66     protected SSLSessionContext engineGetServerSessionContext() {
     67         if (!init) {
     68             throw new RuntimeException("Not initialiazed");
     69         }
     70         return null;
     71     }
     72 
     73     protected SSLSessionContext engineGetClientSessionContext() {
     74         if (!init) {
     75             throw new RuntimeException("Not initialiazed");
     76         }
     77         return null;
     78     }
     79 
     80     /*
     81      * FIXME: add these methods
     82      */
     83     protected SSLEngine engineCreateSSLEngine(String host, int port) {
     84         if (!init) {
     85             throw new RuntimeException("Not initialiazed");
     86         }
     87         return new tmpSSLEngine(host, port);
     88     }
     89 
     90     protected SSLEngine engineCreateSSLEngine() {
     91         if (!init) {
     92             throw new RuntimeException("Not initialiazed");
     93         }
     94         return new tmpSSLEngine();
     95     }
     96 
     97     public class tmpSSLEngine extends SSLEngine {
     98         String tmpHost;
     99         int tmpPort;
    100         public tmpSSLEngine() {
    101             tmpHost = null;
    102             tmpPort = 0;
    103         }
    104         public tmpSSLEngine(String host, int port) {
    105             tmpHost = host;
    106             tmpPort = port;
    107         }
    108         public String getPeerHost() {
    109             return tmpHost;
    110         }
    111         public int getPeerPort() {
    112             return tmpPort;
    113         }
    114         public void beginHandshake() throws SSLException { }
    115         public void closeInbound() throws SSLException { }
    116         public void closeOutbound() {}
    117         public Runnable getDelegatedTask() { return null; }
    118         public String[] getEnabledCipherSuites() { return null; }
    119         public String[] getEnabledProtocols() {return null; }
    120         public boolean getEnableSessionCreation() { return true; }
    121         public SSLEngineResult.HandshakeStatus getHandshakeStatus() { return null; }
    122         public boolean getNeedClientAuth() { return true; }
    123         public SSLSession getSession() { return null; }
    124         public String[] getSupportedCipherSuites()  { return null; }
    125         public String[] getSupportedProtocols()  { return null; }
    126         public boolean getUseClientMode()  { return true; }
    127         public boolean getWantClientAuth()  { return true; }
    128         public boolean isInboundDone()  { return true; }
    129         public boolean isOutboundDone()  { return true; }
    130         public void setEnabledCipherSuites(String[] suites) { }
    131         public void setEnabledProtocols(String[] protocols) { }
    132         public void setEnableSessionCreation(boolean flag) { }
    133         public void setNeedClientAuth(boolean need) { }
    134         public void setUseClientMode(boolean mode) { }
    135         public void setWantClientAuth(boolean want) { }
    136         public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts,
    137                 int offset, int length) throws SSLException {
    138             return null;
    139         }
    140         public SSLEngineResult wrap(ByteBuffer[] srcs, int offset,
    141                 int length, ByteBuffer dst) throws SSLException {
    142             return null;
    143         }
    144     }
    145 }