TestNetworkInterface: add support for MacAddress and MTU
Both MacAddress and MTU size are often needed in tests and very painful to get a hold of without system server permissions. Test: TH Change-Id: Ie1a91959916ea39277e76aa1b61f38d59a5d2a90
This commit is contained in:
parent
2b726efdc0
commit
88f97f4fba
2 changed files with 45 additions and 1 deletions
|
|
@ -197,6 +197,8 @@ package android.net {
|
|||
method public int describeContents();
|
||||
method @NonNull public android.os.ParcelFileDescriptor getFileDescriptor();
|
||||
method @NonNull public String getInterfaceName();
|
||||
method @Nullable public android.net.MacAddress getMacAddress();
|
||||
method public int getMtu();
|
||||
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||
field @NonNull public static final android.os.Parcelable.Creator<android.net.TestNetworkInterface> CREATOR;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,22 +16,32 @@
|
|||
package android.net;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.os.Parcel;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.os.Parcelable;
|
||||
import android.util.Log;
|
||||
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
|
||||
/**
|
||||
* This class is used to return the interface name and fd of the test interface
|
||||
* This class is used to return the interface name, fd, MAC, and MTU of the test interface
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
|
||||
public final class TestNetworkInterface implements Parcelable {
|
||||
private static final String TAG = "TestNetworkInterface";
|
||||
|
||||
@NonNull
|
||||
private final ParcelFileDescriptor mFileDescriptor;
|
||||
@NonNull
|
||||
private final String mInterfaceName;
|
||||
@Nullable
|
||||
private final MacAddress mMacAddress;
|
||||
private final int mMtu;
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
|
|
@ -42,16 +52,39 @@ public final class TestNetworkInterface implements Parcelable {
|
|||
public void writeToParcel(@NonNull Parcel out, int flags) {
|
||||
out.writeParcelable(mFileDescriptor, flags);
|
||||
out.writeString(mInterfaceName);
|
||||
out.writeParcelable(mMacAddress, flags);
|
||||
out.writeInt(mMtu);
|
||||
}
|
||||
|
||||
public TestNetworkInterface(@NonNull ParcelFileDescriptor pfd, @NonNull String intf) {
|
||||
mFileDescriptor = pfd;
|
||||
mInterfaceName = intf;
|
||||
|
||||
MacAddress macAddress = null;
|
||||
int mtu = 1500;
|
||||
try {
|
||||
// This constructor is called by TestNetworkManager which runs inside the system server,
|
||||
// which has permission to read the MacAddress.
|
||||
NetworkInterface nif = NetworkInterface.getByName(mInterfaceName);
|
||||
|
||||
// getHardwareAddress() returns null for tun interfaces.
|
||||
byte[] hardwareAddress = nif.getHardwareAddress();
|
||||
if (hardwareAddress != null) {
|
||||
macAddress = MacAddress.fromBytes(nif.getHardwareAddress());
|
||||
}
|
||||
mtu = nif.getMTU();
|
||||
} catch (SocketException e) {
|
||||
Log.e(TAG, "Failed to fetch MacAddress or MTU size from NetworkInterface", e);
|
||||
}
|
||||
mMacAddress = macAddress;
|
||||
mMtu = mtu;
|
||||
}
|
||||
|
||||
private TestNetworkInterface(@NonNull Parcel in) {
|
||||
mFileDescriptor = in.readParcelable(ParcelFileDescriptor.class.getClassLoader());
|
||||
mInterfaceName = in.readString();
|
||||
mMacAddress = in.readParcelable(MacAddress.class.getClassLoader());
|
||||
mMtu = in.readInt();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
@ -64,6 +97,15 @@ public final class TestNetworkInterface implements Parcelable {
|
|||
return mInterfaceName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public MacAddress getMacAddress() {
|
||||
return mMacAddress;
|
||||
}
|
||||
|
||||
public int getMtu() {
|
||||
return mMtu;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static final Parcelable.Creator<TestNetworkInterface> CREATOR =
|
||||
new Parcelable.Creator<TestNetworkInterface>() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue