Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 08e9df8

Browse files
committed
Merge pull request #2846 from stephentoub/fix_registry_tests
Make registry tests more robust
2 parents 860c9a8 + 30eeac1 commit 08e9df8

31 files changed

+539
-715
lines changed

src/Microsoft.Win32.Registry/tests/Microsoft.Win32.Registry.Tests.csproj

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
<UnsupportedPlatforms>Linux;OSX</UnsupportedPlatforms>
1212
</PropertyGroup>
1313
<!-- Default configurations to help VS understand the configurations -->
14-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15-
</PropertyGroup>
16-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
17-
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
1816
<ItemGroup>
17+
<Compile Include="Helpers.cs" />
18+
<Compile Include="Registry\Registry_Getvalue_str_str_obj.cs" />
19+
<Compile Include="Registry\Registry_SetValue_str_str_obj.cs" />
20+
<Compile Include="Registry\Registry_SetValue_str_str_obj_valuekind.cs" />
1921
<Compile Include="RegistryKey\RegistryKey_CreateSubKey_str.cs" />
2022
<Compile Include="RegistryKey\RegistryKey_CreateSubKey_str_rkpc.cs" />
2123
<Compile Include="RegistryKey\RegistryKey_DeleteSubKeyTree.cs" />
@@ -41,11 +43,7 @@
4143
<Compile Include="RegistryKey\RegistryKey_SetValueKind_str_obj_valueKind.cs" />
4244
<Compile Include="RegistryKey\RegistryKey_SetValue_str_obj.cs" />
4345
<Compile Include="RegistryKey\SafeRegistryHandle.cs" />
44-
<Compile Include="Registry\Registry_Getvalue_str_str_obj.cs" />
45-
<Compile Include="Registry\Registry_SetValue_str_str_obj.cs" />
46-
<Compile Include="Registry\Registry_SetValue_str_str_obj_valuekind.cs" />
47-
<Compile Include="Helpers.cs" />
48-
<Compile Include="TestSubKey.cs" />
46+
<Compile Include="RegistryTestsBase.cs" />
4947
<Compile Include="TestData.cs" />
5048
<Compile Include="XunitAssemblyAttributes.cs" />
5149
</ItemGroup>
@@ -59,4 +57,4 @@
5957
</ProjectReference>
6058
</ItemGroup>
6159
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
62-
</Project>
60+
</Project>

