Debian Codegraph @

external/corefx/src/Microsoft.Win32.Registry/tests/RegistryKey/RegistryKey_CreateSubKey_str.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System;
6 using System.IO;
7 using System.Linq;
8 using Xunit;
9
10 namespace Microsoft.Win32.RegistryTests
11 {
12 public class RegistryKey_CreateSubKey_str : RegistryKeyCreateSubKeyTestsBase
13 {
14 [Fact]
15 public void NegativeTests()
16 {
17 // Should throw if passed subkey name is null
18 Assert.Throws<ArgumentNullException>(() => TestRegistryKey.CreateSubKey(null));
19
20 // Should throw if key length above 255 characters
21 const int maxValueNameLength = 255;
22 AssertExtensions.Throws<ArgumentException>("name", null, () => TestRegistryKey.CreateSubKey(new string('a', maxValueNameLength + 1)));
23
24 // Should throw if RegistryKey is readonly
25 const string name = "FooBar";
26 TestRegistryKey.SetValue(name, 42);
27 using (var rk = Registry.CurrentUser.CreateSubKey(TestRegistryKeyName, writable: false))
28 {
29 Assert.Throws<UnauthorizedAccessException>(() => rk.CreateSubKey(name));
30 Assert.Throws<UnauthorizedAccessException>(() => rk.SetValue(name, "String"));
31 Assert.Throws<UnauthorizedAccessException>(() => rk.DeleteValue(name));
32 Assert.Throws<UnauthorizedAccessException>(() => rk.DeleteSubKey(name));
33 Assert.Throws<UnauthorizedAccessException>(() => rk.DeleteSubKeyTree(name));
34 }
35
36 // Should throw if RegistryKey closed
37 Assert.Throws<ObjectDisposedException>(() =>
38 {
39 TestRegistryKey.Dispose();
40 TestRegistryKey.CreateSubKey(TestRegistryKeyName);
41 });
42 }
43
44 [Fact]
45 public void CreateSubkeyWithEmptyName()
46 {
47 string expectedName = TestRegistryKey.Name + @"\";
48 var rk = TestRegistryKey.CreateSubKey(string.Empty);
49 Assert.NotNull(rk);
50 Assert.Equal(expectedName, rk.Name);
51 }
52
53 [Fact]
54 public void CreateSubKeyAndCheckThatItExists()
55 {
56 TestRegistryKey.CreateSubKey(TestRegistryKeyName);
57 Assert.NotNull(TestRegistryKey.OpenSubKey(TestRegistryKeyName));
58 Assert.Equal(expected: 1, actual:TestRegistryKey.SubKeyCount);
59 }
60
61 [Fact]
62 public void CreateSubKeyShouldOpenExisting()
63 {
64 // CreateSubKey should open subkey if it already exists
65 Assert.NotNull(TestRegistryKey.CreateSubKey(TestRegistryKeyName));
66 Assert.NotNull(TestRegistryKey.OpenSubKey(TestRegistryKeyName));
67 Assert.NotNull(TestRegistryKey.CreateSubKey(TestRegistryKeyName));
68 }
69
70 [Theory]
71 [InlineData("Dyalog APL/W 10.0")]
72 [InlineData(@"a\b\c\d\e\f\g\h")]
73 [InlineData(@"a\b\c\/d\//e\f\g\h\//\\")]
74 public void CreateSubKeyWithName(string subkeyName)
75 {
76 TestRegistryKey.CreateSubKey(subkeyName);
77 Assert.NotNull(TestRegistryKey.OpenSubKey(subkeyName));
78 }
79
80 [Fact]
81 public void DeepTest()
82 {
83 //[] how deep can we go with this
84
85 string subkeyName = string.Empty;
86
87 // Changed the number of times we repeat str1 from 100 to 30 in response to the Windows OS
88 //There is a restriction of 255 characters for the keyname even if it is multikeys. Not worth to pursue as a bug
89 // reduced further to allow for WoW64 changes to the string.
90 for (int i = 0; i < 25 && subkeyName.Length < 230; i++)
91 subkeyName = subkeyName + i.ToString() + @"\";
92
93 TestRegistryKey.CreateSubKey(subkeyName);
94 Assert.NotNull(TestRegistryKey.OpenSubKey(subkeyName));
95
96 //However, we are interested in ensuring that there are no buffer overflow issues with a deeply nested keys
97 RegistryKey rk = TestRegistryKey;
98 for (int i = 0; i < 3; i++)
99 {
100 rk = rk.OpenSubKey(subkeyName, true);
101 Assert.NotNull(rk);
102 rk.CreateSubKey(subkeyName);
103 }
104 }
105
106 [Theory]
107 [MemberData(nameof(TestRegistrySubKeyNames))]
108 public void CreateSubKey_KeyExists_OpensKeyWithFixedUpName(string expected, string subKeyName) =>
109 Verify_CreateSubKey_KeyExists_OpensKeyWithFixedUpName(expected, () => TestRegistryKey.CreateSubKey(subKeyName));
110
111 [Theory]
112 [MemberData(nameof(TestRegistrySubKeyNames))]
113 public void CreateSubKey_KeyDoesNotExist_CreatesKeyWithFixedUpName(string expected, string subKeyName) =>
114 Verify_CreateSubKey_KeyDoesNotExist_CreatesKeyWithFixedUpName(expected, () => TestRegistryKey.CreateSubKey(subKeyName));
115 }
116 }