1 /*
2  * Copyright 2002 Andriy Palamarchuk
3  *
4  * Conformance test of the workstation 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 "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winnls.h"
28 #include "winresrc.h" /* Ensure we use Unicode defns with native headers */
29 #include "nb30.h"
30 #include "lmcons.h"
31 #include "lmerr.h"
32 #include "lmwksta.h"
33 #include "lmapibuf.h"
34 #include "lmjoin.h"
35 
36 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
37 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
38 static NET_API_STATUS (WINAPI *pNetpGetComputerName)(LPWSTR*)=NULL;
39 static NET_API_STATUS (WINAPI *pNetWkstaUserGetInfo)(LPWSTR,DWORD,PBYTE*)=NULL;
40 static NET_API_STATUS (WINAPI *pNetWkstaTransportEnum)(LPWSTR,DWORD,LPBYTE*,
41  DWORD,LPDWORD,LPDWORD,LPDWORD)=NULL;
42 static NET_API_STATUS (WINAPI *pNetGetJoinInformation)(LPCWSTR,LPWSTR*,PNETSETUP_JOIN_STATUS);
43 
44 static WCHAR user_name[UNLEN + 1];
45 static WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
46 
47 static BOOL init_wksta_tests(void)
48 {
49     DWORD dwSize;
50     BOOL rc;
51 
52     user_name[0] = 0;
53     dwSize = ARRAY_SIZE(user_name);
54     rc=GetUserNameW(user_name, &dwSize);
55     if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED) {
56         win_skip("GetUserNameW is not implemented\n");
57         return FALSE;
58     }
59     ok(rc, "User Name Retrieved\n");
60 
61     computer_name[0] = 0;
62     dwSize = ARRAY_SIZE(computer_name);
63     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
64     return TRUE;
65 }
66 
67 static void run_get_comp_name_tests(void)
68 {
69     LPWSTR ws = NULL;
70 
71     ok(pNetpGetComputerName(&ws) == NERR_Success, "Computer name is retrieved\n");
72     ok(!lstrcmpW(computer_name, ws), "This is really computer name\n");
73     pNetApiBufferFree(ws);
74 }
75 
76 static void run_wkstausergetinfo_tests(void)
77 {
78     LPWKSTA_USER_INFO_0 ui0 = NULL;
79     LPWKSTA_USER_INFO_1 ui1 = NULL;
80     LPWKSTA_USER_INFO_1101 ui1101 = NULL;
81     DWORD dwSize;
82     NET_API_STATUS rc;
83 
84     /* Level 0 */
85     rc = pNetWkstaUserGetInfo(NULL, 0, (LPBYTE *)&ui0);
86     if (rc == NERR_WkstaNotStarted)
87     {
88         skip("Workstation service not running\n");
89         return;
90     }
91     ok(!rc && ui0, "got %d and %p (expected NERR_Success and != NULL\n", rc, ui0);
92 
93     /* This failure occurred when I ran sshd as service and didn't authenticate
94      * Since the test dereferences ui0, the rest of this test is worthless
95      */
96     if (!ui0)
97     {
98         return;
99     }
100 
101     ok(!lstrcmpW(user_name, ui0->wkui0_username), "This is really user name\n");
102     pNetApiBufferSize(ui0, &dwSize);
103     ok(dwSize >= (sizeof(WKSTA_USER_INFO_0) +
104                  lstrlenW(ui0->wkui0_username) * sizeof(WCHAR)),
105        "Is allocated with NetApiBufferAllocate\n");
106 
107     /* Level 1 */
108     ok(pNetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui1) == NERR_Success,
109        "NetWkstaUserGetInfo is successful\n");
110     ok(lstrcmpW(ui1->wkui1_username, ui0->wkui0_username) == 0,
111        "the same name as returned for level 0\n");
112     pNetApiBufferSize(ui1, &dwSize);
113     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1) +
114                   (lstrlenW(ui1->wkui1_username) +
115                    lstrlenW(ui1->wkui1_logon_domain) +
116                    lstrlenW(ui1->wkui1_oth_domains) +
117                    lstrlenW(ui1->wkui1_logon_server)) * sizeof(WCHAR)),
118        "Is allocated with NetApiBufferAllocate\n");
119 
120     /* Level 1101 */
121     ok(pNetWkstaUserGetInfo(NULL, 1101, (LPBYTE *)&ui1101) == NERR_Success,
122        "NetWkstaUserGetInfo is successful\n");
123     ok(lstrcmpW(ui1101->wkui1101_oth_domains, ui1->wkui1_oth_domains) == 0,
124        "the same oth_domains as returned for level 1\n");
125     pNetApiBufferSize(ui1101, &dwSize);
126     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1101) +
127                  lstrlenW(ui1101->wkui1101_oth_domains) * sizeof(WCHAR)),
128        "Is allocated with NetApiBufferAllocate\n");
129 
130     pNetApiBufferFree(ui0);
131     pNetApiBufferFree(ui1);
132     pNetApiBufferFree(ui1101);
133 
134     /* errors handling */
135     ok(pNetWkstaUserGetInfo(NULL, 10000, (LPBYTE *)&ui0) == ERROR_INVALID_LEVEL,
136        "Invalid level\n");
137 }
138 
139 static void run_wkstatransportenum_tests(void)
140 {
141     LPBYTE bufPtr;
142     NET_API_STATUS apiReturn;
143     DWORD entriesRead, totalEntries;
144 
145     /* 1st check: is param 2 (level) correct? (only if param 5 passed?) */
146     apiReturn = pNetWkstaTransportEnum(NULL, 1, NULL, MAX_PREFERRED_LENGTH,
147         NULL, &totalEntries, NULL);
148     ok(apiReturn == ERROR_INVALID_LEVEL || apiReturn == ERROR_INVALID_PARAMETER,
149        "NetWkstaTransportEnum returned %d\n", apiReturn);
150 
151     /* 2nd check: is param 5 passed? (only if level passes?) */
152     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
153         NULL, &totalEntries, NULL);
154 
155     /* if no network adapter present, bail, the rest of the test will fail */
156     if (apiReturn == ERROR_NETWORK_UNREACHABLE)
157         return;
158 
159     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == ERROR_INVALID_PARAMETER,
160        "NetWkstaTransportEnum returned %d\n", apiReturn);
161 
162     /* 3rd check: is param 3 passed? */
163     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
164         NULL, NULL, NULL);
165     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == RPC_X_NULL_REF_POINTER || apiReturn == ERROR_INVALID_PARAMETER,
166        "NetWkstaTransportEnum returned %d\n", apiReturn);
167 
168     /* 4th check: is param 6 passed? */
169     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
170         &entriesRead, NULL, NULL);
171     ok(apiReturn == RPC_X_NULL_REF_POINTER,
172        "NetWkstaTransportEnum returned %d\n", apiReturn);
173 
174     /* final check: valid return, actually get data back */
175     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
176         &entriesRead, &totalEntries, NULL);
177     ok(apiReturn == NERR_Success || apiReturn == ERROR_NETWORK_UNREACHABLE || apiReturn == NERR_WkstaNotStarted,
178        "NetWkstaTransportEnum returned %d\n", apiReturn);
179     if (apiReturn == NERR_Success) {
180         /* WKSTA_TRANSPORT_INFO_0 *transports = (WKSTA_TRANSPORT_INFO_0 *)bufPtr; */
181 
182         ok(bufPtr != NULL, "got data back\n");
183         ok(entriesRead > 0, "read at least one transport\n");
184         ok(totalEntries > 0 || broken(totalEntries == 0) /* Win7 */,
185            "at least one transport\n");
186         pNetApiBufferFree(bufPtr);
187     }
188 }
189 
190 static void run_wkstajoininfo_tests(void)
191 {
192     NET_API_STATUS ret;
193     LPWSTR buffer = NULL;
194     NETSETUP_JOIN_STATUS buffertype = 0xdada;
195     /* NT4 doesn't have this function */
196     if (!pNetGetJoinInformation) {
197         win_skip("NetGetJoinInformation not available\n");
198         return;
199     }
200 
201     ret = pNetGetJoinInformation(NULL, NULL, NULL);
202     ok(ret == ERROR_INVALID_PARAMETER, "NetJoinGetInformation returned unexpected 0x%08x\n", ret);
203     ok(buffertype == 0xdada, "buffertype set to unexpected value %d\n", buffertype);
204 
205     ret = pNetGetJoinInformation(NULL, &buffer, &buffertype);
206     ok(ret == NERR_Success, "NetJoinGetInformation returned unexpected 0x%08x\n", ret);
207     ok(buffertype != 0xdada && buffertype != NetSetupUnknownStatus, "buffertype set to unexpected value %d\n", buffertype);
208     trace("workstation joined to %s with status %d\n", wine_dbgstr_w(buffer), buffertype);
209     pNetApiBufferFree(buffer);
210 }
211 
212 START_TEST(wksta)
213 {
214     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
215 
216     pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
217     pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
218     pNetpGetComputerName=(void*)GetProcAddress(hnetapi32,"NetpGetComputerName");
219     pNetWkstaUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetWkstaUserGetInfo");
220     pNetWkstaTransportEnum=(void*)GetProcAddress(hnetapi32,"NetWkstaTransportEnum");
221     pNetGetJoinInformation=(void*)GetProcAddress(hnetapi32,"NetGetJoinInformation");
222 
223     /* These functions were introduced with NT. It's safe to assume that
224      * if one is not available, none are.
225      */
226     if (!pNetApiBufferFree) {
227         win_skip("Needed functions are not available\n");
228         FreeLibrary(hnetapi32);
229         return;
230     }
231 
232     if (init_wksta_tests()) {
233         if (pNetpGetComputerName)
234             run_get_comp_name_tests();
235         else
236             win_skip("Function NetpGetComputerName not available\n");
237         run_wkstausergetinfo_tests();
238         run_wkstatransportenum_tests();
239         run_wkstajoininfo_tests();
240     }
241 
242     FreeLibrary(hnetapi32);
243 }
244