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