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
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',' ',
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',
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 };
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;
67 static int init_access_tests(void)
73 dwSize = sizeof(user_name);
74 rc=GetUserNameW(user_name, &dwSize);
75 if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
77 skip("GetUserNameW is not available.\n");
80 ok(rc, "User Name Retrieved\n");
83 dwSize = sizeof(computer_name);
84 ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
88 static NET_API_STATUS create_test_user(void)
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;
100 return pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
103 static NET_API_STATUS delete_test_user(void)
105 return pNetUserDel(NULL, sTestUserName);
108 static void run_usergetinfo_tests(void)
111 PUSER_INFO_0 ui0 = NULL;
112 PUSER_INFO_10 ui10 = NULL;
115 if((rc = create_test_user()) != NERR_Success )
117 skip("Skipping usergetinfo_tests, create_test_user failed: 0x%08x\n", rc);
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");
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");
142 pNetApiBufferFree(ui0);
143 pNetApiBufferFree(ui10);
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);
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);
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 || rc == ERROR_NETWORK_UNREACHABLE,
159 "Bad Network Path: rc=%d\n",rc);
161 rc=pNetUserGetInfo(sEmptyStr, sTestUserName, 0, (LPBYTE *)&ui0);
162 ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
163 "Bad Network Path: rc=%d\n",rc);
164 rc=pNetUserGetInfo(sInvalidName, sTestUserName, 0, (LPBYTE *)&ui0);
165 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
166 rc=pNetUserGetInfo(sInvalidName2, sTestUserName, 0, (LPBYTE *)&ui0);
167 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
169 if(delete_test_user() != NERR_Success)
170 trace("Deleting the test user failed. You might have to manually delete it.\n");
173 /* checks Level 1 of NetQueryDisplayInformation
174 * FIXME: Needs to be rewritten to not depend on the spelling of the users,
175 * ideally based on the admin and guest user SIDs/RIDs.*/
176 static void run_querydisplayinformation1_tests(void)
178 PNET_DISPLAY_USER Buffer, rec;
179 DWORD Result, EntryCount;
181 BOOL hasAdmin = FALSE;
182 BOOL hasGuest = FALSE;
183 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a',
185 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
189 Result = pNetQueryDisplayInformation(
190 NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
193 ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
194 "Information Retrieved\n");
196 for(; EntryCount > 0; EntryCount--)
198 if (!lstrcmpW(rec->usri1_name, sAdminUserName))
200 ok(!hasAdmin, "One admin user\n");
201 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
202 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
205 else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
207 ok(!hasGuest, "One guest record\n");
208 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
209 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
213 i = rec->usri1_next_index;
217 pNetApiBufferFree(Buffer);
218 } while (Result == ERROR_MORE_DATA);
220 ok(hasAdmin, "Doesn't have 'Administrator' account\n");
223 static void run_usermodalsget_tests(void)
226 USER_MODALS_INFO_2 * umi2 = NULL;
228 rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
229 ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
232 pNetApiBufferFree(umi2);
235 static void run_userhandling_tests(void)
240 usri.usri1_priv = USER_PRIV_USER;
241 usri.usri1_home_dir = NULL;
242 usri.usri1_comment = NULL;
243 usri.usri1_flags = UF_SCRIPT;
244 usri.usri1_script_path = NULL;
246 usri.usri1_name = sTooLongName;
247 usri.usri1_password = sTestUserOldPass;
249 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
250 if (ret == NERR_Success || ret == NERR_UserExists)
252 /* Windows NT4 does create the user. Delete the user and also if it already existed
253 * due to a previous test run on NT4.
255 trace("We are on NT4, we have to delete the user with the too long username\n");
256 ret = pNetUserDel(NULL, sTooLongName);
257 ok(ret == NERR_Success, "Deleting the user failed : %d\n", ret);
260 ok(ret == NERR_BadUsername ||
261 broken(ret == NERR_PasswordTooShort), /* NT4 */
262 "Adding user with too long username returned 0x%08x\n", ret);
264 usri.usri1_name = sTestUserName;
265 usri.usri1_password = sTooLongPassword;
267 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
268 ok(ret == NERR_PasswordTooShort, "Adding user with too long password returned 0x%08x\n", ret);
270 usri.usri1_name = sTooLongName;
271 usri.usri1_password = sTooLongPassword;
273 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
274 /* NT4 doesn't have a problem with the username so it will report the too long password
275 * as the error. NERR_PasswordTooShort is reported for all kind of password related errors.
277 ok(ret == NERR_BadUsername || ret == NERR_PasswordTooShort,
278 "Adding user with too long username/password returned 0x%08x\n", ret);
280 usri.usri1_name = sTestUserName;
281 usri.usri1_password = sTestUserOldPass;
283 ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
284 ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
286 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
287 if(ret == ERROR_ACCESS_DENIED)
289 skip("Insufficient permissions to add users. Skipping test.\n");
292 if(ret == NERR_UserExists)
294 skip("User already exists, skipping test to not mess up the system\n");
298 ok(ret == NERR_Success ||
299 broken(ret == NERR_PasswordTooShort), /* NT4 */
300 "Adding user failed with error 0x%08x\n", ret);
301 if(ret != NERR_Success)
304 /* On Windows XP (and newer), calling NetUserChangePassword with a NULL
305 * domainname parameter creates a user home directory, iff the machine is
306 * not member of a domain.
307 * Using \\127.0.0.1 as domain name does not work on standalone machines
308 * either, unless the ForceGuest option in the registry is turned off.
309 * So let's not test NetUserChangePassword for now.
312 ret = pNetUserDel(NULL, sTestUserName);
313 ok(ret == NERR_Success, "Deleting the user failed.\n");
315 ret = pNetUserDel(NULL, sTestUserName);
316 ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
321 HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
323 pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
324 pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
325 pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
326 pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
327 pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
328 pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
329 pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
331 /* These functions were introduced with NT. It's safe to assume that
332 * if one is not available, none are.
334 if (!pNetApiBufferFree) {
335 skip("Needed functions are not available\n");
336 FreeLibrary(hnetapi32);
340 if (init_access_tests()) {
341 run_userhandling_tests();
342 run_usergetinfo_tests();
343 run_querydisplayinformation1_tests();
344 run_usermodalsget_tests();
347 FreeLibrary(hnetapi32);