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