netapi32: Add test for adding/deleting users.
[wine/gsoc_dplay.git] / dlls / netapi32 / tests / access.c
blob7dd58556b9df642fd658359cc2c9cd5c1ba6c709
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 sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
36 'o','r',0};
37 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
38 static const WCHAR sNonexistentUser[] = {'N','o','n','e','x','i','s','t','e','n','t',' ',
39 'U','s','e','r',0};
40 static const WCHAR sTestUserName[] = {'t', 'e', 's', 't', 'u', 's', 'e', 'r', 0};
41 static const WCHAR sTestUserOldPass[] = {'o', 'l', 'd', 'p', 'a', 's', 's', 0};
42 static const WCHAR sTestUserNewPass[] = {'n', 'e', 'w', 'p', 'a', 's', 's', 0};
43 static const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
44 static const WCHAR sInvalidName[] = {'\\',0};
45 static const WCHAR sInvalidName2[] = {'\\','\\',0};
46 static const WCHAR sEmptyStr[] = { 0 };
48 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
49 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
50 static NET_API_STATUS (WINAPI *pNetQueryDisplayInformation)(LPWSTR,DWORD,DWORD,DWORD,DWORD,LPDWORD,PVOID*)=NULL;
51 static NET_API_STATUS (WINAPI *pNetUserGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
52 static NET_API_STATUS (WINAPI *pNetUserModalsGet)(LPCWSTR,DWORD,LPBYTE*)=NULL;
53 static NET_API_STATUS (WINAPI *pNetUserAdd)(LPCWSTR,DWORD,LPBYTE,LPDWORD)=NULL;
54 static NET_API_STATUS (WINAPI *pNetUserChangePassword)(LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR)=NULL;
55 static NET_API_STATUS (WINAPI *pNetUserDel)(LPCWSTR,LPCWSTR)=NULL;
57 static int init_access_tests(void)
59 DWORD dwSize;
60 BOOL rc;
62 user_name[0] = 0;
63 dwSize = sizeof(user_name);
64 rc=GetUserNameW(user_name, &dwSize);
65 if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
67 skip("netapi32 functions seem to be missing.\n");
68 return 0;
70 ok(rc, "User Name Retrieved\n");
72 computer_name[0] = 0;
73 dwSize = sizeof(computer_name);
74 ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
75 return 1;
78 static void run_usergetinfo_tests(void)
80 NET_API_STATUS rc;
81 PUSER_INFO_0 ui0 = NULL;
82 PUSER_INFO_10 ui10 = NULL;
83 DWORD dwSize;
85 /* If this one is not defined then none of the others will be defined */
86 if (!pNetUserGetInfo)
88 skip("netapi32 functions seem to be missing.\n");
89 return;
92 /* Level 0 */
93 rc=pNetUserGetInfo(NULL, sAdminUserName, 0, (LPBYTE *)&ui0);
94 if (rc != NERR_Success) {
95 skip("Aborting usergetinfo_tests(). NetUserGetInfo: rc=%d\n", rc);
96 return;
98 ok(!lstrcmpW(sAdminUserName, ui0->usri0_name), "This is really user name\n");
99 pNetApiBufferSize(ui0, &dwSize);
100 ok(dwSize >= (sizeof(USER_INFO_0) +
101 (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
102 "Is allocated with NetApiBufferAllocate\n");
104 /* Level 10 */
105 rc=pNetUserGetInfo(NULL, sAdminUserName, 10, (LPBYTE *)&ui10);
106 ok(rc == NERR_Success, "NetUserGetInfo: rc=%d\n", rc);
107 ok(!lstrcmpW(sAdminUserName, ui10->usri10_name), "This is really user name\n");
108 pNetApiBufferSize(ui10, &dwSize);
109 ok(dwSize >= (sizeof(USER_INFO_10) +
110 (lstrlenW(ui10->usri10_name) + 1 +
111 lstrlenW(ui10->usri10_comment) + 1 +
112 lstrlenW(ui10->usri10_usr_comment) + 1 +
113 lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
114 "Is allocated with NetApiBufferAllocate\n");
116 pNetApiBufferFree(ui0);
117 pNetApiBufferFree(ui10);
119 /* errors handling */
120 rc=pNetUserGetInfo(NULL, sAdminUserName, 10000, (LPBYTE *)&ui0);
121 ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%d\n",rc);
122 rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
123 ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%d\n",rc);
124 todo_wine {
125 /* FIXME - Currently Wine can't verify whether the network path is good or bad */
126 rc=pNetUserGetInfo(sBadNetPath, sAdminUserName, 0, (LPBYTE *)&ui0);
127 ok(rc == ERROR_BAD_NETPATH || rc == ERROR_NETWORK_UNREACHABLE,
128 "Bad Network Path: rc=%d\n",rc);
130 rc=pNetUserGetInfo(sEmptyStr, sAdminUserName, 0, (LPBYTE *)&ui0);
131 ok(rc == ERROR_BAD_NETPATH || rc == NERR_Success,
132 "Bad Network Path: rc=%d\n",rc);
133 rc=pNetUserGetInfo(sInvalidName, sAdminUserName, 0, (LPBYTE *)&ui0);
134 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
135 rc=pNetUserGetInfo(sInvalidName2, sAdminUserName, 0, (LPBYTE *)&ui0);
136 ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%d\n",rc);
139 /* checks Level 1 of NetQueryDisplayInformation */
140 static void run_querydisplayinformation1_tests(void)
142 PNET_DISPLAY_USER Buffer, rec;
143 DWORD Result, EntryCount;
144 DWORD i = 0;
145 BOOL hasAdmin = FALSE;
146 BOOL hasGuest = FALSE;
148 if (!pNetQueryDisplayInformation)
150 skip("netapi32 functions seem to be missing.\n");
151 return;
156 Result = pNetQueryDisplayInformation(
157 NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
158 (PVOID *)&Buffer);
160 ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
161 "Information Retrieved\n");
162 rec = Buffer;
163 for(; EntryCount > 0; EntryCount--)
165 if (!lstrcmpW(rec->usri1_name, sAdminUserName))
167 ok(!hasAdmin, "One admin user\n");
168 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
169 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
170 hasAdmin = TRUE;
172 else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
174 ok(!hasGuest, "One guest record\n");
175 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set\n");
176 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set\n");
177 hasGuest = TRUE;
180 i = rec->usri1_next_index;
181 rec++;
184 pNetApiBufferFree(Buffer);
185 } while (Result == ERROR_MORE_DATA);
187 ok(hasAdmin, "Has Administrator account\n");
190 static void run_usermodalsget_tests(void)
192 NET_API_STATUS rc;
193 USER_MODALS_INFO_2 * umi2 = NULL;
195 rc = pNetUserModalsGet(NULL, 2, (LPBYTE *)&umi2);
196 ok(rc == ERROR_SUCCESS, "NetUserModalsGet failed, rc = %d\n", rc);
198 if (umi2)
199 pNetApiBufferFree(umi2);
202 static void run_userhandling_tests(void)
204 NET_API_STATUS ret;
205 USER_INFO_1 usri;
207 if(!pNetUserAdd || !pNetUserChangePassword || !pNetUserDel)
209 skip("Functions for modifying the user database missing. Skipping test.\n");
210 return;
213 usri.usri1_name = (LPWSTR) sTestUserName;
214 usri.usri1_password = (LPWSTR) sTestUserOldPass;
215 usri.usri1_priv = USER_PRIV_USER;
216 usri.usri1_home_dir = NULL;
217 usri.usri1_comment = NULL;
218 usri.usri1_flags = UF_SCRIPT;
219 usri.usri1_script_path = NULL;
221 ret = pNetUserAdd(NULL, 1, (LPBYTE)&usri, NULL);
222 if(ret == ERROR_ACCESS_DENIED)
224 skip("Insufficient permissions to add users. Skipping test.\n");
225 return;
227 if(ret == NERR_UserExists)
229 skip("User already exists, skipping test to not mess up the system\n");
230 return;
233 ok(ret == NERR_Success, "Adding user failed with error 0x%08x\n", ret);
234 if(ret != NERR_Success)
235 return;
237 ret = pNetUserChangePassword(NULL, sTestUserName, sTestUserOldPass,
238 sTestUserNewPass);
239 todo_wine ok(ret == NERR_Success, "Changing the password failed.\n");
241 ret = pNetUserDel(NULL, sTestUserName);
242 todo_wine ok(ret == NERR_Success, "Deleting the user failed.\n");
245 START_TEST(access)
247 HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
248 pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
249 pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
250 pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
251 pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
252 pNetUserModalsGet=(void*)GetProcAddress(hnetapi32,"NetUserModalsGet");
253 pNetUserAdd=(void*)GetProcAddress(hnetapi32, "NetUserAdd");
254 pNetUserChangePassword=(void*)GetProcAddress(hnetapi32, "NetUserChangePassword");
255 pNetUserDel=(void*)GetProcAddress(hnetapi32, "NetUserDel");
257 if (!pNetApiBufferSize)
258 trace("It appears there is no netapi32 functionality on this platform\n");
260 if (init_access_tests()) {
261 run_usergetinfo_tests();
262 run_querydisplayinformation1_tests();
263 run_usermodalsget_tests();
264 run_userhandling_tests();