advapi32: Set the backup/restore privilege

James Hawkins truiken at gmail.com
Mon Apr 18 17:52:48 CDT 2005


Hi,

With this patch, the RegSave/Load/Unload tests won't fail because of
insufficient privileges anymore.  This requires that the RegUnLoad
test patch is committed.

Changelog
* Set the backup/restore privilege

-- 
James Hawkins
-------------- next part --------------
--- dlls/advapi32/tests/registry.c	2005-04-18 17:32:24.927988704 -0500
+++ dlls/advapi32/tests/registry.c.temp	2005-04-18 17:43:02.015136736 -0500
@@ -366,7 +366,6 @@ static void test_reg_save_key()
     DWORD ret;
 
     ret = RegSaveKey(hkey_main, "saved_key", NULL);
-    if (ERROR_PRIVILEGE_NOT_HELD == ret) return;
     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
 }
 
@@ -376,7 +375,6 @@ static void test_reg_load_key()
     HKEY hkHandle;
 
     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
-    if (ERROR_PRIVILEGE_NOT_HELD == ret) return;
     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
 
     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
@@ -395,6 +393,40 @@ static void test_reg_unload_key()
     DeleteFile("saved_key");
 }
 
+static BOOL set_privileges(LPCSTR privilege, BOOL set)
+{
+    TOKEN_PRIVILEGES tp;
+    HANDLE hToken;
+    LUID luid;
+
+    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
+        return FALSE;
+
+    if(!LookupPrivilegeValue(NULL, privilege, &luid))
+    {
+        CloseHandle(hToken);
+        return FALSE;
+    }
+
+    tp.PrivilegeCount = 1;
+    tp.Privileges[0].Luid = luid;
+    
+    if (set)
+        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
+    else
+        tp.Privileges[0].Attributes = 0;
+
+    AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
+    if (GetLastError() != ERROR_SUCCESS)
+    {
+        CloseHandle(hToken);
+        return FALSE;
+    }
+
+    CloseHandle(hToken);
+    return TRUE;
+}
+
 START_TEST(registry)
 {
     setup_main_key();
@@ -404,9 +436,18 @@ START_TEST(registry)
     test_reg_open_key();
     test_reg_close_key();
     test_reg_delete_key();
-    test_reg_save_key();
-    test_reg_load_key();
-    test_reg_unload_key();
+
+    /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
+    if (set_privileges(SE_BACKUP_NAME, TRUE) &&
+        set_privileges(SE_RESTORE_NAME, TRUE))
+    {
+        test_reg_save_key();
+        test_reg_load_key();
+        test_reg_unload_key();
+
+        set_privileges(SE_BACKUP_NAME, FALSE);
+        set_privileges(SE_RESTORE_NAME, FALSE);
+    }
 
     /* cleanup */
     delete_key( hkey_main );


More information about the wine-patches mailing list