Debian Codegraph @

external/corefx/src/Microsoft.Win32.Registry/tests/RegistryKey/SafeRegistryHandle.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 Microsoft.Win32.SafeHandles;
8 using Xunit;
9
10 namespace Microsoft.Win32.RegistryTests
11 {
12 public class SafeRegistryHandleTests : RegistryTestsBase
13 {
14 [Fact]
15 public void NegativeTests()
16 {
17 // null handle
18 Assert.Throws<ArgumentNullException>(() => RegistryKey.FromHandle(handle: null, view: RegistryView.Default));
19
20 // invalid view
21 AssertExtensions.Throws<ArgumentException>("view", () => RegistryKey.FromHandle(TestRegistryKey.Handle, (RegistryView)(-1)));
22 AssertExtensions.Throws<ArgumentException>("view", () => RegistryKey.FromHandle(TestRegistryKey.Handle, (RegistryView)3));
23
24 // get handle of disposed RegistryKey
25 Assert.Throws<ObjectDisposedException>(() =>
26 {
27 TestRegistryKey.Dispose();
28 return TestRegistryKey.Handle;
29 });
30
31 }
32
33 [Fact]
34 public void TestDeletedRegistryKey()
35 {
36 const string subKeyName = "TestKeyThatWillBeDeleted";
37 //Try getting registrykey from handle for key that has been deleted.
38 Assert.Throws<IOException>(() =>
39 {
40 RegistryKey rk = TestRegistryKey.CreateSubKey(subKeyName);
41 SafeRegistryHandle handle = rk.Handle;
42 TestRegistryKey.DeleteSubKey(subKeyName);
43 rk = RegistryKey.FromHandle(handle, RegistryView.Default);
44 rk.CreateSubKey("TestThrows");
45 });
46
47 //Try getting handle on deleted key.
48 Assert.Throws<IOException>(() =>
49 {
50 RegistryKey rk = TestRegistryKey.CreateSubKey(subKeyName);
51 TestRegistryKey.DeleteSubKey(subKeyName);
52 SafeRegistryHandle handle = rk.Handle;
53 rk = RegistryKey.FromHandle(handle, RegistryView.Default);
54 rk.CreateSubKey("TestThrows");
55 });
56 }
57 }
58 }