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
19package org.apache.harmony.xnet.tests.javax.net.ssl;
20
21import java.io.IOException;
22import java.net.URL;
23import java.security.cert.Certificate;
24
25import javax.net.ssl.HostnameVerifier;
26import javax.net.ssl.HttpsURLConnection;
27import javax.net.ssl.SSLPeerUnverifiedException;
28import javax.net.ssl.SSLSocketFactory;
29
30import junit.framework.TestCase;
31
32
33/**
34 * Tests for <code>HttpsURLConnection</code> class constructors and methods.
35 */
36public class HttpsURLConnectionTest extends TestCase {
37
38    public final void testGetPeerPrincipal() throws Exception {
39        HttpsURLConnection con = new MyHttpsURLConnection(new URL(
40                "http://foo.com"));
41        try {
42            con.getPeerPrincipal();
43            fail("No expected SSLPeerUnverifiedException");
44        } catch (SSLPeerUnverifiedException e) {
45        }
46    }
47
48    public final void testGetLocalPrincipal() {
49        HttpsURLConnection con = new MyHttpsURLConnection(null);
50        if (con.getLocalPrincipal() != null) {
51            fail("Non null result");
52        }
53    }
54
55    public final void testSetDefaultHostnameVerifier() {
56        try {
57            HttpsURLConnection.setDefaultHostnameVerifier(null);
58            fail("No expected IllegalArgumentException");
59        } catch (IllegalArgumentException e) {
60        }
61    }
62
63    public final void testSetHostnameVerifier() {
64        HttpsURLConnection con = new MyHttpsURLConnection(null);
65        try {
66            con.setHostnameVerifier(null);
67            fail("No expected IllegalArgumentException");
68        } catch (IllegalArgumentException e) {
69        }
70    }
71
72    public final void testGetDefaultSSLSocketFactory() {
73        SSLSocketFactory sf = HttpsURLConnection.getDefaultSSLSocketFactory();
74        if (!sf.equals(SSLSocketFactory.getDefault())) {
75            fail("incorrect DefaultSSLSocketFactory");
76        }
77    }
78
79    public final void testGetSSLSocketFactory() {
80        HttpsURLConnection con = new MyHttpsURLConnection(null);
81        SSLSocketFactory sf = con.getSSLSocketFactory();
82        if (!sf.equals(SSLSocketFactory.getDefault())) {
83            fail("incorrect DefaultSSLSocketFactory");
84        }
85    }
86
87    public final void testSetDefaultSSLSocketFactory() {
88        try {
89            HttpsURLConnection.setDefaultSSLSocketFactory(null);
90            fail("No expected IllegalArgumentException");
91        } catch (IllegalArgumentException e) {
92        }
93    }
94
95    public final void testSetSSLSocketFactory() {
96        HttpsURLConnection con = new MyHttpsURLConnection(null);
97        try {
98            con.setSSLSocketFactory(null);
99            fail("No expected IllegalArgumentException");
100        } catch (IllegalArgumentException e) {
101        }
102    }
103}
104
105class MyHttpsURLConnection extends HttpsURLConnection {
106
107    public MyHttpsURLConnection(URL url) {
108        super(url);
109    }
110
111    /*
112     * @see javax.net.ssl.HttpsURLConnection#getCipherSuite()
113     */
114    @Override
115    public String getCipherSuite() {
116        return null;
117    }
118
119    /*
120     * @see javax.net.ssl.HttpsURLConnection#getLocalCertificates()
121     */
122    @Override
123    public Certificate[] getLocalCertificates() {
124        return null;
125    }
126
127    /*
128     * @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
129     */
130    @Override
131    public Certificate[] getServerCertificates()
132            throws SSLPeerUnverifiedException {
133        return null;
134    }
135
136    /*
137     * @see java.net.HttpURLConnection#disconnect()
138     */
139    @Override
140    public void disconnect() {
141    }
142
143    /*
144     * @see java.net.HttpURLConnection#usingProxy()
145     */
146    @Override
147    public boolean usingProxy() {
148        return false;
149    }
150
151    /*
152     * @see java.net.URLConnection#connect()
153     */
154    @Override
155    public void connect() throws IOException {
156    }
157
158}
159
160