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 /* FIXME : Tests should be language independent */
36 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
38 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
39 static const WCHAR sNonexistentUser[] = {'N','o','n','e','x','i','s','t','e','n','t',' ',
41 static WCHAR sTooLongName[] = {'T','h','i','s',' ','i','s',' ','a',' ','b','a','d',
42 ' ','u','s','e','r','n','a','m','e',0};
43 static WCHAR sTooLongPassword[] = {'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','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
51 '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 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
53 'a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h','a','b','c','d','e','f','g','h',
56 static WCHAR sTestUserName[] = {'t', 'e', 's', 't', 'u', 's', 'e', 'r', 0};
57 static WCHAR sTestUserOldPass[] = {'o', 'l', 'd', 'p', 'a', 's', 's', 0};
58 static WCHAR sTestUserNewPass[] = {'n', 'e', 'w', 'p', 'a', 's', 's', 0};
59 static const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
60 static const WCHAR sInvalidName[] = {'\\',0};
61 static const WCHAR sInvalidName2[] = {'\\','\\',0};
62 static const WCHAR sEmptyStr[] = { 0 };
64 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
65 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
66 static NET_API_STATUS (WINAPI *pNetQueryDisplayInformation)(LPWSTR,DWORD,DWORD,DWORD,DWORD,LPDWORD,PVOID*)=NULL;
67 static NET_API_STATUS (WINAPI *pNetUserGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
68 static NET_API_STATUS (WINAPI *pNetUserModalsGet)(LPCWSTR,DWORD,LPBYTE*)=NULL;
69 static NET_API_STATUS (WINAPI *pNetUserAdd)(LPCWSTR,DWORD,LPBYTE,LPDWORD)=NULL;
70 static NET_API_STATUS (WINAPI *pNetUserChangePassword)(LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR)=NULL;
71 static NET_API_STATUS (WINAPI *pNetUserDel)(LPCWSTR,LPCWSTR)=NULL;
73 static int init_access_tests(void)
79 dwSize = sizeof(user_name);
80 rc=GetUserNameW(user_name, &dwSize);
81 if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
83 skip("GetUserNameW is not available.\n");
86 ok(rc, "User Name Retrieved\n");
89 dwSize = sizeof(computer_name);
90 ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
94 static void run_usergetinfo_tests(void)
97 PUSER_INFO_0 ui0 = NULL;
98 PUSER_INFO_10 ui10 = NULL;
102 rc=pNetUserGetInfo(NULL, sAdminUserName, 0, (LPBYTE *)&ui0);
103 if (rc != NERR_Success) {
104 skip("Aborting usergetinfo_tests(). NetUserGetInfo: rc=%d\n", rc);
107 ok(!lstrcmpW(sAdminUserName, ui0->usri0_name), "This is really user name\n");
108 pNetApiBufferSize(ui0, &dwSize);
109 ok(dwSize >= (sizeof(USER_INFO_0) +
110 (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
111 "Is allocated with NetApiBufferAllocate\n");
114 rc=pNetUserGetInfo(NULL, sAdminUserName, 10, (LPBYTE *)&ui10);
115 ok(rc == NERR_Success, "NetUserGetInfo: rc=%d\n", rc);
116 ok(!lstrcmpW(sAdminUserName, ui10->usri10_name), "This is really user name\n");
117 pNetApiBufferSize(ui10, &dwSize);
118 ok(dwSize >= (sizeof(USER_INFO_10) +
119 (lstrlenW(ui10->usri10_name) + 1 +
120 lstrlenW(ui10->usri10_comment) + 1 +
121 lstrlenW(ui10->usri10_usr_comment) + 1 +
122 lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
123 "Is allocated with NetApiBufferAllocate\n");
125 pNetApiBufferFree(ui0);
126 pNetApiBufferFree(ui10);
128 /* errors handling */
129 rc=pNetUserGetInfo(NULL, sAdminUserName, 10000, (LPBYTE *)&ui0);
130 ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%d\n",rc);
131 rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
132 ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%d\n",rc);
134 /* FIXME - Currently Wine can't verify whether the network path is good or bad */
135 rc=pNetUserGetInfo(sBadNetPath, sAdminUserName, 0, (LPBYTE *)&ui0);
136 ok(rc == ERROR_BAD_NETPATH || rc == ERROR_NETWORK_UNREACHABLE,
137 "Bad Network Path: rc=%d\n",rc);
139 rc=pNetUserGetInfo(sEmptyStr, sAdminUserName, 0, (LPBYTE *)&ui0);
140 ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
141 "Bad Network Path: rc=%d\n",rc);
142 rc=pNetUserGetInfo(sInvalidName, sAdminUserName, 0, (LPBYTE *)&ui0);
143 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
144 rc=pNetUserGetInfo(sInvalidName2, sAdminUserName, 0, (LPBYTE *)&ui0);
145 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
148 /* checks Level 1 of NetQueryDisplayInformation */
149 static void run_querydisplayinformation1_tests(void)
151 PNET_DISPLAY_USER Buffer, rec;
152 DWORD Result, EntryCount;
154 BOOL hasAdmin = FALSE;
155 BOOL hasGuest = FALSE;
159 Result = pNetQueryDisplayInformation(
160 NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
163 ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
164 "Information Retrieved\n");
166 for(; EntryCount > 0; EntryCount--)
168 if (!lstrcmpW(rec->usri1_name, sAdminUserName))
170 ok(!hasAdmin, "One admin user\n");
171 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
172 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
175 else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
177 ok(!hasGuest, "One guest record\n");
178 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
179 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
183 i = rec->usri1_next_index;
187 pNetApiBufferFree(Buffer);
188 } while (Result == ERROR_MORE_DATA);
190 ok(hasAdmin, "Has Administrator account\n");
193 static void run_usermodalsget_tests(void)
196 USER_MODALS_INFO_2 * umi2 = NULL;
198 rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
199 ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
202 pNetApiBufferFree(umi2);
205 static void run_userhandling_tests(void)
210 usri.usri1_priv = USER_PRIV_USER;
211 usri.usri1_home_dir = NULL;
212 usri.usri1_comment = NULL;
213 usri.usri1_flags = UF_SCRIPT;
214 usri.usri1_script_path = NULL;
216 usri.usri1_name = sTooLongName;
217 usri.usri1_password = sTestUserOldPass;
219 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
220 ok(ret == NERR_BadUsername, "Adding user with too long username returned 0x%08x\n", ret);
222 usri.usri1_name = sTestUserName;
223 usri.usri1_password = sTooLongPassword;
225 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
226 ok(ret == NERR_PasswordTooShort, "Adding user with too long password returned 0x%08x\n", ret);
228 usri.usri1_name = sTooLongName;
229 usri.usri1_password = sTooLongPassword;
231 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
232 ok(ret == NERR_BadUsername,
233 "Adding user with too long username/password returned 0x%08x\n", ret);
235 usri.usri1_name = sTestUserName;
236 usri.usri1_password = sTestUserOldPass;
238 ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
239 ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
241 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
242 if(ret == ERROR_ACCESS_DENIED)
244 skip("Insufficient permissions to add users. Skipping test.\n");
247 if(ret == NERR_UserExists)
249 skip("User already exists, skipping test to not mess up the system\n");
253 ok(ret == NERR_Success, "Adding user failed with error 0x%08x\n", ret);
254 if(ret != NERR_Success)
257 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
259 todo_wine ok(ret == NERR_Success, "Changing the password failed.\n");
261 ret = pNetUserDel(NULL, sTestUserName);
262 ok(ret == NERR_Success, "Deleting the user failed.\n");
264 ret = pNetUserDel(NULL, sTestUserName);
265 ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
270 HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
272 pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
273 pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
274 pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
275 pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
276 pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
277 pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
278 pNetUserChangePassword=(void*)GetProcAddress(hnetapi32, "NetUserChangePassword");
279 pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
281 /* These functions were introduced with NT. It's safe to assume that
282 * if one is not available, none are.
284 if (!pNetApiBufferFree) {
285 skip("Needed functions are not available\n");
286 FreeLibrary(hnetapi32);
290 if (init_access_tests()) {
291 run_usergetinfo_tests();
292 run_querydisplayinformation1_tests();
293 run_usermodalsget_tests();
294 run_userhandling_tests();
297 FreeLibrary(hnetapi32);