src/Microsoft.Win32.Registry/tests/Registry/Registry_Getvalue_str_str_obj.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
namespace Microsoft.Win32.RegistryTests
1010
{
11-
public class Registry_GetValue_str_str_obj : TestSubKey
11+
public class Registry_GetValue_str_str_obj : RegistryTestsBase
1212
{
13-
private const string TestKey = "CM1001_TEST";
14-
15-
public Registry_GetValue_str_str_obj()
16-
: base(TestKey)
17-
{
18-
}
19-
2013
[Fact]
2114
public static void NegativeTests()
2215
{
@@ -35,10 +28,10 @@ public static void NegativeTests()
3528
public void ShouldReturnNull()
3629
{
3730
// Passing null object as default object to return shouldn't throw
38-
Assert.Null(Registry.GetValue(_testRegistryKey.Name, "xzy", defaultValue: null));
31+
Assert.Null(Registry.GetValue(TestRegistryKey.Name, "xzy", defaultValue: null));
3932

4033
// If the key does not exists, then the method should return null all time
41-
Assert.Null(Registry.GetValue(_testRegistryKey.Name + "\\XYZ", null, -1));
34+
Assert.Null(Registry.GetValue(TestRegistryKey.Name + "\\XYZ", null, -1));
4235
}
4336

4437
public static IEnumerable<object[]> TestValueTypes { get { return TestData.TestValueTypes; } }
@@ -47,9 +40,9 @@ public void ShouldReturnNull()
4740
[MemberData("TestValueTypes")]
4841
public void TestGetValueWithValueTypes(string valueName, object testValue)
4942
{
50-
_testRegistryKey.SetValue(valueName, testValue);
51-
Assert.Equal(testValue.ToString(), Registry.GetValue(_testRegistryKey.Name, valueName, null).ToString());
52-
_testRegistryKey.DeleteValue(valueName);
43+
TestRegistryKey.SetValue(valueName, testValue);
44+
Assert.Equal(testValue.ToString(), Registry.GetValue(TestRegistryKey.Name, valueName, null).ToString());
45+
TestRegistryKey.DeleteValue(valueName);
5346
}
5447

5548
public static IEnumerable<object[]> TestRegistryKeys

src/Microsoft.Win32.Registry/tests/Registry/Registry_SetValue_str_str_obj.cs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,23 @@
77

88
namespace Microsoft.Win32.RegistryTests
99
{
10-
public class Registry_SetValue_str_str_obj : TestSubKey
10+
public class Registry_SetValue_str_str_obj : RegistryTestsBase
1111
{
12-
private const string TestKey = "CM2001_TEST";
13-
14-
public Registry_SetValue_str_str_obj()
15-
: base(TestKey)
16-
{
17-
}
18-
1912
[Fact]
2013
public void Test01()
2114
{
2215
// [] Passing in null should throw ArgumentNullException
2316
//UPDATE: This sets the default value. We should move this test to a newly defined reg key so as not to screw up the system
2417
const string expected = "This is a test";
25-
Registry.SetValue(_testRegistryKey.Name, null, expected);
26-
Assert.Equal(expected, _testRegistryKey.GetValue(null));
18+
Registry.SetValue(TestRegistryKey.Name, null, expected);
19+
Assert.Equal(expected, TestRegistryKey.GetValue(null));
2720
}
2821

2922
[Fact]
3023
public void NegativeTests()
3124
{
3225
// Should throw if passed value is null
33-
Assert.Throws<ArgumentNullException>(() => Registry.SetValue(_testRegistryKey.Name, "test", value: null));
26+
Assert.Throws<ArgumentNullException>(() => Registry.SetValue(TestRegistryKey.Name, "test", value: null));
3427

3528
// Should throw if passed keyName is null
3629
Assert.Throws<ArgumentNullException>(() => Registry.SetValue(keyName: null, valueName: "test", value: "test"));
@@ -43,14 +36,14 @@ public void NegativeTests()
4336

4437
// Should throw if key length above 255 characters but prior to V4, the limit is 16383
4538
const int maxValueNameLength = 16383;
46-
Assert.Throws<ArgumentException>(() => Registry.SetValue(_testRegistryKey.Name, new string('a', maxValueNameLength + 1), 5));
39+
Assert.Throws<ArgumentException>(() => Registry.SetValue(TestRegistryKey.Name, new string('a', maxValueNameLength + 1), 5));
4740

4841
// Should throw if passed value is array with uninitialized elements
49-
Assert.Throws<ArgumentException>(() => Registry.SetValue(_testRegistryKey.Name, "StringArr", value: new string[1]));
42+
Assert.Throws<ArgumentException>(() => Registry.SetValue(TestRegistryKey.Name, "StringArr", value: new string[1]));
5043

5144
// Should throw because only String[] (REG_MULTI_SZ) and byte[] (REG_BINARY) are supported.
5245
// RegistryKey.SetValue does not support arrays of type UInt32[].
53-
Assert.Throws<ArgumentException>(() => Registry.SetValue(_testRegistryKey.Name, "IntArray", new[] { 1, 2, 3 }));
46+
Assert.Throws<ArgumentException>(() => Registry.SetValue(TestRegistryKey.Name, "IntArray", new[] { 1, 2, 3 }));
5447
}
5548

5649
public static IEnumerable<object[]> TestValueTypes { get { return TestData.TestValueTypes; } }
@@ -59,9 +52,9 @@ public void NegativeTests()
5952
[MemberData("TestValueTypes")]
6053
public void SetValueWithValueTypes(string valueName, object testValue)
6154
{
62-
Registry.SetValue(_testRegistryKey.Name, valueName, testValue);
63-
Assert.Equal(testValue.ToString(), _testRegistryKey.GetValue(valueName).ToString());
64-
_testRegistryKey.DeleteValue(valueName);
55+
Registry.SetValue(TestRegistryKey.Name, valueName, testValue);
56+
Assert.Equal(testValue.ToString(), TestRegistryKey.GetValue(valueName).ToString());
57+
TestRegistryKey.DeleteValue(valueName);
6558
}
6659

6760
[Fact]
@@ -70,9 +63,9 @@ public void SetValueWithInt32()
7063
const string testValueName = "Int32";
7164
const int expected = -5;
7265

73-
Registry.SetValue(_testRegistryKey.Name, testValueName, expected);
74-
Assert.Equal(expected, (int)_testRegistryKey.GetValue(testValueName));
75-
_testRegistryKey.DeleteValue(testValueName);
66+
Registry.SetValue(TestRegistryKey.Name, testValueName, expected);
67+
Assert.Equal(expected, (int)TestRegistryKey.GetValue(testValueName));
68+
TestRegistryKey.DeleteValue(testValueName);
7669
}
7770

7871
[Fact]
@@ -82,9 +75,9 @@ public void SetValueWithUInt64()
8275
const string testValueName = "UInt64";
8376
const ulong expected = ulong.MaxValue;
8477

85-
Registry.SetValue(_testRegistryKey.Name, testValueName, expected);
86-
Assert.Equal(expected, Convert.ToUInt64(_testRegistryKey.GetValue(testValueName)));
87-
_testRegistryKey.DeleteValue(testValueName);
78+
Registry.SetValue(TestRegistryKey.Name, testValueName, expected);
79+
Assert.Equal(expected, Convert.ToUInt64(TestRegistryKey.GetValue(testValueName)));
80+
TestRegistryKey.DeleteValue(testValueName);
8881
}
8982

9083
[Fact]
@@ -94,9 +87,9 @@ public void SetValueWithByteArray()
9487
const string testValueName = "UBArr";
9588
byte[] expected = { 1, 2, 3 };
9689

97-
Registry.SetValue(_testRegistryKey.Name, testValueName, expected);
98-
Assert.Equal(expected, (byte[])_testRegistryKey.GetValue(testValueName));
99-
_testRegistryKey.DeleteValue(testValueName);
90+
Registry.SetValue(TestRegistryKey.Name, testValueName, expected);
91+
Assert.Equal(expected, (byte[])TestRegistryKey.GetValue(testValueName));
92+
TestRegistryKey.DeleteValue(testValueName);
10093
}
10194

10295
[Fact]
@@ -111,9 +104,9 @@ public void SetValueWithMultiString()
111104
"lot of things. one of which"
112105
};
113106

114-
Registry.SetValue(_testRegistryKey.Name, testValueName, expected);
115-
Assert.Equal(expected, (string[])_testRegistryKey.GetValue(testValueName));
116-
_testRegistryKey.DeleteValue(testValueName);
107+
Registry.SetValue(TestRegistryKey.Name, testValueName, expected);
108+
Assert.Equal(expected, (string[])TestRegistryKey.GetValue(testValueName));
109+
TestRegistryKey.DeleteValue(testValueName);
117110
}
118111

