advapi32: Try again with different access mask if LsaOpenPolicy fails with STATUS_ACC...
[wine] / dlls / advapi32 / tests / lsa.c
1 /*
2  * Unit tests for lsa functions
3  *
4  * Copyright (c) 2006 Robert Reif
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "ntstatus.h"
25 #define WIN32_NO_STATUS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "ntsecapi.h"
31 #define INITGUID
32 #include "guiddef.h"
33 #include "wine/test.h"
34
35 static HMODULE hadvapi32;
36 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
37 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
38 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
39 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
40
41 static BOOL init(void)
42 {
43     hadvapi32 = GetModuleHandle("advapi32.dll");
44
45     if (hadvapi32) {
46         pLsaClose = (void*)GetProcAddress(hadvapi32, "LsaClose");
47         pLsaFreeMemory = (void*)GetProcAddress(hadvapi32, "LsaFreeMemory");
48         pLsaOpenPolicy = (void*)GetProcAddress(hadvapi32, "LsaOpenPolicy");
49         pLsaQueryInformationPolicy = (void*)GetProcAddress(hadvapi32, "LsaQueryInformationPolicy");
50
51         if (pLsaClose && pLsaFreeMemory && pLsaOpenPolicy && pLsaQueryInformationPolicy)
52             return TRUE;
53     }
54
55     return FALSE;
56 }
57
58 static void test_lsa(void)
59 {
60     NTSTATUS status;
61     LSA_HANDLE handle;
62     LSA_OBJECT_ATTRIBUTES object_attributes;
63
64     ZeroMemory(&object_attributes, sizeof(object_attributes));
65     object_attributes.Length = sizeof(object_attributes);
66
67     status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
68     ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
69        "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08lx\n", status);
70
71     /* try a more restricted access mask if necessary */
72     if (status == STATUS_ACCESS_DENIED) {
73         trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
74         status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
75         ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08lx\n", status);
76     }
77
78     if (status == STATUS_SUCCESS) {
79         PPOLICY_AUDIT_EVENTS_INFO audit_events_info;
80         PPOLICY_PRIMARY_DOMAIN_INFO primary_domain_info;
81         PPOLICY_ACCOUNT_DOMAIN_INFO account_domain_info;
82         PPOLICY_DNS_DOMAIN_INFO dns_domain_info;
83
84         status = pLsaQueryInformationPolicy(handle, PolicyAuditEventsInformation, (PVOID*)&audit_events_info);
85         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAuditEventsInformation) failed, returned 0x%08lx\n", status);
86         if (status == STATUS_SUCCESS) {
87             pLsaFreeMemory((LPVOID)audit_events_info);
88         }
89
90         status = pLsaQueryInformationPolicy(handle, PolicyPrimaryDomainInformation, (PVOID*)&primary_domain_info);
91         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyPrimaryDomainInformation) failed, returned 0x%08lx\n", status);
92         if (status == STATUS_SUCCESS) {
93             ok(primary_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
94             pLsaFreeMemory((LPVOID)primary_domain_info);
95         }
96
97         status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&account_domain_info);
98         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) failed, returned 0x%08lx\n", status);
99         if (status == STATUS_SUCCESS) {
100             pLsaFreeMemory((LPVOID)account_domain_info);
101         }
102
103         status = pLsaQueryInformationPolicy(handle, PolicyDnsDomainInformation, (PVOID*)&dns_domain_info);
104         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyDnsDomainInformation) failed, returned 0x%08lx\n", status);
105         if (status == STATUS_SUCCESS) {
106             ok(IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL), "DomainGUID should be GUID_NULL on local computer\n");
107             ok(dns_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
108             pLsaFreeMemory((LPVOID)dns_domain_info);
109         }
110
111         status = pLsaClose(handle);
112         ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08lx\n", status);
113     }
114 }
115
116 START_TEST(lsa)
117 {
118     if (!init())
119         return;
120
121     test_lsa();
122 }