advapi32: More tests for RegDeleteTreeA.
[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 "ntsecapi.h"
30 #include "sddl.h"
31 #include "winnls.h"
32 #include "objbase.h"
33 #define INITGUID
34 #include "guiddef.h"
35 #include "wine/test.h"
36
37 static HMODULE hadvapi32;
38 static NTSTATUS (WINAPI *pLsaClose)(LSA_HANDLE);
39 static NTSTATUS (WINAPI *pLsaFreeMemory)(PVOID);
40 static NTSTATUS (WINAPI *pLsaOpenPolicy)(PLSA_UNICODE_STRING,PLSA_OBJECT_ATTRIBUTES,ACCESS_MASK,PLSA_HANDLE);
41 static NTSTATUS (WINAPI *pLsaQueryInformationPolicy)(LSA_HANDLE,POLICY_INFORMATION_CLASS,PVOID*);
42 static BOOL     (WINAPI *pConvertSidToStringSidA)(PSID pSid, LPSTR *str);
43
44 static BOOL init(void)
45 {
46     hadvapi32 = GetModuleHandle("advapi32.dll");
47
48     pLsaClose = (void*)GetProcAddress(hadvapi32, "LsaClose");
49     pLsaFreeMemory = (void*)GetProcAddress(hadvapi32, "LsaFreeMemory");
50     pLsaOpenPolicy = (void*)GetProcAddress(hadvapi32, "LsaOpenPolicy");
51     pLsaQueryInformationPolicy = (void*)GetProcAddress(hadvapi32, "LsaQueryInformationPolicy");
52     pConvertSidToStringSidA = (void*)GetProcAddress(hadvapi32, "ConvertSidToStringSidA");
53
54     if (pLsaClose && pLsaFreeMemory && pLsaOpenPolicy && pLsaQueryInformationPolicy && pConvertSidToStringSidA)
55         return TRUE;
56
57     return FALSE;
58 }
59
60 static void test_lsa(void)
61 {
62     NTSTATUS status;
63     LSA_HANDLE handle;
64     LSA_OBJECT_ATTRIBUTES object_attributes;
65
66     ZeroMemory(&object_attributes, sizeof(object_attributes));
67     object_attributes.Length = sizeof(object_attributes);
68
69     status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
70     ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
71        "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
72
73     /* try a more restricted access mask if necessary */
74     if (status == STATUS_ACCESS_DENIED) {
75         trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
76         status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
77         ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
78     }
79
80     if (status == STATUS_SUCCESS) {
81         PPOLICY_AUDIT_EVENTS_INFO audit_events_info;
82         PPOLICY_PRIMARY_DOMAIN_INFO primary_domain_info;
83         PPOLICY_ACCOUNT_DOMAIN_INFO account_domain_info;
84         PPOLICY_DNS_DOMAIN_INFO dns_domain_info;
85
86         status = pLsaQueryInformationPolicy(handle, PolicyAuditEventsInformation, (PVOID*)&audit_events_info);
87         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAuditEventsInformation) failed, returned 0x%08x\n", status);
88         if (status == STATUS_SUCCESS) {
89             pLsaFreeMemory((LPVOID)audit_events_info);
90         }
91
92         status = pLsaQueryInformationPolicy(handle, PolicyPrimaryDomainInformation, (PVOID*)&primary_domain_info);
93         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyPrimaryDomainInformation) failed, returned 0x%08x\n", status);
94         if (status == STATUS_SUCCESS) {
95             ok(primary_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
96             if (primary_domain_info->Sid) {
97                 LPSTR strsid;
98                 if (pConvertSidToStringSidA(primary_domain_info->Sid, &strsid))
99                 {
100                     if (primary_domain_info->Name.Buffer) {
101                         LPSTR name = NULL;
102                         UINT len;
103                         len = WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
104                         name = LocalAlloc( 0, len );
105                         WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
106                         trace("  name: %s sid: %s\n", name, strsid);
107                         LocalFree( name );
108                     } else
109                         trace("  name: NULL sid: %s\n", strsid);
110                     LocalFree( strsid );
111                 }
112                 else
113                     trace("invalid sid\n");
114             }
115             pLsaFreeMemory((LPVOID)primary_domain_info);
116         }
117
118         status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&account_domain_info);
119         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) failed, returned 0x%08x\n", status);
120         if (status == STATUS_SUCCESS) {
121             pLsaFreeMemory((LPVOID)account_domain_info);
122         }
123
124         /* This isn't supported in NT4 */
125         status = pLsaQueryInformationPolicy(handle, PolicyDnsDomainInformation, (PVOID*)&dns_domain_info);
126         ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER,
127            "LsaQueryInformationPolicy(PolicyDnsDomainInformation) failed, returned 0x%08x\n", status);
128         if (status == STATUS_SUCCESS) {
129             ok(IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL), "DomainGUID should be GUID_NULL on local computer\n");
130             ok(dns_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
131             if (dns_domain_info->Sid || !IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL)) {
132                 LPSTR strsid = NULL;
133                 LPSTR name = NULL;
134                 LPSTR domain = NULL;
135                 LPSTR forest = NULL;
136                 LPSTR guidstr = NULL;
137                 WCHAR guidstrW[64];
138                 UINT len;
139                 guidstrW[0] = '\0';
140                 pConvertSidToStringSidA(dns_domain_info->Sid, &strsid);
141                 StringFromGUID2(&dns_domain_info->DomainGuid, guidstrW, sizeof(guidstrW)/sizeof(WCHAR));
142                 len = WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, NULL, 0, NULL, NULL );
143                 guidstr = LocalAlloc( 0, len );
144                 WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, guidstr, len, NULL, NULL );
145                 if (dns_domain_info->Name.Buffer) {
146                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
147                     name = LocalAlloc( 0, len );
148                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
149                 }
150                 if (dns_domain_info->DnsDomainName.Buffer) {
151                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, NULL, 0, NULL, NULL );
152                     domain = LocalAlloc( 0, len );
153                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, domain, len, NULL, NULL );
154                 }
155                 if (dns_domain_info->DnsForestName.Buffer) {
156                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, NULL, 0, NULL, NULL );
157                     forest = LocalAlloc( 0, len );
158                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, forest, len, NULL, NULL );
159                 }
160                 trace("  name: %s domain: %s forest: %s guid: %s sid: %s\n",
161                       name ? name : "NULL", domain ? domain : "NULL",
162                       forest ? forest : "NULL", guidstr, strsid ? strsid : "NULL");
163                 LocalFree( name );
164                 LocalFree( forest );
165                 LocalFree( domain );
166                 LocalFree( guidstr );
167                 LocalFree( strsid );
168             }
169             pLsaFreeMemory((LPVOID)dns_domain_info);
170         }
171
172         status = pLsaClose(handle);
173         ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
174     }
175 }
176
177 START_TEST(lsa)
178 {
179     if (!init()) {
180         skip("Needed functions are not available\n");
181         return;
182     }
183
184     test_lsa();
185 }