[KERNEL32_APITEST] Improve SetComputerNameExW test a bit (#1580)
[reactos.git] / modules / rostests / apitests / kernel32 / SetComputerNameExW.c
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
29 static HKEY OpenComputerNameKey(void)
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 START_TEST(SetComputerNameExW)
41 {
42 static const WCHAR szNewName[] = L"SRVROSTEST";
43 LONG Error;
44 BOOL ret;
45 HKEY hKeyHN, hKeyCN;
46 DWORD cbData;
47 WCHAR szHostNameOld[MAX_PATH], szHostNameNew[MAX_PATH];
48 WCHAR szComputerNameOld[MAX_PATH], szComputerNameNew[MAX_PATH];
49
50 /* Open keys */
51 hKeyHN = OpenHostNameKey();
52 ok(hKeyHN, "hKeyHN is NULL\n");
53 hKeyCN = OpenComputerNameKey();
54 ok(hKeyCN, "hKeyCN is NULL\n");
55 if (!hKeyHN || !hKeyCN)
56 {
57 if (hKeyHN)
58 RegCloseKey(hKeyHN);
59 if (hKeyCN)
60 RegCloseKey(hKeyCN);
61 skip("Unable to open keys. Missing Admin rights?\n");
62 return;
63 }
64
65 /* Get Old Hostname */
66 szHostNameOld[0] = UNICODE_NULL;
67 cbData = sizeof(szHostNameOld);
68 Error = RegQueryValueExW(hKeyHN, L"Hostname", NULL, NULL, (LPBYTE)szHostNameOld, &cbData);
69 ok_long(Error, ERROR_SUCCESS);
70 ok(szHostNameOld[0], "szHostNameOld is empty\n");
71
72 /* Get Old Computer Name */
73 szComputerNameOld[0] = UNICODE_NULL;
74 cbData = sizeof(szComputerNameOld);
75 Error = RegQueryValueExW(hKeyCN, L"ComputerName", NULL, NULL, (LPBYTE)szComputerNameOld, &cbData);
76 ok_long(Error, ERROR_SUCCESS);
77 ok(szComputerNameOld[0], "szComputerNameOld is empty\n");
78
79 /* Change the value */
80 ret = SetComputerNameExW(ComputerNamePhysicalDnsHostname, szNewName);
81 ok_int(ret, TRUE);
82
83 /* Get New Hostname */
84 szHostNameNew[0] = UNICODE_NULL;
85 cbData = sizeof(szHostNameNew);
86 Error = RegQueryValueExW(hKeyHN, L"Hostname", NULL, NULL, (LPBYTE)szHostNameNew, &cbData);
87 ok_long(Error, ERROR_SUCCESS);
88 ok(szHostNameNew[0], "szHostNameNew is empty\n");
89 ok(lstrcmpW(szHostNameNew, szHostNameOld) == 0,
90 "szHostNameNew '%S' should be szHostNameOld '%S'\n", szHostNameNew, szHostNameOld);
91
92 /* Get New Computer Name */
93 szComputerNameNew[0] = UNICODE_NULL;
94 cbData = sizeof(szComputerNameNew);
95 Error = RegQueryValueExW(hKeyCN, L"ComputerName", NULL, NULL, (LPBYTE)szComputerNameNew, &cbData);
96 ok_long(Error, ERROR_SUCCESS);
97 ok(szComputerNameNew[0], "szComputerNameNew is empty\n");
98 ok(lstrcmpW(szComputerNameNew, szNewName) == 0,
99 "szComputerNameNew '%S' should be szNewName '%S'\n", szComputerNameNew, szNewName);
100
101 /* Restore the registry values */
102 cbData = (lstrlenW(szHostNameOld) + 1) * sizeof(WCHAR);
103 Error = RegSetValueExW(hKeyHN, L"Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
104 ok_long(Error, ERROR_SUCCESS);
105
106 cbData = (lstrlenW(szComputerNameOld) + 1) * sizeof(WCHAR);
107 Error = RegSetValueExW(hKeyCN, L"ComputerName", 0, REG_SZ, (LPBYTE)szComputerNameOld, cbData);
108 ok_long(Error, ERROR_SUCCESS);
109
110 /* Close keys */
111 Error = RegCloseKey(hKeyHN);
112 ok_long(Error, ERROR_SUCCESS);
113 Error = RegCloseKey(hKeyCN);
114 ok_long(Error, ERROR_SUCCESS);
115 }