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