Skip to content

Commit

Permalink
Merge pull request #768 from dorssel/remove_wsl_list
Browse files Browse the repository at this point in the history
Remove obsolete 'usbipd wsl list' command
  • Loading branch information
dorssel committed Nov 18, 2023
2 parents 8c9298b + 016c287 commit e75310d
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 180 deletions.
18 changes: 2 additions & 16 deletions UnitTests/Automation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed class Automation_Tests
const string TestStubInstanceId = "testStubInstanceId";
static readonly IPAddress TestClientIPAddress = IPAddress.Parse("1.2.3.4");

static readonly Device TestDevice = new(TestInstanceId, TestDescription, true, TestBusId, TestPersistedGuid, TestStubInstanceId, TestClientIPAddress, true);
static readonly Device TestDevice = new(TestInstanceId, TestDescription, true, TestBusId, TestPersistedGuid, TestStubInstanceId, TestClientIPAddress);
readonly Device[] TestDevices = [TestDevice, new()];
const string TestJson = """
{
Expand All @@ -30,7 +30,6 @@ sealed class Automation_Tests
"Description": "testDescription",
"InstanceId": "testInstanceId\\***VID_1234&PID_CDEF***",
"IsForced": true,
"IsWslAttached": true,
"PersistedGuid": "ad9d8376-6284-495e-a80b-ff1826d7447d",
"StubInstanceId": "testStubInstanceId"
},
Expand All @@ -40,7 +39,6 @@ sealed class Automation_Tests
"Description": "",
"InstanceId": "",
"IsForced": false,
"IsWslAttached": false,
"PersistedGuid": null,
"StubInstanceId": null
}
Expand Down Expand Up @@ -91,7 +89,6 @@ public void Device_Constructor()
Assert.IsNull(device.PersistedGuid);
Assert.IsNull(device.StubInstanceId);
Assert.IsNull(device.ClientIPAddress);
Assert.IsFalse(device.IsWslAttached);
Assert.IsFalse(device.IsBound);
Assert.IsFalse(device.IsConnected);
Assert.IsFalse(device.IsAttached);
Expand All @@ -100,7 +97,7 @@ public void Device_Constructor()
[TestMethod]
public void Device_JsonConstructor()
{
var device = new Device(TestInstanceId, TestDescription, true, TestBusId, TestPersistedGuid, TestStubInstanceId, TestClientIPAddress, true);
var device = new Device(TestInstanceId, TestDescription, true, TestBusId, TestPersistedGuid, TestStubInstanceId, TestClientIPAddress);
Assert.AreEqual(TestInstanceId, device.InstanceId);
Assert.AreEqual(TestHardwareId, device.HardwareId);
Assert.AreEqual(TestDescription, device.Description);
Expand All @@ -109,7 +106,6 @@ public void Device_JsonConstructor()
Assert.AreEqual(TestPersistedGuid, device.PersistedGuid);
Assert.AreEqual(TestStubInstanceId, device.StubInstanceId);
Assert.AreEqual(TestClientIPAddress, device.ClientIPAddress);
Assert.IsTrue(device.IsWslAttached);
Assert.IsTrue(device.IsBound);
Assert.IsTrue(device.IsConnected);
Assert.IsTrue(device.IsAttached);
Expand Down Expand Up @@ -198,16 +194,6 @@ public void Device_ClientIPAddress()
Assert.IsTrue(device.IsAttached);
}

[TestMethod]
public void IsWslAttached()
{
var device = new Device()
{
IsWslAttached = true,
};
Assert.IsTrue(device.IsWslAttached);
}

[TestMethod]
public void NullableBusIdJsonConverter_Read_Valid()
{
Expand Down
4 changes: 4 additions & 0 deletions UnitTests/ExportedDevice_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sealed class ExportedDevice_Tests
/// See <seealso href="https://www.kernel.org/doc/html/latest/usb/usbip_protocol.html"/>.
/// </summary>
static readonly byte[] TestDeviceBytes = [
#pragma warning disable format
// path (256 bytes, text)
(byte)'S', (byte)'O', (byte)'M', (byte)'E', (byte)'\\', (byte)'D', (byte)'e', (byte)'v',
(byte)'i', (byte)'c', (byte)'e', (byte)'\\', (byte)'P', (byte)'a', (byte)'t', (byte)'h',
Expand Down Expand Up @@ -68,16 +69,19 @@ sealed class ExportedDevice_Tests
4,
// bNumInterfaces (1 byte)
2,
#pragma warning restore format
];

/// <summary>
/// See <seealso href="https://www.kernel.org/doc/html/latest/usb/usbip_protocol.html"/>.
/// </summary>
static readonly byte[] TestDeviceInterfacesBytes = [
#pragma warning disable format
// interface: 3 bytes + 1 padding byte
1, 2, 3, 0,
// interface: 3 bytes + 1 padding byte
4, 5, 6, 0,
#pragma warning restore format
];

[TestMethod]
Expand Down
4 changes: 4 additions & 0 deletions UnitTests/Interop_UsbIp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace UnitTests;
sealed class Interop_UsbIp_Tests
{
static readonly byte[] TestUsbIpHeaderBytes = [
#pragma warning disable format
0x01, 0x02, 0x03, 0x04, // basic.command
0x11, 0x12, 0x13, 0x14, // basic.seqnum
0x21, 0x22, 0x23, 0x24, // basic.devid
Expand All @@ -29,6 +30,7 @@ sealed class Interop_UsbIp_Tests
0xa3, 0xa4, // cmd_submit.setup.wValue
0xa5, 0xa6, // cmd_submit.setup.wIndex
0xa7, 0xa8, // cmd_submit.setup.wLength
#pragma warning restore format
];

static readonly UsbIpHeader TestUsbIpHeader = new()
Expand Down Expand Up @@ -112,10 +114,12 @@ public void UsbIpHeader_ToBytes_Success()
}

static readonly byte[] TestUsbIpIsoPacketDescriptorBytes = [
#pragma warning disable format
0x01, 0x02, 0x03, 0x04, // offset
0x11, 0x12, 0x13, 0x14, // length
0x21, 0x22, 0x23, 0x24, // actual_length
0x31, 0x32, 0x33, 0x34, // status
#pragma warning restore format
];

static readonly UsbIpIsoPacketDescriptor TestUsbIpIsoPacketDescriptor = new()
Expand Down
66 changes: 0 additions & 66 deletions UnitTests/Parse_wsl_list_Tests.cs

This file was deleted.

4 changes: 4 additions & 0 deletions UnitTests/Tools_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace UnitTests;
sealed class Tools_Tests
{
static readonly byte[] TestStreamBytes = [
#pragma warning disable format
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
#pragma warning restore format
];

[TestMethod]
Expand Down Expand Up @@ -94,9 +96,11 @@ struct TestStructType
};

static readonly byte[] TestStructBytes = [
#pragma warning disable format
0x04, 0x03, 0x02, 0x01, // i
0x01, 0x00, 0x00, 0x00, // b
0x18, 0x17, 0x16, 0x015, 0x14, 0x13, 0x12, 0x11, // l
#pragma warning restore format
];

[TestMethod]
Expand Down
14 changes: 0 additions & 14 deletions UnitTests/WslDistributions_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ public void LookupByName_NotFound()
Assert.IsNull(distro);
}

[TestMethod]
public void LookupByIPAddress_Success()
{
var distro = TestDistributions.LookupByIPAddress(IPAddress.Parse("1.2.3.4"));
Assert.AreEqual(TestNewDistribution2, distro);
}

[TestMethod]
public void LookupByIPAddress_NotFound()
{
var distro = TestDistributions.LookupByIPAddress(IPAddress.Parse("1.2.3.5"));
Assert.IsNull(distro);
}

sealed class NetworkData
{
static readonly (string host, string client)[] SameNetworkData = [
Expand Down
15 changes: 5 additions & 10 deletions Usbipd.Automation/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public sealed partial class Device
public Device() { }

[JsonConstructor]
public Device(string instanceId, string description, bool isForced, BusId? busId, Guid? persistedGuid, string? stubInstanceId,
IPAddress? clientIPAddress, bool isWslAttached)
=> (InstanceId, Description, IsForced, BusId, PersistedGuid, StubInstanceId, ClientIPAddress, IsWslAttached)
= (instanceId, description, isForced, busId, persistedGuid, stubInstanceId, clientIPAddress, isWslAttached);
public Device(string instanceId, string description, bool isForced, BusId? busId, Guid? persistedGuid, string? stubInstanceId, IPAddress? clientIPAddress)
=> (InstanceId, Description, IsForced, BusId, PersistedGuid, StubInstanceId, ClientIPAddress)
= (instanceId, description, isForced, busId, persistedGuid, stubInstanceId, clientIPAddress);

[DataMember]
[JsonPropertyOrder(4)]
Expand Down Expand Up @@ -62,11 +61,11 @@ public VidPid HardwareId
}

[DataMember]
[JsonPropertyOrder(7)]
[JsonPropertyOrder(6)]
public Guid? PersistedGuid { get; init; }

[DataMember]
[JsonPropertyOrder(8)]
[JsonPropertyOrder(7)]
public string? StubInstanceId { get; init; }

/// <summary>
Expand All @@ -83,10 +82,6 @@ public VidPid HardwareId
init => _ClientIPAddress = value?.ToString();
}

[DataMember]
[JsonPropertyOrder(6)]
public bool IsWslAttached { get; init; }

[JsonIgnore]
public bool IsBound { get => PersistedGuid is not null; }

Expand Down
44 changes: 0 additions & 44 deletions Usbipd/CommandHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ interface ICommandHandlers
public Task<ExitCode> WslDetach(BusId busId, IConsole console, CancellationToken cancellationToken);
public Task<ExitCode> WslDetach(VidPid vidPid, IConsole console, CancellationToken cancellationToken);
public Task<ExitCode> WslDetachAll(IConsole console, CancellationToken cancellationToken);
public Task<ExitCode> WslList(bool usbids, IConsole console, CancellationToken cancellationToken);

public Task<ExitCode> State(IConsole console, CancellationToken cancellationToken);
}
Expand Down Expand Up @@ -781,48 +780,6 @@ Task<ExitCode> ICommandHandlers.WslDetachAll(IConsole console, CancellationToken
return Task.FromResult(ExitCode.Success);
}

async Task<ExitCode> ICommandHandlers.WslList(bool usbids, IConsole console, CancellationToken cancellationToken)
{
if (await GetDistributionsAsync(console, cancellationToken) is not WslDistributions distros)
{
return ExitCode.Failure;
}

console.WriteLine($"{"BUSID",-5} {"VID:PID",-9} {"DEVICE",-60} STATE");
foreach (var device in UsbDevice.GetAll().Where(d => d.BusId.HasValue).OrderBy(d => d.BusId))
{
string state;
if (device.IPAddress is not null)
{
if (distros.LookupByIPAddress(device.IPAddress) is not null)
{
// All WSL instances for the user share the same IP address; it cannot be determined
// which distro was used to attach. That does not matter, since it is attached
// to the one and only kernel, so it is now available in all distros.
state = "Attached - WSL";
}
else
{
state = "Attached - non-WSL";
}
}
else
{
state = "Not attached";
}
// NOTE: Strictly speaking, both Bus and Port can be > 99. If you have one of those, you win a prize!
console.Write($"{device.BusId,-5} ");
console.Write($"{device.HardwareId,-9} ");
console.WriteTruncated(GetDescription(device, usbids), 60, true);
console.WriteLine($" {state}");
}
console.WriteLine(string.Empty);

console.ReportIfServerNotRunning();
console.ReportIfForceNeeded();
return ExitCode.Success;
}

async Task<ExitCode> ICommandHandlers.State(IConsole console, CancellationToken cancellationToken)
{
Console.SetError(TextWriter.Null);
Expand All @@ -846,7 +803,6 @@ async Task<ExitCode> ICommandHandlers.State(IConsole console, CancellationToken
PersistedGuid = device.Guid,
StubInstanceId = device.StubInstanceId,
ClientIPAddress = device.IPAddress,
IsWslAttached = device.IPAddress is not null && distros is not null && distros.LookupByIPAddress(device.IPAddress) is not null,
});
}

Expand Down
28 changes: 0 additions & 28 deletions Usbipd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,34 +567,6 @@ await commandHandlers.WslDetachAll(invocationContext.Console, invocationContext.
});
wslCommand.AddCommand(detachCommand);
}
{
//
// wsl list [--usbids]
//
var usbidsOption = new Option<bool>(
aliases: ["--usbids", "-u"]
)
{
Description = "Show device description from Linux database",
Arity = ArgumentArity.Zero,
};
//
// wsl list
//
var listCommand = new Command("list", "List USB devices\0"
+ "Lists all USB devices that are available for being attached to a WSL instance.")
{
usbidsOption,
};
listCommand.SetHandler(async (invocationContext) =>
{
invocationContext.ExitCode = (int)(
await commandHandlers.WslList(invocationContext.ParseResult.HasOption(usbidsOption),
invocationContext.Console, invocationContext.GetCancellationToken())
);
});
wslCommand.AddCommand(listCommand);
}
}

// Same as UseDefaults() minus exception handling.
Expand Down
2 changes: 0 additions & 2 deletions Usbipd/WslDistributions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,4 @@ public static string GetVMSwitchName()
}

public Distribution? LookupByName(string name) => Distributions.FirstOrDefault(distro => distro.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

public Distribution? LookupByIPAddress(IPAddress address) => Distributions.FirstOrDefault(distro => address.Equals(distro.IPAddress));
}

0 comments on commit e75310d

Please sign in to comment.