1 /* 2 * Copyright (C) 2016 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 com.android.cts.deviceandprofileowner; 18 19 import android.content.pm.PackageManager; 20 import android.system.ErrnoException; 21 import android.system.OsConstants; 22 23 import com.android.cts.deviceandprofileowner.vpn.VpnTestHelper; 24 25 import static com.android.cts.deviceandprofileowner.vpn.VpnTestHelper.VPN_PACKAGE; 26 import static com.android.cts.deviceandprofileowner.vpn.VpnTestHelper.TEST_ADDRESS; 27 28 /** 29 * Contains methods to test always-on VPN invoked by DeviceAndProfileOwnerTest 30 */ 31 public class AlwaysOnVpnMultiStageTest extends BaseDeviceAdminTest { 32 33 public void testAlwaysOnSet() throws Exception { 34 // Setup always-on vpn 35 VpnTestHelper.setAndWaitForVpn(mContext, VPN_PACKAGE, /* usable */ true); 36 assertTrue(VpnTestHelper.isNetworkVpn(mContext)); 37 VpnTestHelper.checkPing(TEST_ADDRESS); 38 } 39 40 public void testNetworkBlocked() throws Exception { 41 // After the vpn app being force-stop, expect that always-on package stays the same 42 assertEquals(VPN_PACKAGE, mDevicePolicyManager.getAlwaysOnVpnPackage( 43 ADMIN_RECEIVER_COMPONENT)); 44 assertFalse(VpnTestHelper.isNetworkVpn(mContext)); 45 // Expect the network is still locked down after the vpn app process is killed 46 try { 47 VpnTestHelper.tryPosixConnect(TEST_ADDRESS); 48 fail("sendIcmpMessage doesn't throw Exception during network lockdown"); 49 } catch (ErrnoException e) { 50 // Os.connect returns ENETUNREACH or EACCES errno after the vpn app process is killed 51 assertTrue((e.errno == OsConstants.ENETUNREACH) || 52 (e.errno == OsConstants.EACCES)); 53 } 54 } 55 56 public void testAlwaysOnVpnDisabled() throws Exception { 57 // After the vpn app being uninstalled, check that always-on vpn is null 58 assertNull(mDevicePolicyManager.getAlwaysOnVpnPackage(ADMIN_RECEIVER_COMPONENT)); 59 assertFalse(VpnTestHelper.isNetworkVpn(mContext)); 60 } 61 62 public void testSetNonExistingPackage() throws Exception { 63 assertNull(mDevicePolicyManager.getAlwaysOnVpnPackage(ADMIN_RECEIVER_COMPONENT)); 64 65 // Verify it throws NameNotFoundException for non-existing package after uninstallation 66 try { 67 mDevicePolicyManager.setAlwaysOnVpnPackage(ADMIN_RECEIVER_COMPONENT, VPN_PACKAGE, 68 true); 69 fail("setAlwaysOnVpnPackage should not accept non-vpn package"); 70 } catch (PackageManager.NameNotFoundException e) { 71 // success 72 } 73 74 assertNull(mDevicePolicyManager.getAlwaysOnVpnPackage(ADMIN_RECEIVER_COMPONENT)); 75 } 76 77 public void testCleanup() throws Exception { 78 mDevicePolicyManager.setAlwaysOnVpnPackage(ADMIN_RECEIVER_COMPONENT, null, false); 79 } 80 } 81