119112
public static IEnumerable<object[]> TestEnvironment { get { return TestData.TestEnvironment; } }
@@ -123,12 +116,12 @@ public void SetValueWithMultiString()
123116
public void SetValueWithEnvironmentVariable(string valueName, string envVariableName, string expectedVariableValue)
124117
{
125118
string value = "%" + envVariableName + "%";
126-
Registry.SetValue(_testRegistryKey.Name, valueName, value);
119+
Registry.SetValue(TestRegistryKey.Name, valueName, value);
127120

128-
string result = (string)_testRegistryKey.GetValue(valueName);
121+
string result = (string)TestRegistryKey.GetValue(valueName);
129122
//we don't expand for the user, REG_SZ_EXPAND not supported
130123
Assert.Equal(expectedVariableValue, Environment.ExpandEnvironmentVariables(result));
131-
_testRegistryKey.DeleteValue(valueName);
124+
TestRegistryKey.DeleteValue(valueName);
132125
}
133126

134127
[Fact]
@@ -138,9 +131,9 @@ public void SetValueWithEmptyString()
138131
const string testValueName = "test_122018";
139132
string expected = string.Empty;
140133

141-
Registry.SetValue(_testRegistryKey.Name, testValueName, expected);
142-
Assert.Equal(expected, (string)_testRegistryKey.GetValue(testValueName));
143-
_testRegistryKey.DeleteValue(testValueName);
134+
Registry.SetValue(TestRegistryKey.Name, testValueName, expected);
135+
Assert.Equal(expected, (string)TestRegistryKey.GetValue(testValueName));
136+
TestRegistryKey.DeleteValue(testValueName);
144137
}
145138
}
146139
}

0 commit comments

Comments
 (0)