janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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 #include "sddl.h"
32 #include "winnls.h"
33 #include "objbase.h"
34 #define INITGUID
35 #include "guiddef.h"
36 #include "wine/test.h"
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     if (hadvapi32) {
50         pLsaClose = (void*)GetProcAddress(hadvapi32, "LsaClose");
51         pLsaFreeMemory = (void*)GetProcAddress(hadvapi32, "LsaFreeMemory");
52         pLsaOpenPolicy = (void*)GetProcAddress(hadvapi32, "LsaOpenPolicy");
53         pLsaQueryInformationPolicy = (void*)GetProcAddress(hadvapi32, "LsaQueryInformationPolicy");
54         pConvertSidToStringSidA = (void*)GetProcAddress(hadvapi32, "ConvertSidToStringSidA");
55
56         if (pLsaClose && pLsaFreeMemory && pLsaOpenPolicy && pLsaQueryInformationPolicy && pConvertSidToStringSidA)
57             return TRUE;
58     }
59
60     return FALSE;
61 }
62
63 static void test_lsa(void)
64 {
65     NTSTATUS status;
66     LSA_HANDLE handle;
67     LSA_OBJECT_ATTRIBUTES object_attributes;
68
69     ZeroMemory(&object_attributes, sizeof(object_attributes));
70     object_attributes.Length = sizeof(object_attributes);
71
72     status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_ALL_ACCESS, &handle);
73     ok(status == STATUS_SUCCESS || status == STATUS_ACCESS_DENIED,
74        "LsaOpenPolicy(POLICY_ALL_ACCESS) returned 0x%08x\n", status);
75
76     /* try a more restricted access mask if necessary */
77     if (status == STATUS_ACCESS_DENIED) {
78         trace("LsaOpenPolicy(POLICY_ALL_ACCESS) failed, trying POLICY_VIEW_LOCAL_INFORMATION\n");
79         status = pLsaOpenPolicy( NULL, &object_attributes, POLICY_VIEW_LOCAL_INFORMATION, &handle);
80         ok(status == STATUS_SUCCESS, "LsaOpenPolicy(POLICY_VIEW_LOCAL_INFORMATION) returned 0x%08x\n", status);
81     }
82
83     if (status == STATUS_SUCCESS) {
84         PPOLICY_AUDIT_EVENTS_INFO audit_events_info;
85         PPOLICY_PRIMARY_DOMAIN_INFO primary_domain_info;
86         PPOLICY_ACCOUNT_DOMAIN_INFO account_domain_info;
87         PPOLICY_DNS_DOMAIN_INFO dns_domain_info;
88
89         status = pLsaQueryInformationPolicy(handle, PolicyAuditEventsInformation, (PVOID*)&audit_events_info);
90         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAuditEventsInformation) failed, returned 0x%08x\n", status);
91         if (status == STATUS_SUCCESS) {
92             pLsaFreeMemory((LPVOID)audit_events_info);
93         }
94
95         status = pLsaQueryInformationPolicy(handle, PolicyPrimaryDomainInformation, (PVOID*)&primary_domain_info);
96         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyPrimaryDomainInformation) failed, returned 0x%08x\n", status);
97         if (status == STATUS_SUCCESS) {
98             ok(primary_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
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             pLsaFreeMemory((LPVOID)primary_domain_info);
119         }
120
121         status = pLsaQueryInformationPolicy(handle, PolicyAccountDomainInformation, (PVOID*)&account_domain_info);
122         ok(status == STATUS_SUCCESS, "LsaQueryInformationPolicy(PolicyAccountDomainInformation) failed, returned 0x%08x\n", status);
123         if (status == STATUS_SUCCESS) {
124             pLsaFreeMemory((LPVOID)account_domain_info);
125         }
126
127         /* This isn't supported in NT4 */
128         status = pLsaQueryInformationPolicy(handle, PolicyDnsDomainInformation, (PVOID*)&dns_domain_info);
129         ok(status == STATUS_SUCCESS || status == STATUS_INVALID_PARAMETER,
130            "LsaQueryInformationPolicy(PolicyDnsDomainInformation) failed, returned 0x%08x\n", status);
131         if (status == STATUS_SUCCESS) {
132             ok(IsEqualGUID(&dns_domain_info->DomainGuid, &GUID_NULL), "DomainGUID should be GUID_NULL on local computer\n");
133             ok(dns_domain_info->Sid==0,"Sid should be NULL on the local computer\n");
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             pLsaFreeMemory((LPVOID)dns_domain_info);
173         }
174
175         status = pLsaClose(handle);
176         ok(status == STATUS_SUCCESS, "LsaClose() failed, returned 0x%08x\n", status);
177     }
178 }
179
180 START_TEST(lsa)
181 {
182     if (!init())
183         return;
184
185     test_lsa();
186 }