3 #include <winpr/wtypes.h>
4 #include <winpr/sysinfo.h>
5 #include <winpr/error.h>
7 static BOOL Test_GetComputerName(
void)
27 CHAR netbiosName1[MAX_COMPUTERNAME_LENGTH + 1];
28 CHAR netbiosName2[MAX_COMPUTERNAME_LENGTH + 1];
29 const DWORD netbiosBufferSize =
sizeof(netbiosName1) /
sizeof(CHAR);
31 DWORD dwNameLength = 0;
34 memset(netbiosName1, 0xAA, netbiosBufferSize);
35 memset(netbiosName2, 0xBB, netbiosBufferSize);
39 if (GetComputerNameA(NULL, &dwSize) == TRUE)
41 (void)fprintf(stderr,
"%s: (1) GetComputerNameA unexpectedly succeeded with null buffer\n",
45 if ((dwError = GetLastError()) != ERROR_BUFFER_OVERFLOW)
48 "%s: (2) GetLastError returned 0x%08" PRIX32
49 " (expected ERROR_BUFFER_OVERFLOW)\n",
56 if (GetComputerNameA(netbiosName1, &dwSize) == TRUE)
59 "%s: (3) GetComputerNameA unexpectedly succeeded with zero size parameter\n",
63 if ((dwError = GetLastError()) != ERROR_BUFFER_OVERFLOW)
66 "%s: (4) GetLastError returned 0x%08" PRIX32
67 " (expected ERROR_BUFFER_OVERFLOW)\n",
73 if (dwSize < 2 || dwSize > netbiosBufferSize)
76 "%s: (5) GetComputerNameA returned wrong size %" PRIu32
77 " (expected something in the range from 2 to %" PRIu32
")\n",
78 __func__, dwSize, netbiosBufferSize);
81 dwNameLength = dwSize - 1;
84 if (GetComputerNameA(netbiosName1, &dwSize) == FALSE)
86 (void)fprintf(stderr,
"%s: (6) GetComputerNameA failed with error: 0x%08" PRIX32
"\n",
87 __func__, GetLastError());
91 if (dwSize != dwNameLength)
94 "%s: (7) GetComputerNameA returned wrong size %" PRIu32
" (expected %" PRIu32
96 __func__, dwSize, dwNameLength);
100 if (netbiosName1[dwSize] != 0)
102 (void)fprintf(stderr,
"%s: (8) string termination error\n", __func__);
107 dwSize = netbiosBufferSize;
108 if (GetComputerNameA(netbiosName2, &dwSize) == FALSE)
110 (void)fprintf(stderr,
"%s: (9) GetComputerNameA failed with error: 0x%08" PRIX32
"\n",
111 __func__, GetLastError());
115 if (dwSize != dwNameLength)
117 (void)fprintf(stderr,
118 "%s: (10) GetComputerNameA returned wrong size %" PRIu32
" (expected %" PRIu32
120 __func__, dwSize, dwNameLength);
124 if (netbiosName2[dwSize] != 0)
126 (void)fprintf(stderr,
"%s: (11) string termination error\n", __func__);
131 if (strcmp(netbiosName1, netbiosName2) != 0)
133 (void)fprintf(stderr,
"%s: (12) string compare mismatch\n", __func__);
138 dwSize = dwNameLength;
139 if (GetComputerNameA(netbiosName1, &dwSize) == TRUE)
141 (void)fprintf(stderr,
142 "%s: (13) GetComputerNameA unexpectedly succeeded with limited buffer size\n",
147 if (dwSize != dwNameLength + 1)
149 (void)fprintf(stderr,
150 "%s: (14) GetComputerNameA returned wrong size %" PRIu32
" (expected %" PRIu32
152 __func__, dwSize, dwNameLength + 1);
159 static BOOL Test_GetComputerNameEx_Format(COMPUTER_NAME_FORMAT format)
192 CHAR computerName1[255 + 1];
193 CHAR computerName2[255 + 1];
195 const DWORD nameBufferSize =
sizeof(computerName1) /
sizeof(CHAR);
198 DWORD dwNameLength = 0;
201 memset(computerName1, 0xAA, nameBufferSize);
202 memset(computerName2, 0xBB, nameBufferSize);
204 if (format == ComputerNameDnsDomain || format == ComputerNamePhysicalDnsDomain)
217 if (GetComputerNameExA(format, NULL, &dwSize) == TRUE)
219 (void)fprintf(stderr,
220 "%s: (1/%d) GetComputerNameExA unexpectedly succeeded with null buffer\n",
224 if ((dwError = GetLastError()) != ERROR_MORE_DATA)
227 stderr,
"%s: (2/%d) GetLastError returned 0x%08" PRIX32
" (expected ERROR_MORE_DATA)\n",
228 __func__, format, dwError);
234 if (GetComputerNameExA(format, computerName1, &dwSize) == TRUE)
238 "%s: (3/%d) GetComputerNameExA unexpectedly succeeded with zero size parameter\n",
242 if ((dwError = GetLastError()) != ERROR_MORE_DATA)
245 stderr,
"%s: (4/%d) GetLastError returned 0x%08" PRIX32
" (expected ERROR_MORE_DATA)\n",
246 __func__, format, dwError);
251 if (dwSize < dwMinSize || dwSize > nameBufferSize)
253 (void)fprintf(stderr,
254 "%s: (5/%d) GetComputerNameExA returned wrong size %" PRIu32
255 " (expected something in the range from %" PRIu32
" to %" PRIu32
")\n",
256 __func__, format, dwSize, dwMinSize, nameBufferSize);
259 dwNameLength = dwSize - 1;
262 if (GetComputerNameExA(format, computerName1, &dwSize) == FALSE)
264 (void)fprintf(stderr,
"%s: (6/%d) GetComputerNameExA failed with error: 0x%08" PRIX32
"\n",
265 __func__, format, GetLastError());
269 if (dwSize != dwNameLength)
271 (void)fprintf(stderr,
272 "%s: (7/%d) GetComputerNameExA returned wrong size %" PRIu32
273 " (expected %" PRIu32
")\n",
274 __func__, format, dwSize, dwNameLength);
278 if (computerName1[dwSize] != 0)
280 (void)fprintf(stderr,
"%s: (8/%d) string termination error\n", __func__, format);
285 dwSize = nameBufferSize;
286 if (GetComputerNameExA(format, computerName2, &dwSize) == FALSE)
288 (void)fprintf(stderr,
"%s: (9/%d) GetComputerNameExA failed with error: 0x%08" PRIX32
"\n",
289 __func__, format, GetLastError());
293 if (dwSize != dwNameLength)
295 (void)fprintf(stderr,
296 "%s: (10/%d) GetComputerNameExA returned wrong size %" PRIu32
297 " (expected %" PRIu32
")\n",
298 __func__, format, dwSize, dwNameLength);
302 if (computerName2[dwSize] != 0)
304 (void)fprintf(stderr,
"%s: (11/%d) string termination error\n", __func__, format);
309 if (strcmp(computerName1, computerName2) != 0)
311 (void)fprintf(stderr,
"%s: (12/%d) string compare mismatch\n", __func__, format);
316 dwSize = dwNameLength;
317 if (GetComputerNameExA(format, computerName1, &dwSize) == TRUE)
321 "%s: (13/%d) GetComputerNameExA unexpectedly succeeded with limited buffer size\n",
326 if (dwSize != dwNameLength + 1)
328 (void)fprintf(stderr,
329 "%s: (14/%d) GetComputerNameExA returned wrong size %" PRIu32
330 " (expected %" PRIu32
")\n",
331 __func__, format, dwSize, dwNameLength + 1);
338 int TestGetComputerName(
int argc,
char* argv[])
344 if (!Test_GetComputerName())
347 if (!Test_GetComputerNameEx_Format(ComputerNameNetBIOS))
350 if (!Test_GetComputerNameEx_Format(ComputerNameDnsHostname))
353 if (!Test_GetComputerNameEx_Format(ComputerNameDnsDomain))
356 if (!Test_GetComputerNameEx_Format(ComputerNameDnsFullyQualified))
359 if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalNetBIOS))
362 if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsHostname))
365 if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsDomain))
368 if (!Test_GetComputerNameEx_Format(ComputerNamePhysicalDnsFullyQualified))