Documentation fix.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
35 static NET_API_STATUS (WINAPI *pNetApiBufferFree)(LPVOID)=NULL;
36 static NET_API_STATUS (WINAPI *pNetApiBufferSize)(LPVOID,LPDWORD)=NULL;
37 static NET_API_STATUS (WINAPI *pNetpGetComputerName)(LPWSTR*)=NULL;
38 static NET_API_STATUS (WINAPI *pNetWkstaUserGetInfo)(LPWSTR,DWORD,PBYTE*)=NULL;
39 static NET_API_STATUS (WINAPI *pNetWkstaTransportEnum)(LPWSTR,DWORD,LPBYTE*,
40  DWORD,LPDWORD,LPDWORD,LPDWORD)=NULL;
41
42 WCHAR user_name[UNLEN + 1];
43 WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
44
45 static int init_wksta_tests(void)
46 {
47     DWORD dwSize;
48     BOOL rc;
49
50     user_name[0] = 0;
51     dwSize = sizeof(user_name);
52     rc=GetUserNameW(user_name, &dwSize);
53     if (rc==FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
54         return 0;
55     ok(rc, "User Name Retrieved\n");
56
57     computer_name[0] = 0;
58     dwSize = sizeof(computer_name);
59     ok(GetComputerNameW(computer_name, &dwSize), "Computer Name Retrieved\n");
60     return 1;
61 }
62
63 static void run_get_comp_name_tests(void)
64 {
65     LPWSTR ws = NULL;
66     if (!pNetpGetComputerName)
67         return;
68
69     ok(pNetpGetComputerName(&ws) == NERR_Success, "Computer name is retrieved\n");
70     ok(!lstrcmpW(computer_name, ws), "This is really computer name\n");
71     pNetApiBufferFree(ws);
72 }
73
74 static void run_wkstausergetinfo_tests(void)
75 {
76     LPWKSTA_USER_INFO_0 ui0 = NULL;
77     LPWKSTA_USER_INFO_1 ui1 = NULL;
78     LPWKSTA_USER_INFO_1101 ui1101 = NULL;
79     DWORD dwSize;
80
81     if (!pNetWkstaUserGetInfo)
82         return;
83
84     /* Level 0 */
85     ok(pNetWkstaUserGetInfo(NULL, 0, (LPBYTE *)&ui0) == NERR_Success,
86        "NetWkstaUserGetInfo is successful\n");
87     ok(!lstrcmpW(user_name, ui0->wkui0_username), "This is really user name\n");
88     pNetApiBufferSize(ui0, &dwSize);
89     ok(dwSize >= (sizeof(WKSTA_USER_INFO_0) +
90                  lstrlenW(ui0->wkui0_username) * sizeof(WCHAR)),
91        "Is allocated with NetApiBufferAllocate\n");
92
93     /* Level 1 */
94     ok(pNetWkstaUserGetInfo(NULL, 1, (LPBYTE *)&ui1) == NERR_Success,
95        "NetWkstaUserGetInfo is successful\n");
96     ok(lstrcmpW(ui1->wkui1_username, ui0->wkui0_username) == 0,
97        "the same name as returned for level 0\n");
98     pNetApiBufferSize(ui1, &dwSize);
99     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1) +
100                   (lstrlenW(ui1->wkui1_username) +
101                    lstrlenW(ui1->wkui1_logon_domain) +
102                    lstrlenW(ui1->wkui1_oth_domains) +
103                    lstrlenW(ui1->wkui1_logon_server)) * sizeof(WCHAR)),
104        "Is allocated with NetApiBufferAllocate\n");
105
106     /* Level 1101 */
107     ok(pNetWkstaUserGetInfo(NULL, 1101, (LPBYTE *)&ui1101) == NERR_Success,
108        "NetWkstaUserGetInfo is successful\n");
109     ok(lstrcmpW(ui1101->wkui1101_oth_domains, ui1->wkui1_oth_domains) == 0,
110        "the same oth_domains as returned for level 1\n");
111     pNetApiBufferSize(ui1101, &dwSize);
112     ok(dwSize >= (sizeof(WKSTA_USER_INFO_1101) +
113                  lstrlenW(ui1101->wkui1101_oth_domains) * sizeof(WCHAR)),
114        "Is allocated with NetApiBufferAllocate\n");
115
116     pNetApiBufferFree(ui0);
117     pNetApiBufferFree(ui1);
118     pNetApiBufferFree(ui1101);
119
120     /* errors handling */
121     ok(pNetWkstaUserGetInfo(NULL, 10000, (LPBYTE *)&ui0) == ERROR_INVALID_LEVEL,
122        "Invalid level\n");
123 }
124
125 static void run_wkstatransportenum_tests(void)
126 {
127     LPBYTE bufPtr;
128     NET_API_STATUS apiReturn;
129     DWORD entriesRead, totalEntries;
130
131     if (!pNetWkstaTransportEnum)
132         return;
133
134     /* 1st check: is param 2 (level) correct? (only if param 5 passed?) */
135     apiReturn = pNetWkstaTransportEnum(NULL, 1, NULL, MAX_PREFERRED_LENGTH,
136         NULL, &totalEntries, NULL);
137     ok(apiReturn == ERROR_INVALID_LEVEL || apiReturn == ERROR_INVALID_PARAMETER,
138        "NetWkstaTransportEnum returned %ld\n", apiReturn);
139
140     /* 2nd check: is param 5 passed? (only if level passes?) */
141     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
142         NULL, &totalEntries, NULL);
143
144     /* if no network adapter present, bail, the rest of the test will fail */
145     if (apiReturn == ERROR_NETWORK_UNREACHABLE)
146         return;
147
148     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == ERROR_INVALID_PARAMETER,
149        "NetWkstaTransportEnum returned %ld\n", apiReturn);
150
151     /* 3rd check: is param 3 passed? */
152     apiReturn = pNetWkstaTransportEnum(NULL, 0, NULL, MAX_PREFERRED_LENGTH,
153         NULL, NULL, NULL);
154     ok(apiReturn == STATUS_ACCESS_VIOLATION || apiReturn == RPC_X_NULL_REF_POINTER,
155        "NetWkstaTransportEnum returned %ld\n", apiReturn);
156
157     /* 4th check: is param 6 passed? */
158     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
159         &entriesRead, NULL, NULL);
160     ok(apiReturn == RPC_X_NULL_REF_POINTER, "null pointer\n");
161
162     /* final check: valid return, actually get data back */
163     apiReturn = pNetWkstaTransportEnum(NULL, 0, &bufPtr, MAX_PREFERRED_LENGTH,
164         &entriesRead, &totalEntries, NULL);
165     ok(apiReturn == NERR_Success || apiReturn == ERROR_NETWORK_UNREACHABLE,
166        "NetWkstaTransportEnum returned %ld\n", apiReturn);
167     if (apiReturn == NERR_Success) {
168         /* WKSTA_TRANSPORT_INFO_0 *transports = (WKSTA_TRANSPORT_INFO_0 *)bufPtr; */
169
170         ok(bufPtr != NULL, "got data back\n");
171         ok(entriesRead > 0, "read at least one transport\n");
172         ok(totalEntries > 0, "at least one transport\n");
173         pNetApiBufferFree(bufPtr);
174     }
175 }
176
177 START_TEST(wksta)
178 {
179     HMODULE hnetapi32=LoadLibraryA("netapi32.dll");
180     pNetApiBufferFree=(void*)GetProcAddress(hnetapi32,"NetApiBufferFree");
181     pNetApiBufferSize=(void*)GetProcAddress(hnetapi32,"NetApiBufferSize");
182     pNetpGetComputerName=(void*)GetProcAddress(hnetapi32,"NetpGetComputerName");
183     pNetWkstaUserGetInfo=(void*)GetProcAddress(hnetapi32,"NetWkstaUserGetInfo");
184     pNetWkstaTransportEnum=(void*)GetProcAddress(hnetapi32,"NetWkstaTransportEnum");
185     if (!pNetApiBufferSize)
186         trace("It appears there is no netapi32 functionality on this platform\n");
187
188     if (init_wksta_tests()) {
189         run_get_comp_name_tests();
190         run_wkstausergetinfo_tests();
191         run_wkstatransportenum_tests();
192     }
193 }