1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#include <stdio.h>
#include <string.h>
#include <winpr/wtypes.h>
#include <winpr/sysinfo.h>
#include <winpr/error.h>
static BOOL Test_GetComputerName(void)
{
/**
* BOOL WINAPI GetComputerName(LPTSTR lpBuffer, LPDWORD lpnSize);
*
* GetComputerName retrieves the NetBIOS name of the local computer.
*
* lpBuffer [out]
* A pointer to a buffer that receives the computer name or the cluster virtual server name.
* The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
*
* lpnSize [in, out]
* On input, specifies the size of the buffer, in TCHARs.
* On output, the number of TCHARs copied to the destination buffer, not including the
* terminating null character. If the buffer is too small, the function fails and GetLastError
* returns ERROR_BUFFER_OVERFLOW. The lpnSize parameter specifies the size of the buffer
* required, including the terminating null character
*
*/
CHAR netbiosName1[MAX_COMPUTERNAME_LENGTH + 1];
CHAR netbiosName2[MAX_COMPUTERNAME_LENGTH + 1];
const DWORD netbiosBufferSize = sizeof(netbiosName1) / sizeof(CHAR);
DWORD dwSize = 0;
DWORD dwNameLength = 0;
DWORD dwError = 0;
memset(netbiosName1, 0xAA, netbiosBufferSize);
memset(netbiosName2, 0xBB, netbiosBufferSize);
/* test with null buffer and zero size (required if buffer is null) */
dwSize = 0;
if (GetComputerNameA(NULL, &dwSize) == TRUE)
{
fprintf(stderr, "%s: (1) GetComputerNameA unexpectedly succeeded with null buffer\n",
__func__);
return FALSE;
}
if ((dwError = GetLastError()) != ERROR_BUFFER_OVERFLOW)
{
fprintf(stderr,
"%s: (2) GetLastError returned 0x%08" PRIX32 " (expected ERROR_BUFFER_OVERFLOW)\n",
__func__, dwError);
return FALSE;
}
/* test with valid buffer and zero size */
dwSize = 0;
if (GetComputerNameA(netbiosName1, &dwSize) == TRUE)
{
fprintf(stderr,
"%s: (3) GetComputerNameA unexpectedly succeeded with zero size parameter\n",
__func__);
return FALSE;
}
if ((dwError = GetLastError()) != ERROR_BUFFER_OVERFLOW)
{
fprintf(stderr,
"%s: (4) GetLastError returned 0x%08" PRIX32 " (expected ERROR_BUFFER_OVERFLOW)\n",
__func__, dwError);
return FALSE;
}
/* check if returned size is valid: must be the size of the buffer required, including the
* terminating null character in this case */
if (dwSize < 2 || dwSize > netbiosBufferSize)
{
fprintf(stderr,
"%s: (5) GetComputerNameA returned wrong size %" PRIu32
" (expected something in the range from 2 to %" PRIu32 ")\n",
__func__, dwSize, netbiosBufferSize);
return FALSE;
}
dwNameLength = dwSize - 1;
/* test with returned size */
if (GetComputerNameA(netbiosName1, &dwSize) == FALSE)
{
fprintf(stderr, "%s: (6) GetComputerNameA failed with error: 0x%08" PRIX32 "\n", __func__,
GetLastError());
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength)
{
fprintf(stderr,
"%s: (7) GetComputerNameA returned wrong size %" PRIu32 " (expected %" PRIu32 ")\n",
__func__, dwSize, dwNameLength);
return FALSE;
}
/* check if string is correctly terminated */
if (netbiosName1[dwSize] != 0)
{
fprintf(stderr, "%s: (8) string termination error\n", __func__);
return FALSE;
}
/* test with real buffer size */
dwSize = netbiosBufferSize;
if (GetComputerNameA(netbiosName2, &dwSize) == FALSE)
{
fprintf(stderr, "%s: (9) GetComputerNameA failed with error: 0x%08" PRIX32 "\n", __func__,
GetLastError());
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength)
{
fprintf(stderr,
"%s: (10) GetComputerNameA returned wrong size %" PRIu32 " (expected %" PRIu32
")\n",
__func__, dwSize, dwNameLength);
return FALSE;
}
/* check if string is correctly terminated */
if (netbiosName2[dwSize] != 0)
{
fprintf(stderr, "%s: (11) string termination error\n", __func__);
return FALSE;
}
/* compare the results */
if (strcmp(netbiosName1, netbiosName2))
{
fprintf(stderr, "%s: (12) string compare mismatch\n", __func__);
return FALSE;
}
/* test with off by one buffer size */
dwSize = dwNameLength;
if (GetComputerNameA(netbiosName1, &dwSize) == TRUE)
{
fprintf(stderr,
"%s: (13) GetComputerNameA unexpectedly succeeded with limited buffer size\n",
__func__);
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength + 1)
{
fprintf(stderr,
"%s: (14) GetComputerNameA returned wrong size %" PRIu32 " (expected %" PRIu32
")\n",
__func__, dwSize, dwNameLength + 1);
return FALSE;
}
return TRUE;
}
static BOOL Test_GetComputerNameEx_Format(COMPUTER_NAME_FORMAT format)
{
/**
* BOOL WINAPI GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, LPTSTR lpBuffer, LPDWORD
* lpnSize);
*
* Retrieves a NetBIOS or DNS name associated with the local computer.
*
* NameType [in]
* ComputerNameNetBIOS
* ComputerNameDnsHostname
* ComputerNameDnsDomain
* ComputerNameDnsFullyQualified
* ComputerNamePhysicalNetBIOS
* ComputerNamePhysicalDnsHostname
* ComputerNamePhysicalDnsDomain
* ComputerNamePhysicalDnsFullyQualified
*
* lpBuffer [out]
* A pointer to a buffer that receives the computer name or the cluster virtual server name.
* The length of the name may be greater than MAX_COMPUTERNAME_LENGTH characters because DNS
* allows longer names. To ensure that this buffer is large enough, set this parameter to NULL
* and use the required buffer size returned in the lpnSize parameter.
*
* lpnSize [in, out]
* On input, specifies the size of the buffer, in TCHARs.
* On output, receives the number of TCHARs copied to the destination buffer, not including the
* terminating null character. If the buffer is too small, the function fails and GetLastError
* returns ERROR_MORE_DATA. This parameter receives the size of the buffer required, including
* the terminating null character. If lpBuffer is NULL, this parameter must be zero.
*
*/
CHAR computerName1[255 + 1];
CHAR computerName2[255 + 1];
const DWORD nameBufferSize = sizeof(computerName1) / sizeof(CHAR);
DWORD dwSize = 0;
DWORD dwMinSize = 0;
DWORD dwNameLength = 0;
DWORD dwError = 0;
memset(computerName1, 0xAA, nameBufferSize);
memset(computerName2, 0xBB, nameBufferSize);
if (format == ComputerNameDnsDomain || format == ComputerNamePhysicalDnsDomain)
{
/* domain names may be empty, terminating null only */
dwMinSize = 1;
}
else
{
/* computer names must be at least 1 character */
dwMinSize = 2;
}
/* test with null buffer and zero size (required if buffer is null) */
dwSize = 0;
if (GetComputerNameExA(format, NULL, &dwSize) == TRUE)
{
fprintf(stderr, "%s: (1/%d) GetComputerNameExA unexpectedly succeeded with null buffer\n",
__func__, format);
return FALSE;
}
if ((dwError = GetLastError()) != ERROR_MORE_DATA)
{
fprintf(stderr,
"%s: (2/%d) GetLastError returned 0x%08" PRIX32 " (expected ERROR_MORE_DATA)\n",
__func__, format, dwError);
return FALSE;
}
/* test with valid buffer and zero size */
dwSize = 0;
if (GetComputerNameExA(format, computerName1, &dwSize) == TRUE)
{
fprintf(stderr,
"%s: (3/%d) GetComputerNameExA unexpectedly succeeded with zero size parameter\n",
__func__, format);
return FALSE;
}
if ((dwError = GetLastError()) != ERROR_MORE_DATA)
{
fprintf(stderr,
"%s: (4/%d) GetLastError returned 0x%08" PRIX32 " (expected ERROR_MORE_DATA)\n",
__func__, format, dwError);
return FALSE;
}
/* check if returned size is valid: must be the size of the buffer required, including the
* terminating null character in this case */
if (dwSize < dwMinSize || dwSize > nameBufferSize)
{
fprintf(stderr,
"%s: (5/%d) GetComputerNameExA returned wrong size %" PRIu32
" (expected something in the range from %" PRIu32 " to %" PRIu32 ")\n",
__func__, format, dwSize, dwMinSize, nameBufferSize);
return FALSE;
}
dwNameLength = dwSize - 1;
/* test with returned size */
if (GetComputerNameExA(format, computerName1, &dwSize) == FALSE)
{
fprintf(stderr, "%s: (6/%d) GetComputerNameExA failed with error: 0x%08" PRIX32 "\n",
__func__, format, GetLastError());
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength)
{
fprintf(stderr,
"%s: (7/%d) GetComputerNameExA returned wrong size %" PRIu32 " (expected %" PRIu32
")\n",
__func__, format, dwSize, dwNameLength);
return FALSE;
}
/* check if string is correctly terminated */
if (computerName1[dwSize] != 0)
{
fprintf(stderr, "%s: (8/%d) string termination error\n", __func__, format);
return FALSE;
}
/* test with real buffer size */
dwSize = nameBufferSize;
if (GetComputerNameExA(format, computerName2, &dwSize) == FALSE)
{
fprintf(stderr, "%s: (9/%d) GetComputerNameExA failed with error: 0x%08" PRIX32 "\n",
__func__, format, GetLastError());
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength)
{
fprintf(stderr,
"%s: (10/%d) GetComputerNameExA returned wrong size %" PRIu32 " (expected %" PRIu32
")\n",
__func__, format, dwSize, dwNameLength);
return FALSE;
}
/* check if string is correctly terminated */
if (computerName2[dwSize] != 0)
{
fprintf(stderr, "%s: (11/%d) string termination error\n", __func__, format);
return FALSE;
}
/* compare the results */
if (strcmp(computerName1, computerName2))
{
fprintf(stderr, "%s: (12/%d) string compare mismatch\n", __func__, format);
return FALSE;
}
/* test with off by one buffer size */
dwSize = dwNameLength;
if (GetComputerNameExA(format, computerName1, &dwSize) == TRUE)
{
fprintf(stderr,
"%s: (13/%d) GetComputerNameExA unexpectedly succeeded with limited buffer size\n",
__func__, format);
return FALSE;
}
/* check if returned size is valid */
if (dwSize != dwNameLength + 1)
{
fprintf(stderr,
"%s: (14/%d) GetComputerNameExA returned wrong size %" PRIu32 " (expected %" PRIu32
")\n",
__func__, format, dwSize, dwNameLength + 1);
return FALSE;
}
return TRUE;
}
int TestGetComputerName(int argc, char* argv[])
{
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!Test_GetComputerName())
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNameNetBIOS))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNameDnsHostname))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNameDnsDomain))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNameDnsFullyQualified))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalNetBIOS))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsHostname))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsDomain))
return -1;
if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsFullyQualified))
return -1;
return 0;
}
|