push 7d59f97ddb2317e6350cdd96ee6bc6e63ea046f4
[wine/hacks.git] / dlls / netapi32 / tests / access.c
blobeae713f6e3bef7be14d1125ec009372a4a09a72c
1 /*
2 * Copyright 2002 Andriy Palamarchuk
4 * Conformance test of the access functions.
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.
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.
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
21 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winerror.h>
26 #include <lmaccess.h>
27 #include <lmerr.h>
28 #include <lmapibuf.h>
30 #include "wine/test.h"
32 WCHAR user_name[UNLEN + 1];
33 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
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};
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 WCHAR sTestUserNewPass[] = {'N', 'e', 'w', 'P', 'a', 's', 's', 'W', '0', 'r', 'd', 'S', 'e', 't', '!', '~', 0};
55 static const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
56 static const WCHAR sInvalidName[] = {'\\',0};
57 static const WCHAR sInvalidName2[] = {'\\','\\',0};
58 static const WCHAR sEmptyStr[] = { 0 };
60 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
61 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
62 static NET_API_STATUS (WINAPI *pNetQueryDisplayInformation)(LPWSTR,DWORD,DWORD,DWORD,DWORD,LPDWORD,PVOID*)=NULL;
63 static NET_API_STATUS (WINAPI *pNetUserGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
64 static NET_API_STATUS (WINAPI *pNetUserModalsGet)(LPCWSTR,DWORD,LPBYTE*)=NULL;
65 static NET_API_STATUS (WINAPI *pNetUserAdd)(LPCWSTR,DWORD,LPBYTE,LPDWORD)=NULL;
66 static NET_API_STATUS (WINAPI *pNetUserChangePassword)(LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR)=NULL;
67 static NET_API_STATUS (WINAPI *pNetUserDel)(LPCWSTR,LPCWSTR)=NULL;
69 static int init_access_tests(void)
71 DWORD dwSize;
72 BOOL rc;
74 user_name[0] = 0;
75 dwSize = sizeof(user_name);
76 rc=GetUserNameW(user_name, &dwSize);
77 if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
79 skip("GetUserNameW is not available.\n");
80 return 0;
82 ok(rc, "User Name Retrieved\n");
84 computer_name[0] = 0;
85 dwSize = sizeof(computer_name);
86 ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
87 return 1;
90 static NET_API_STATUS create_test_user(void)
92 USER_INFO_1 usri;
94 usri.usri1_name = sTestUserName;
95 usri.usri1_password = sTestUserOldPass;
96 usri.usri1_priv = USER_PRIV_USER;
97 usri.usri1_home_dir = NULL;
98 usri.usri1_comment = NULL;
99 usri.usri1_flags = UF_SCRIPT;
100 usri.usri1_script_path = NULL;
102 return pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
105 static NET_API_STATUS delete_test_user(void)
107 return pNetUserDel(NULL, sTestUserName);
110 static void run_usergetinfo_tests(void)
112 NET_API_STATUS rc;
113 PUSER_INFO_0 ui0 = NULL;
114 PUSER_INFO_10 ui10 = NULL;
115 DWORD dwSize;
117 if((rc = create_test_user()) != NERR_Success )
119 skip("Skipping usergetinfo_tests, create_test_user failed: 0x%08x\n", rc);
120 return;
123 /* Level 0 */
124 rc=pNetUserGetInfo(NULL, sTestUserName, 0, (LPBYTE *)&ui0);
125 ok(rc == NERR_Success, "NetUserGetInfo level 0 failed: 0x%08x.\n", rc);
126 ok(!lstrcmpW(sTestUserName, ui0->usri0_name),"Username mismatch for level 0.\n");
127 pNetApiBufferSize(ui0, &dwSize);
128 ok(dwSize >= (sizeof(USER_INFO_0) +
129 (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
130 "Is allocated with NetApiBufferAllocate\n");
132 /* Level 10 */
133 rc=pNetUserGetInfo(NULL, sTestUserName, 10, (LPBYTE *)&ui10);
134 ok(rc == NERR_Success, "NetUserGetInfo level 10 failed: 0x%08x.\n", rc);
135 ok(!lstrcmpW(sTestUserName, ui10->usri10_name), "Username mismatch for level 10.\n");
136 pNetApiBufferSize(ui10, &dwSize);
137 ok(dwSize >= (sizeof(USER_INFO_10) +
138 (lstrlenW(ui10->usri10_name) + 1 +
139 lstrlenW(ui10->usri10_comment) + 1 +
140 lstrlenW(ui10->usri10_usr_comment) + 1 +
141 lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
142 "Is allocated with NetApiBufferAllocate\n");
144 pNetApiBufferFree(ui0);
145 pNetApiBufferFree(ui10);
147 /* errors handling */
148 rc=pNetUserGetInfo(NULL, sTestUserName, 10000, (LPBYTE *)&ui0);
149 ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%d\n",rc);
150 rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
151 ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%d\n",rc);
152 todo_wine {
153 /* FIXME - Currently Wine can't verify whether the network path is good or bad */
154 rc=pNetUserGetInfo(sBadNetPath, sTestUserName, 0, (LPBYTE *)&ui0);
155 ok(rc == ERROR_BAD_NETPATH || rc == ERROR_NETWORK_UNREACHABLE,
156 "Bad Network Path: rc=%d\n",rc);
158 rc=pNetUserGetInfo(sEmptyStr, sTestUserName, 0, (LPBYTE *)&ui0);
159 ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
160 "Bad Network Path: rc=%d\n",rc);
161 rc=pNetUserGetInfo(sInvalidName, sTestUserName, 0, (LPBYTE *)&ui0);
162 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
163 rc=pNetUserGetInfo(sInvalidName2, sTestUserName, 0, (LPBYTE *)&ui0);
164 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
166 if(delete_test_user() != NERR_Success)
167 trace("Deleting the test user failed. You might have to manually delete it.\n");
170 /* checks Level 1 of NetQueryDisplayInformation
171 * FIXME: Needs to be rewritten to not depend on the spelling of the users,
172 * ideally based on the admin and guest user SIDs/RIDs.*/
173 static void run_querydisplayinformation1_tests(void)
175 PNET_DISPLAY_USER Buffer, rec;
176 DWORD Result, EntryCount;
177 DWORD i = 0;
178 BOOL hasAdmin = FALSE;
179 BOOL hasGuest = FALSE;
180 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a',
181 't','o','r',0};
182 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
186 Result = pNetQueryDisplayInformation(
187 NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
188 (PVOID *)&Buffer);
190 ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
191 "Information Retrieved\n");
192 rec = Buffer;
193 for(; EntryCount > 0; EntryCount--)
195 if (!lstrcmpW(rec->usri1_name, sAdminUserName))
197 ok(!hasAdmin, "One admin user\n");
198 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
199 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
200 hasAdmin = TRUE;
202 else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
204 ok(!hasGuest, "One guest record\n");
205 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
206 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
207 hasGuest = TRUE;
210 i = rec->usri1_next_index;
211 rec++;
214 pNetApiBufferFree(Buffer);
215 } while (Result == ERROR_MORE_DATA);
217 ok(hasAdmin, "Has Administrator account\n");
220 static void run_usermodalsget_tests(void)
222 NET_API_STATUS rc;
223 USER_MODALS_INFO_2 * umi2 = NULL;
225 rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
226 ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
228 if (umi2)
229 pNetApiBufferFree(umi2);
232 static void run_userhandling_tests(void)
234 NET_API_STATUS ret;
235 USER_INFO_1 usri;
237 usri.usri1_priv = USER_PRIV_USER;
238 usri.usri1_home_dir = NULL;
239 usri.usri1_comment = NULL;
240 usri.usri1_flags = UF_SCRIPT;
241 usri.usri1_script_path = NULL;
243 usri.usri1_name = sTooLongName;
244 usri.usri1_password = sTestUserOldPass;
246 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
247 if (ret == NERR_Success || ret == NERR_UserExists)
249 /* Windows NT4 does create the user. Delete the user and also if it already existed
250 * due to a previous test run on NT4.
252 trace("We are on NT4, we have to delete the user with the too long username\n");
253 ret = pNetUserDel(NULL, sTooLongName);
254 ok(ret == NERR_Success, "Deleting the user failed : %d\n", ret);
256 else
257 ok(ret == NERR_BadUsername, "Adding user with too long username returned 0x%08x\n", ret);
259 usri.usri1_name = sTestUserName;
260 usri.usri1_password = sTooLongPassword;
262 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
263 ok(ret == NERR_PasswordTooShort, "Adding user with too long password returned 0x%08x\n", ret);
265 usri.usri1_name = sTooLongName;
266 usri.usri1_password = sTooLongPassword;
268 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
269 /* NT4 doesn't have a problem with the username so it will report the too long password
270 * as the error. NERR_PasswordTooShort is reported for all kind of password related errors.
272 ok(ret == NERR_BadUsername || ret == NERR_PasswordTooShort,
273 "Adding user with too long username/password returned 0x%08x\n", ret);
275 usri.usri1_name = sTestUserName;
276 usri.usri1_password = sTestUserOldPass;
278 ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
279 ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
281 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
282 if(ret == ERROR_ACCESS_DENIED)
284 skip("Insufficient permissions to add users. Skipping test.\n");
285 return;
287 if(ret == NERR_UserExists)
289 skip("User already exists, skipping test to not mess up the system\n");
290 return;
293 ok(ret == NERR_Success, "Adding user failed with error 0x%08x\n", ret);
294 if(ret != NERR_Success)
295 return;
297 ret = pNetUserChangePassword(NULL, sNonexistentUser, sTestUserOldPass,
298 sTestUserNewPass);
299 ok(ret == NERR_UserNotFound || ret == ERROR_INVALID_PASSWORD ||
300 ret == ERROR_CANT_ACCESS_DOMAIN_INFO,
301 "Changing password for nonexistent user returned 0x%08x.\n", ret);
303 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
304 sTestUserOldPass);
305 /* Apparently NERR_PasswordTooShort can be returned on windows xp if a
306 * strict password policy is enforced
308 ok(ret == NERR_Success || ret == NERR_PasswordTooShort ||
309 ret == ERROR_CANT_ACCESS_DOMAIN_INFO || ret == ERROR_INVALID_PASSWORD,
310 "Changing old password to old password returned 0x%08x.\n", ret);
312 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserNewPass,
313 sTestUserOldPass);
314 ok(ret == ERROR_INVALID_PASSWORD || ret == ERROR_CANT_ACCESS_DOMAIN_INFO,
315 "Trying to change password giving an invalid password returned 0x%08x.\n", ret);
317 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
318 sTooLongPassword);
319 ok(ret == ERROR_PASSWORD_RESTRICTION,
320 "Changing to a password that's too long returned 0x%08x.\n", ret);
322 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
323 sTestUserNewPass);
324 ok(ret == NERR_Success || ret == ERROR_CANT_ACCESS_DOMAIN_INFO ||
325 ret == ERROR_INVALID_PASSWORD, "Changing the password correctly returned 0x%08x.\n", ret);
327 ret = pNetUserDel(NULL, sTestUserName);
328 ok(ret == NERR_Success, "Deleting the user failed.\n");
330 ret = pNetUserDel(NULL, sTestUserName);
331 ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
334 START_TEST(access)
336 HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
338 pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
339 pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
340 pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
341 pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
342 pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
343 pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
344 pNetUserChangePassword=(void*)GetProcAddress(hnetapi32, "NetUserChangePassword");
345 pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
347 /* These functions were introduced with NT. It's safe to assume that
348 * if one is not available, none are.
350 if (!pNetApiBufferFree) {
351 skip("Needed functions are not available\n");
352 FreeLibrary(hnetapi32);
353 return;
356 if (init_access_tests()) {
357 run_userhandling_tests();
358 run_usergetinfo_tests();
359 run_querydisplayinformation1_tests();
360 run_usermodalsget_tests();
363 FreeLibrary(hnetapi32);