Move createTunInterface to Dependencies for testing

Test: TH
Change-Id: I7b253db7242084c7343a589c733eccf68b3be728
This commit is contained in:
Patrick Rohr 2025-02-18 19:45:06 -08:00
commit 1ad7cbfb74

View file

@ -237,32 +237,11 @@ public class L2capNetworkProvider {
mProvider.unregisterNetworkOffer(reservedOffer);
}
@Nullable
private static ParcelFileDescriptor createTunInterface(String ifname) {
final ParcelFileDescriptor fd;
try {
fd = ParcelFileDescriptor.adoptFd(
ServiceConnectivityJni.createTunTap(
true /*isTun*/, true /*hasCarrier*/, true /*setIffMulticast*/, ifname));
ServiceConnectivityJni.bringUpInterface(ifname);
// TODO: consider adding a parameter to createTunTap() (or the Builder that should
// be added) to configure i/o blocking.
final int flags = Os.fcntlInt(fd.getFileDescriptor(), F_GETFL, 0);
Os.fcntlInt(fd.getFileDescriptor(), F_SETFL, flags & ~O_NONBLOCK);
} catch (Exception e) {
// Note: createTunTap currently throws an IllegalStateException on failure.
// TODO: native functions should throw ErrnoException.
Log.e(TAG, "Failed to create tun interface", e);
return null;
}
return fd;
}
@Nullable
private L2capNetwork createL2capNetwork(BluetoothSocket socket, NetworkCapabilities caps,
L2capNetwork.ICallback cb) {
final String ifname = TUN_IFNAME + String.valueOf(sTunIndex++);
final ParcelFileDescriptor tunFd = createTunInterface(ifname);
final ParcelFileDescriptor tunFd = mDeps.createTunInterface(ifname);
if (tunFd == null) {
return null;
}
@ -652,6 +631,29 @@ public class L2capNetworkProvider {
thread.start();
return thread;
}
@Nullable
public ParcelFileDescriptor createTunInterface(String ifname) {
final ParcelFileDescriptor fd;
try {
fd = ParcelFileDescriptor.adoptFd(ServiceConnectivityJni.createTunTap(
true /*isTun*/,
true /*hasCarrier*/,
true /*setIffMulticast*/,
ifname));
ServiceConnectivityJni.bringUpInterface(ifname);
// TODO: consider adding a parameter to createTunTap() (or the Builder that should
// be added) to configure i/o blocking.
final int flags = Os.fcntlInt(fd.getFileDescriptor(), F_GETFL, 0);
Os.fcntlInt(fd.getFileDescriptor(), F_SETFL, flags & ~O_NONBLOCK);
} catch (Exception e) {
// Note: createTunTap currently throws an IllegalStateException on failure.
// TODO: native functions should throw ErrnoException.
Log.e(TAG, "Failed to create tun interface", e);
return null;
}
return fd;
}
}
public L2capNetworkProvider(Context context) {