From fef1f852ffc358374a9ea5c3b8f661704f928685 Mon Sep 17 00:00:00 2001 From: Gitii Date: Sat, 17 Aug 2024 13:13:34 +0200 Subject: [PATCH] Ignore invalid sub reg keys --- Community.Wsl.Sdk.Tests/WslApiTests.cs | 54 ++++++++++++++++++++++++++ Community.Wsl.Sdk/WslApi.cs | 1 + 2 files changed, 55 insertions(+) create mode 100644 Community.Wsl.Sdk.Tests/WslApiTests.cs diff --git a/Community.Wsl.Sdk.Tests/WslApiTests.cs b/Community.Wsl.Sdk.Tests/WslApiTests.cs new file mode 100644 index 0000000..58bbe76 --- /dev/null +++ b/Community.Wsl.Sdk.Tests/WslApiTests.cs @@ -0,0 +1,54 @@ +using FluentAssertions; +using NUnit.Framework; +using System; +using Community.Wsx.Shared; +using FakeItEasy; +using Microsoft.VisualBasic; + +namespace Community.Wsl.Sdk.Tests; + +public class WslApiTests +{ + [Test] + public void GetDistributionList_ShouldSkipNonGuidSubKeys() + { + var currentUser = A.Fake(); + var lxssKey = A.Fake(); + + var validSubKey = A.Fake(); + var validSubKeyGuid = Guid.NewGuid().ToString(); + + var invalidSubKeyGuid = "invalid"; + + var reg = A.Fake(); + + var randomString = "string"; + + A.CallTo(() => reg.GetCurrentUser()).Returns(currentUser); + A.CallTo(() => currentUser.OpenSubKey(A.Ignored)).Returns(lxssKey); + A.CallTo(() => lxssKey.GetSubKeyNames()).Returns([validSubKeyGuid, invalidSubKeyGuid]); + A.CallTo(() => lxssKey.OpenSubKey(validSubKeyGuid)).Returns(validSubKey); + + A.CallTo(() => validSubKey.GetValue(A.Ignored)).Returns(randomString); + A.CallTo(() => validSubKey.GetValue(A.Ignored)).Returns(0); + A.CallTo(() => validSubKey.GetValue(A.Ignored, A.Ignored)).Returns(randomString); + + var wsl = new WslApi(reg); + + var list = wsl.GetDistributionList(); + + list.Should().HaveCount(1); + + list[0].Should().BeEquivalentTo(new DistroInfo() + { + DistroId = Guid.Parse(validSubKeyGuid), + DistroName = randomString, + KernelCommandLine = [randomString], + IsDefault = false, + WslVersion = 0, + DistroFlags = 0, + DefaultUid = 0, + DefaultEnvironmentVariables = [] + }, (options) => options.ComparingByMembers().Excluding((info => info.BasePath))); + } +} diff --git a/Community.Wsl.Sdk/WslApi.cs b/Community.Wsl.Sdk/WslApi.cs index 8b4887b..2fe6f2f 100644 --- a/Community.Wsl.Sdk/WslApi.cs +++ b/Community.Wsl.Sdk/WslApi.cs @@ -109,6 +109,7 @@ public IReadOnlyList GetDistributionList() return lxssKey .GetSubKeyNames() + .Where((keyName => Guid.TryParse(keyName, out _))) .Select( (keyName) => ReadFromRegistryKey(