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