netapi32: Implement NetUserDel.
[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 WCHAR user_name[UNLEN + 1];
33 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
34
35 /* FIXME : Tests should be language independent */
36 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
37                                 'o','r',0};
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',' ',
40                                 'U','s','e','r',0};
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',
54     'a', 0};
55
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 };
63
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;
72
73 static int init_access_tests(void)
74 {
75     DWORD dwSize;
76     BOOL rc;
77
78     user_name[0] = 0;
79     dwSize = sizeof(user_name);
80     rc=GetUserNameW(user_name, &dwSize);
81     if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
82     {
83         skip("GetUserNameW is not available.\n");
84         return 0;
85     }
86     ok(rc, "User Name Retrieved\n");
87
88     computer_name[0] = 0;
89     dwSize = sizeof(computer_name);
90     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
91     return 1;
92 }
93
94 static void run_usergetinfo_tests(void)
95 {
96     NET_API_STATUS rc;
97     PUSER_INFO_0 ui0 = NULL;
98     PUSER_INFO_10 ui10 = NULL;
99     DWORD dwSize;
100
101     /* Level 0 */
102     rc=pNetUserGetInfo(NULL, sAdminUserName, 0, (LPBYTE *)&ui0);
103     if (rc != NERR_Success) {
104         skip("Aborting usergetinfo_tests().   NetUserGetInfo: rc=%d\n", rc);
105         return;
106     }
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");
112
113     /* Level 10 */
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");
124
125     pNetApiBufferFree(ui0);
126     pNetApiBufferFree(ui10);
127
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);
133     todo_wine {
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);
138     }
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);
146 }
147
148 /* checks Level 1 of NetQueryDisplayInformation */
149 static void run_querydisplayinformation1_tests(void)
150 {
151     PNET_DISPLAY_USER Buffer, rec;
152     DWORD Result, EntryCount;
153     DWORD i = 0;
154     BOOL hasAdmin = FALSE;
155     BOOL hasGuest = FALSE;
156
157     do
158     {
159         Result = pNetQueryDisplayInformation(
160             NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
161             (PVOID *)&Buffer);
162
163         ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
164            "Information Retrieved\n");
165         rec = Buffer;
166         for(; EntryCount > 0; EntryCount--)
167         {
168             if (!lstrcmpW(rec->usri1_name, sAdminUserName))
169             {
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");
173                 hasAdmin = TRUE;
174             }
175             else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
176             {
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");
180                 hasGuest = TRUE;
181             }
182
183             i = rec->usri1_next_index;
184             rec++;
185         }
186
187         pNetApiBufferFree(Buffer);
188     } while (Result == ERROR_MORE_DATA);
189
190     ok(hasAdmin, "Has Administrator account\n");
191 }
192
193 static void run_usermodalsget_tests(void)
194 {
195     NET_API_STATUS rc;
196     USER_MODALS_INFO_2 * umi2 = NULL;
197
198     rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
199     ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
200
201     if (umi2)
202         pNetApiBufferFree(umi2);
203 }
204
205 static void run_userhandling_tests(void)
206 {
207     NET_API_STATUS ret;
208     USER_INFO_1 usri;
209
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;
215
216     usri.usri1_name = sTooLongName;
217     usri.usri1_password = sTestUserOldPass;
218
219     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
220     ok(ret == NERR_BadUsername, "Adding user with too long username returned 0x%08x\n", ret);
221
222     usri.usri1_name = sTestUserName;
223     usri.usri1_password = sTooLongPassword;
224
225     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
226     ok(ret == NERR_PasswordTooShort, "Adding user with too long password returned 0x%08x\n", ret);
227
228     usri.usri1_name = sTooLongName;
229     usri.usri1_password = sTooLongPassword;
230
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);
234
235     usri.usri1_name = sTestUserName;
236     usri.usri1_password = sTestUserOldPass;
237
238     ret = pNetUserAdd(NULL, 5, (LPBYTE)&usri, NULL);
239     ok(ret == ERROR_INVALID_LEVEL, "Adding user with level 5 returned 0x%08x\n", ret);
240
241     ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
242     if(ret == ERROR_ACCESS_DENIED)
243     {
244         skip("Insufficient permissions to add users. Skipping test.\n");
245         return;
246     }
247     if(ret == NERR_UserExists)
248     {
249         skip("User already exists, skipping test to not mess up the system\n");
250         return;
251     }
252
253     ok(ret == NERR_Success, "Adding user failed with error 0x%08x\n", ret);
254     if(ret != NERR_Success)
255         return;
256
257     ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
258             sTestUserNewPass);
259     todo_wine ok(ret == NERR_Success, "Changing the password failed.\n");
260
261     ret = pNetUserDel(NULL, sTestUserName);
262     ok(ret == NERR_Success, "Deleting the user failed.\n");
263
264     ret = pNetUserDel(NULL, sTestUserName);
265     ok(ret == NERR_UserNotFound, "Deleting a nonexistent user returned 0x%08x\n",ret);
266 }
267
268 START_TEST(access)
269 {
270     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
271
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");
280
281     /* These functions were introduced with NT. It's safe to assume that
282      * if one is not available, none are.
283      */
284     if (!pNetApiBufferFree) {
285         skip("Needed functions are not available\n");
286         FreeLibrary(hnetapi32);
287         return;
288     }
289
290     if (init_access_tests()) {
291         run_usergetinfo_tests();
292         run_querydisplayinformation1_tests();
293         run_usermodalsget_tests();
294         run_userhandling_tests();
295     }
296
297     FreeLibrary(hnetapi32);
298 }