ce67fa2d6b
- RegistryPath reads HKLM\..., HKEY_LOCAL_MACHINE\..., PowerShell drives (HKLM:\, HKCU:), Registry:: and Microsoft.PowerShell.Core\Registry:: provider paths, regedit's Computer\..., forward slashes (when there is no backslash), quotes, brackets and stray backslashes, into one canonical form. - RegistryValue carries a value by type (text, lines, unsigned number as a string, hex) and turns into raw bytes and back. - RegFile parses Version 5.00 and REGEDIT4 text ([key], [-key], @=, "x"=-, dword:, hex:, hex(n): with continuations, escapes, comments), reporting each skipped line; Decode reads UTF-16 with BOM, UTF-8 and ANSI. RegFileWriter writes regedit-style exports. - Registry operations ride DeviceFileRequest/Reply (Registry field), with registry-disabled and no-user statuses; API routes and DTOs, including the import preview. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
446 lines
21 KiB
C#
446 lines
21 KiB
C#
using System.Text;
|
|
using Shared.Contracts.Api;
|
|
using Shared.Contracts.Registry;
|
|
using Xunit;
|
|
|
|
namespace Shared.Tests;
|
|
|
|
/// <summary>.reg files as regedit and people write them: what each line changes, and the lines that are skipped.</summary>
|
|
public class RegFileTests
|
|
{
|
|
private const string V5 = "Windows Registry Editor Version 5.00";
|
|
|
|
private static RegFile Parse(params string[] lines) => RegFile.Parse(string.Join("\r\n", lines));
|
|
|
|
private static byte[] Utf16(string text) => Encoding.Unicode.GetBytes(text);
|
|
|
|
// ── Real-world files ─────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void ARegeditExportReadsWhole()
|
|
{
|
|
// As regedit writes it: wrapped hex(2) with continuations, the default value, every common type.
|
|
var file = Parse(
|
|
V5,
|
|
string.Empty,
|
|
@"[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]",
|
|
"\"ComSpec\"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,\\",
|
|
" 74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,63,\\",
|
|
" 00,6d,00,64,00,2e,00,65,00,78,00,65,00,00,00",
|
|
"\"NUMBER_OF_PROCESSORS\"=\"8\"",
|
|
"\"OS\"=\"Windows_NT\"",
|
|
"\"PSModulePath\"=hex(2):25,00,50,00,72,00,6f,00,00,00",
|
|
string.Empty,
|
|
@"[HKEY_CURRENT_USER\Software\Contoso\App]",
|
|
"@=\"Default text\"",
|
|
"\"Enabled\"=dword:00000001",
|
|
"\"Big\"=hex(b):ff,ff,ff,ff,ff,ff,ff,ff",
|
|
"\"Servers\"=hex(7):61,00,00,00,62,00,00,00,00,00",
|
|
"\"Blob\"=hex:de,ad,be,ef",
|
|
"\"Nothing\"=hex(0):",
|
|
string.Empty);
|
|
|
|
Assert.Equal(RegFile.Version5Header, file.Format);
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal(12, file.Operations.Count);
|
|
|
|
var comSpec = file.Operations[1];
|
|
Assert.Equal((4, RegActions.SetValue, "ComSpec", RegistryTypes.ExpandString), (comSpec.Line, comSpec.Action, comSpec.ValueName, comSpec.Type));
|
|
Assert.Equal(@"%SystemRoot%\system32\cmd.exe", comSpec.Value()!.Text);
|
|
Assert.Equal(7, file.Operations[2].Line);
|
|
|
|
var values = file.Operations.Where(o => o.Key.EndsWith("App", StringComparison.Ordinal) && o.Action == RegActions.SetValue)
|
|
.ToDictionary(o => o.ValueName!, o => o.Value()!);
|
|
Assert.Equal("Default text", values[string.Empty].Text);
|
|
Assert.Equal("1", values["Enabled"].Number);
|
|
Assert.Equal("18446744073709551615", values["Big"].Number);
|
|
Assert.Equal(["a", "b"], values["Servers"].Lines!);
|
|
Assert.Equal("deadbeef", values["Blob"].Hex);
|
|
Assert.Equal("REG_NONE", values["Nothing"].Type);
|
|
Assert.Equal(0, values["Nothing"].Size);
|
|
}
|
|
|
|
[Fact]
|
|
public void ATweakFileWithCommentsAndDeletes()
|
|
{
|
|
var file = Parse(
|
|
V5,
|
|
"; Turn off web results in Start",
|
|
@"[HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Explorer]",
|
|
"\"DisableSearchBoxSuggestions\"=dword:1",
|
|
string.Empty,
|
|
" ; an indented comment",
|
|
@"[-HKEY_CURRENT_USER\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}]",
|
|
string.Empty,
|
|
@"[HKEY_CLASSES_ROOT\*\shell\Open with Notepad\command]",
|
|
"@=\"notepad.exe \\\"%1\\\"\"",
|
|
"\"Unwanted\"=-",
|
|
string.Empty,
|
|
@"[HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/html]",
|
|
"\"Extension\"=\".htm\" ; the usual one");
|
|
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal(
|
|
[
|
|
(RegActions.CreateKey, @"HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Explorer", (string?)null),
|
|
(RegActions.SetValue, @"HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\Explorer", "DisableSearchBoxSuggestions"),
|
|
(RegActions.DeleteKey, @"HKEY_CURRENT_USER\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}", null),
|
|
(RegActions.CreateKey, @"HKEY_CLASSES_ROOT\*\shell\Open with Notepad\command", null),
|
|
(RegActions.SetValue, @"HKEY_CLASSES_ROOT\*\shell\Open with Notepad\command", string.Empty),
|
|
(RegActions.DeleteValue, @"HKEY_CLASSES_ROOT\*\shell\Open with Notepad\command", "Unwanted"),
|
|
(RegActions.CreateKey, @"HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/html", null),
|
|
(RegActions.SetValue, @"HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/html", "Extension"),
|
|
],
|
|
file.Operations.Select(o => (o.Action, o.Key, o.ValueName)));
|
|
Assert.Equal("notepad.exe \"%1\"", file.Operations[4].Value()!.Text);
|
|
Assert.Equal(".htm", file.Operations[7].Value()!.Text);
|
|
Assert.Equal("1", file.Operations[1].Value()!.Number);
|
|
}
|
|
|
|
[Fact]
|
|
public void ARegedit4FileReadsItsAnsiHexAsText()
|
|
{
|
|
var file = Parse(
|
|
"REGEDIT4",
|
|
string.Empty,
|
|
@"[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]",
|
|
"\"Tool\"=\"C:\\\\Program Files\\\\Tool\\\\tool.exe\"",
|
|
"\"Expand\"=hex(2):25,54,45,4d,50,25,00",
|
|
"\"Multi\"=hex(7):61,00,e9,00,00",
|
|
"\"Raw\"=hex:e9");
|
|
|
|
Assert.Equal(RegFile.Version4Header, file.Format);
|
|
Assert.Empty(file.Errors);
|
|
var values = file.Operations.Where(o => o.Action == RegActions.SetValue).ToDictionary(o => o.ValueName!, o => o.Value()!);
|
|
Assert.Equal(@"C:\Program Files\Tool\tool.exe", values["Tool"].Text);
|
|
Assert.Equal("%TEMP%", values["Expand"].Text);
|
|
Assert.Equal(["a", "é"], values["Multi"].Lines!);
|
|
Assert.Equal("e9", values["Raw"].Hex);
|
|
}
|
|
|
|
// ── Encodings ────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void FilesDecodeByTheirMark_OrTheirLook()
|
|
{
|
|
var text = $"{V5}\r\n\r\n[HKEY_CURRENT_USER\\Software\\Café]\r\n\"Name\"=\"naïve\"\r\n";
|
|
|
|
foreach (var bytes in new[]
|
|
{
|
|
[.. Encoding.Unicode.GetPreamble(), .. Utf16(text)],
|
|
[.. Encoding.BigEndianUnicode.GetPreamble(), .. Encoding.BigEndianUnicode.GetBytes(text)],
|
|
[.. Encoding.UTF8.GetPreamble(), .. Encoding.UTF8.GetBytes(text)],
|
|
Encoding.UTF8.GetBytes(text),
|
|
Utf16(text),
|
|
CodePagesEncodingProvider.Instance.GetEncoding(1252)!.GetBytes(text),
|
|
})
|
|
{
|
|
var decoded = RegFile.Decode(bytes);
|
|
Assert.Equal(text, decoded);
|
|
var file = RegFile.Parse(decoded);
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal(@"HKEY_CURRENT_USER\Software\Café", file.Operations[0].Key);
|
|
Assert.Equal("naïve", file.Operations[1].Value()!.Text);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("\r\n")]
|
|
[InlineData("\n")]
|
|
[InlineData("\r")]
|
|
public void AnyLineEndingWorks_AndLineNumbersCount(string newline)
|
|
{
|
|
var file = RegFile.Parse(string.Join(newline, "\uFEFF" + V5, string.Empty, "[HKCU\\A]", "\"x\"=hex:01,\\", " 02", "\"y\"=dword:2", "bad"));
|
|
|
|
Assert.Equal([3, 4, 6], file.Operations.Select(o => o.Line));
|
|
Assert.Equal("0102", file.Operations[1].Value()!.Hex);
|
|
Assert.Equal(7, Assert.Single(file.Errors).Line);
|
|
}
|
|
|
|
// ── Malformed ────────────────────────────────────────────────────────
|
|
|
|
[Theory]
|
|
[InlineData("")]
|
|
[InlineData("\r\n \r\n")]
|
|
[InlineData("[HKEY_CURRENT_USER\\Software]")]
|
|
[InlineData("Windows Registry Editor Version 4.00\r\n[HKCU\\A]")]
|
|
[InlineData("REGEDIT5")]
|
|
public void WithoutAHeaderNothingIsRead(string text)
|
|
{
|
|
var file = RegFile.Parse(text);
|
|
|
|
Assert.Null(file.Format);
|
|
Assert.Empty(file.Operations);
|
|
Assert.Single(file.Errors);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("[HKEY_CURRENT_USER\\Software", "ends with ]")]
|
|
[InlineData("[HKEY_NOWHERE\\Software]", "not a hive")]
|
|
[InlineData("[-HKEY_LOCAL_MACHINE]", "hive can't be deleted")]
|
|
[InlineData("[]", "registry path")]
|
|
[InlineData("\"orphan\"=\"x\"", "before any [key]")]
|
|
[InlineData("Just some words", "Not a key")]
|
|
public void ABadKeyLineOrStrayLineIsSkipped(string line, string reason)
|
|
{
|
|
var file = Parse(V5, line, "[HKCU\\Fine]");
|
|
|
|
var error = Assert.Single(file.Errors);
|
|
Assert.Equal(2, error.Line);
|
|
Assert.Contains(reason, error.Message, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Equal(@"HKEY_CURRENT_USER\Fine", Assert.Single(file.Operations).Key);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("\"a\"=dword:", "dword:")]
|
|
[InlineData("\"a\"=dword:123456789", "dword:")]
|
|
[InlineData("\"a\"=dword:zz", "dword:")]
|
|
[InlineData("\"a\"=hex:0g", "hex byte")]
|
|
[InlineData("\"a\"=hex:123", "hex byte")]
|
|
[InlineData("\"a\"=hex:01,,02", "hex byte")]
|
|
[InlineData("\"a\"=hex(z):01", "not a type")]
|
|
[InlineData("\"a\"=hexx:01", "not a type")]
|
|
[InlineData("\"a\"=hex(2)", "hex:01,02")]
|
|
[InlineData("\"a\"=\"unterminated", "closing quote")]
|
|
[InlineData("\"a\"=\"x\" trailing", "follows the string")]
|
|
[InlineData("\"a\"=qword:1", "isn't")]
|
|
[InlineData("\"a\"=", "isn't")]
|
|
[InlineData("\"a\" \"b\"", "\"name\"=data")]
|
|
[InlineData("\"unterminated=\"x\"", "\"name\"=data")]
|
|
[InlineData("\"never closed", "no closing quote")]
|
|
public void BadDataIsSkippedWithItsLine(string line, string reason)
|
|
{
|
|
var file = Parse(V5, "[HKCU\\A]", line, "\"ok\"=\"fine\"");
|
|
|
|
var error = Assert.Single(file.Errors);
|
|
Assert.Equal(3, error.Line);
|
|
Assert.Contains(reason, error.Message, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Equal(["create-key", "set-value"], file.Operations.Select(o => o.Action));
|
|
}
|
|
|
|
[Fact]
|
|
public void ValuesUnderADeletedOrUnreadableKeyAreSkipped()
|
|
{
|
|
var file = Parse(V5, "[-HKCU\\Gone]", "\"a\"=\"b\"", "[HKXX\\Bad]", "\"c\"=\"d\"", "[HKCU\\Next]", "\"e\"=\"f\"");
|
|
|
|
Assert.Equal([3, 4, 5], file.Errors.Select(e => e.Line));
|
|
Assert.Contains("deleted key", file.Errors[0].Message);
|
|
Assert.Contains("could not be read", file.Errors[2].Message);
|
|
Assert.Equal([RegActions.DeleteKey, RegActions.CreateKey, RegActions.SetValue], file.Operations.Select(o => o.Action));
|
|
}
|
|
|
|
[Fact]
|
|
public void EscapesInNamesAndStrings()
|
|
{
|
|
var file = Parse(V5, "[HKCU\\A]", "\"Quote\\\"And\\\\Slash\"=\"C:\\\\Dir\\\\\"", "\"Other\\n\"=\"keeps \\t as is\"", "\"Eq=Sign\" = \"spaced\"");
|
|
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal("Quote\"And\\Slash", file.Operations[1].ValueName);
|
|
Assert.Equal(@"C:\Dir\", file.Operations[1].Value()!.Text);
|
|
Assert.Equal("Other\\n", file.Operations[2].ValueName);
|
|
Assert.Equal("keeps \\t as is", file.Operations[2].Value()!.Text);
|
|
Assert.Equal("Eq=Sign", file.Operations[3].ValueName);
|
|
Assert.Equal("spaced", file.Operations[3].Value()!.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void KeysTakeEveryPathForm()
|
|
{
|
|
var file = Parse(V5, "[HKLM:\\SOFTWARE\\A]", "[Computer\\HKEY_USERS\\.DEFAULT]", "[ HKCU/Software/B ]", "[-hkcr\\.xyz\\]");
|
|
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal(
|
|
[@"HKEY_LOCAL_MACHINE\SOFTWARE\A", @"HKEY_USERS\.DEFAULT", @"HKEY_CURRENT_USER\Software\B", @"HKEY_CLASSES_ROOT\.xyz"],
|
|
file.Operations.Select(o => o.Key));
|
|
Assert.Equal(RegActions.DeleteKey, file.Operations[3].Action);
|
|
}
|
|
|
|
[Fact]
|
|
public void AnyTypeNumberAndEmptyHexWork()
|
|
{
|
|
var file = Parse(V5, "[HKCU\\A]", "\"r\"=hex(8):01,02", "\"big\"=hex(ffff0011):00", "\"e\"=hex:", "\"t\"=hex:01,02,", "\"z\"=hex(2):");
|
|
|
|
Assert.Empty(file.Errors);
|
|
Assert.Equal([8, unchecked((int)0xffff0011), RegistryTypes.Binary, RegistryTypes.Binary, RegistryTypes.ExpandString], file.Operations.Skip(1).Select(o => o.Type));
|
|
Assert.Equal("REG_RESOURCE_LIST", file.Operations[1].Value()!.Type);
|
|
Assert.Empty(file.Operations[3].Data!);
|
|
Assert.Equal(new byte[] { 1, 2 }, file.Operations[4].Data);
|
|
}
|
|
|
|
// ── Writing ──────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void WhatIsWrittenReadsBackTheSame()
|
|
{
|
|
var values = new (string Name, int Type, byte[] Data)[]
|
|
{
|
|
(string.Empty, RegistryTypes.String, Utf16("default\0")),
|
|
("Quote\"\\Name", RegistryTypes.String, Utf16("C:\\Path\\\0")),
|
|
("Lines", RegistryTypes.String, Utf16("one\r\ntwo\0")),
|
|
("NoTerminator", RegistryTypes.String, Utf16("abc")),
|
|
("Expand", RegistryTypes.ExpandString, Utf16("%TEMP%\0")),
|
|
("Multi", RegistryTypes.MultiString, Utf16("a\0b\0\0")),
|
|
("Dword", RegistryTypes.DWord, [0x2a, 0, 0, 0]),
|
|
("ShortDword", RegistryTypes.DWord, [1, 2]),
|
|
("Qword", RegistryTypes.QWord, [1, 2, 3, 4, 5, 6, 7, 8]),
|
|
("Binary", RegistryTypes.Binary, [.. Enumerable.Range(0, 300).Select(i => (byte)i)]),
|
|
("Empty", RegistryTypes.Binary, []),
|
|
("None", RegistryTypes.None, [9]),
|
|
("Link", RegistryTypes.Link, Utf16("\\Registry\\Machine")),
|
|
};
|
|
|
|
var text = new StringWriter();
|
|
var writer = new RegFileWriter(text);
|
|
writer.WriteHeader();
|
|
writer.WriteKey(@"HKEY_CURRENT_USER\Software\Round Trip");
|
|
foreach (var (name, type, data) in values)
|
|
{
|
|
writer.WriteValue(name, type, data);
|
|
}
|
|
|
|
writer.WriteDeleteValue("Old");
|
|
writer.WriteDeleteKey(@"HKEY_CURRENT_USER\Software\Gone");
|
|
writer.WriteEnd();
|
|
|
|
var output = text.ToString();
|
|
Assert.Contains("\"Dword\"=dword:0000002a\r\n", output);
|
|
Assert.Contains("@=\"default\"\r\n", output);
|
|
Assert.Contains("\"Quote\\\"\\\\Name\"=\"C:\\\\Path\\\\\"\r\n", output);
|
|
Assert.Contains("\"Lines\"=hex(1):", output);
|
|
Assert.Contains("\"Expand\"=hex(2):", output);
|
|
Assert.All(output.Split("\r\n"), line => Assert.True(line.Length <= 80, $"{line.Length}: {line}"));
|
|
|
|
var file = RegFile.Parse(output);
|
|
Assert.Empty(file.Errors);
|
|
var set = file.Operations.Where(o => o.Action == RegActions.SetValue).ToList();
|
|
Assert.Equal(values.Length, set.Count);
|
|
foreach (var ((name, type, data), operation) in values.Zip(set))
|
|
{
|
|
Assert.Equal(name, operation.ValueName);
|
|
Assert.Equal(type, operation.Type);
|
|
Assert.Equal(data, operation.Data);
|
|
}
|
|
|
|
Assert.Equal(RegActions.DeleteValue, file.Operations[^2].Action);
|
|
Assert.Equal(RegActions.DeleteKey, file.Operations[^1].Action);
|
|
}
|
|
|
|
// ── Values ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void ValuesTurnIntoBytesAndBack()
|
|
{
|
|
var cases = new[]
|
|
{
|
|
new RegistryValue("s", "REG_SZ", RegistryTypes.String, Text: "héllo"),
|
|
new RegistryValue("e", "REG_EXPAND_SZ", RegistryTypes.ExpandString, Text: "%PATH%"),
|
|
new RegistryValue("m", "REG_MULTI_SZ", RegistryTypes.MultiString, Lines: ["a", "", "c"]),
|
|
new RegistryValue("d", "REG_DWORD", RegistryTypes.DWord, Number: "4294967295"),
|
|
new RegistryValue("q", "REG_QWORD", RegistryTypes.QWord, Number: "18446744073709551615"),
|
|
new RegistryValue("b", "REG_BINARY", RegistryTypes.Binary, Hex: "00ff10"),
|
|
new RegistryValue("n", "REG_NONE", RegistryTypes.None, Hex: string.Empty),
|
|
};
|
|
|
|
foreach (var value in cases)
|
|
{
|
|
var data = value.ToData();
|
|
var back = RegistryValue.FromData(value.Name, value.TypeId, data);
|
|
Assert.Equal(value with { Size = data.Length }, back with { Lines = back.Lines }, ValueComparer.Instance);
|
|
}
|
|
|
|
Assert.Equal("0x0000002a (42)", new RegistryValue("d", "REG_DWORD", 4, Number: "42").Display());
|
|
Assert.Equal("[\"a\", \"b\"]", new RegistryValue("m", "REG_MULTI_SZ", 7, Lines: ["a", "b"]).Display());
|
|
Assert.Equal("de ad", new RegistryValue("b", "REG_BINARY", 3, Hex: "dead").Display());
|
|
Assert.Equal("(zero-length)", new RegistryValue("b", "REG_BINARY", 3, Hex: string.Empty).Display());
|
|
Assert.Equal(10, new RegistryValue("s", "REG_SZ", 1, Text: new string('x', 100)).Display(10).Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void ADwordOfTheWrongLengthShowsAsHex_AndLongDataIsCut()
|
|
{
|
|
Assert.Equal("0102", RegistryValue.FromData("d", RegistryTypes.DWord, [1, 2]).Hex);
|
|
Assert.Null(RegistryValue.FromData("d", RegistryTypes.DWord, [1, 2]).Number);
|
|
|
|
var cut = RegistryValue.FromData("s", RegistryTypes.String, Utf16(new string('x', 100) + "\0"), maxBytes: 11);
|
|
Assert.True(cut.Truncated);
|
|
Assert.Equal(202, cut.Size);
|
|
Assert.Equal("xxxxx", cut.Text);
|
|
Assert.Contains("202 bytes", cut.Display());
|
|
|
|
var dword = RegistryValue.FromData("d", RegistryTypes.DWord, [1, 0, 0, 0], maxBytes: 0);
|
|
Assert.False(dword.Truncated);
|
|
Assert.Equal("1", dword.Number);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("42", 42ul)]
|
|
[InlineData(" 0x2A ", 42ul)]
|
|
[InlineData("0XFFFFFFFF", 4294967295ul)]
|
|
[InlineData("1_000", 1000ul)]
|
|
public void NumbersAreDecimalOrHex(string text, ulong expected) => Assert.Equal(expected, RegistryValue.ParseNumber(text, ulong.MaxValue));
|
|
|
|
[Theory]
|
|
[InlineData("-1")]
|
|
[InlineData("0x")]
|
|
[InlineData("")]
|
|
[InlineData("12ab")]
|
|
[InlineData("4294967296")]
|
|
public void OtherNumbersAreRefused(string text) => Assert.Throws<ArgumentException>(() => RegistryValue.ParseNumber(text, uint.MaxValue));
|
|
|
|
[Fact]
|
|
public void HexTakesSeparators()
|
|
{
|
|
Assert.Equal(new byte[] { 0xde, 0xad, 0xbe, 0xef }, RegistryValue.ParseHex("de ad,be:ef"));
|
|
Assert.Equal(new byte[] { 1 }, RegistryValue.ParseHex("0-1"));
|
|
Assert.Empty(RegistryValue.ParseHex(null));
|
|
Assert.Throws<ArgumentException>(() => RegistryValue.ParseHex("abc"));
|
|
Assert.Throws<ArgumentException>(() => RegistryValue.ParseHex("0x12"));
|
|
}
|
|
|
|
[Fact]
|
|
public void TypeNamesAndNumbers()
|
|
{
|
|
Assert.Equal("REG_SZ", RegistryTypes.Name(1));
|
|
Assert.Equal("REG_QWORD", RegistryTypes.Name(11));
|
|
Assert.Equal("REG_0x1234", RegistryTypes.Name(0x1234));
|
|
Assert.Equal(4, RegistryTypes.Parse("reg_dword"));
|
|
Assert.Equal(0x1234, RegistryTypes.Parse("REG_0x1234"));
|
|
Assert.Null(RegistryTypes.Parse("REG_TEXT"));
|
|
Assert.True(RegistryTypes.IsEditable(RegistryTypes.MultiString));
|
|
Assert.False(RegistryTypes.IsEditable(RegistryTypes.None));
|
|
Assert.False(RegistryTypes.IsEditable(RegistryTypes.ResourceList));
|
|
}
|
|
|
|
// ── Preview ──────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void ThePreviewCountsEveryChange_AndListsTheFirst()
|
|
{
|
|
var lines = new List<string> { V5, "[-HKCU\\Old]", "[HKCU\\New]", "\"gone\"=-", "\"bad\"=dword:x" };
|
|
lines.AddRange(Enumerable.Range(0, 30).Select(i => $"\"v{i}\"=\"{i}\""));
|
|
|
|
var preview = RegistryImportPreviewDto.From(RegFile.Parse(string.Join("\n", lines)), maxItems: 10);
|
|
|
|
Assert.Equal(RegFile.Version5Header, preview.Format);
|
|
Assert.Equal((1, 1, 30, 1), (preview.KeysCreated, preview.KeysDeleted, preview.ValuesSet, preview.ValuesDeleted));
|
|
Assert.Equal(10, preview.Items.Count);
|
|
Assert.Equal(23, preview.ItemsNotListed);
|
|
Assert.Equal(5, Assert.Single(preview.Errors).Line);
|
|
Assert.Equal(RegActions.DeleteKey, preview.Items[0].Action);
|
|
Assert.Equal(@"HKEY_CURRENT_USER\Old", preview.Items[0].Key);
|
|
Assert.Equal("0", preview.Items[3].Value!.Text);
|
|
Assert.Null(preview.Items[2].Value);
|
|
}
|
|
|
|
private sealed class ValueComparer : IEqualityComparer<RegistryValue>
|
|
{
|
|
public static readonly ValueComparer Instance = new();
|
|
|
|
public bool Equals(RegistryValue? x, RegistryValue? y)
|
|
=> x is not null && y is not null && x.Name == y.Name && x.TypeId == y.TypeId && x.Text == y.Text && x.Number == y.Number
|
|
&& (x.Hex ?? string.Empty) == (y.Hex ?? string.Empty) && (x.Lines ?? []).SequenceEqual(y.Lines ?? []) && x.Size == y.Size;
|
|
|
|
public int GetHashCode(RegistryValue obj) => obj.Name.GetHashCode(StringComparison.Ordinal);
|
|
}
|
|
}
|