crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / netapi32 / tests / wksta.c
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 int init_wksta_tests(void)
48 {
49     DWORD dwSize;
50     BOOL rc;
51
52     user_name[0] = 0;
53     dwSize = sizeof(user_name)/sizeof(user_name[0]);
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 0;
58     }
59     ok(rc, "User Name Retrieved\n");
60
61     computer_name[0] = 0;
62     dwSize = sizeof(computer_name)/sizeof(computer_name[0]);
63     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
64     return 1;
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
83     /* Level 0 */
84     ok(pNetWkstaUserGetInfo(NULL, 0, (LPBYTE *)&ui0) == NERR_Success,
85        "NetWkstaUserGetInfo is unsuccessful\n");
86
87     ok(ui0 != NULL, "ui0 is NULL\n");
88     /* This failure occurred when I ran sshd as service and didn't authenticate
89      * Since the test dereferences ui0, the rest of this test is worthless
90      */
91     if (!ui0)
92     {
93         return;
94     }
95
96     ok(!lstrcmpW(user_name, ui0->wkui0_username), "This is really user name\n");
97     pNetApiBufferSize(ui0, &dwSize);
98     ok(dwSize >= (sizeof(WKSTA_USER_INFO_0) +
99                  lstrlenW(ui0->wkui0_username) * sizeof(WCHAR)),
100        "Is allocated with NetApiBufferAllocate\n");
101
102     /* Level 1 */
103     ok(pNetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui1) == NERR_Success,
104        "NetWkstaUserGetInfo is successful\n");
105     ok(lstrcmpW(ui1->wkui1_username, ui0->wkui0_username) == 0,
106        "the same name as returned for level 0\n");
107     pNetApiBufferSize(ui1, &dwSize);
108     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1) +
109                   (lstrlenW(ui1->wkui1_username) +
110                    lstrlenW(ui1->wkui1_logon_domain) +
111                    lstrlenW(ui1->wkui1_oth_domains) +
112                    lstrlenW(ui1->wkui1_logon_server)) * sizeof(WCHAR)),
113        "Is allocated with NetApiBufferAllocate\n");
114
115     /* Level 1101 */
116     ok(pNetWkstaUserGetInfo(NULL, 1101, (LPBYTE *)&ui1101) == NERR_Success,
117        "NetWkstaUserGetInfo is successful\n");
118     ok(lstrcmpW(ui1101->wkui1101_oth_domains, ui1->wkui1_oth_domains) == 0,
119        "the same oth_domains as returned for level 1\n");
120     pNetApiBufferSize(ui1101, &dwSize);
121     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1101) +
122                  lstrlenW(ui1101->wkui1101_oth_domains) * sizeof(WCHAR)),
123        "Is allocated with NetApiBufferAllocate\n");
124
125     pNetApiBufferFree(ui0);
126     pNetApiBufferFree(ui1);
127     pNetApiBufferFree(ui1101);
128
129     /* errors handling */
130     ok(pNetWkstaUserGetInfo(NULL, 10000, (LPBYTE *)&ui0) == ERROR_INVALID_LEVEL,
131        "Invalid level\n");
132 }
133
134 static void run_wkstatransportenum_tests(void)
135 {
136     LPBYTE bufPtr;
137     NET_API_STATUS apiReturn;
138     DWORD entriesRead, totalEntries;
139
140     /* 1st check: is param 2 (level) correct? (only if param 5 passed?) */
141     apiReturn = pNetWkstaTransportEnum(NULL, 1, NULL, MAX_PREFERRED_LENGTH,
142         NULL, &totalEntries, NULL);
143     ok(apiReturn == ERROR_INVALID_LEVEL || apiReturn == ERROR_INVALID_PARAMETER,
144        "NetWkstaTransportEnum returned %d\n", apiReturn);
145
146     /* 2nd check: is param 5 passed? (only if level passes?) */
147     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
148         NULL, &totalEntries, NULL);
149
150     /* if no network adapter present, bail, the rest of the test will fail */
151     if (apiReturn == ERROR_NETWORK_UNREACHABLE)
152         return;
153
154     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == ERROR_INVALID_PARAMETER,
155        "NetWkstaTransportEnum returned %d\n", apiReturn);
156
157     /* 3rd check: is param 3 passed? */
158     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
159         NULL, NULL, NULL);
160     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == RPC_X_NULL_REF_POINTER || apiReturn == ERROR_INVALID_PARAMETER,
161        "NetWkstaTransportEnum returned %d\n", apiReturn);
162
163     /* 4th check: is param 6 passed? */
164     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
165         &entriesRead, NULL, NULL);
166     ok(apiReturn == RPC_X_NULL_REF_POINTER, "null pointer\n");
167
168     /* final check: valid return, actually get data back */
169     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
170         &entriesRead, &totalEntries, NULL);
171     ok(apiReturn == NERR_Success || apiReturn == ERROR_NETWORK_UNREACHABLE,
172        "NetWkstaTransportEnum returned %d\n", apiReturn);
173     if (apiReturn == NERR_Success) {
174         /* WKSTA_TRANSPORT_INFO_0 *transports = (WKSTA_TRANSPORT_INFO_0 *)bufPtr; */
175
176         ok(bufPtr != NULL, "got data back\n");
177         ok(entriesRead > 0, "read at least one transport\n");
178         ok(totalEntries > 0 || broken(totalEntries == 0) /* Win7 */,
179            "at least one transport\n");
180         pNetApiBufferFree(bufPtr);
181     }
182 }
183
184 static void run_wkstajoininfo_tests(void)
185 {
186     NET_API_STATUS ret;
187     LPWSTR buffer = NULL;
188     NETSETUP_JOIN_STATUS buffertype = 0xdada;
189     /* NT4 doesn't have this function */
190     if (!pNetGetJoinInformation) {
191         win_skip("NetGetJoinInformation not available\n");
192         return;
193     }
194
195     ret = pNetGetJoinInformation(NULL, NULL, NULL);
196     ok(ret == ERROR_INVALID_PARAMETER, "NetJoinGetInformation returned unexpected 0x%08x\n", ret);
197     ok(buffertype == 0xdada, "buffertype set to unexpected value %d\n", buffertype);
198
199     ret = pNetGetJoinInformation(NULL, &buffer, &buffertype);
200     ok(ret == NERR_Success, "NetJoinGetInformation returned unexpected 0x%08x\n", ret);
201     ok(buffertype != 0xdada && buffertype != NetSetupUnknownStatus, "buffertype set to unexpected value %d\n", buffertype);
202     trace("workstation joined to %s with status %d\n", wine_dbgstr_w(buffer), buffertype);
203     pNetApiBufferFree(buffer);
204 }
205
206 START_TEST(wksta)
207 {
208     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
209
210     pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
211     pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
212     pNetpGetComputerName=(void*)GetProcAddress(hnetapi32,"NetpGetComputerName");
213     pNetWkstaUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetWkstaUserGetInfo");
214     pNetWkstaTransportEnum=(void*)GetProcAddress(hnetapi32,"NetWkstaTransportEnum");
215     pNetGetJoinInformation=(void*)GetProcAddress(hnetapi32,"NetGetJoinInformation");
216
217     /* These functions were introduced with NT. It's safe to assume that
218      * if one is not available, none are.
219      */
220     if (!pNetApiBufferFree) {
221         win_skip("Needed functions are not available\n");
222         FreeLibrary(hnetapi32);
223         return;
224     }
225
226     if (init_wksta_tests()) {
227         if (pNetpGetComputerName)
228             run_get_comp_name_tests();
229         else
230             win_skip("Function NetpGetComputerName not available\n");
231         run_wkstausergetinfo_tests();
232         run_wkstatransportenum_tests();
233         run_wkstajoininfo_tests();
234     }
235
236     FreeLibrary(hnetapi32);
237 }