1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.core; 18 19import org.apache.http.HttpHost; 20 21import android.content.Context; 22import android.net.Proxy; 23import android.test.AndroidTestCase; 24 25/** 26 * Proxy tests 27 */ 28public class ProxyTest extends AndroidTestCase { 29 private Context mContext; 30 private HttpHost mHttpHost; 31 32 @Override 33 protected void setUp() throws Exception { 34 super.setUp(); 35 36 mContext = getContext(); 37 mHttpHost = null; 38 String proxyHost = Proxy.getHost(mContext); 39 int proxyPort = Proxy.getPort(mContext); 40 if (proxyHost != null) { 41 mHttpHost = new HttpHost(proxyHost, proxyPort, "http"); 42 } 43 } 44 45 @Override 46 protected void tearDown() throws Exception { 47 super.tearDown(); 48 } 49 50 /** 51 * Bad url parameter should not cause any exception. 52 */ 53 public void testProxyGetPreferredHttpHost_UrlBad() throws Exception { 54 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, null)); 55 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "")); 56 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad:")); 57 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad")); 58 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad:\\")); 59 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "bad://#")); 60 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "://#")); 61 } 62 63 /** 64 * Proxy (if available) should be returned when url parameter is not localhost. 65 */ 66 public void testProxyGetPreferredHttpHost_UrlNotlLocalhost() throws Exception { 67 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://")); 68 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://example.com")); 69 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://example.com/")); 70 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "http://192.168.0.1/")); 71 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "file:///foo/bar")); 72 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "rtsp://example.com")); 73 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "rtsp://example.com/")); 74 assertEquals(mHttpHost, Proxy.getPreferredHttpHost(mContext, "javascript:alert(1)")); 75 } 76 77 /** 78 * No proxy should be returned when url parameter is localhost. 79 */ 80 public void testProxyGetPreferredHttpHost_UrlLocalhost() throws Exception { 81 assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost")); 82 assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost/")); 83 assertNull(Proxy.getPreferredHttpHost(mContext, "http://localhost/hej.html")); 84 assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1")); 85 assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1/")); 86 assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1/hej.html")); 87 assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1:80/")); 88 assertNull(Proxy.getPreferredHttpHost(mContext, "http://127.0.0.1:8080/")); 89 assertNull(Proxy.getPreferredHttpHost(mContext, "rtsp://127.0.0.1/")); 90 assertNull(Proxy.getPreferredHttpHost(mContext, "rtsp://localhost/")); 91 assertNull(Proxy.getPreferredHttpHost(mContext, "https://localhost/")); 92 } 93} 94