Release 1.5.29.
[wine] / dlls / netapi32 / tests / 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 static WCHAR user_name[UNLEN + 1];
33 static 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 static NET_API_STATUS (WINAPI *pNetLocalGroupGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
67 static NET_API_STATUS (WINAPI *pNetLocalGroupGetMembers)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*,DWORD,LPDWORD,LPDWORD,PDWORD_PTR)=NULL;
68
69 static int init_access_tests(void)
70 {
71     DWORD dwSize;
72     BOOL rc;
73
74     user_name[0] = 0;
75     dwSize = sizeof(user_name)/sizeof(WCHAR);
76     rc=GetUserNameW(user_name, &dwSize);
77     if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
78     {
79         win_skip("GetUserNameW is not available.\n");
80         return 0;
81     }
82     ok(rc, "User Name Retrieved\n");
83
84     computer_name[0] = 0;
85     dwSize = sizeof(computer_name)/sizeof(WCHAR);
86     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
87     return 1;
88 }
89
90 static NET_API_STATUS create_test_user(void)
91 {
92     USER_INFO_1 usri;
93
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;
101
102     return pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
103 }
104
105 static NET_API_STATUS delete_test_user(void)
106 {
107     return pNetUserDel(NULL, sTestUserName);
108 }
109
110 static void run_usergetinfo_tests(void)
111 {
112     NET_API_STATUS rc;
113     PUSER_INFO_0 ui0 = NULL;
114     PUSER_INFO_10 ui10 = NULL;
115     DWORD dwSize;
116
117     if((rc = create_test_user()) != NERR_Success )
118     {
119         skip("Skipping usergetinfo_tests, create_test_user failed: 0x%08x\n", rc);
120         return;
121     }
122
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");
131
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");
143
144     pNetApiBufferFree(ui0);
145     pNetApiBufferFree(ui10);
146
147     /* NetUserGetInfo should always work for the current user. */
148     rc=pNetUserGetInfo(NULL, user_name, 0, (LPBYTE*)&ui0);
149     ok(rc == NERR_Success, "NetUsetGetInfo for current user failed: 0x%08x.\n", rc);
150     pNetApiBufferFree(ui0);
151
152     /* errors handling */
153     rc=pNetUserGetInfo(NULL, sTestUserName, 10000, (LPBYTE *)&ui0);
154     ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%d\n",rc);
155     rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
156     ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%d\n",rc);
157     todo_wine {
158         /* FIXME - Currently Wine can't verify whether the network path is good or bad */
159         rc=pNetUserGetInfo(sBadNetPath, sTestUserName, 0, (LPBYTE *)&ui0);
160         ok(rc == ERROR_BAD_NETPATH ||
161            rc == ERROR_NETWORK_UNREACHABLE ||
162            rc == RPC_S_SERVER_UNAVAILABLE ||
163            rc == NERR_WkstaNotStarted || /* workstation service not running */
164            rc == RPC_S_INVALID_NET_ADDR, /* Some Win7 */
165            "Bad Network Path: rc=%d\n",rc);
166     }
167     rc=pNetUserGetInfo(sEmptyStr, sTestUserName, 0, (LPBYTE *)&ui0);
168     ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
169        "Bad Network Path: rc=%d\n",rc);
170     rc=pNetUserGetInfo(sInvalidName, sTestUserName, 0, (LPBYTE *)&ui0);
171     ok(rc == ERROR_INVALID_NAME || rc == ERROR_INVALID_HANDLE,"Invalid Server Name: rc=%d\n",rc);
172     rc=pNetUserGetInfo(sInvalidName2, sTestUserName, 0, (LPBYTE *)&ui0);
173     ok(rc == ERROR_INVALID_NAME || rc == ERROR_INVALID_HANDLE,"Invalid Server Name: rc=%d\n",rc);
174
175     if(delete_test_user() != NERR_Success)
176         trace("Deleting the test user failed. You might have to manually delete it.\n");
177 }
178
179 /* Checks Level 1 of NetQueryDisplayInformation */
180 static void run_querydisplayinformation1_tests(void)
181 {
182     PNET_DISPLAY_USER Buffer, rec;
183     DWORD Result, EntryCount;
184     DWORD i = 0;
185     BOOL hasAdmin = FALSE;
186     BOOL hasGuest = FALSE;
187
188     do
189     {
190         Result = pNetQueryDisplayInformation(
191             NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
192             (PVOID *)&Buffer);
193
194         ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
195            "Information Retrieved\n");
196         rec = Buffer;
197         for(; EntryCount > 0; EntryCount--)
198         {
199             if (rec->usri1_user_id == DOMAIN_USER_RID_ADMIN)
200             {
201                 ok(!hasAdmin, "One admin user\n");
202                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
203                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
204                 hasAdmin = TRUE;
205             }
206             else if (rec->usri1_user_id == DOMAIN_USER_RID_GUEST)
207             {
208                 ok(!hasGuest, "One guest record\n");
209                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
210                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
211                 hasGuest = TRUE;
212             }
213
214             i = rec->usri1_next_index;
215             rec++;
216         }
217
218         pNetApiBufferFree(Buffer);
219     } while (Result == ERROR_MORE_DATA);
220
221     ok(hasAdmin, "Doesn't have 'Administrator' account\n");
222 }
223
224 static void run_usermodalsget_tests(void)
225 {
226     NET_API_STATUS rc;
227     USER_MODALS_INFO_2 * umi2 = NULL;
228
229     rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
230     ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
231
232     if (umi2)
233         pNetApiBufferFree(umi2);
234 }
235
236 static void run_userhandling_tests(void)
237 {
238     NET_API_STATUS ret;
239     USER_INFO_1 usri;
240
241     usri.usri1_priv = USER_PRIV_USER;
242     usri.usri1_home_dir = NULL;
243     usri.usri1_comment = NULL;
244     usri.usri1_flags = UF_SCRIPT;
245     usri.usri1_script_path = NULL;
246
247     usri.usri1_name = sTooLongName;
248     usri.usri1_password = sTestUserOldPass;
249
250     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
251     if (ret == NERR_Success || ret == NERR_UserExists)
252     {
253         /* Windows NT4 does create the user. Delete the user and also if it already existed
254          * due to a previous test run on NT4.
255          */
256         trace("We are on NT4, we have to delete the user with the too long username\n");
257         ret = pNetUserDel(NULL, sTooLongName);
258         ok(ret == NERR_Success, "Deleting the user failed : %d\n", ret);
259     }
260     else if (ret == ERROR_ACCESS_DENIED)
261     {
262         skip("not enough permissions to add a user\n");
263         return;
264     }
265     else
266         ok(ret == NERR_BadUsername ||
267            broken(ret == NERR_PasswordTooShort), /* NT4 */
268            "Adding user with too long username returned 0x%08x\n", ret);
269
270     usri.usri1_name = sTestUserName;
271     usri.usri1_password = sTooLongPassword;
272
273     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
274     ok(ret == NERR_PasswordTooShort || ret == ERROR_ACCESS_DENIED /* Win2003 */,
275        "Adding user with too long password returned 0x%08x\n", ret);
276
277     usri.usri1_name = sTooLongName;
278     usri.usri1_password = sTooLongPassword;
279
280     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
281     /* NT4 doesn't have a problem with the username so it will report the too long password
282      * as the error. NERR_PasswordTooShort is reported for all kind of password related errors.
283      */
284     ok(ret == NERR_BadUsername || ret == NERR_PasswordTooShort,
285             "Adding user with too long username/password returned 0x%08x\n", ret);
286
287     usri.usri1_name = sTestUserName;
288     usri.usri1_password = sTestUserOldPass;
289
290     ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
291     ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
292
293     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
294     if(ret == ERROR_ACCESS_DENIED)
295     {
296         skip("Insufficient permissions to add users. Skipping test.\n");
297         return;
298     }
299     if(ret == NERR_UserExists)
300     {
301         skip("User already exists, skipping test to not mess up the system\n");
302         return;
303     }
304
305     ok(ret == NERR_Success ||
306        broken(ret == NERR_PasswordTooShort), /* NT4 */
307        "Adding user failed with error 0x%08x\n", ret);
308     if(ret != NERR_Success)
309         return;
310
311     /* On Windows XP (and newer), calling NetUserChangePassword with a NULL
312      * domainname parameter creates a user home directory, iff the machine is
313      * not member of a domain.
314      * Using \\127.0.0.1 as domain name does not work on standalone machines
315      * either, unless the ForceGuest option in the registry is turned off.
316      * So let's not test NetUserChangePassword for now.
317      */
318
319     ret = pNetUserDel(NULL, sTestUserName);
320     ok(ret == NERR_Success, "Deleting the user failed.\n");
321
322     ret = pNetUserDel(NULL, sTestUserName);
323     ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
324 }
325
326 static void run_localgroupgetinfo_tests(void)
327 {
328     NET_API_STATUS status;
329     static const WCHAR admins[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
330     PLOCALGROUP_INFO_1 lgi = NULL;
331     PLOCALGROUP_MEMBERS_INFO_3 buffer = NULL;
332     DWORD entries_read = 0, total_entries =0;
333     int i;
334
335     status = pNetLocalGroupGetInfo(NULL, admins, 1, (LPBYTE *)&lgi);
336     ok(status == NERR_Success || broken(status == NERR_GroupNotFound),
337        "NetLocalGroupGetInfo unexpectedly returned %d\n", status);
338     if (status != NERR_Success) return;
339
340     trace("Local groupname:%s\n", wine_dbgstr_w( lgi->lgrpi1_name));
341     trace("Comment: %s\n", wine_dbgstr_w( lgi->lgrpi1_comment));
342
343     pNetApiBufferFree(lgi);
344
345     status = pNetLocalGroupGetMembers(NULL, admins, 3, (LPBYTE *)&buffer, MAX_PREFERRED_LENGTH, &entries_read, &total_entries, NULL);
346     ok(status == NERR_Success, "NetLocalGroupGetMembers unexpectedly returned %d\n", status);
347     ok(entries_read > 0 && total_entries > 0, "Amount of entries is unexpectedly 0\n");
348
349     for(i=0;i<entries_read;i++)
350         trace("domain and name: %s\n", wine_dbgstr_w(buffer[i].lgrmi3_domainandname));
351 }
352
353 START_TEST(access)
354 {
355     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
356
357     pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
358     pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
359     pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
360     pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
361     pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
362     pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
363     pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
364     pNetLocalGroupGetInfo=(void*)GetProcAddress(hnetapi32, "NetLocalGroupGetInfo");
365     pNetLocalGroupGetMembers=(void*)GetProcAddress(hnetapi32, "NetLocalGroupGetMembers");
366
367     /* These functions were introduced with NT. It's safe to assume that
368      * if one is not available, none are.
369      */
370     if (!pNetApiBufferFree) {
371         win_skip("Needed functions are not available\n");
372         FreeLibrary(hnetapi32);
373         return;
374     }
375
376     if (init_access_tests()) {
377         run_userhandling_tests();
378         run_usergetinfo_tests();
379         run_querydisplayinformation1_tests();
380         run_usermodalsget_tests();
381         run_localgroupgetinfo_tests();
382     }
383
384     FreeLibrary(hnetapi32);
385 }