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 17package android.net.vpn; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.net.vpn.L2tpProfile; 23import android.net.vpn.L2tpIpsecProfile; 24import android.net.vpn.L2tpIpsecPskProfile; 25import android.net.vpn.PptpProfile; 26import android.net.vpn.VpnManager; 27import android.net.vpn.VpnProfile; 28import android.net.vpn.VpnState; 29import android.net.vpn.VpnType; 30import android.os.ConditionVariable; 31import android.os.Parcel; 32import android.test.AndroidTestCase; 33import android.test.suitebuilder.annotation.SmallTest; 34import android.text.TextUtils; 35 36/** 37 * Unit test class to test VPN api 38 * Use the below command to run the vpn unit test only 39 * runtest vpntest or 40 * adb shell am instrument -e class 'com.android.unit_tests.VpnTest' 41 * -w com.android.unit_tests/android.test.InstrumentationTestRunner 42 */ 43public class VpnTest extends AndroidTestCase { 44 private static final String NAME = "a name"; 45 private static final String SERVER_NAME = "a server name"; 46 private static final String ID = "some id"; 47 private static final String SUFFICES = "some suffices"; 48 private static final String ROUTES = "some routes"; 49 private static final String SAVED_NAME = "some name"; 50 51 @Override 52 public void setUp() { 53 } 54 55 @Override 56 public void tearDown() { 57 } 58 59 @SmallTest 60 public void testVpnType() { 61 testVpnType(VpnType.L2TP); 62 testVpnType(VpnType.L2TP_IPSEC); 63 testVpnType(VpnType.L2TP_IPSEC_PSK); 64 testVpnType(VpnType.PPTP); 65 } 66 67 @SmallTest 68 public void testVpnProfile() { 69 VpnState state = VpnState.CONNECTING; 70 testVpnProfile(createTestProfile(state), state); 71 } 72 73 @SmallTest 74 public void testGetType() { 75 assertEquals(VpnType.L2TP, new L2tpProfile().getType()); 76 assertEquals(VpnType.L2TP_IPSEC, new L2tpIpsecProfile().getType()); 77 assertEquals(VpnType.L2TP_IPSEC_PSK, 78 new L2tpIpsecPskProfile().getType()); 79 assertEquals(VpnType.PPTP, new PptpProfile().getType()); 80 } 81 82 @SmallTest 83 public void testVpnTypes() { 84 assertTrue(VpnManager.getSupportedVpnTypes().length > 0); 85 } 86 87 @SmallTest 88 public void testGetTypeFromManager() { 89 VpnManager m = new VpnManager(getContext()); 90 VpnType[] types = VpnManager.getSupportedVpnTypes(); 91 for (VpnType t : types) { 92 assertEquals(t, m.createVpnProfile(t).getType()); 93 } 94 } 95 96 @SmallTest 97 public void testParcelable() { 98 VpnProfile p = createTestProfile(VpnState.CONNECTED); 99 Parcel parcel = Parcel.obtain(); 100 p.writeToParcel(parcel, 0); 101 parcel.setDataPosition(0); 102 103 // VpnState is transient and not saved in the parcel 104 testVpnProfile(VpnProfile.CREATOR.createFromParcel(parcel), null); 105 } 106 107 @SmallTest 108 public void testReceiver() { 109 final String profileName = "whatever"; 110 final VpnState state = VpnState.DISCONNECTING; 111 final ConditionVariable cv = new ConditionVariable(); 112 cv.close(); 113 BroadcastReceiver r = new BroadcastReceiver() { 114 public void onReceive(Context c, Intent i) { 115 assertEquals(profileName, 116 i.getStringExtra(VpnManager.BROADCAST_PROFILE_NAME)); 117 assertEquals(state, i.getSerializableExtra( 118 VpnManager.BROADCAST_CONNECTION_STATE)); 119 cv.open(); 120 } 121 }; 122 123 VpnManager m = new VpnManager(getContext()); 124 m.registerConnectivityReceiver(r); 125 m.broadcastConnectivity(profileName, state); 126 127 // fail it if onReceive() doesn't get executed in 5 sec 128 assertTrue(cv.block(5000)); 129 } 130 131 private void testVpnType(VpnType type) { 132 assertFalse(TextUtils.isEmpty(type.getDisplayName())); 133 assertNotNull(type.getProfileClass()); 134 } 135 136 private VpnProfile createTestProfile(VpnState state) { 137 VpnProfile p = new L2tpProfile(); 138 p.setName(NAME); 139 p.setServerName(SERVER_NAME); 140 p.setId(ID); 141 p.setDomainSuffices(SUFFICES); 142 p.setRouteList(ROUTES); 143 p.setSavedUsername(SAVED_NAME); 144 p.setState(state); 145 return p; 146 } 147 148 private void testVpnProfile(VpnProfile p, VpnState state) { 149 assertEquals(NAME, p.getName()); 150 assertEquals(SERVER_NAME, p.getServerName()); 151 assertEquals(ID, p.getId()); 152 assertEquals(SUFFICES, p.getDomainSuffices()); 153 assertEquals(ROUTES, p.getRouteList()); 154 assertEquals(SAVED_NAME, p.getSavedUsername()); 155 if (state != null) assertEquals(state, p.getState()); 156 } 157} 158