Fixed a race condition on RPC worker thread creation, and a typo.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <wine/test.h>
22 #include <winbase.h>
23 #include <winerror.h>
24 #include <lmaccess.h>
25 #include <lmerr.h>
26 #include <lmapibuf.h>
27
28 WCHAR user_name[UNLEN + 1];
29 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
30
31 const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
32                                 'o','r',0};
33 const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
34 const WCHAR sNonexistentUser[] = {'N','o','n','e','x','i','s','t','e','n','t',' ',
35                                 'U','s','e','r',0};
36 const WCHAR sBadNetPath[] = {'\\','\\','B','a',' ',' ','p','a','t','h',0};
37 const WCHAR sInvalidName[] = {'\\',0};
38 const WCHAR sInvalidName2[] = {'\\','\\',0};
39 const WCHAR sEmptyStr[] = { 0 };
40
41 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
42 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
43 static NET_API_STATUS (WINAPI *pNetQueryDisplayInformation)(LPWSTR,DWORD,DWORD,DWORD,DWORD,LPDWORD,PVOID*)=NULL;
44 static NET_API_STATUS (WINAPI *pNetUserGetInfo)(LPCWSTR,LPCWSTR,DWORD,LPBYTE*)=NULL;
45
46 static int init_access_tests(void)
47 {
48     DWORD dwSize;
49     BOOL rc;
50
51     user_name[0] = 0;
52     dwSize = sizeof(user_name);
53     rc=GetUserNameW(user_name, &dwSize);
54     if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
55         return 0;
56     ok(rc, "User Name Retrieved");
57
58     computer_name[0] = 0;
59     dwSize = sizeof(computer_name);
60     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved");
61     return 1;
62 }
63
64 void run_usergetinfo_tests(void)
65 {
66     NET_API_STATUS rc;
67     PUSER_INFO_0 ui0 = NULL;
68     PUSER_INFO_10 ui10 = NULL;
69     DWORD dwSize;
70
71     /* If this one is not defined then none of the others will be defined */
72     if (!pNetUserGetInfo)
73         return;
74
75     /* Level 0 */
76     rc=pNetUserGetInfo(NULL, sAdminUserName, 0, (LPBYTE *)&ui0);
77     ok(rc == NERR_Success, "NetUserGetInfo: rc=%ld", rc);
78     ok(!lstrcmpW(sAdminUserName, ui0->usri0_name), "This is really user name");
79     pNetApiBufferSize(ui0, &dwSize);
80     ok(dwSize >= (sizeof(USER_INFO_0) +
81                   (lstrlenW(ui0->usri0_name) + 1) * sizeof(WCHAR)),
82        "Is allocated with NetApiBufferAllocate");
83
84     /* Level 10 */
85     rc=pNetUserGetInfo(NULL, sAdminUserName, 10, (LPBYTE *)&ui10);
86     ok(rc == NERR_Success, "NetUserGetInfo: rc=%ld", rc);
87     ok(!lstrcmpW(sAdminUserName, ui10->usri10_name), "This is really user name");
88     pNetApiBufferSize(ui10, &dwSize);
89     ok(dwSize >= (sizeof(USER_INFO_10) +
90                   (lstrlenW(ui10->usri10_name) + 1 +
91                    lstrlenW(ui10->usri10_comment) + 1 +
92                    lstrlenW(ui10->usri10_usr_comment) + 1 +
93                    lstrlenW(ui10->usri10_full_name) + 1) * sizeof(WCHAR)),
94        "Is allocated with NetApiBufferAllocate");
95
96     pNetApiBufferFree(ui0);
97     pNetApiBufferFree(ui10);
98
99     /* errors handling */
100     rc=pNetUserGetInfo(NULL, sAdminUserName, 10000, (LPBYTE *)&ui0);
101     ok(rc == ERROR_INVALID_LEVEL,"Invalid Level: rc=%ld",rc);
102     rc=pNetUserGetInfo(NULL, sNonexistentUser, 0, (LPBYTE *)&ui0);
103     ok(rc == NERR_UserNotFound,"Invalid User Name: rc=%ld",rc);
104     todo_wine {
105         /* FIXME - Currently Wine can't verify whether the network path is good or bad */
106         rc=pNetUserGetInfo(sBadNetPath, sAdminUserName, 0, (LPBYTE *)&ui0);
107         ok(rc == ERROR_BAD_NETPATH,"Bad Network Path: rc=%ld",rc);
108     }
109     rc=pNetUserGetInfo(sEmptyStr, sAdminUserName, 0, (LPBYTE *)&ui0);
110     ok(rc == ERROR_BAD_NETPATH,"Bad Network Path: rc=%ld",rc);
111     rc=pNetUserGetInfo(sInvalidName, sAdminUserName, 0, (LPBYTE *)&ui0);
112     ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%ld",rc);
113     rc=pNetUserGetInfo(sInvalidName2, sAdminUserName, 0, (LPBYTE *)&ui0);
114     ok(rc == ERROR_INVALID_NAME,"Invalid Server Name: rc=%ld",rc);
115 }
116
117 /* checks Level 1 of NetQueryDisplayInformation */
118 void run_querydisplayinformation1_tests(void)
119 {
120     PNET_DISPLAY_USER Buffer, rec;
121     DWORD Result, EntryCount;
122     DWORD i = 0;
123     BOOL hasAdmin = FALSE;
124     BOOL hasGuest = FALSE;
125
126     if (!pNetQueryDisplayInformation)
127         return;
128
129     do
130     {
131         Result = pNetQueryDisplayInformation(
132             NULL, 1, i, 1000, MAX_PREFERRED_LENGTH, &EntryCount,
133             (PVOID *)&Buffer);
134
135         ok((Result == ERROR_SUCCESS) || (Result == ERROR_MORE_DATA),
136            "Information Retrieved");
137         rec = Buffer;
138         for(; EntryCount > 0; EntryCount--)
139         {
140             if (!lstrcmpW(rec->usri1_name, sAdminUserName))
141             {
142                 ok(!hasAdmin, "One admin user");
143                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set");
144                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set");
145                 hasAdmin = TRUE;
146             }
147             else if (!lstrcmpW(rec->usri1_name, sGuestUserName))
148             {
149                 ok(!hasGuest, "One guest record");
150                 ok(rec->usri1_flags & UF_SCRIPT, "UF_SCRIPT flag is set");
151                 ok(rec->usri1_flags & UF_NORMAL_ACCOUNT, "UF_NORMAL_ACCOUNT flag is set");
152                 hasGuest = TRUE;
153             }
154
155             i = rec->usri1_next_index;
156             rec++;
157         }
158
159         pNetApiBufferFree(Buffer);
160     } while (Result == ERROR_MORE_DATA);
161
162     ok(hasAdmin, "Has Administrator account");
163     ok(hasGuest, "Has Guest account");
164 }
165
166 START_TEST(access)
167 {
168     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
169     pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
170     pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
171     pNetQueryDisplayInformation=(void*)GetProcAddress(hnetapi32,"NetQueryDisplayInformation");
172     pNetUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetUserGetInfo");
173     if (!pNetApiBufferSize)
174         trace("It appears there is no netapi32 functionality on this platform\n");
175
176     if (init_access_tests()) {
177         run_usergetinfo_tests();
178         run_querydisplayinformation1_tests();
179     }
180 }