• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 54321;
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) {
85             // We don't care about the result - ideally it should automatically cancel, but if
86             // some custom component doesn't do that, try to establish the connection anyway
87             // and see what happens.
88             establishVpn();
89         } else {
90             Log.w(TAG, "Unexpected request code: " + requestCode);
91         }
92     }
93 
testVpnEstablishFails()94     public void testVpnEstablishFails() {
95         Intent newIntent = VpnService.prepare(this);
96         if (newIntent != null) {
97             startActivityForResult(newIntent, REQUEST_VPN_CODE);
98         } else {
99             establishVpn();
100         }
101     }
102 
establishVpn()103     public void establishVpn() {
104         MyTestVpnService service = new MyTestVpnService();
105         descriptor = service.new Builder().addAddress("8.8.8.8", 30).establish();
106         if (descriptor == null) {
107             // vpn connection not established, as expected, test case succeeds
108             Log.i(TAG, "Test succeeded: descriptor is null");
109             populateInfo(R.string.device_owner_no_vpn_connection);
110             return;
111         }
112         // vpn connection established, not expected, test case fails
113         Log.w(TAG, "vpn connection established, not expected, test case fails");
114         try {
115             descriptor.close();
116             populateInfo(R.string.device_owner_vpn_connection);
117         } catch (IOException e) {
118             Log.i(TAG, "Closing vpn connection failed. Caught exception: ", e);
119             populateInfo(R.string.device_owner_vpn_connection_close_failed);
120         }
121     }
122 
populateInfo(int messageId)123     private void populateInfo(int messageId) {
124         TextView vpnInfoTextView = (TextView) findViewById(R.id.device_owner_vpn_info);
125         vpnInfoTextView.setText(getString(messageId));
126     }
127 
128 }
129