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
18package org.apache.harmony.tests.javax.net.ssl;
19
20import java.security.Principal;
21import java.security.cert.Certificate;
22
23import javax.net.ssl.SSLPeerUnverifiedException;
24import javax.net.ssl.SSLSession;
25import javax.net.ssl.SSLSessionContext;
26import javax.net.ssl.SSLSessionBindingEvent;
27import javax.security.cert.X509Certificate;
28
29import junit.framework.TestCase;
30
31/**
32 * Tests for <code>SSLSessionBindingEvent</code> class constructors and methods.
33 *
34 */
35public class SSLSessionBindingEventTest extends TestCase {
36
37    public final void test_ConstructorLjavax_net_ssl_SSLSessionLjava_lang_String() {
38        SSLSession ses = new MySSLSession();
39
40        try {
41            SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test");
42            if (!"test".equals(event.getName())) {
43                fail("incorrect name");
44            }
45            if (!event.getSession().equals(ses)) {
46                fail("incorrect session");
47            }
48        } catch (Exception e) {
49            fail("Unexpected exception " + e);
50        }
51
52        try {
53            SSLSessionBindingEvent event = new SSLSessionBindingEvent(null, "test");
54            fail("IllegalArgumentException expected");
55        } catch (IllegalArgumentException e) {
56          // expected
57        }
58
59        try {
60            SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, null);
61        } catch (IllegalArgumentException e) {
62          fail("Unexpected IllegalArgumentException: " + e);
63        }
64    }
65
66    /**
67     * javax.net.ssl.SSLSessionBindingEvent#getName()
68     */
69    public void test_getName() {
70        SSLSession ses = new MySSLSession();
71        SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test");
72        assertEquals("Incorrect session name", "test", event.getName());
73        event = new SSLSessionBindingEvent(ses, null);
74        assertEquals("Incorrect session name", null, event.getName());
75    }
76
77    /**
78     * javax.net.ssl.SSLSessionBindingEvent#getSession()
79     */
80    public void test_getSession() {
81        SSLSession ses = new MySSLSession();
82        SSLSessionBindingEvent event = new SSLSessionBindingEvent(ses, "test");
83        assertEquals("Incorrect session", ses, event.getSession());
84    }
85}
86
87class MySSLSession implements SSLSession {
88    /*
89     * @see javax.net.ssl.SSLSession#getApplicationBufferSize()
90     */
91    public int getApplicationBufferSize() {
92        return 0;
93    }
94
95    /*
96     * @see javax.net.ssl.SSLSession#getCipherSuite()
97     */
98    public String getCipherSuite() {
99        return "MyTestCipherSuite";
100    }
101
102    /*
103     * @see javax.net.ssl.SSLSession#getCreationTime()
104     */
105    public long getCreationTime() {
106        return 0;
107    }
108
109    /*
110     * @see javax.net.ssl.SSLSession#getId()
111     */
112    public byte[] getId() {
113        return null;
114    }
115
116    /*
117     * @see javax.net.ssl.SSLSession#getLastAccessedTime()
118     */
119    public long getLastAccessedTime() {
120        return 0;
121    }
122
123    /*
124     * @see javax.net.ssl.SSLSession#getLocalCertificates()
125     */
126    public Certificate[] getLocalCertificates() {
127        return null;
128    }
129
130    /*
131     * @see javax.net.ssl.SSLSession#getLocalPrincipal()
132     */
133    public Principal getLocalPrincipal() {
134        return null;
135    }
136
137    /*
138     * @see javax.net.ssl.SSLSession#getPacketBufferSize()
139     */
140    public int getPacketBufferSize() {
141        return 0;
142    }
143
144    /*
145     * @see javax.net.ssl.SSLSession#getPeerCertificateChain()
146     */
147    public X509Certificate[] getPeerCertificateChain()
148    throws SSLPeerUnverifiedException {
149        throw new SSLPeerUnverifiedException("test exception");
150    }
151
152    /*
153     * @see javax.net.ssl.SSLSession#getPeerCertificates()
154     */
155    public Certificate[] getPeerCertificates()
156    throws SSLPeerUnverifiedException {
157        throw new SSLPeerUnverifiedException("test exception");
158    }
159
160    /*
161     * @see javax.net.ssl.SSLSession#getPeerHost()
162     */
163    public String getPeerHost() {
164        return null;
165    }
166
167    /*
168     * @see javax.net.ssl.SSLSession#getPeerPort()
169     */
170    public int getPeerPort() {
171        return 0;
172    }
173
174    /*
175     * @see javax.net.ssl.SSLSession#getPeerPrincipal()
176     */
177    public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
178        return null;
179    }
180
181    /*
182     * @see javax.net.ssl.SSLSession#getProtocol()
183     */
184    public String getProtocol() {
185        return null;
186    }
187
188    /*
189     * @see javax.net.ssl.SSLSession#getSessionContext()
190     */
191    public SSLSessionContext getSessionContext() {
192        return null;
193    }
194
195    /*
196     * @see javax.net.ssl.SSLSession#getValue(java.lang.String)
197     */
198    public Object getValue(String name) {
199        return null;
200    }
201
202    /*
203     * @see javax.net.ssl.SSLSession#getValueNames()
204     */
205    public String[] getValueNames() {
206        return null;
207    }
208
209    /*
210     * @see javax.net.ssl.SSLSession#invalidate()
211     */
212    public void invalidate() {
213    }
214
215    /*
216     * @see javax.net.ssl.SSLSession#isValid()
217     */
218    public boolean isValid() {
219        return false;
220    }
221
222    /*
223     * @see javax.net.ssl.SSLSession#putValue(java.lang.String,
224     *      java.lang.Object)
225     */
226    public void putValue(String name, Object value) {
227    }
228
229    /*
230     * @see javax.net.ssl.SSLSession#removeValue(java.lang.String)
231     */
232    public void removeValue(String name) {
233    }
234
235}
236
237