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 tests.api.javax.net.ssl;
19
20import javax.net.ssl.SSLServerSocket;
21import javax.net.ssl.SSLServerSocketFactory;
22import javax.net.ssl.SSLSession;
23import javax.net.ssl.SSLSessionBindingEvent;
24import javax.net.ssl.SSLSessionBindingListener;
25import javax.net.ssl.SSLSocket;
26import javax.net.ssl.SSLSocketFactory;
27
28import java.io.IOException;
29import java.net.UnknownHostException;
30
31import junit.framework.TestCase;
32
33/**
34 * Tests for SSLSessionBindingListener class
35 *
36 */
37public class SSLSessionBindingListenerTest extends TestCase {
38
39    public class mySSLSessionBindingListener implements SSLSessionBindingListener {
40
41        public boolean boundDone = false;
42        public boolean unboundDone = false;
43
44        mySSLSessionBindingListener() {
45        }
46
47        public void valueBound(SSLSessionBindingEvent event) {
48            if (event != null) boundDone = true;
49        }
50        public void valueUnbound(SSLSessionBindingEvent event) {
51            if (event != null) unboundDone = true;
52        }
53    }
54
55    /**
56     * @throws IOException
57     * @throws UnknownHostException
58     * @throws InterruptedException
59     * javax.net.ssl.SSLSessionBindingListener#valueBound(SSLSessionBindingEvent event)
60     */
61    public void test_valueBound() throws UnknownHostException, IOException,
62            InterruptedException {
63        SSLSocket sock = (SSLSocket) SSLSocketFactory.getDefault()
64                .createSocket();
65        SSLSession ss = sock.getSession();
66        mySSLSessionBindingListener sbl = new mySSLSessionBindingListener();
67        ss.putValue("test", sbl);
68        assertTrue("valueBound was not called.", sbl.boundDone);
69    }
70
71    /**
72     * @throws IOException
73     * @throws UnknownHostException
74     * javax.net.ssl.SSLSessionBindingListener#valueUnbound(SSLSessionBindingEvent event)
75     */
76    public void test_valueUnbound() throws UnknownHostException, IOException {
77        SSLSocket sock = (SSLSocket) SSLSocketFactory.getDefault()
78                .createSocket();
79        SSLSession ss = sock.getSession();
80        mySSLSessionBindingListener sbl = new mySSLSessionBindingListener();
81        ss.putValue("test", sbl);
82        ss.removeValue("test");
83        assertTrue("valueUnbound was not called.", sbl.unboundDone);
84    }
85}
86