mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 14:07:46 +08:00
Refactor registry settings interaction classes.
This commit refactors the registry settings interaction classes by consolidating the multiple entry classes for int, string, bool, etc., into a single class that supports multiple data types. This change simplifies the code and enhances readability. - Consolidate multiple entry classes (int, string, bool, etc.) into a single class - Introduce WindowsRegistryEntry<T> to support multiple data types
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
|
||||
using Microsoft.Win32;
|
||||
using NUnit.Framework.Internal;
|
||||
using System.Runtime.Versioning;
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry.RegistryEntryTest
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class BaseRegistryEntryTest
|
||||
{
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\EntryTests";
|
||||
|
||||
[Test]
|
||||
public void Path_SetValidValue_GetReturnsSameValue()
|
||||
{
|
||||
string expectedPath = @"SOFTWARE\Microsoft";
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Path = expectedPath
|
||||
};
|
||||
Assert.That(expectedPath, Is.EqualTo(entry.Path));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Path_SetNullValue_ThrowsArgumentNullException()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>();
|
||||
Assert.Throws<ArgumentNullException>(() => entry.Path = null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Name_SetValidValue_GetReturnsSameValue()
|
||||
{
|
||||
string expectedName = "Version";
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Name = expectedName
|
||||
};
|
||||
Assert.That(expectedName, Is.EqualTo(entry.Name));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Value_SetValidValue_GetReturnsSameValue()
|
||||
{
|
||||
string expectedValue = "1.0";
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Value = expectedValue
|
||||
};
|
||||
Assert.That(expectedValue, Is.EqualTo(entry.Value));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValueKind_SetInvalidValue_ThrowsArgumentException()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>();
|
||||
Assert.Throws<ArgumentException>(() => entry.SetValueKind((RegistryValueKind)100));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsSet_ValueHasBeenSet_NotRead_ReturnsFalse()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Value = "Test"
|
||||
};
|
||||
Assert.That(entry.IsSet, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsKeyReadable_AllPropertiesSet_ReturnsTrue()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Hive = RegistryHive.LocalMachine,
|
||||
Path = @"SOFTWARE\Microsoft",
|
||||
Name = "Version"
|
||||
};
|
||||
Assert.That(entry.IsReadable, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsKeyWritable_AllPropertiesSet_ReturnsTrue()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Hive = RegistryHive.LocalMachine,
|
||||
Path = @"SOFTWARE\Microsoft",
|
||||
Name = "Version",
|
||||
Value = "1.0"
|
||||
};
|
||||
Assert.That(entry.IsWritable, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Read_WhenRegistryKeyIsNull_ThrowsInvalidOperationException()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>();
|
||||
Assert.Throws<InvalidOperationException>(() => entry.Read());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Write_WhenKeyIsUnwritable_ThrowsInvalidOperationException()
|
||||
{
|
||||
var entry = new WinRegistryEntry<string>
|
||||
{
|
||||
Name = "Version"
|
||||
};
|
||||
Assert.Throws<InvalidOperationException>(() => entry.Write());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteDefaultAndReadDefault_Entry_DoesNotThrowAndReadsCorrectly()
|
||||
{
|
||||
var entry = new WinRegistryEntry<int>(TestHive, TestPath, 0)
|
||||
{
|
||||
Hive = TestHive,
|
||||
Path = TestPath,
|
||||
Value = 0,
|
||||
};
|
||||
|
||||
var readEntry = new WinRegistryEntry<int>
|
||||
{
|
||||
Hive = TestHive,
|
||||
Path = TestPath,
|
||||
};
|
||||
|
||||
Assert.That(() => entry.Write(), Throws.Nothing);
|
||||
Assert.That(() => readEntry.Read(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readEntry.ValueKind, Is.EqualTo(RegistryValueKind.String));
|
||||
Assert.That(readEntry.Value, Is.EqualTo(entry.Value));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteAndRead_Entry_DoesNotThrowAndReadsCorrectly()
|
||||
{
|
||||
var entry = new WinRegistryEntry<long>
|
||||
{
|
||||
Hive = TestHive,
|
||||
Path = TestPath,
|
||||
Name = "TestRead",
|
||||
Value = 200
|
||||
};
|
||||
|
||||
var readEntry = new WinRegistryEntry<long>
|
||||
{
|
||||
Hive = TestHive,
|
||||
Path = TestPath,
|
||||
Name = "TestRead"
|
||||
};
|
||||
|
||||
Assert.That(() => entry.Write(), Throws.Nothing);
|
||||
Assert.That(() => readEntry.Read(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readEntry.ValueKind, Is.EqualTo(entry.ValueKind));
|
||||
Assert.That(readEntry.Value, Is.EqualTo(entry.Value));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWriteAndRead_DoesNotThrow_ReturnsStrJustATest()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "FluentReadAndWriteTest", "JustATest").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "FluentReadAndWriteTest").Read();
|
||||
Assert.That(entry.Value, Is.EqualTo("JustATest"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWriteReadAndChange_DoesNotThrow_WriteReadsCorrectly()
|
||||
{
|
||||
var entry = WinRegistryEntry<string>
|
||||
.New(TestHive, TestPath, "FluentReadWriteAndChangeTest", "JustASecondTest")
|
||||
.Write()
|
||||
.Read();
|
||||
string result1 = entry.Value;
|
||||
|
||||
string result2 = entry
|
||||
.Write("JustAChangeTest")
|
||||
.Read()
|
||||
.Value;
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(result1, Is.EqualTo("JustASecondTest"));
|
||||
Assert.That(result2, Is.EqualTo("JustAChangeTest"));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Read_LockAfter_IsLocked()
|
||||
{
|
||||
var entry = WinRegistryEntry<string>
|
||||
.New(TestHive, TestPath, "IsLockedAfter", "After")
|
||||
.Write()
|
||||
.Read()
|
||||
.Lock();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.IsLocked, Is.EqualTo(true));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Read_LockBevore_IsLocked()
|
||||
{
|
||||
var entry = WinRegistryEntry<string>
|
||||
.New(TestHive, TestPath, "IsLockedBevore", "Bevore")
|
||||
.Write()
|
||||
.Read()
|
||||
.Lock();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.IsLocked, Is.EqualTo(true));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Read_Lock_ThrowWhenRead()
|
||||
{
|
||||
var entry = WinRegistryEntry<string>
|
||||
.New(TestHive, TestPath, "ReadLockThrow", "ReadingIsLocked")
|
||||
.Write()
|
||||
.Read()
|
||||
.Lock();
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => entry.Read());
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
WinRegistry winReg = new();
|
||||
winReg.DeleteTree(TestHive, TestRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
using Microsoft.Win32;
|
||||
using NUnit.Framework.Internal;
|
||||
using System.Runtime.Versioning;
|
||||
using NUnit.Framework;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry.RegistryEntryTest
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class BoolEntryTest
|
||||
{
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\BoolEntryTest";
|
||||
|
||||
[Test]
|
||||
public void StringTrue_SuccessfulToBool_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<bool>.New(TestHive, TestPath, "IsTrueString", true).Write());
|
||||
var entry = WinRegistryEntry<bool>.New(TestHive, TestPath, "IsTrueString").Read();
|
||||
Assert.That(entry.Value, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringFalse_SuccessfulToBool_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<bool>.New(TestHive, TestPath, "IsFalseString", false).Write());
|
||||
|
||||
var entry = WinRegistryEntry<bool>.New(TestHive, TestPath, "IsFalseString");
|
||||
entry.Value = true; // change Value to true to ensure a value was readed
|
||||
entry.Read();
|
||||
|
||||
Assert.That(entry.Value, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DWordTrue_SuccessfulToBool_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<bool>.New(TestHive, TestPath, "IsTrueDword", true).SetValueKind(RegistryValueKind.DWord).Write());
|
||||
var entry = WinRegistryEntry<bool>.New(TestHive, TestPath, "IsTrueDword").Read();
|
||||
Assert.That(entry.Value, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DWordFalse_SuccessfulToBool_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<bool>.New(TestHive, TestPath, "IsFalseDword", false).SetValueKind(RegistryValueKind.DWord).Write());
|
||||
var entry = WinRegistryEntry<bool>.New(TestHive, TestPath, "IsFalseDword");
|
||||
entry.Value = true; // change Value to true to ensure a value was readed
|
||||
entry.Read();
|
||||
Assert.That(entry.Value, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWrite_And_Read_DoesNotThrow_ReturnsBoolFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<bool>.New(TestHive, TestPath, "FluentReadAndWriteTest", false).SetValueKind(RegistryValueKind.DWord).Write());
|
||||
var entry = WinRegistryEntry<bool>.New(TestHive, TestPath, "FluentReadAndWriteTest", true).Read();
|
||||
Assert.That(entry.Value, Is.False);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
WinRegistry winReg = new();
|
||||
winReg.DeleteTree(TestHive, TestRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
|
||||
using Microsoft.Win32;
|
||||
using NUnit.Framework.Internal;
|
||||
using System;
|
||||
using System.Runtime.Versioning;
|
||||
using NUnit.Framework;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry.RegistryEntryTest
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class IntegerEntryTest
|
||||
{
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\IntegerEntryTest";
|
||||
|
||||
public enum TestEnum
|
||||
{
|
||||
First = 1,
|
||||
Second = 2,
|
||||
Third = 3
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_NoValidationSet_EntryComplete_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 1).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueInAllowedValues_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 2).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation(new int[] { 1, 2, 3 }).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueNotInAllowedValues_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 4).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation(new int[] { 1, 2, 3 }).Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_ValueInRange_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 5).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation(1,10).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_ValueOutOfRange_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 50).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation(1, 10).Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_Value0_ValueOutOfRange_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidZero", 0).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidZero").SetValidation(0, 10).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_DefaultMin_ValueInRange_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidDefMin", 10).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidDefMin").SetValidation("*", "10").Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_DefaultMax_ValueInRange_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidDefMax", 1000).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValidDefMax").SetValidation("50", "*").Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_EnumSet_ValueInEnum_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 2).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation<TestEnum>().Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_EnumSet_ValueNotInEnum_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 5).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid").SetValidation<TestEnum>().Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_InvalidValueKind_ThrowArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", 5).SetValueKind(RegistryValueKind.Unknown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Value_SetToNegativeNumber_ThrowArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => WinRegistryEntry<int>.New(TestHive, TestPath, "IsValid", -100));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWrite_And_Read_DoesNotThrow_ReturnsInt12()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<int>.New(TestHive, TestPath, "FluentReadAndWriteTest", 12).Write());
|
||||
var entry = WinRegistryEntry<int>.New(TestHive, TestPath, "FluentReadAndWriteTest", 42).Read();
|
||||
Assert.That(entry.Value, Is.EqualTo(12));
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
WinRegistry winReg = new();
|
||||
winReg.DeleteTree(TestHive, TestPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
using Microsoft.Win32;
|
||||
using NUnit.Framework.Internal;
|
||||
using System;
|
||||
using System.Runtime.Versioning;
|
||||
using NUnit.Framework;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry.RegistryEntryTest
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class LongIntegerEntryTest
|
||||
{
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\LongIntegerEntryTest";
|
||||
|
||||
[Test]
|
||||
public void IsValid_NoValidationSet_EntryComplete_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 3047483647).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid").Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueInAllowedValues_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 2147483649).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid").SetValidation(new long[] { 2147483648, 2147483649, 2147483650 }).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueNotInAllowedValues_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 4).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid").SetValidation(new long[] { 2147483648, 2147483649, 2147483650 }).Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_ValueInRange_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 2147483652).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid").SetValidation(2147483647, 2200000000).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_RangeSet_ValueOutOfRange_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 20).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid").SetValidation(2147483647, 2200000000).Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_InvalidValueKind_ThrowArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", 5).SetValueKind(RegistryValueKind.Unknown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Value_SetToNegativeNumber_ThrowArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => WinRegistryEntry<long>.New(TestHive, TestPath, "IsValid", -100));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWrite_And_Read_DoesNotThrow_ReturnsLong15()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<long>.New(TestHive, TestPath, "FluentReadAndWriteTest", 15).Write());
|
||||
var entry = WinRegistryEntry<long>.New(TestHive, TestPath, "FluentReadAndWriteTest", 42).Read();
|
||||
Assert.That(entry.Value, Is.EqualTo(15));
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
WinRegistry winReg = new();
|
||||
winReg.DeleteTree(TestHive, TestRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
|
||||
using Microsoft.Win32;
|
||||
using NUnit.Framework.Internal;
|
||||
using System;
|
||||
using System.Runtime.Versioning;
|
||||
using NUnit.Framework;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry.RegistryEntryTest
|
||||
{
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class StringEntryTest
|
||||
{
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\StringEntryTest";
|
||||
|
||||
public enum TestEnum
|
||||
{
|
||||
First = 1,
|
||||
Second = 2,
|
||||
Third = 3
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_NoValidationSet_EntryComplete_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "IsValid", "IsValid").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "IsValid").Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueInAllowedValues_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayIsValid", "Banana").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayIsValid").SetValidation(new string[] { "Banana", "Strawberry", "Apple" }).Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_ValueNotInAllowedValues_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayIsInValid", "Cheese").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayIsInValid").SetValidation(new string[] { "Banana", "Strawberry", "Apple" }).Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_AllowedValuesSet_CorrectsValueSpellingAndValidatesSuccessfully()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayCorrectsSpellingIsValid", "StrawBerry").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "ArrayCorrectsSpellingIsValid").SetValidation(new string[] { "Banana", "Strawberry", "Apple" }).Read();
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
Assert.That(entry.Value, Is.EqualTo("Strawberry"));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_EnumSet_ValueInEnum_ReturnsTrue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "IsValid", "Second").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "IsValid").SetValidation<TestEnum>().Read();
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_EnumSet_ValueNotInEnum_ReturnsFalse()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "IsInValid", "Fourth").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "IsInValid").SetValidation<TestEnum>().Read();
|
||||
Assert.That(entry.IsValid, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_EnumSet_CorrectsValueSpellingAndValidatesSuccessfully()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "IsValidCorrectsSpelling", "SecOND").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "IsValidCorrectsSpelling").SetValidation<TestEnum>().Read();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.IsValid, Is.True);
|
||||
Assert.That(entry.Value, Is.EqualTo("Second"));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsValid_InvalidValueKind_ThrowArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => WinRegistryEntry<string>.New(TestHive, TestPath, "IsValid", "Windows").SetValueKind(RegistryValueKind.Unknown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FluentWrite_And_Read_DoesNotThrow_ReturnsStrJustATest()
|
||||
{
|
||||
Assert.DoesNotThrow(() => WinRegistryEntry<string>.New(TestHive, TestPath, "FluentReadAndWriteTest", "JustATest").Write());
|
||||
var entry = WinRegistryEntry<string>.New(TestHive, TestPath, "FluentReadAndWriteTest", "TestFailed").Read();
|
||||
Assert.That(entry.Value, Is.EqualTo("JustATest"));
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
WinRegistry winReg = new();
|
||||
winReg.DeleteTree(TestHive, TestRoot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,195 +1,756 @@
|
||||
using System;
|
||||
using Microsoft.Win32;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.Win32;
|
||||
using System.Reflection;
|
||||
using mRemoteNG.Tools.WindowsRegistry;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace mRemoteNGTests.Tools.Registry
|
||||
{
|
||||
public class WindowsRegistryTests
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal class WindowsRegistryTests : WinRegistry
|
||||
{
|
||||
private IRegistryRead _registryReader;
|
||||
private IRegistryWrite _registryWriter;
|
||||
private const string TestRoot = @"Software\mRemoteNGTest";
|
||||
private const RegistryHive TestHive = RegistryHive.CurrentUser;
|
||||
private const string TestPath = $"{TestRoot}\\WinRegistryTests";
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_registryReader = new WindowsRegistry();
|
||||
_registryWriter = new WindowsRegistry();
|
||||
}
|
||||
|
||||
#region GetSubKeyNames() tests
|
||||
[Test]
|
||||
public void CanGetSubkeyNames()
|
||||
#region WindowsRegistry.ThrowIfHiveInvalid()
|
||||
|
||||
private static MethodInfo? GetPrivateStaticMethod(string methodName)
|
||||
{
|
||||
var subKeyNames = _registryReader.GetSubKeyNames(RegistryHive.CurrentUser, "Software");
|
||||
Assert.That(subKeyNames, Does.Contain("Microsoft"));
|
||||
return typeof(WinRegistry).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSubkeyNamesThrowsIfGivenNullKeyPath()
|
||||
public void ThrowIfHiveInvalid_ValidHive_DoesNotThrow()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _registryReader.GetSubKeyNames(RegistryHive.CurrentUser, null));
|
||||
var method = GetPrivateStaticMethod("ThrowIfHiveInvalid");
|
||||
if (method != null)
|
||||
Assert.DoesNotThrow(() => method.Invoke(null, new object[] { RegistryHive.LocalMachine }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfHiveInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetSubkeyNamesThrowsIfGivenUnknownHive()
|
||||
public void ThrowIfHiveInvalid_CurrentConfig_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _registryReader.GetSubKeyNames(new RegistryHive(), "Software"));
|
||||
var method = GetPrivateStaticMethod("ThrowIfHiveInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { null }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfHiveInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowIfHiveInvalid_InvalidHive_ThrowsArgumentException()
|
||||
{
|
||||
var method = GetPrivateStaticMethod("ThrowIfHiveInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method ? .Invoke(null, new object[] { (RegistryHive)100 }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfHiveInvalid could not be found.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetPropertyValue() tests
|
||||
#region WindowsRegistry.ThrowIfPathInvalid()
|
||||
|
||||
[Test]
|
||||
public void CanGetPropertyValue()
|
||||
public void ThrowIfPathInvalid_ValidPath_DoesNotThrow()
|
||||
{
|
||||
var keyValue = _registryReader.GetPropertyValue(RegistryHive.ClassesRoot, @".dll\PersistentHandler", "");
|
||||
Assert.That(keyValue, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
|
||||
var method = GetPrivateStaticMethod("ThrowIfPathInvalid");
|
||||
if (method != null)
|
||||
Assert.DoesNotThrow(() => method.Invoke(null, new object[] { @"SOFTWARE\Microsoft" }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfPathInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanGetPropertyValueByRegistryKeyObject()
|
||||
public void ThrowIfPathInvalid_NullPath_ThrowsArgumentException()
|
||||
{
|
||||
WindowsRegistryKey key = new()
|
||||
{
|
||||
Hive = RegistryHive.ClassesRoot,
|
||||
Path = @".dll\PersistentHandler",
|
||||
Name = ""
|
||||
};
|
||||
|
||||
var keyValue = _registryReader.GetPropertyValue(key);
|
||||
Assert.That(keyValue, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
|
||||
}
|
||||
[Test]
|
||||
public void GetPropertyValueThrowsIfGivenNullKeyPath()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _registryReader.GetPropertyValue(RegistryHive.CurrentUser, null, ""));
|
||||
var method = GetPrivateStaticMethod("ThrowIfPathInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { null }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfPathInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetPropertyValueThrowsIfGivenNullPropertyName()
|
||||
public void ThrowIfPathInvalid_EmptyPath_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => _registryReader.GetPropertyValue(RegistryHive.CurrentUser, "", null));
|
||||
var method = GetPrivateStaticMethod("ThrowIfPathInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { string.Empty }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfPathInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowIfPathInvalid_WhitespacePath_ThrowsArgumentException()
|
||||
{
|
||||
var method = GetPrivateStaticMethod("ThrowIfPathInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { " " }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfPathInvalid could not be found.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetWindowsRegistryKey() tests
|
||||
#region WindowsRegistry.ThrowIfNameInvalid()
|
||||
|
||||
[Test]
|
||||
public void CanGetWindowsRegistryKey()
|
||||
public void ThrowIfNameInvalid_ValidName_DoesNotThrow()
|
||||
{
|
||||
WindowsRegistryKey keyValue = _registryReader.GetWindowsRegistryKey(RegistryHive.ClassesRoot, @".dll\PersistentHandler", "");
|
||||
Assert.That(keyValue.Value, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
|
||||
var method = GetPrivateStaticMethod("ThrowIfNameInvalid");
|
||||
if (method != null)
|
||||
Assert.DoesNotThrow(() => method.Invoke(null, new object[] { "TestName" }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfNameInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanGetWindowsRegistryKeyByObject()
|
||||
public void ThrowIfNameInvalid_NullName_ThrowsArgumentNullException()
|
||||
{
|
||||
WindowsRegistryKey key = new()
|
||||
{
|
||||
Hive = RegistryHive.ClassesRoot,
|
||||
Path = @".dll\PersistentHandler",
|
||||
Name = ""
|
||||
};
|
||||
var method = GetPrivateStaticMethod("ThrowIfNameInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { null }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfNameInvalid could not be found.");
|
||||
}
|
||||
|
||||
WindowsRegistryKey keyValue = _registryReader.GetWindowsRegistryKey(key);
|
||||
Assert.That(keyValue.Value, Is.EqualTo("{098f2470-bae0-11cd-b579-08002b30bfeb}"));
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.ThrowIfValueKindInvalid()
|
||||
|
||||
[Test]
|
||||
public void ThrowIfValueKindInvalid_ValidValueKind_DoesNotThrow()
|
||||
{
|
||||
var method = GetPrivateStaticMethod("ThrowIfValueKindInvalid");
|
||||
if (method != null)
|
||||
Assert.DoesNotThrow(() => method.Invoke(null, new object[] { RegistryValueKind.String }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfValueKindInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanGetWindowsRegistryKeyForKeyNotExists()
|
||||
public void ThrowIfValueKindInvalid_InvalidValueKind_ThrowsArgumentException()
|
||||
{
|
||||
// No exception. Only null value
|
||||
WindowsRegistryKey keyValue = _registryReader.GetWindowsRegistryKey(RegistryHive.LocalMachine, @"Software\Testabcdefg", "abcdefg");
|
||||
Assert.That(keyValue.Value, Is.EqualTo(null));
|
||||
var method = GetPrivateStaticMethod("ThrowIfValueKindInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { (RegistryValueKind)100 }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfValueKindInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetWindowsRegistryThrowNotReadable()
|
||||
public void ThrowIfValueKindInvalid_ValueKindUnknown_ThrowsArgumentException()
|
||||
{
|
||||
WindowsRegistryKey key = new()
|
||||
{
|
||||
Hive = RegistryHive.ClassesRoot,
|
||||
};
|
||||
var method = GetPrivateStaticMethod("ThrowIfValueKindInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { RegistryValueKind.Unknown }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfValueKindInvalid could not be found.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowIfValueKindInvalid_ValueKindNone_ThrowsArgumentException()
|
||||
{
|
||||
var method = GetPrivateStaticMethod("ThrowIfValueKindInvalid");
|
||||
if (method != null)
|
||||
Assert.Throws<TargetInvocationException>(() => method.Invoke(null, new object[] { RegistryValueKind.None }));
|
||||
else
|
||||
Assert.Fail("The method ThrowIfValueKindInvalid could not be found.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.SetValue()
|
||||
|
||||
[Test]
|
||||
public void SetValue_NewKey_SetsValueSuccessfully()
|
||||
{
|
||||
const string testName = "TestValue";
|
||||
const string testValue = "Test1";
|
||||
const string testPath = TestPath + @"\SetValue";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
string key = GetStringValue(TestHive, testPath, testName, testValue);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetValue_OverwriteValue_SetsValueSuccessfully()
|
||||
{
|
||||
const string testName = "TestValue";
|
||||
const string testValue = "Test2";
|
||||
const string testPath = TestPath + @"\SetValue";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
string key = GetStringValue(TestHive, testPath, testName, testValue);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetValue_DefaultValueWithWrongValueKind_SetsValueSuccessfully()
|
||||
{
|
||||
const string? testName = null;
|
||||
const string testValue = "SetDefault";
|
||||
const string testPath = TestPath + @"\SetValue";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.Unknown));
|
||||
|
||||
string key = GetStringValue(TestHive, testPath, testName, testValue);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.CreateKey()
|
||||
|
||||
[Test]
|
||||
public void CreateKey_NewKey_CreatesKeySuccessfully()
|
||||
{
|
||||
const string testPath = TestPath + @"\TestSubKey";
|
||||
Assert.DoesNotThrow(() => CreateKey(TestHive, testPath));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateKey_ExistingKey_DoesNotThrow()
|
||||
{
|
||||
const string testPath = TestPath + @"\TestSubKey";
|
||||
Assert.DoesNotThrow(() => CreateKey(TestHive, testPath));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateKey_InvalidHive_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => CreateKey((RegistryHive)(-1), @"Software\Test"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.GetSubKeyNames()
|
||||
|
||||
[Test]
|
||||
public void GetSubKeyNames_ExistingKey_ReturnsSubKeyNames()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetSubKeyNames";
|
||||
const string testSubKey1 = testPath + @"\subkey1";
|
||||
const string testSubKey2 = testPath + @"\subkey2";
|
||||
const string testSubKey3 = testPath + @"\subkey3";
|
||||
|
||||
CreateKey(TestHive, testSubKey1);
|
||||
CreateKey(TestHive, testSubKey2);
|
||||
CreateKey(TestHive, testSubKey3);
|
||||
|
||||
string[] subKeyNames = GetSubKeyNames(TestHive, testPath);
|
||||
|
||||
Assert.That(subKeyNames.Length, Is.EqualTo(3));
|
||||
Assert.That(subKeyNames, Contains.Item("subkey1"));
|
||||
Assert.That(subKeyNames, Contains.Item("subkey2"));
|
||||
Assert.That(subKeyNames, Contains.Item("subkey3"));
|
||||
}
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => _registryReader.GetWindowsRegistryKey(key));
|
||||
[Test]
|
||||
public void GetSubKeyNames_NonExistingKey_ReturnsEmptyArray()
|
||||
{
|
||||
string[] subKeyNames = GetSubKeyNames(TestHive, @"Software\NonExistingKey");
|
||||
Assert.That(subKeyNames, Is.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetRegistryEntries() + Recurse tests
|
||||
#region WindowsRegistry.GetValue()
|
||||
|
||||
[Test]
|
||||
public void CanGetRegistryEntries()
|
||||
public void GetValue_RetrieveValue_ReturnsValue()
|
||||
{
|
||||
List<WindowsRegistryKey> keys = _registryReader.GetRegistryEntries(RegistryHive.LocalMachine, @"HARDWARE\DESCRIPTION\System\BIOS");
|
||||
Assert.That(keys.Count, Is.Not.EqualTo(0));
|
||||
const string testPath = TestPath + @"\GetValue";
|
||||
const string testName = "TestValue";
|
||||
const string testValue = "Test";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
string key = GetValue(TestHive, testPath, testName);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanGetRegistryEntriesRecurse()
|
||||
public void GetValue_RetrieveDefault_RetrunsValue()
|
||||
{
|
||||
List<WindowsRegistryKey> keys = _registryReader.GetRegistryEntryiesRecursive(RegistryHive.LocalMachine, @"SOFTWARE\Microsoft\Windows\Windows Search");
|
||||
Assert.That(keys.Count, Is.Not.EqualTo(0));
|
||||
const string testPath = TestPath + @"\GetValue";
|
||||
const string testName = "";
|
||||
const string testValue = "TestDefault{098f2470-bae0-11cd-b579-08002b30bfeb}";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
var key = GetValue(TestHive, testPath, testName);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetValue_NonExistingValue_ReturnsNull()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetValue";
|
||||
const string nonExistingName = "NonExistingValue";
|
||||
|
||||
string key = GetValue(TestHive, testPath, nonExistingName);
|
||||
Assert.That(key, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetAndGetDefaultValue_RetrieveValue_ReturnsValue()
|
||||
{
|
||||
const string testPath = TestPath + @"\DefautValue";
|
||||
const string testValue = "DefaultTestTestValue";
|
||||
|
||||
Assert.DoesNotThrow(() => SetDefaultValue(TestHive, testPath, testValue));
|
||||
|
||||
string key = GetDefaultValue(TestHive, testPath);
|
||||
Assert.That(key, Is.EqualTo(testValue));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region new WindowsRegistryKey() tests
|
||||
#region WindowsRegistry.GetStringValue()
|
||||
|
||||
[Test]
|
||||
public void IsWindowsRegistryKeyValid()
|
||||
public void GetStringValue_RetrieveValue_ReturnsValue()
|
||||
{
|
||||
// Tests property rules of WindowsRegistryKey
|
||||
WindowsRegistryKey key = new();
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string testName = "TestValue";
|
||||
const string testValue = "Test";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
var key = GetStringValue(TestHive, testPath, testName);
|
||||
Assert.That(key, Is.TypeOf<string>().And.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetStringValue_RetrieveDefault_ReturnsValue()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string testName = "";
|
||||
const string testValue = "TestDefault{098f2470-bae0-11cd-b579-08002b30bfeb}";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
var key = GetStringValue(TestHive, testPath, testName);
|
||||
Assert.That(key, Is.TypeOf<string>().And.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetStringValue_NonExistingValue_ReturnsNull()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string nonExistingName = "NonExistingValue";
|
||||
|
||||
var key = GetStringValue(TestHive, testPath, nonExistingName);
|
||||
Assert.That(key, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetStringValue_NonExistingValue_ReturnsDefault()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string nonExistingName = "NonExistingValue";
|
||||
const string defaultValue = "DefaultValue";
|
||||
|
||||
string key = GetStringValue(TestHive, testPath, nonExistingName, defaultValue);
|
||||
Assert.That(key, Is.TypeOf<string>().And.EqualTo(defaultValue));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.GetBoolValue()
|
||||
|
||||
[Test]
|
||||
public void GetBoolValue_FromString_ReturnsTrue()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetBoolValue";
|
||||
const string testName = "strBool_true";
|
||||
const string testValue = "true";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
var key = GetBoolValue(TestHive, testPath, testName, false); // set default to false
|
||||
Assert.That(key, Is.TypeOf<bool>().And.EqualTo(true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetBoolValue_FromString_ReturnsFalse()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetBoolValue";
|
||||
const string testName = "strBool_false";
|
||||
const string testValue = "false";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
|
||||
var key = GetBoolValue(TestHive, testPath, testName, true); // set default to true
|
||||
Assert.That(key, Is.TypeOf<bool>().And.EqualTo(false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetBoolValue_FromDword_ReturnsFalse()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetBoolValue";
|
||||
const string testName = "intBool_false";
|
||||
const int testValue = 0;
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.DWord));
|
||||
|
||||
var key = GetBoolValue(TestHive, testPath, testName, true); // set default to true
|
||||
Assert.That(key, Is.TypeOf<bool>().And.EqualTo(false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetBoolValue_FromDword_ReturnsTrue()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetBoolValue";
|
||||
const string testName = "intBool_true";
|
||||
const int testValue = 1;
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.DWord));
|
||||
|
||||
var key = GetBoolValue(TestHive, testPath, testName, false); // set default to false
|
||||
Assert.That(key, Is.TypeOf<bool>().And.EqualTo(true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetBoolValue_NonExistingValue_ReturnsFalse()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string nonExistingName = "NonExistingValue";
|
||||
|
||||
var key = GetBoolValue(TestHive, testPath, nonExistingName);
|
||||
Assert.That(key, Is.TypeOf<bool>().And.EqualTo(false));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.GetDwordValue()
|
||||
|
||||
[Test]
|
||||
public void GetIntegerValue_RetrieveValue_ReturnsValue()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetIntegerValue";
|
||||
const string testName = "ExistingDword";
|
||||
const int testValue = 2;
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.DWord));
|
||||
|
||||
var key = GetIntegerValue(TestHive, testPath, testName);
|
||||
Assert.That(key, Is.TypeOf<int>().And.EqualTo(testValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetIntegerValue_NonExistingValue_ReturnsZero()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string nonExistingName = "NonExistingValue";
|
||||
|
||||
var key = GetIntegerValue(TestHive, testPath, nonExistingName);
|
||||
Assert.That(key, Is.TypeOf<int>().And.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetIntegerValue_NotExistingKey_ReturnsDefault()
|
||||
{
|
||||
const string testPath = TestPath + @"\GetStringValue";
|
||||
const string testName = "NotExistingDword";
|
||||
|
||||
var key = GetIntegerValue(TestHive, testPath, testName, 12); // set default to true
|
||||
Assert.That(key, Is.TypeOf<int>().And.EqualTo(12));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.GetEntry()
|
||||
|
||||
[Test]
|
||||
public void GetRegistryEntry_ReturnsCorrectEntry()
|
||||
{
|
||||
const string testName = "TestValue";
|
||||
const int testValue = 2011;
|
||||
const string testPath = TestPath + @"\GetRegistryEntry";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.DWord));
|
||||
|
||||
var entry = GetEntry(TestHive, testPath, testName);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.DoesNotThrow(() => key.Hive = RegistryHive.CurrentUser);
|
||||
Assert.DoesNotThrow(() => key.ValueKind = RegistryValueKind.String);
|
||||
Assert.DoesNotThrow(() => key.Path = "Software");
|
||||
Assert.DoesNotThrow(() => key.Name = "NotThereButOK");
|
||||
//Assert.DoesNotThrow(() => key.Value = "Equal", "");
|
||||
Assert.That(entry.Hive, Is.EqualTo(TestHive));
|
||||
Assert.That(entry.Path, Is.EqualTo(testPath));
|
||||
Assert.That(entry.Name, Is.EqualTo(testName));
|
||||
Assert.That(entry.ValueKind, Is.EqualTo(RegistryValueKind.DWord));
|
||||
Assert.That(entry.IsSet, Is.EqualTo(true));
|
||||
});
|
||||
|
||||
}
|
||||
[Test]
|
||||
public void WindowsRegistryKeyThrowHiveNullException()
|
||||
{
|
||||
WindowsRegistryKey key = new();
|
||||
Assert.Throws<ArgumentNullException>(() => key.Hive = 0, "Expected IsHiveValid to throw ArgumentNullException");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WindowsRegistryKeyValueKindUnknown()
|
||||
{
|
||||
WindowsRegistryKey key = new();
|
||||
Assert.That(key.ValueKind, Is.EqualTo(RegistryValueKind.Unknown));
|
||||
#endregion
|
||||
|
||||
#region WinRegistryEC.GetRegistryEntries()
|
||||
|
||||
}
|
||||
[Test]
|
||||
public void WindowsRegistryKeyThrowPathNullException()
|
||||
public void GetRegistryEntries_ReturnsCorrectEntries()
|
||||
{
|
||||
WindowsRegistryKey key = new();
|
||||
Assert.Throws<ArgumentNullException>(() => key.Path = null, "Expected IsPathValid to throw ArgumentNullException");
|
||||
const string testPath = TestPath + @"\GetRegistryEntries";
|
||||
const string testNamePrefix = "TestEntry";
|
||||
const string testValue = "winRegEntriesTest";
|
||||
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
string testName = $"{testNamePrefix}{i}";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
}
|
||||
|
||||
const string testPathSubkeys = testPath + @"\Subkey";
|
||||
const string testNameSubKey = "TestSubEntry";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPathSubkeys, testNameSubKey, testValue, RegistryValueKind.String));
|
||||
|
||||
var entries = GetEntries(TestHive, testPath);
|
||||
|
||||
|
||||
Assert.That(entries, Is.Not.Null);
|
||||
Assert.That(entries, Is.Not.Empty);
|
||||
Assert.That(entries, Is.InstanceOf<List<WinRegistryEntry<string>>>());
|
||||
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.Name, Is.Not.Null & Is.Not.EqualTo(testNameSubKey)); //Subkeys should not be read (non-recursive).
|
||||
|
||||
Assert.That(entry.Hive, Is.EqualTo(TestHive));
|
||||
Assert.That(entry.Path, Is.EqualTo(testPath));
|
||||
Assert.That(entry.Value, Is.EqualTo(testValue));
|
||||
Assert.That(entry.ValueKind, Is.EqualTo(RegistryValueKind.String));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WinRegistryEC.GetRegistryEntriesRecursive()
|
||||
|
||||
[Test]
|
||||
public void WindowsRegistryKeyThrowNameNullException()
|
||||
public void GetRegistryEntriesRecursive_ReturnsCorrectEntries()
|
||||
{
|
||||
WindowsRegistryKey key = new();
|
||||
Assert.Throws<ArgumentNullException>(() => key.Name = null, "Expected IsNameValid to throw ArgumentNullException");
|
||||
const string testPath = TestPath + @"\GetRegistryEntriesRecursive";
|
||||
const string testNamePrefix = "TestEntry";
|
||||
const string testValue = "winRegEntriesTest";
|
||||
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
string testName = $"{testNamePrefix}{i}";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName, testValue, RegistryValueKind.String));
|
||||
}
|
||||
|
||||
const string testSubkeyPath = testPath + @"\Subkey";
|
||||
const string testNameSubKey = "TestSubEntry";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testSubkeyPath, testNameSubKey, testValue, RegistryValueKind.String));
|
||||
|
||||
var entries = GetEntriesRecursive(TestHive, testPath);
|
||||
|
||||
Assert.That(entries, Is.Not.Null);
|
||||
Assert.That(entries, Is.Not.Empty);
|
||||
Assert.That(entries, Is.InstanceOf<List< WinRegistryEntry<string>>> ());
|
||||
|
||||
// Assert that the subkey is included in the entries list
|
||||
Assert.That(entries.Any(e => e.Name == testNameSubKey), Is.True, "Subkey entry should be included in the entries list.");
|
||||
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(entry.Name, Is.Not.Null);
|
||||
Assert.That(entry.Hive, Is.EqualTo(TestHive));
|
||||
Assert.That(entry.Value, Is.EqualTo(testValue));
|
||||
Assert.That(entry.ValueKind, Is.EqualTo(RegistryValueKind.String));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.DeleteRegistryValue()
|
||||
[Test]
|
||||
public void DeleteRegistryValue_DeletesValue()
|
||||
{
|
||||
const string testPath = TestPath + @"\DeleteRegistryValue";
|
||||
const string testName01 = "TestValue01";
|
||||
const string testName02 = "TestValue02";
|
||||
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName01, "Test", RegistryValueKind.String));
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, testPath, testName02, "Test", RegistryValueKind.String));
|
||||
|
||||
Assert.DoesNotThrow(() => DeleteRegistryValue(TestHive, testPath, testName01));
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(GetStringValue(TestHive, testPath, testName01), Is.Null);
|
||||
Assert.That(GetStringValue(TestHive, testPath, testName02), Is.Not.Null);
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SetRegistryValue() tests
|
||||
[Test]
|
||||
public void CanSetRegistryValue()
|
||||
{
|
||||
Assert.DoesNotThrow(() => _registryWriter.SetRegistryValue(RegistryHive.CurrentUser, @"SOFTWARE\mRemoteNGTest", "TestKey", "A value string", RegistryValueKind.String));
|
||||
}
|
||||
#region WindowsRegistry.DeleteTree()
|
||||
|
||||
[Test]
|
||||
public void SetRegistryValueThrowAccessDenied()
|
||||
public void DeleteTree_RemovesKeyAndSubkeys()
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(() => _registryWriter.SetRegistryValue(RegistryHive.LocalMachine, @"SOFTWARE\mRemoteNGTest", "TestKey", "A value string", RegistryValueKind.String));
|
||||
const string testPath = TestPath + @"\DeleteTree";
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, $"{testPath}\\Subkey0", "Test", "Test", RegistryValueKind.String));
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, $"{testPath}\\Subkey1", "Test", "Test", RegistryValueKind.String));
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, $"{testPath}\\Subkey2", "Test", "Test", RegistryValueKind.String));
|
||||
Assert.DoesNotThrow(() => SetValue(TestHive, $"{testPath}\\Subkey3", "Test", "Test", RegistryValueKind.String));
|
||||
|
||||
Assert.DoesNotThrow(() => DeleteTree(TestHive, testPath));
|
||||
Assert.That(GetSubKeyNames(TestHive, testPath), Is.Empty);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.ConvertStringToRegistryHive()
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryHive_ReturnsCorrectValue()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ConvertStringToRegistryHive("HKCR"), Is.EqualTo(RegistryHive.ClassesRoot));
|
||||
Assert.That(ConvertStringToRegistryHive("HKey_Classes_Root"), Is.EqualTo(RegistryHive.ClassesRoot));
|
||||
Assert.That(ConvertStringToRegistryHive("ClassesRoot"), Is.EqualTo(RegistryHive.ClassesRoot));
|
||||
|
||||
Assert.That(ConvertStringToRegistryHive("HKCU"), Is.EqualTo(RegistryHive.CurrentUser));
|
||||
Assert.That(ConvertStringToRegistryHive("HKey_Current_User"), Is.EqualTo(RegistryHive.CurrentUser));
|
||||
Assert.That(ConvertStringToRegistryHive("currentuser"), Is.EqualTo(RegistryHive.CurrentUser));
|
||||
|
||||
Assert.That(ConvertStringToRegistryHive("HKLM"), Is.EqualTo(RegistryHive.LocalMachine));
|
||||
Assert.That(ConvertStringToRegistryHive("HKey_Local_Machine"), Is.EqualTo(RegistryHive.LocalMachine));
|
||||
Assert.That(ConvertStringToRegistryHive("LocalMachine"), Is.EqualTo(RegistryHive.LocalMachine));
|
||||
|
||||
Assert.That(ConvertStringToRegistryHive("HKU"), Is.EqualTo(RegistryHive.Users));
|
||||
Assert.That(ConvertStringToRegistryHive("HKey_users"), Is.EqualTo(RegistryHive.Users));
|
||||
Assert.That(ConvertStringToRegistryHive("Users"), Is.EqualTo(RegistryHive.Users));
|
||||
|
||||
Assert.That(ConvertStringToRegistryHive("HKCC"), Is.EqualTo(RegistryHive.CurrentConfig));
|
||||
Assert.That(ConvertStringToRegistryHive("HKey_Current_Config"), Is.EqualTo(RegistryHive.CurrentConfig));
|
||||
Assert.That(ConvertStringToRegistryHive("CurrentConfig"), Is.EqualTo(RegistryHive.CurrentConfig));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryHive_InvalidHiveNull_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.That(() => ConvertStringToRegistryHive(null), Throws.ArgumentNullException);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryHive_InvalidHive_ThrowArgumentException()
|
||||
{
|
||||
Assert.That(() => ConvertStringToRegistryHive("InvalidHive"), Throws.ArgumentException);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.ConvertStringToRegistryValueKind()
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryValueKind_ReturnsCorrectValue()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ConvertStringToRegistryValueKind("string"), Is.EqualTo(RegistryValueKind.String));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_sz"), Is.EqualTo(RegistryValueKind.String));
|
||||
|
||||
Assert.That(ConvertStringToRegistryValueKind("dword"), Is.EqualTo(RegistryValueKind.DWord));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_dword"), Is.EqualTo(RegistryValueKind.DWord));
|
||||
|
||||
Assert.That(ConvertStringToRegistryValueKind("binary"), Is.EqualTo(RegistryValueKind.Binary));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_binary"), Is.EqualTo(RegistryValueKind.Binary));
|
||||
|
||||
Assert.That(ConvertStringToRegistryValueKind("qword"), Is.EqualTo(RegistryValueKind.QWord));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_qword"), Is.EqualTo(RegistryValueKind.QWord));
|
||||
|
||||
Assert.That(ConvertStringToRegistryValueKind("multistring"), Is.EqualTo(RegistryValueKind.MultiString));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_multi_sz"), Is.EqualTo(RegistryValueKind.MultiString));
|
||||
|
||||
Assert.That(ConvertStringToRegistryValueKind("expandstring"), Is.EqualTo(RegistryValueKind.ExpandString));
|
||||
Assert.That(ConvertStringToRegistryValueKind("reg_expand_sz"), Is.EqualTo(RegistryValueKind.ExpandString));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryValueKind_InvalidHiveNull_ThrowArgumentNullException()
|
||||
{
|
||||
Assert.That(() => ConvertStringToRegistryValueKind(null), Throws.ArgumentNullException);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertStringToRegistryValueKind_InvalidHive_ThrowArgumentException()
|
||||
{
|
||||
Assert.That(() => ConvertStringToRegistryValueKind("InvalidKind"), Throws.ArgumentException);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.ConvertTypeToRegistryValueKind()
|
||||
|
||||
[Test]
|
||||
public void ConvertTypeToRegistryValueKind_ReturnsCorrectValue()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(string)), Is.EqualTo(RegistryValueKind.String));
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(int)), Is.EqualTo(RegistryValueKind.DWord));
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(long)), Is.EqualTo(RegistryValueKind.QWord));
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(bool)), Is.EqualTo(RegistryValueKind.DWord));
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(byte)), Is.EqualTo(RegistryValueKind.Binary));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertTypeToRegistryValueKind_UnknownType_ReturnsString()
|
||||
{
|
||||
Assert.That(ConvertTypeToRegistryValueKind(typeof(Single)), Is.EqualTo(RegistryValueKind.String));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConvertTypeToRegistryValueKind_WithNullValueType_ThrowsArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => ConvertTypeToRegistryValueKind(null));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region WindowsRegistry.ConvertRegistryValueKindToType()
|
||||
|
||||
[Test]
|
||||
public void ConvertRegistryValueKindToType_ReturnsCorrectType()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.String), Is.EqualTo(typeof(string)));
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.ExpandString), Is.EqualTo(typeof(string)));
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.DWord), Is.EqualTo(typeof(int)));
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.QWord), Is.EqualTo(typeof(long)));
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.Binary), Is.EqualTo(typeof(byte[])));
|
||||
Assert.That(ConvertRegistryValueKindToType(RegistryValueKind.MultiString), Is.EqualTo(typeof(string[])));
|
||||
Assert.That(ConvertRegistryValueKindToType((RegistryValueKind)100), Is.EqualTo(typeof(object)));
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// Delete all created tests
|
||||
DeleteTree(TestHive, TestPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user