ReactOS  0.4.14-dev-390-g34947ad
SetComputerNameExW.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE: Tests for the SetComputerNameExW API
5  * COPYRIGHT: Victor Martinez Calvo (victor.martinez@reactos.org)
6  * Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
7  */
8 #include <apitest.h>
9 
10 #define WIN32_NO_STATUS
11 #include <stdio.h>
12 #include <ndk/rtltypes.h>
13 
14 #undef _WIN32_WINNT
15 #define _WIN32_WINNT 0x0600
16 #include <winreg.h>
17 
18 static HKEY OpenHostNameKey(void)
19 {
20  static const WCHAR
21  RegHostNameKey[] = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
22  HKEY hKey = NULL;
23  LONG Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegHostNameKey, 0, KEY_ALL_ACCESS, &hKey);
24  if (!Error)
25  return hKey;
26  return NULL;
27 }
28 
30 {
31  static const WCHAR
32  RegComputerNameKey[] = L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName";
33  HKEY hKey = NULL;
34  LONG Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegComputerNameKey, 0, KEY_ALL_ACCESS, &hKey);
35  if (!Error)
36  return hKey;
37  return NULL;
38 }
39 
40 static void DoTestComputerName(HKEY hKeyHN, HKEY hKeyCN, LPCWSTR pszNewName, BOOL bValid)
41 {
42  LONG Error;
43  BOOL ret;
44  DWORD cbData;
45  WCHAR szNVHostNameOld[MAX_PATH], szNVHostNameNew[MAX_PATH];
46  WCHAR szHostNameOld[MAX_PATH], szHostNameNew[MAX_PATH];
47  WCHAR szComputerNameOld[MAX_PATH], szComputerNameNew[MAX_PATH];
48 
49  trace("Testing '%S':\n", pszNewName);
50 
51  /* Get Old NV Hostname */
52  szNVHostNameOld[0] = UNICODE_NULL;
53  cbData = sizeof(szNVHostNameOld);
54  Error = RegQueryValueExW(hKeyHN, L"NV Hostname", NULL, NULL, (LPBYTE)szNVHostNameOld, &cbData);
56  ok(szNVHostNameOld[0], "szNVHostNameOld is empty\n");
57 
58  /* Get Old Hostname */
59  szHostNameOld[0] = UNICODE_NULL;
60  cbData = sizeof(szHostNameOld);
61  Error = RegQueryValueExW(hKeyHN, L"Hostname", NULL, NULL, (LPBYTE)szHostNameOld, &cbData);
63  ok(szHostNameOld[0], "szHostNameOld is empty\n");
64 
65  /* Get Old Computer Name */
66  szComputerNameOld[0] = UNICODE_NULL;
67  cbData = sizeof(szComputerNameOld);
68  Error = RegQueryValueExW(hKeyCN, L"ComputerName", NULL, NULL, (LPBYTE)szComputerNameOld, &cbData);
70  ok(szComputerNameOld[0], "szComputerNameOld is empty\n");
71 
72  /* Change the value */
73  SetLastError(0xDEADFACE);
74  ret = SetComputerNameExW(ComputerNamePhysicalDnsHostname, pszNewName);
75  ok_int(ret, bValid);
76  Error = GetLastError();
77  if (bValid)
79  else
81 
82  /* Get New NV Hostname */
83  szNVHostNameNew[0] = UNICODE_NULL;
84  cbData = sizeof(szNVHostNameNew);
85  Error = RegQueryValueExW(hKeyHN, L"NV Hostname", NULL, NULL, (LPBYTE)szNVHostNameNew, &cbData);
87  if (bValid)
88  {
89  ok(szNVHostNameNew[0], "szNVHostNameNew is empty\n");
90  ok(lstrcmpW(szNVHostNameNew, pszNewName) == 0,
91  "szNVHostNameNew '%S' should be pszNewName '%S'\n", szNVHostNameNew, pszNewName);
92  }
93 
94  /* Get New Hostname */
95  szHostNameNew[0] = UNICODE_NULL;
96  cbData = sizeof(szHostNameNew);
97  Error = RegQueryValueExW(hKeyHN, L"Hostname", NULL, NULL, (LPBYTE)szHostNameNew, &cbData);
99  if (bValid)
100  {
101  ok(szHostNameNew[0], "szHostNameNew is empty\n");
102  ok(lstrcmpW(szHostNameNew, szHostNameOld) == 0,
103  "szHostNameNew '%S' should be szHostNameOld '%S'\n", szHostNameNew, szHostNameOld);
104  }
105 
106  /* Get New Computer Name */
107  szComputerNameNew[0] = UNICODE_NULL;
108  cbData = sizeof(szComputerNameNew);
109  Error = RegQueryValueExW(hKeyCN, L"ComputerName", NULL, NULL, (LPBYTE)szComputerNameNew, &cbData);
111  if (bValid)
112  {
113  ok(szComputerNameNew[0], "szComputerNameNew is empty\n");
114  if (lstrlenW(pszNewName) > MAX_COMPUTERNAME_LENGTH)
115  {
116  WCHAR szTruncatedNewName[MAX_COMPUTERNAME_LENGTH + 1];
117  lstrcpynW(szTruncatedNewName, pszNewName, ARRAYSIZE(szTruncatedNewName));
118  ok(lstrcmpiW(szComputerNameNew, szTruncatedNewName) == 0,
119  "szComputerNameNew '%S' should be szTruncatedNewName '%S'\n",
120  szComputerNameNew, szTruncatedNewName);
121  }
122  else
123  {
124  ok(lstrcmpiW(szComputerNameNew, pszNewName) == 0,
125  "szComputerNameNew '%S' should be pszNewName '%S'\n",
126  szComputerNameNew, pszNewName);
127  }
128  }
129 
130  /* Restore the registry values */
131  cbData = (lstrlenW(szNVHostNameOld) + 1) * sizeof(WCHAR);
132  Error = RegSetValueExW(hKeyHN, L"NV Hostname", 0, REG_SZ, (LPBYTE)szNVHostNameOld, cbData);
134 
135  cbData = (lstrlenW(szHostNameOld) + 1) * sizeof(WCHAR);
136  Error = RegSetValueExW(hKeyHN, L"Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
138 
139  cbData = (lstrlenW(szComputerNameOld) + 1) * sizeof(WCHAR);
140  Error = RegSetValueExW(hKeyCN, L"ComputerName", 0, REG_SZ, (LPBYTE)szComputerNameOld, cbData);
142 }
143 
145 {
146  HKEY hKeyHN, hKeyCN;
147  static const WCHAR ValidSymbols[] = L"-_";
148  static const WCHAR InvalidSymbols[] = L"\"/\\[]:|<>+=;,?";
149  WCHAR szName[32];
150  INT i, cchValidSymbols, cchInvalidSymbols;
151  LONG Error;
152 
153  /* Open keys */
154  hKeyHN = OpenHostNameKey();
155  ok(hKeyHN != NULL, "hKeyHN is NULL\n");
156  hKeyCN = OpenComputerNameKey();
157  ok(hKeyCN != NULL, "hKeyCN is NULL\n");
158  if (!hKeyHN || !hKeyCN)
159  {
160  if (hKeyHN)
161  RegCloseKey(hKeyHN);
162  if (hKeyCN)
163  RegCloseKey(hKeyCN);
164  skip("Unable to open keys. Missing Admin rights?\n");
165  return;
166  }
167 
168  cchValidSymbols = lstrlenW(ValidSymbols);
169  cchInvalidSymbols = lstrlenW(InvalidSymbols);
170 
171  /* Test names */
172 
173  DoTestComputerName(hKeyHN, hKeyCN, L"SRVROSTEST", TRUE);
174  DoTestComputerName(hKeyHN, hKeyCN, L"SrvRosTest", TRUE);
175 
176  DoTestComputerName(hKeyHN, hKeyCN, L"", FALSE);
177  DoTestComputerName(hKeyHN, hKeyCN, L"a", TRUE);
178  DoTestComputerName(hKeyHN, hKeyCN, L"A", TRUE);
179  DoTestComputerName(hKeyHN, hKeyCN, L"1", FALSE); // numeric-only
180  DoTestComputerName(hKeyHN, hKeyCN, L"@", FALSE);
181  DoTestComputerName(hKeyHN, hKeyCN, L".", FALSE);
182 
183  DoTestComputerName(hKeyHN, hKeyCN, L" ", FALSE);
184  DoTestComputerName(hKeyHN, hKeyCN, L"\t", FALSE);
185  DoTestComputerName(hKeyHN, hKeyCN, L"\b", FALSE);
186 
187  DoTestComputerName(hKeyHN, hKeyCN, L" a", FALSE);
188  DoTestComputerName(hKeyHN, hKeyCN, L" A", FALSE);
189  DoTestComputerName(hKeyHN, hKeyCN, L" 1", FALSE);
190  DoTestComputerName(hKeyHN, hKeyCN, L"\ta", FALSE);
191  DoTestComputerName(hKeyHN, hKeyCN, L"\tA", FALSE);
192  DoTestComputerName(hKeyHN, hKeyCN, L"\t1", FALSE);
193  DoTestComputerName(hKeyHN, hKeyCN, L"\ba", FALSE);
194  DoTestComputerName(hKeyHN, hKeyCN, L"\bA", FALSE);
195  DoTestComputerName(hKeyHN, hKeyCN, L"\b1", FALSE);
196 
197  DoTestComputerName(hKeyHN, hKeyCN, L"a ", FALSE);
198  DoTestComputerName(hKeyHN, hKeyCN, L"A ", FALSE);
199  DoTestComputerName(hKeyHN, hKeyCN, L"1 ", FALSE);
200  DoTestComputerName(hKeyHN, hKeyCN, L"a\t", FALSE);
201  DoTestComputerName(hKeyHN, hKeyCN, L"A\t", FALSE);
202  DoTestComputerName(hKeyHN, hKeyCN, L"1\t", FALSE);
203  DoTestComputerName(hKeyHN, hKeyCN, L"a\b", FALSE);
204  DoTestComputerName(hKeyHN, hKeyCN, L"A\b", FALSE);
205  DoTestComputerName(hKeyHN, hKeyCN, L"1\b", FALSE);
206 
207  DoTestComputerName(hKeyHN, hKeyCN, L"a c", FALSE);
208  DoTestComputerName(hKeyHN, hKeyCN, L"A C", FALSE);
209  DoTestComputerName(hKeyHN, hKeyCN, L"1 c", FALSE);
210 
211  DoTestComputerName(hKeyHN, hKeyCN, L"a\tc", FALSE);
212  DoTestComputerName(hKeyHN, hKeyCN, L"A\tC", FALSE);
213  DoTestComputerName(hKeyHN, hKeyCN, L"1\tc", FALSE);
214 
215  DoTestComputerName(hKeyHN, hKeyCN, L"a\bc", FALSE);
216  DoTestComputerName(hKeyHN, hKeyCN, L"A\bC", FALSE);
217  DoTestComputerName(hKeyHN, hKeyCN, L"1\bc", FALSE);
218 
219  DoTestComputerName(hKeyHN, hKeyCN, ValidSymbols, TRUE);
220  DoTestComputerName(hKeyHN, hKeyCN, InvalidSymbols, FALSE);
221 
222  DoTestComputerName(hKeyHN, hKeyCN, L"123456", FALSE); // numeric-only
223  DoTestComputerName(hKeyHN, hKeyCN, L"123.456", FALSE);
224  DoTestComputerName(hKeyHN, hKeyCN, L"123X456", TRUE);
225  DoTestComputerName(hKeyHN, hKeyCN, L"123X.456", FALSE);
226 
227  DoTestComputerName(hKeyHN, hKeyCN, L"ThisIsLongLongComputerName", TRUE);
228 
229  for (i = 0; i < cchValidSymbols; ++i)
230  {
231  szName[0] = ValidSymbols[i];
232  szName[1] = UNICODE_NULL;
233  DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE);
234  }
235 
236  for (i = 0; i < cchValidSymbols; ++i)
237  {
238  szName[0] = L'a';
239  szName[1] = ValidSymbols[i];
240  szName[2] = UNICODE_NULL;
241  DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE);
242  }
243 
244  for (i = 0; i < cchValidSymbols; ++i)
245  {
246  szName[0] = L'A';
247  szName[1] = ValidSymbols[i];
248  szName[2] = UNICODE_NULL;
249  DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE);
250  }
251 
252  for (i = 0; i < cchValidSymbols; ++i)
253  {
254  szName[0] = L'1';
255  szName[1] = ValidSymbols[i];
256  szName[2] = UNICODE_NULL;
257  DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE);
258  }
259 
260  for (i = 0; i < cchInvalidSymbols; ++i)
261  {
262  szName[0] = L'A';
263  szName[1] = InvalidSymbols[i];
264  szName[2] = UNICODE_NULL;
265  DoTestComputerName(hKeyHN, hKeyCN, szName, FALSE);
266  }
267 
268  for (i = 0; i < cchInvalidSymbols; ++i)
269  {
270  szName[0] = L'1';
271  szName[1] = InvalidSymbols[i];
272  szName[2] = UNICODE_NULL;
273  DoTestComputerName(hKeyHN, hKeyCN, szName, FALSE);
274  }
275 
276  /* Close keys */
277  Error = RegCloseKey(hKeyHN);
279  Error = RegCloseKey(hKeyCN);
281 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define TRUE
Definition: types.h:120
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
int WINAPI lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2)
Definition: lstring.c:170
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
static HKEY OpenHostNameKey(void)
#define ok_long(expression, result)
Definition: atltest.h:133
#define lstrlenW
Definition: compat.h:415
int32_t INT
Definition: typedefs.h:56
#define lstrcpynW
Definition: compat.h:405
int WINAPI lstrcmpiW(LPCWSTR lpString1, LPCWSTR lpString2)
Definition: lstring.c:194
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
#define UNICODE_NULL
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
smooth NULL
Definition: ftsmooth.c:416
#define ok_int(expression, result)
Definition: atltest.h:134
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4895
#define trace
Definition: atltest.h:70
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4116
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
#define SetLastError(x)
Definition: compat.h:417
int ret
static const WCHAR L[]
Definition: oid.c:1250
static HKEY OpenComputerNameKey(void)
#define MAX_COMPUTERNAME_LENGTH
Definition: winbase.h:240
#define KEY_ALL_ACCESS
Definition: nt_native.h:1041
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
START_TEST(SetComputerNameExW)
static void DoTestComputerName(HKEY hKeyHN, HKEY hKeyCN, LPCWSTR pszNewName, BOOL bValid)
static const WCHAR szName[]
Definition: msipriv.h:1194
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3366
BOOL WINAPI SetComputerNameExW(COMPUTER_NAME_FORMAT NameType, LPCWSTR lpBuffer)
Definition: compname.c:592
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define REG_SZ
Definition: layer.c:22