1package org.apache.harmony.xnet.tests.support;
2
3import java.security.Principal;
4import java.security.cert.Certificate;
5import java.security.cert.CertificateEncodingException;
6import java.util.Enumeration;
7import java.util.Hashtable;
8import java.util.Vector;
9
10import javax.net.ssl.SSLPeerUnverifiedException;
11import javax.net.ssl.SSLSessionBindingEvent;
12import javax.net.ssl.SSLSessionBindingListener;
13import javax.net.ssl.SSLSessionContext;
14import javax.security.cert.CertificateException;
15import javax.security.cert.X509Certificate;
16
17import javax.net.ssl.SSLSession;
18
19import org.apache.harmony.security.tests.support.TestCertUtils;
20
21public class mySSLSession implements SSLSession {
22
23    private byte[] idData;
24    private String nameHost = null;
25    private int namePort = -1;
26    private Hashtable table;
27    private boolean invalidateDone = false;
28    private Certificate[] certs = null;
29    private X509Certificate[] xCerts = null;
30
31    public mySSLSession(String host, int port, byte[] id) {
32        certs = null;
33        xCerts = null;
34        nameHost = host;
35        namePort = port;
36        idData = id;
37        table = new Hashtable();
38    }
39
40    public mySSLSession(X509Certificate[] xc) {
41        certs = TestCertUtils.getCertChain();
42        xCerts = xc;
43    }
44
45    public mySSLSession(Certificate[] xc) throws CertificateEncodingException, CertificateException {
46        certs = xc;
47        xCerts = new X509Certificate[xc.length];
48        int i = 0;
49        for (Certificate cert : xc) {
50            xCerts[i++] = X509Certificate.getInstance(cert.getEncoded());
51        }
52    }
53
54    public mySSLSession() {
55    }
56
57    public int getApplicationBufferSize() {
58        return 1234567;
59    }
60
61    public String getCipherSuite() {
62        return "SuiteName";
63    }
64
65    public long getCreationTime() {
66        return 1000l;
67    }
68
69    public byte[] getId() {
70        return idData;
71    }
72
73    public long getLastAccessedTime() {
74        return 2000l;
75    }
76
77    public Certificate[] getLocalCertificates() {
78        return null;
79    }
80
81    public Principal getLocalPrincipal() {
82        return null;
83    }
84
85    public int getPacketBufferSize() {
86        return 12345;
87    }
88
89    public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
90        if (certs == null) {
91            throw new SSLPeerUnverifiedException("peer not authenticated");
92        } else {
93            return certs;
94        }
95    }
96
97    public X509Certificate[] getPeerCertificateChain() throws SSLPeerUnverifiedException {
98        if(xCerts == null) {
99            throw new SSLPeerUnverifiedException("peer not authenticated");
100        } else {
101            return xCerts;
102        }
103    }
104
105    public String getPeerHost() {
106        return nameHost;
107    }
108
109    public int getPeerPort() {
110        return namePort;
111    }
112
113    public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
114        return null;
115    }
116
117    public String getProtocol() {
118        return "ProtocolName";
119    }
120
121    public SSLSessionContext getSessionContext() {
122        return null;
123    }
124
125    public void putValue(String s, Object obj) {
126        if(s == null || obj == null)
127            throw new IllegalArgumentException("arguments can not be null");
128        Object obj1 = table.put(s, obj);
129        if(obj1 instanceof SSLSessionBindingListener) {
130            SSLSessionBindingEvent sslsessionbindingevent = new SSLSessionBindingEvent(this, s);
131            ((SSLSessionBindingListener)obj1).valueUnbound(sslsessionbindingevent);
132        }
133        if(obj instanceof SSLSessionBindingListener) {
134            SSLSessionBindingEvent sslsessionbindingevent1 = new SSLSessionBindingEvent(this, s);
135            ((SSLSessionBindingListener)obj).valueBound(sslsessionbindingevent1);
136        }
137    }
138
139    public void removeValue(String s) {
140        if(s == null)
141            throw new IllegalArgumentException("argument can not be null");
142        Object obj = table.remove(s);
143        if(obj instanceof SSLSessionBindingListener) {
144            SSLSessionBindingEvent sslsessionbindingevent = new SSLSessionBindingEvent(this, s);
145            ((SSLSessionBindingListener)obj).valueUnbound(sslsessionbindingevent);
146        }
147    }
148
149    public Object getValue(String s) {
150        if(s == null) {
151            throw new IllegalArgumentException("argument can not be null");
152        } else {
153            return table.get(s);
154        }
155    }
156
157    public String[] getValueNames() {
158        Vector vector = new Vector();
159        Enumeration enumeration = table.keys();
160        while (enumeration.hasMoreElements()) {
161            vector.addElement(enumeration.nextElement());
162        }
163        String as[] = new String[vector.size()];
164        vector.copyInto(as);
165        return as;
166    }
167
168    public void invalidate() {
169        invalidateDone = true;
170    }
171
172    public boolean isValid() {
173        return invalidateDone;
174    }
175
176}
177