kernel32: GlobalMemoryStatusEx: return the size of physical memory + swapsize in...
[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     if (hadvapi32) {
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
59     return FALSE;
60 }
61
62 static void test_lsa(void)
63 {
64     NTSTATUS status;
65     LSA_HANDLE handle;
66     LSA_OBJECT_ATTRIBUTES object_attributes;
67
68     ZeroMemory(&object_attributes, sizeof(object_attributes));
69     object_attributes.Length = sizeof(object_attributes);
70
71     status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
72     ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
73        "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
74
75     /* try a more restricted access mask if necessary */
76     if (status == STATUS_ACCESS_DENIED) {
77         trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
78         status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
79         ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
80     }
81
82     if (status == STATUS_SUCCESS) {
83         PPOLICY_AUDIT_EVENTS_INFO audit_events_info;
84         PPOLICY_PRIMARY_DOMAIN_INFO primary_domain_info;
85         PPOLICY_ACCOUNT_DOMAIN_INFO account_domain_info;
86         PPOLICY_DNS_DOMAIN_INFO dns_domain_info;
87
88         status = pLsaQueryInformationPolicy(handle, PolicyAuditEventsInformation, (PVOID*)&audit_events_info);
89         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAuditEventsInformation) failed, returned 0x%08x\n", status);
90         if (status == STATUS_SUCCESS) {
91             pLsaFreeMemory((LPVOID)audit_events_info);
92         }
93
94         status = pLsaQueryInformationPolicy(handle, PolicyPrimaryDomainInformation, (PVOID*)&primary_domain_info);
95         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyPrimaryDomainInformation) failed, returned 0x%08x\n", status);
96         if (status == STATUS_SUCCESS) {
97             ok(primary_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
98             if (primary_domain_info->Sid) {
99                 LPSTR strsid;
100                 if (pConvertSidToStringSidA(primary_domain_info->Sid, &strsid))
101                 {
102                     if (primary_domain_info->Name.Buffer) {
103                         LPSTR name = NULL;
104                         UINT len;
105                         len = WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
106                         name = LocalAlloc( 0, len );
107                         WideCharToMultiByte( CP_ACP, 0, primary_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
108                         trace("  name: %s sid: %s\n", name, strsid);
109                         LocalFree( name );
110                     } else
111                         trace("  name: NULL sid: %s\n", strsid);
112                     LocalFree( strsid );
113                 }
114                 else
115                     trace("invalid sid\n");
116             }
117             pLsaFreeMemory((LPVOID)primary_domain_info);
118         }
119
120         status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&account_domain_info);
121         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) failed, returned 0x%08x\n", status);
122         if (status == STATUS_SUCCESS) {
123             pLsaFreeMemory((LPVOID)account_domain_info);
124         }
125
126         /* This isn't supported in NT4 */
127         status = pLsaQueryInformationPolicy(handle, PolicyDnsDomainInformation, (PVOID*)&dns_domain_info);
128         ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER,
129            "LsaQueryInformationPolicy(PolicyDnsDomainInformation) failed, returned 0x%08x\n", status);
130         if (status == STATUS_SUCCESS) {
131             ok(IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL), "DomainGUID should be GUID_NULL on local computer\n");
132             ok(dns_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
133             if (dns_domain_info->Sid || !IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL)) {
134                 LPSTR strsid = NULL;
135                 LPSTR name = NULL;
136                 LPSTR domain = NULL;
137                 LPSTR forest = NULL;
138                 LPSTR guidstr = NULL;
139                 WCHAR guidstrW[64];
140                 UINT len;
141                 guidstrW[0] = '\0';
142                 pConvertSidToStringSidA(dns_domain_info->Sid, &strsid);
143                 StringFromGUID2(&dns_domain_info->DomainGuid, guidstrW, sizeof(guidstrW)/sizeof(WCHAR));
144                 len = WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, NULL, 0, NULL, NULL );
145                 guidstr = LocalAlloc( 0, len );
146                 WideCharToMultiByte( CP_ACP, 0, guidstrW, -1, guidstr, len, NULL, NULL );
147                 if (dns_domain_info->Name.Buffer) {
148                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, NULL, 0, NULL, NULL );
149                     name = LocalAlloc( 0, len );
150                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->Name.Buffer, -1, name, len, NULL, NULL );
151                 }
152                 if (dns_domain_info->DnsDomainName.Buffer) {
153                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, NULL, 0, NULL, NULL );
154                     domain = LocalAlloc( 0, len );
155                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsDomainName.Buffer, -1, domain, len, NULL, NULL );
156                 }
157                 if (dns_domain_info->DnsForestName.Buffer) {
158                     len = WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, NULL, 0, NULL, NULL );
159                     forest = LocalAlloc( 0, len );
160                     WideCharToMultiByte( CP_ACP, 0, dns_domain_info->DnsForestName.Buffer, -1, forest, len, NULL, NULL );
161                 }
162                 trace("  name: %s domain: %s forest: %s guid: %s sid: %s\n",
163                       name ? name : "NULL", domain ? domain : "NULL",
164                       forest ? forest : "NULL", guidstr, strsid ? strsid : "NULL");
165                 LocalFree( name );
166                 LocalFree( forest );
167                 LocalFree( domain );
168                 LocalFree( guidstr );
169                 LocalFree( strsid );
170             }
171             pLsaFreeMemory((LPVOID)dns_domain_info);
172         }
173
174         status = pLsaClose(handle);
175         ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
176     }
177 }
178
179 START_TEST(lsa)
180 {
181     if (!init()) {
182         skip("Needed functions are not available\n");
183         return;
184     }
185
186     test_lsa();
187 }