M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.net.vpn.VpnProfile;
/**
* Interface to access a VPN service.
* {@hide}
*/
interface IVpnService {
/**
* Sets up the VPN connection.
* @param profile the profile object
* @param username the username for authentication
* @param password the corresponding password for authentication
*/
boolean connect(in VpnProfile profile, String username, String password);
/**
* Tears down the VPN connection.
*/
void disconnect();
/**
* Makes the service broadcast the connectivity state.
*/
void checkStatus(in VpnProfile profile);
}
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for certificate-based L2TP-over-IPSec type of VPN.
* {@hide}
*/
public class L2tpIpsecProfile extends L2tpProfile {
private static final long serialVersionUID = 1L;
private String mUserCertificate;
private String mCaCertificate;
@Override
public VpnType getType() {
return VpnType.L2TP_IPSEC;
}
public void setCaCertificate(String name) {
mCaCertificate = name;
}
public String getCaCertificate() {
return mCaCertificate;
}
public void setUserCertificate(String name) {
mUserCertificate = name;
}
public String getUserCertificate() {
return mUserCertificate;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mCaCertificate = in.readString();
mUserCertificate = in.readString();
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeString(mCaCertificate);
parcel.writeString(mUserCertificate);
}
}
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for pre-shared-key-based L2TP-over-IPSec type of VPN.
* {@hide}
*/
public class L2tpIpsecPskProfile extends L2tpProfile {
private static final long serialVersionUID = 1L;
private String mPresharedKey;
@Override
public VpnType getType() {
return VpnType.L2TP_IPSEC_PSK;
}
public void setPresharedKey(String key) {
mPresharedKey = key;
}
public String getPresharedKey() {
return mPresharedKey;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mPresharedKey = in.readString();
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeString(mPresharedKey);
}
}
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for L2TP type of VPN.
* {@hide}
*/
public class L2tpProfile extends VpnProfile {
private static final long serialVersionUID = 1L;
private boolean mSecret;
private String mSecretString;
@Override
public VpnType getType() {
return VpnType.L2TP;
}
/**
* Enables/disables the secret for authenticating tunnel connection.
*/
public void setSecretEnabled(boolean enabled) {
mSecret = enabled;
}
public boolean isSecretEnabled() {
return mSecret;
}
public void setSecretString(String secret) {
mSecretString = secret;
}
public String getSecretString() {
return mSecretString;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mSecret = in.readInt() > 0;
mSecretString = in.readString();
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeInt(mSecret ? 1 : 0);
parcel.writeString(mSecretString);
}
}
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.os.Parcel;
/**
* The profile for PPTP type of VPN.
* {@hide}
*/
public class PptpProfile extends VpnProfile {
private static final long serialVersionUID = 1L;
private boolean mEncryption = true;
@Override
public VpnType getType() {
return VpnType.PPTP;
}
/**
* Enables/disables the encryption for PPTP tunnel.
*/
public void setEncryptionEnabled(boolean enabled) {
mEncryption = enabled;
}
public boolean isEncryptionEnabled() {
return mEncryption;
}
@Override
protected void readFromParcel(Parcel in) {
super.readFromParcel(in);
mEncryption = in.readInt() > 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeInt(mEncryption ? 1 : 0);
}
}
@@ -0,0 +1,204 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import java.io.File;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Environment;
import android.os.SystemProperties;
import android.util.Log;
/**
* The class provides interface to manage all VPN-related tasks, including:
* <ul>
* <li>The list of supported VPN types.
* <li>API's to start/stop the service of a particular type.
* <li>API's to start the settings activity.
* <li>API's to create a profile.
* <li>API's to register/unregister a connectivity receiver and the keys to
* access the fields in a connectivity broadcast event.
* </ul>
* {@hide}
*/
public class VpnManager {
// Action for broadcasting a connectivity state.
private static final String ACTION_VPN_CONNECTIVITY = "vpn.connectivity";
/** Key to the profile name of a connectivity broadcast event. */
public static final String BROADCAST_PROFILE_NAME = "profile_name";
/** Key to the connectivity state of a connectivity broadcast event. */
public static final String BROADCAST_CONNECTION_STATE = "connection_state";
/** Key to the error code of a connectivity broadcast event. */
public static final String BROADCAST_ERROR_CODE = "err";
/** Error code to indicate an error from authentication. */
public static final int VPN_ERROR_AUTH = 51;
/** Error code to indicate the connection attempt failed. */
public static final int VPN_ERROR_CONNECTION_FAILED = 101;
/** Error code to indicate the server is not known. */
public static final int VPN_ERROR_UNKNOWN_SERVER = 102;
/** Error code to indicate an error from challenge response. */
public static final int VPN_ERROR_CHALLENGE = 5;
/** Error code to indicate an error of remote server hanging up. */
public static final int VPN_ERROR_REMOTE_HUNG_UP = 7;
/** Error code to indicate an error of remote PPP server hanging up. */
public static final int VPN_ERROR_REMOTE_PPP_HUNG_UP = 48;
/** Error code to indicate a PPP negotiation error. */
public static final int VPN_ERROR_PPP_NEGOTIATION_FAILED = 42;
/** Error code to indicate an error of losing connectivity. */
public static final int VPN_ERROR_CONNECTION_LOST = 103;
/** Largest error code used by VPN. */
public static final int VPN_ERROR_LARGEST = 200;
/** Error code to indicate a successful connection. */
public static final int VPN_ERROR_NO_ERROR = 0;
public static final String PROFILES_PATH = "/misc/vpn/profiles";
private static final String PACKAGE_PREFIX =
VpnManager.class.getPackage().getName() + ".";
// Action to start VPN service
private static final String ACTION_VPN_SERVICE = PACKAGE_PREFIX + "SERVICE";
// Action to start VPN settings
private static final String ACTION_VPN_SETTINGS =
PACKAGE_PREFIX + "SETTINGS";
public static final String TAG = VpnManager.class.getSimpleName();
// TODO(oam): Test VPN when EFS is enabled (will do later)...
public static String getProfilePath() {
// This call will return the correct path if Encrypted FS is enabled or not.
return Environment.getSecureDataDirectory().getPath() + PROFILES_PATH;
}
/**
* Returns all supported VPN types.
*/
public static VpnType[] getSupportedVpnTypes() {
return VpnType.values();
}
private Context mContext;
/**
* Creates a manager object with the specified context.
*/
public VpnManager(Context c) {
mContext = c;
}
/**
* Creates a VPN profile of the specified type.
*
* @param type the VPN type
* @return the profile object
*/
public VpnProfile createVpnProfile(VpnType type) {
return createVpnProfile(type, false);
}
/**
* Creates a VPN profile of the specified type.
*
* @param type the VPN type
* @param customized true if the profile is custom made
* @return the profile object
*/
public VpnProfile createVpnProfile(VpnType type, boolean customized) {
try {
VpnProfile p = (VpnProfile) type.getProfileClass().newInstance();
p.setCustomized(customized);
return p;
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
/**
* Starts the VPN service to establish VPN connection.
*/
public void startVpnService() {
mContext.startService(new Intent(ACTION_VPN_SERVICE));
}
/**
* Stops the VPN service.
*/
public void stopVpnService() {
mContext.stopService(new Intent(ACTION_VPN_SERVICE));
}
/**
* Binds the specified ServiceConnection with the VPN service.
*/
public boolean bindVpnService(ServiceConnection c) {
if (!mContext.bindService(new Intent(ACTION_VPN_SERVICE), c, 0)) {
Log.w(TAG, "failed to connect to VPN service");
return false;
} else {
Log.d(TAG, "succeeded to connect to VPN service");
return true;
}
}
/** Broadcasts the connectivity state of the specified profile. */
public void broadcastConnectivity(String profileName, VpnState s) {
broadcastConnectivity(profileName, s, VPN_ERROR_NO_ERROR);
}
/** Broadcasts the connectivity state with an error code. */
public void broadcastConnectivity(String profileName, VpnState s,
int error) {
Intent intent = new Intent(ACTION_VPN_CONNECTIVITY);
intent.putExtra(BROADCAST_PROFILE_NAME, profileName);
intent.putExtra(BROADCAST_CONNECTION_STATE, s);
if (error != VPN_ERROR_NO_ERROR) {
intent.putExtra(BROADCAST_ERROR_CODE, error);
}
mContext.sendBroadcast(intent);
}
public void registerConnectivityReceiver(BroadcastReceiver r) {
IntentFilter filter = new IntentFilter();
filter.addAction(VpnManager.ACTION_VPN_CONNECTIVITY);
mContext.registerReceiver(r, filter);
}
public void unregisterConnectivityReceiver(BroadcastReceiver r) {
mContext.unregisterReceiver(r);
}
/** Starts the VPN settings activity. */
public void startSettingsActivity() {
Intent intent = new Intent(ACTION_VPN_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
/** Creates an intent to start the VPN settings activity. */
public Intent createSettingsActivityIntent() {
Intent intent = new Intent(ACTION_VPN_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
parcelable VpnProfile;
@@ -0,0 +1,177 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.IOException;
import java.io.Serializable;
/**
* A VPN profile.
* {@hide}
*/
public abstract class VpnProfile implements Parcelable, Serializable {
private static final long serialVersionUID = 1L;
private String mName; // unique display name
private String mId; // unique identifier
private String mServerName; // VPN server name
private String mDomainSuffices; // space separated list
private String mRouteList; // space separated list
private String mSavedUsername;
private boolean mIsCustomized;
private transient VpnState mState = VpnState.IDLE;
/** Sets a user-friendly name for this profile. */
public void setName(String name) {
mName = name;
}
public String getName() {
return mName;
}
/**
* Sets an ID for this profile. The caller should make sure the
* uniqueness of the ID.
*/
public void setId(String id) {
mId = id;
}
public String getId() {
return mId;
}
/**
* Sets the name of the VPN server. Used for DNS lookup.
*/
public void setServerName(String name) {
mServerName = name;
}
public String getServerName() {
return mServerName;
}
/**
* Sets the domain suffices for DNS resolution.
*
* @param entries a comma-separated list of domain suffices
*/
public void setDomainSuffices(String entries) {
mDomainSuffices = entries;
}
public String getDomainSuffices() {
return mDomainSuffices;
}
/**
* Sets the routing info for this VPN connection.
*
* @param entries a comma-separated list of routes; each entry is in the
* format of "(network address)/(network mask)"
*/
public void setRouteList(String entries) {
mRouteList = entries;
}
public String getRouteList() {
return mRouteList;
}
public void setSavedUsername(String name) {
mSavedUsername = name;
}
public String getSavedUsername() {
return mSavedUsername;
}
public void setState(VpnState state) {
mState = state;
}
public VpnState getState() {
return ((mState == null) ? VpnState.IDLE : mState);
}
public boolean isIdle() {
return (mState == VpnState.IDLE);
}
/**
* Returns whether this profile is custom made (as opposed to being
* created by provided user interface).
*/
public boolean isCustomized() {
return mIsCustomized;
}
/**
* Returns the VPN type of the profile.
*/
public abstract VpnType getType();
void setCustomized(boolean customized) {
mIsCustomized = customized;
}
protected void readFromParcel(Parcel in) {
mName = in.readString();
mId = in.readString();
mServerName = in.readString();
mDomainSuffices = in.readString();
mRouteList = in.readString();
mSavedUsername = in.readString();
}
public static final Parcelable.Creator<VpnProfile> CREATOR =
new Parcelable.Creator<VpnProfile>() {
public VpnProfile createFromParcel(Parcel in) {
VpnType type = Enum.valueOf(VpnType.class, in.readString());
boolean customized = in.readInt() > 0;
VpnProfile p = new VpnManager(null).createVpnProfile(type,
customized);
if (p == null) return null;
p.readFromParcel(in);
return p;
}
public VpnProfile[] newArray(int size) {
return new VpnProfile[size];
}
};
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(getType().toString());
parcel.writeInt(mIsCustomized ? 1 : 0);
parcel.writeString(mName);
parcel.writeString(mId);
parcel.writeString(mServerName);
parcel.writeString(mDomainSuffices);
parcel.writeString(mRouteList);
parcel.writeString(mSavedUsername);
}
public int describeContents() {
return 0;
}
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
/**
* Enumeration of all VPN states.
*
* A normal VPN connection lifetime starts in {@link IDLE}. When a new
* connection is about to be set up, it goes to {@link CONNECTING} and then
* {@link CONNECTED} if successful; back to {@link IDLE} if failed.
* When the connection is about to be torn down, it goes to
* {@link DISCONNECTING} and then {@link IDLE}.
* {@link CANCELLED} is a state when a VPN connection attempt is aborted, and
* is in transition to {@link IDLE}.
* The {@link UNUSABLE} state indicates that the profile is not in a state for
* connecting due to possibly the integrity of the fields or another profile is
* connecting etc.
* The {@link UNKNOWN} state indicates that the profile state is to be
* determined.
* {@hide}
*/
public enum VpnState {
CONNECTING, DISCONNECTING, CANCELLED, CONNECTED, IDLE, UNUSABLE, UNKNOWN
}
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import com.android.internal.R;
/**
* Enumeration of all supported VPN types.
* {@hide}
*/
public enum VpnType {
PPTP("PPTP", R.string.pptp_vpn_description, PptpProfile.class),
L2TP("L2TP", R.string.l2tp_vpn_description, L2tpProfile.class),
L2TP_IPSEC_PSK("L2TP/IPSec PSK", R.string.l2tp_ipsec_psk_vpn_description,
L2tpIpsecPskProfile.class),
L2TP_IPSEC("L2TP/IPSec CRT", R.string.l2tp_ipsec_crt_vpn_description,
L2tpIpsecProfile.class);
private String mDisplayName;
private int mDescriptionId;
private Class<? extends VpnProfile> mClass;
VpnType(String displayName, int descriptionId,
Class<? extends VpnProfile> klass) {
mDisplayName = displayName;
mDescriptionId = descriptionId;
mClass = klass;
}
public String getDisplayName() {
return mDisplayName;
}
public int getDescriptionId() {
return mDescriptionId;
}
public Class<? extends VpnProfile> getProfileClass() {
return mClass;
}
}
+14
View File
@@ -0,0 +1,14 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
# We only want this apk build for tests.
LOCAL_MODULE_TAGS := tests
# Include all test java files.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_JAVA_LIBRARIES := android.test.runner
LOCAL_PACKAGE_NAME := FrameworksVpnTests
include $(BUILD_PACKAGE)
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.frameworks.vpntests">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.android.frameworks.vpntests"
android:label="Frameworks VPN Tests" />
</manifest>
+157
View File
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.vpn;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.vpn.L2tpProfile;
import android.net.vpn.L2tpIpsecProfile;
import android.net.vpn.L2tpIpsecPskProfile;
import android.net.vpn.PptpProfile;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnProfile;
import android.net.vpn.VpnState;
import android.net.vpn.VpnType;
import android.os.ConditionVariable;
import android.os.Parcel;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;
/**
* Unit test class to test VPN api
* Use the below command to run the vpn unit test only
* runtest vpntest or
* adb shell am instrument -e class 'com.android.unit_tests.VpnTest'
* -w com.android.unit_tests/android.test.InstrumentationTestRunner
*/
public class VpnTest extends AndroidTestCase {
private static final String NAME = "a name";
private static final String SERVER_NAME = "a server name";
private static final String ID = "some id";
private static final String SUFFICES = "some suffices";
private static final String ROUTES = "some routes";
private static final String SAVED_NAME = "some name";
@Override
public void setUp() {
}
@Override
public void tearDown() {
}
@SmallTest
public void testVpnType() {
testVpnType(VpnType.L2TP);
testVpnType(VpnType.L2TP_IPSEC);
testVpnType(VpnType.L2TP_IPSEC_PSK);
testVpnType(VpnType.PPTP);
}
@SmallTest
public void testVpnProfile() {
VpnState state = VpnState.CONNECTING;
testVpnProfile(createTestProfile(state), state);
}
@SmallTest
public void testGetType() {
assertEquals(VpnType.L2TP, new L2tpProfile().getType());
assertEquals(VpnType.L2TP_IPSEC, new L2tpIpsecProfile().getType());
assertEquals(VpnType.L2TP_IPSEC_PSK,
new L2tpIpsecPskProfile().getType());
assertEquals(VpnType.PPTP, new PptpProfile().getType());
}
@SmallTest
public void testVpnTypes() {
assertTrue(VpnManager.getSupportedVpnTypes().length > 0);
}
@SmallTest
public void testGetTypeFromManager() {
VpnManager m = new VpnManager(getContext());
VpnType[] types = VpnManager.getSupportedVpnTypes();
for (VpnType t : types) {
assertEquals(t, m.createVpnProfile(t).getType());
}
}
@SmallTest
public void testParcelable() {
VpnProfile p = createTestProfile(VpnState.CONNECTED);
Parcel parcel = Parcel.obtain();
p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
// VpnState is transient and not saved in the parcel
testVpnProfile(VpnProfile.CREATOR.createFromParcel(parcel), null);
}
@SmallTest
public void testReceiver() {
final String profileName = "whatever";
final VpnState state = VpnState.DISCONNECTING;
final ConditionVariable cv = new ConditionVariable();
cv.close();
BroadcastReceiver r = new BroadcastReceiver() {
public void onReceive(Context c, Intent i) {
assertEquals(profileName,
i.getStringExtra(VpnManager.BROADCAST_PROFILE_NAME));
assertEquals(state, i.getSerializableExtra(
VpnManager.BROADCAST_CONNECTION_STATE));
cv.open();
}
};
VpnManager m = new VpnManager(getContext());
m.registerConnectivityReceiver(r);
m.broadcastConnectivity(profileName, state);
// fail it if onReceive() doesn't get executed in 5 sec
assertTrue(cv.block(5000));
}
private void testVpnType(VpnType type) {
assertFalse(TextUtils.isEmpty(type.getDisplayName()));
assertNotNull(type.getProfileClass());
}
private VpnProfile createTestProfile(VpnState state) {
VpnProfile p = new L2tpProfile();
p.setName(NAME);
p.setServerName(SERVER_NAME);
p.setId(ID);
p.setDomainSuffices(SUFFICES);
p.setRouteList(ROUTES);
p.setSavedUsername(SAVED_NAME);
p.setState(state);
return p;
}
private void testVpnProfile(VpnProfile p, VpnState state) {
assertEquals(NAME, p.getName());
assertEquals(SERVER_NAME, p.getServerName());
assertEquals(ID, p.getId());
assertEquals(SUFFICES, p.getDomainSuffices());
assertEquals(ROUTES, p.getRouteList());
assertEquals(SAVED_NAME, p.getSavedUsername());
if (state != null) assertEquals(state, p.getState());
}
}