[NTDLL_APITESTS]
[reactos.git] / rostests / apitests / ntdll / NtSaveKey.c
1 /*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for NtSaveKey
5 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <ndk/rtlfuncs.h>
12 #include <ndk/cmfuncs.h>
13 #include <ndk/obfuncs.h>
14 #include <ndk/setypes.h>
15
16 static
17 NTSTATUS
18 OpenRegistryKeyHandle(PHANDLE KeyHandle,
19 ACCESS_MASK AccessMask,
20 PWCHAR RegistryPath)
21 {
22 UNICODE_STRING KeyName;
23 OBJECT_ATTRIBUTES Attributes;
24
25 RtlInitUnicodeString(&KeyName, RegistryPath);
26 InitializeObjectAttributes(&Attributes,
27 &KeyName,
28 OBJ_CASE_INSENSITIVE,
29 NULL,
30 NULL);
31
32 return NtOpenKey(KeyHandle, AccessMask, &Attributes);
33 }
34
35 START_TEST(NtSaveKey)
36 {
37 NTSTATUS Status;
38 HANDLE KeyHandle;
39 HANDLE FileHandle;
40 BOOLEAN OldPrivilegeStatus;
41
42 /* Open the file */
43 FileHandle = CreateFileW(L"saved_key.dat",
44 GENERIC_READ | GENERIC_WRITE,
45 0,
46 NULL,
47 CREATE_ALWAYS,
48 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
49 NULL);
50 if (FileHandle == INVALID_HANDLE_VALUE)
51 {
52 skip("CreateFileW failed with error: %lu\n", GetLastError());
53 return;
54 }
55
56 /* Try saving HKEY_LOCAL_MACHINE\Hardware */
57 Status = OpenRegistryKeyHandle(&KeyHandle, KEY_READ, L"\\Registry\\Machine\\Hardware");
58 if (!NT_SUCCESS(Status))
59 {
60 skip("NtOpenKey failed with status: 0x%08lX\n", Status);
61 NtClose(FileHandle);
62 return;
63 }
64
65 Status = NtSaveKey(KeyHandle, FileHandle);
66 ok_ntstatus(Status, STATUS_PRIVILEGE_NOT_HELD);
67
68 NtClose(KeyHandle);
69
70 /* Set the SeBackupPrivilege */
71 Status = RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE,
72 TRUE,
73 FALSE,
74 &OldPrivilegeStatus);
75 if (!NT_SUCCESS(Status))
76 {
77 skip("RtlAdjustPrivilege failed with status: 0x%08lX\n", (ULONG)Status);
78 NtClose(FileHandle);
79 return;
80 }
81
82 /* Try saving HKEY_LOCAL_MACHINE\Hardware again */
83 Status = OpenRegistryKeyHandle(&KeyHandle, KEY_READ, L"\\Registry\\Machine\\Hardware");
84 if (!NT_SUCCESS(Status))
85 {
86 skip("NtOpenKey failed with status: 0x%08lX\n", Status);
87 goto Cleanup;
88 }
89
90 Status = NtSaveKey(KeyHandle, FileHandle);
91 ok_ntstatus(Status, STATUS_SUCCESS);
92
93 NtClose(KeyHandle);
94
95 /* Try saving HKEY_LOCAL_MACHINE */
96 Status = OpenRegistryKeyHandle(&KeyHandle, KEY_READ, L"\\Registry\\Machine");
97 if (!NT_SUCCESS(Status))
98 {
99 skip("NtOpenKey failed with status: 0x%08lX\n", Status);
100 goto Cleanup;
101 }
102
103 Status = NtSaveKey(KeyHandle, FileHandle);
104 ok_ntstatus(Status, STATUS_ACCESS_DENIED);
105
106 NtClose(KeyHandle);
107
108 /* Try saving HKEY_USERS */
109 Status = OpenRegistryKeyHandle(&KeyHandle, KEY_READ, L"\\Registry\\User");
110 if (!NT_SUCCESS(Status))
111 {
112 skip("NtOpenKey failed with status: 0x%08lX\n", Status);
113 goto Cleanup;
114 }
115
116 Status = NtSaveKey(KeyHandle, FileHandle);
117 ok_ntstatus(Status, STATUS_ACCESS_DENIED);
118
119 NtClose(KeyHandle);
120
121 Cleanup:
122
123 /* Restore the SeBackupPrivilege */
124 RtlAdjustPrivilege(SE_BACKUP_PRIVILEGE,
125 OldPrivilegeStatus,
126 FALSE,
127 &OldPrivilegeStatus);
128
129 /* Close the file handle */
130 NtClose(FileHandle);
131 }