1 /* 2 * Copyright (C) 2015 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.verifier.managedprovisioning; 18 19 import android.app.AlertDialog; 20 import android.app.admin.DevicePolicyManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.net.VpnService; 25 import android.net.VpnService.Builder; 26 import android.os.Bundle; 27 import android.os.ParcelFileDescriptor; 28 import android.os.UserManager; 29 import android.provider.Settings; 30 import android.util.Log; 31 import android.widget.TextView; 32 import android.net.VpnService; 33 import android.os.ParcelFileDescriptor; 34 35 import com.android.cts.verifier.PassFailButtons; 36 import com.android.cts.verifier.R; 37 38 import java.io.IOException; 39 40 /** 41 * Activity to test Vpn configuration 42 */ 43 public class VpnTestActivity extends PassFailButtons.Activity { 44 45 public static final String ACTION_VPN = "com.android.cts.verifier.managedprovisioning.VPN"; 46 47 public static class MyTestVpnService extends VpnService { 48 /* 49 * MyVpnTestService is just a stub. This class exists because the framework needs a class 50 * inside the app to refer back to, just using VpnService itself won't work. 51 */ 52 } 53 54 private ParcelFileDescriptor descriptor = null; 55 private ComponentName mAdminReceiverComponent; 56 private DevicePolicyManager mDevicePolicyManager; 57 private UserManager mUserManager; 58 private static final String TAG = "DeviceOwnerPositiveTestActivity"; 59 private static final int REQUEST_VPN_CODE = 1; 60 61 @Override onCreate(Bundle savedInstanceState)62 protected void onCreate(Bundle savedInstanceState) { 63 super.onCreate(savedInstanceState); 64 setContentView(R.layout.vpn_test); 65 setPassFailButtonClickListeners(); 66 mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName()); 67 mDevicePolicyManager = (DevicePolicyManager) getSystemService( 68 Context.DEVICE_POLICY_SERVICE); 69 mDevicePolicyManager.addUserRestriction(mAdminReceiverComponent, 70 UserManager.DISALLOW_CONFIG_VPN); 71 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); 72 testVpnEstablishFails(); 73 } 74 75 @Override finish()76 public void finish() { 77 mDevicePolicyManager.clearUserRestriction(mAdminReceiverComponent, 78 UserManager.DISALLOW_CONFIG_VPN); 79 super.finish(); 80 } 81 82 @Override onActivityResult(int requestCode, int result, Intent data)83 protected void onActivityResult(int requestCode, int result, Intent data) { 84 if (requestCode == REQUEST_VPN_CODE && result == RESULT_OK) { 85 establishVpn(); 86 } else { 87 // vpn connection canceled by user 88 Log.w(TAG, "Test failed, canceled by user"); 89 populateInfo(R.string.device_owner_vpn_connection_canceled); 90 } 91 } 92 testVpnEstablishFails()93 public void testVpnEstablishFails() { 94 Intent newIntent = VpnService.prepare(this); 95 if (newIntent != null) { 96 startActivityForResult(newIntent, REQUEST_VPN_CODE); 97 } else { 98 establishVpn(); 99 } 100 } 101 establishVpn()102 public void establishVpn() { 103 MyTestVpnService service = new MyTestVpnService(); 104 descriptor = service.new Builder().addAddress("8.8.8.8", 30).establish(); 105 if (descriptor == null) { 106 // vpn connection not established, as expected, test case succeeds 107 Log.i(TAG, "Test succeeded: descriptor is null"); 108 populateInfo(R.string.device_owner_no_vpn_connection); 109 return; 110 } 111 // vpn connection established, not expected, test case fails 112 Log.w(TAG, "vpn connection established, not expected, test case fails"); 113 try { 114 descriptor.close(); 115 populateInfo(R.string.device_owner_vpn_connection); 116 } catch (IOException e) { 117 Log.i(TAG, "Closing vpn connection failed. Caught exception: ", e); 118 populateInfo(R.string.device_owner_vpn_connection_close_failed); 119 } 120 } 121 populateInfo(int messageId)122 private void populateInfo(int messageId) { 123 TextView vpnInfoTextView = (TextView) findViewById(R.id.device_owner_vpn_info); 124 vpnInfoTextView.setText(getString(messageId)); 125 } 126 127 } 128