sync netapi32_winetest with wine 1.1.34
[reactos.git] / rostests / winetests / netapi32 / access.c
1 /*
2 * Copyright 2002 Andriy Palamarchuk
3 *
4 * Conformance test of the access functions.
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
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <lmaccess.h>
27 #include <lmerr.h>
28 #include <lmapibuf.h>
29
30 #include "wine/test.h"
31
32 WCHAR user_name[UNLEN + 1];
33 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
34
35 static const WCHAR sNonexistentUser[] = {'N','o','n','e','x','i','s','t','e','n','t',' ',
36 'U','s','e','r',0};
37 static WCHAR sTooLongName[] = {'T','h','i','s',' ','i','s',' ','a',' ','b','a','d',
38 ' ','u','s','e','r','n','a','m','e',0};
39 static WCHAR sTooLongPassword[] = {'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
40 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
41 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
42 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
43 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
44 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
45 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
46 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
47 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
48 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
49 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
50 'a', 0};
51
52 static WCHAR sTestUserName[] = {'t', 'e', 's', 't', 'u', 's', 'e', 'r', 0};
53 static WCHAR sTestUserOldPass[] = {'O', 'l', 'd', 'P', 'a', 's', 's', 'W', '0', 'r', 'd', 'S', 'e', 't', '!', '~', 0};
54 static const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
55 static const WCHAR sInvalidName[] = {'\\',0};
56 static const WCHAR sInvalidName2[] = {'\\','\\',0};
57 static const WCHAR sEmptyStr[] = { 0 };
58
59 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
60 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
61 static NET_API_STATUS (WINAPI *pNetQueryDisplayInformation)(LPWSTR,DWORD,DWORD,DWORD,DWORD,LPDWORD,PVOID*)=NULL;
62 static NET_API_STATUS (WINAPI *pNetUserGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
63 static NET_API_STATUS (WINAPI *pNetUserModalsGet)(LPCWSTR,DWORD,LPBYTE*)=NULL;
64 static NET_API_STATUS (WINAPI *pNetUserAdd)(LPCWSTR,DWORD,LPBYTE,LPDWORD)=NULL;
65 static NET_API_STATUS (WINAPI *pNetUserDel)(LPCWSTR,LPCWSTR)=NULL;
66
67 static int init_access_tests(void)
68 {
69 DWORD dwSize;
70 BOOL rc;
71
72 user_name[0] = 0;
73 dwSize = sizeof(user_name)/sizeof(WCHAR);
74 rc=GetUserNameW(user_name, &dwSize);
75 if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
76 {
77 win_skip("GetUserNameW is not available.\n");
78 return 0;
79 }
80 ok(rc, "User Name Retrieved\n");
81
82 computer_name[0] = 0;
83 dwSize = sizeof(computer_name)/sizeof(WCHAR);
84 ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
85 return 1;
86 }
87
88 static NET_API_STATUS create_test_user(void)
89 {
90 USER_INFO_1 usri;
91
92 usri.usri1_name = sTestUserName;
93 usri.usri1_password = sTestUserOldPass;
94 usri.usri1_priv = USER_PRIV_USER;
95 usri.usri1_home_dir = NULL;
96 usri.usri1_comment = NULL;
97 usri.usri1_flags = UF_SCRIPT;
98 usri.usri1_script_path = NULL;
99
100 return pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
101 }
102
103 static NET_API_STATUS delete_test_user(void)
104 {
105 return pNetUserDel(NULL, sTestUserName);
106 }
107
108 static void run_usergetinfo_tests(void)
109 {
110 NET_API_STATUS rc;
111 PUSER_INFO_0 ui0 = NULL;
112 PUSER_INFO_10 ui10 = NULL;
113 DWORD dwSize;
114
115 if((rc = create_test_user()) != NERR_Success )
116 {
117 skip("Skipping usergetinfo_tests, create_test_user failed: 0x%08x\n", rc);
118 return;
119 }
120
121 /* Level 0 */
122 rc=pNetUserGetInfo(NULL, sTestUserName, 0, (LPBYTE *)&ui0);
123 ok(rc == NERR_Success, "NetUserGetInfo level 0 failed: 0x%08x.\n", rc);
124 ok(!lstrcmpW(sTestUserName, ui0->usri0_name),"Username mismatch for level 0.\n");
125 pNetApiBufferSize(ui0, &dwSize);
126 ok(dwSize >= (sizeof(USER_INFO_0) +
127 (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
128 "Is allocated with NetApiBufferAllocate\n");
129
130 /* Level 10 */
131 rc=pNetUserGetInfo(NULL, sTestUserName, 10, (LPBYTE *)&ui10);
132 ok(rc == NERR_Success, "NetUserGetInfo level 10 failed: 0x%08x.\n", rc);
133 ok(!lstrcmpW(sTestUserName, ui10->usri10_name), "Username mismatch for level 10.\n");
134 pNetApiBufferSize(ui10, &dwSize);
135 ok(dwSize >= (sizeof(USER_INFO_10) +
136 (lstrlenW(ui10->usri10_name) + 1 +
137 lstrlenW(ui10->usri10_comment) + 1 +
138 lstrlenW(ui10->usri10_usr_comment) + 1 +
139 lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
140 "Is allocated with NetApiBufferAllocate\n");
141
142 pNetApiBufferFree(ui0);
143 pNetApiBufferFree(ui10);
144
145 /* NetUserGetInfo should always work for the current user. */
146 rc=pNetUserGetInfo(NULL, user_name, 0, (LPBYTE*)&ui0);
147 ok(rc == NERR_Success, "NetUsetGetInfo for current user failed: 0x%08x.\n", rc);
148 pNetApiBufferFree(ui0);
149
150 /* errors handling */
151 rc=pNetUserGetInfo(NULL, sTestUserName, 10000, (LPBYTE *)&ui0);
152 ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%d\n",rc);
153 rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
154 ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%d\n",rc);
155 todo_wine {
156 /* FIXME - Currently Wine can't verify whether the network path is good or bad */
157 rc=pNetUserGetInfo(sBadNetPath, sTestUserName, 0, (LPBYTE *)&ui0);
158 ok(rc == ERROR_BAD_NETPATH ||
159 rc == ERROR_NETWORK_UNREACHABLE ||
160 rc == RPC_S_SERVER_UNAVAILABLE ||
161 rc == RPC_S_INVALID_NET_ADDR, /* Some Win7 */
162 "Bad Network Path: rc=%d\n",rc);
163 }
164 rc=pNetUserGetInfo(sEmptyStr, sTestUserName, 0, (LPBYTE *)&ui0);
165 ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
166 "Bad Network Path: rc=%d\n",rc);
167 rc=pNetUserGetInfo(sInvalidName, sTestUserName, 0, (LPBYTE *)&ui0);
168 ok(rc == ERROR_INVALID_NAME || rc == ERROR_INVALID_HANDLE,"Invalid Server Name: rc=%d\n",rc);
169 rc=pNetUserGetInfo(sInvalidName2, sTestUserName, 0, (LPBYTE *)&ui0);
170 ok(rc == ERROR_INVALID_NAME || rc == ERROR_INVALID_HANDLE,"Invalid Server Name: rc=%d\n",rc);
171
172 if(delete_test_user() != NERR_Success)
173 trace("Deleting the test user failed. You might have to manually delete it.\n");
174 }
175
176 /* Checks Level 1 of NetQueryDisplayInformation */
177 static void run_querydisplayinformation1_tests(void)
178 {
179 PNET_DISPLAY_USER Buffer, rec;
180 DWORD Result, EntryCount;
181 DWORD i = 0;
182 BOOL hasAdmin = FALSE;
183 BOOL hasGuest = FALSE;
184
185 do
186 {
187 Result = pNetQueryDisplayInformation(
188 NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
189 (PVOID *)&Buffer);
190
191 ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
192 "Information Retrieved\n");
193 rec = Buffer;
194 for(; EntryCount > 0; EntryCount--)
195 {
196 if (rec->usri1_user_id == DOMAIN_USER_RID_ADMIN)
197 {
198 ok(!hasAdmin, "One admin user\n");
199 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
200 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
201 hasAdmin = TRUE;
202 }
203 else if (rec->usri1_user_id == DOMAIN_USER_RID_GUEST)
204 {
205 ok(!hasGuest, "One guest record\n");
206 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
207 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
208 hasGuest = TRUE;
209 }
210
211 i = rec->usri1_next_index;
212 rec++;
213 }
214
215 pNetApiBufferFree(Buffer);
216 } while (Result == ERROR_MORE_DATA);
217
218 ok(hasAdmin, "Doesn't have 'Administrator' account\n");
219 }
220
221 static void run_usermodalsget_tests(void)
222 {
223 NET_API_STATUS rc;
224 USER_MODALS_INFO_2 * umi2 = NULL;
225
226 rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
227 ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
228
229 if (umi2)
230 pNetApiBufferFree(umi2);
231 }
232
233 static void run_userhandling_tests(void)
234 {
235 NET_API_STATUS ret;
236 USER_INFO_1 usri;
237
238 usri.usri1_priv = USER_PRIV_USER;
239 usri.usri1_home_dir = NULL;
240 usri.usri1_comment = NULL;
241 usri.usri1_flags = UF_SCRIPT;
242 usri.usri1_script_path = NULL;
243
244 usri.usri1_name = sTooLongName;
245 usri.usri1_password = sTestUserOldPass;
246
247 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
248 if (ret == NERR_Success || ret == NERR_UserExists)
249 {
250 /* Windows NT4 does create the user. Delete the user and also if it already existed
251 * due to a previous test run on NT4.
252 */
253 trace("We are on NT4, we have to delete the user with the too long username\n");
254 ret = pNetUserDel(NULL, sTooLongName);
255 ok(ret == NERR_Success, "Deleting the user failed : %d\n", ret);
256 }
257 else if (ret == ERROR_ACCESS_DENIED)
258 {
259 skip("not enough permissions to add a user\n");
260 return;
261 }
262 else
263 ok(ret == NERR_BadUsername ||
264 broken(ret == NERR_PasswordTooShort), /* NT4 */
265 "Adding user with too long username returned 0x%08x\n", ret);
266
267 usri.usri1_name = sTestUserName;
268 usri.usri1_password = sTooLongPassword;
269
270 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
271 ok(ret == NERR_PasswordTooShort || ret == ERROR_ACCESS_DENIED /* Win2003 */,
272 "Adding user with too long password returned 0x%08x\n", ret);
273
274 usri.usri1_name = sTooLongName;
275 usri.usri1_password = sTooLongPassword;
276
277 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
278 /* NT4 doesn't have a problem with the username so it will report the too long password
279 * as the error. NERR_PasswordTooShort is reported for all kind of password related errors.
280 */
281 ok(ret == NERR_BadUsername || ret == NERR_PasswordTooShort,
282 "Adding user with too long username/password returned 0x%08x\n", ret);
283
284 usri.usri1_name = sTestUserName;
285 usri.usri1_password = sTestUserOldPass;
286
287 ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
288 ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
289
290 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
291 if(ret == ERROR_ACCESS_DENIED)
292 {
293 skip("Insufficient permissions to add users. Skipping test.\n");
294 return;
295 }
296 if(ret == NERR_UserExists)
297 {
298 skip("User already exists, skipping test to not mess up the system\n");
299 return;
300 }
301
302 ok(ret == NERR_Success ||
303 broken(ret == NERR_PasswordTooShort), /* NT4 */
304 "Adding user failed with error 0x%08x\n", ret);
305 if(ret != NERR_Success)
306 return;
307
308 /* On Windows XP (and newer), calling NetUserChangePassword with a NULL
309 * domainname parameter creates a user home directory, iff the machine is
310 * not member of a domain.
311 * Using \\127.0.0.1 as domain name does not work on standalone machines
312 * either, unless the ForceGuest option in the registry is turned off.
313 * So let's not test NetUserChangePassword for now.
314 */
315
316 ret = pNetUserDel(NULL, sTestUserName);
317 ok(ret == NERR_Success, "Deleting the user failed.\n");
318
319 ret = pNetUserDel(NULL, sTestUserName);
320 ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
321 }
322
323 START_TEST(access)
324 {
325 HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
326
327 pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
328 pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
329 pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
330 pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
331 pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
332 pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
333 pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
334
335 /* These functions were introduced with NT. It's safe to assume that
336 * if one is not available, none are.
337 */
338 if (!pNetApiBufferFree) {
339 win_skip("Needed functions are not available\n");
340 FreeLibrary(hnetapi32);
341 return;
342 }
343
344 if (init_access_tests()) {
345 run_userhandling_tests();
346 run_usergetinfo_tests();
347 run_querydisplayinformation1_tests();
348 run_usermodalsget_tests();
349 }
350
351 FreeLibrary(hnetapi32);
352 }