1 /* 2 * Copyright (C) 2009 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 17 package android.permission.cts; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 import java.net.InetAddress; 24 25 /** 26 * Verify ConnectivityManager related methods without specific network state permissions. 27 */ 28 public class NoNetworkStatePermissionTest extends AndroidTestCase { 29 private ConnectivityManager mConnectivityManager; 30 private static final int TEST_NETWORK_TYPE = ConnectivityManager.TYPE_MOBILE; 31 private static final String TEST_FEATURE = "enableHIPRI"; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 mConnectivityManager = (ConnectivityManager) mContext.getSystemService( 37 Context.CONNECTIVITY_SERVICE); 38 assertNotNull(mConnectivityManager); 39 } 40 41 /** 42 * Verify that ConnectivityManager#getActiveNetworkInfo() requires permissions. 43 * <p>Requires Permission: 44 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. 45 */ 46 @SmallTest 47 public void testGetActiveNetworkInfo() { 48 try { 49 mConnectivityManager.getActiveNetworkInfo(); 50 fail("ConnectivityManager.getActiveNetworkInfo didn't throw SecurityException as" 51 + " expected"); 52 } catch (SecurityException e) { 53 // expected 54 } 55 } 56 57 /** 58 * Verify that ConnectivityManager#getNetworkInfo() requires permissions. 59 * <p>Requires Permission: 60 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. 61 */ 62 @SmallTest 63 public void testGetNetworkInfo() { 64 try { 65 mConnectivityManager.getNetworkInfo(TEST_NETWORK_TYPE); 66 fail("ConnectivityManager.getNetworkInfo didn't throw SecurityException as" 67 + " expected"); 68 } catch (SecurityException e) { 69 // expected 70 } 71 } 72 73 /** 74 * Verify that ConnectivityManager#getAllNetworkInfo() requires permissions. 75 * <p>Requires Permission: 76 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. 77 */ 78 @SmallTest 79 public void testGetAllNetworkInfo() { 80 try { 81 mConnectivityManager.getAllNetworkInfo(); 82 fail("ConnectivityManager.getAllNetworkInfo didn't throw SecurityException as" 83 + " expected"); 84 } catch (SecurityException e) { 85 // expected 86 } 87 } 88 89 @SmallTest 90 public void testSecurityExceptionFromDns() throws Exception { 91 try { 92 InetAddress.getByName("www.google.com"); 93 fail(); 94 } catch (SecurityException expected) { 95 } 96 } 97 } 98