1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * PROJECT:     ReactOS API tests
 * LICENSE:     LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
 * PURPOSE:     Tests for Hostname command and Winsock gethostname() function.
 * COPYRIGHT:   Copyright 2019 Doug Lyons <douglyons@douglyons.com>
 *              Copyright 2019 Hermes Belusca-Maito
 */

#include "ws2_32.h"

#include <winreg.h>

#define REG_HOSTNAME_KEY        L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
#define REG_COMPUTERNAME_KEY    L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName"

HKEY
OpenRegKey(
    IN HKEY   hRootKey,
    IN PCWSTR pszSubKey,
    IN DWORD  ulOptions)
{
    HKEY hKey = NULL;
    LONG Error = RegOpenKeyExW(hRootKey, pszSubKey, 0, ulOptions, &hKey);
    if (Error == ERROR_SUCCESS)
        return hKey;
    return NULL;
}

BOOL
GetHostnameFromCommand(
    OUT PSTR pszHostnameBuffer,
    IN ULONG ulBufferSize)
{
    PCSTR file_name = "HostName123.txt";
    CHAR cmdline[MAX_PATH] = "hostname > ";
    FILE *fp;
    INT iResult;
    INT len;
    PCSTR ptr;

    ZeroMemory(pszHostnameBuffer, ulBufferSize * sizeof(CHAR));

    /* Run the 'hostname' command, piping out its result to the temporary file */
    strcat(cmdline, file_name);
    system(cmdline);

    /* Open the temporary file in read mode */
    fp = fopen(file_name, "r");
    ok(fp != NULL, "An error occurred while opening the file.\n");
    if (fp == NULL)
        return FALSE;

    /* Read its contents */
    ptr = fgets(pszHostnameBuffer, ulBufferSize * sizeof(CHAR), fp);
    ok(ptr != NULL, "An error occurred while reading the file.\n");
    if (ptr == NULL)
        goto Cleanup;

    /* Delete the expected ending line feed character */
    len = lstrlenA(pszHostnameBuffer);
    if (pszHostnameBuffer[len-1] == '\r' || pszHostnameBuffer[len-1] == '\n')
        pszHostnameBuffer[len-1] = ANSI_NULL;

Cleanup:
    /* Close and remove the file */
    iResult = fclose(fp);
    ok(iResult == 0, "An error occurred while closing the file: %i.\n", iResult);
    iResult = remove(file_name);
    ok(iResult == 0, "An error occurred while deleting the file: %i.\n", iResult);

    return (ptr ? TRUE : FALSE);
}

START_TEST(gethostname)
{
    INT pos;
    HKEY hKeyHN;
    DWORD cbData;
    LONG Error;
    INT hnError;
    INT iResult;
    WSADATA wsaData;
    DWORD uApiHostNameSize;
    CHAR szApiHostName[256] = "";
    CHAR szHostNameOld[256] = "";
    CHAR szHostNameNew[256] = "";
    CHAR hostbuffer[256] = "";

/*
 * Notes about the retrieval of the computer name on Windows.
 *
 * - GetComputerName() (equivalently, GetComputerNameEx(ComputerNameNetBIOS))
 *   retrieves the name cached in the registry value "ComputerName" under
 *   "System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName" .
 *
 * - GetComputerNameEx(ComputerNameDnsHostname) as well as the 'hostname' command
     return the registry value "Hostname" under "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" .
 *
 * - In case a new computer name is set, its value is cached in the registry value
 *   "ComputerName" under "System\\CurrentControlSet\\Control\\ComputerName\\ComputerName",
 *   and also in the registry value "NV Hostname" under "System\\CurrentControlSet\\Services\\Tcpip\\Parameters" .
 */

    /* Retrieve the current host name using API */
    uApiHostNameSize = _countof(szApiHostName);
    if (!GetComputerNameExA(ComputerNameDnsHostname, szApiHostName, &uApiHostNameSize))
    {
        /* Fail in case of error */
        skip("GetComputerNameExA(ComputerNameDnsHostname) failed, error %lu\n", GetLastError());
        return;
    }
    printf("The Hostname from API is '%s'.\n", szApiHostName);

    /* Retrieve the current host name using the 'hostname' command */
    if (!GetHostnameFromCommand(hostbuffer, _countof(hostbuffer)))
    {
        /* Fail in case of error */
        skip("Error while retrieving the host name using the 'hostname' command!\n");
        return;
    }
    printf("The Hostname from command is '%s'.\n", hostbuffer);

    pos = strcmp(szApiHostName, hostbuffer);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "hostbuffer '%s' should have been szApiHostName '%s'.\n", hostbuffer, szApiHostName);

    hKeyHN = OpenRegKey(HKEY_LOCAL_MACHINE, REG_HOSTNAME_KEY, KEY_ALL_ACCESS);
    ok(hKeyHN != NULL, "Error while opening hostname registry key.\n");
    if (hKeyHN == NULL)
        return;

    /* Get Old Hostname */
    szHostNameOld[0] = ANSI_NULL;
    cbData = sizeof(szHostNameOld);
    Error = RegQueryValueExA(hKeyHN, "Hostname", NULL, NULL, (LPBYTE)szHostNameOld, &cbData);<--- Error is assigned

    printf("Hostname from Registry is '%s'.\n", szHostNameOld);

    pos = strcmp(szHostNameOld, szApiHostName);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "szApiHostName '%s' should have been szHostNameOld '%s'.\n", szApiHostName, szHostNameOld);

    /* Change Hostname in the Registry */
    szHostNameNew[0] = ANSI_NULL;
    strcat(szHostNameNew, "ROSHOSTNAME1");
    cbData = lstrlenA(szHostNameNew) + 1;

    Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameNew, cbData);<--- Error is overwritten
    ok(Error == ERROR_SUCCESS, "Error setting new registry value (%ld).\n", GetLastError());

    /* Retrieve the current host name using API */
    uApiHostNameSize = _countof(szApiHostName);
    if (!GetComputerNameExA(ComputerNameDnsHostname, szApiHostName, &uApiHostNameSize))
    {
        /* Fail in case of error */
        skip("GetComputerNameExA(ComputerNameDnsHostname) failed, error %lu\n", GetLastError());
        goto Cleanup;
    }
    printf("The Hostname from API is '%s'.\n", szApiHostName);

    /* Retrieve the current host name using the 'hostname' command */
    if (!GetHostnameFromCommand(hostbuffer, _countof(hostbuffer)))
    {
        /* Fail in case of error */
        skip("Error while retrieving the host name using the 'hostname' command!\n");
        goto Cleanup;
    }
    printf("The Hostname from command is '%s'.\n", hostbuffer);

    pos = strcmp(szHostNameNew, szApiHostName);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "szApiHostName '%s' should be szHostNameNew '%s'.\n", szApiHostName, szHostNameNew);

    pos = strcmp(szHostNameNew, hostbuffer);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "hostbuffer '%s' should have been szHostNameNew '%s'.\n", hostbuffer, szHostNameNew);

    /* Reset the original registry entry */
    cbData = lstrlenA(szHostNameOld) + 1;

    Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
    ok(Error == ERROR_SUCCESS, "Error resetting new registry value back (%ld).\n", GetLastError());

/*============ Winsock Checks ===============*/

    /* Start up Winsock to use gethostname() */
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    ok(iResult == 0, "Error occurred starting Winsock");
    if (iResult != 0)
        goto Cleanup;

    /* Retrieve gethostname() */
    hnError = gethostname(hostbuffer, sizeof(hostbuffer));
    ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());

    /* Display results */
    if (!hnError)
        printf("Winsock gethostname() is '%s'.\n", hostbuffer);

    pos = strcmp(szHostNameOld, hostbuffer);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer);

    /* Change Hostname in the Registry */
    szHostNameNew[0] = ANSI_NULL;
    strcat(szHostNameNew, "ROSHOSTNAME1");
    cbData = lstrlenA(szHostNameNew) + 1;

    Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameNew, cbData);
    ok(Error == ERROR_SUCCESS, "Error setting new registry value (%ld).\n", GetLastError());

    /* Retrieve gethostname() */
    hnError = gethostname(hostbuffer, sizeof(hostbuffer));
    ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());

    /* Display results */
    if (!hnError)
        printf("Winsock gethostname() is '%s'.\n", hostbuffer);

    pos = strcmp(szHostNameNew, hostbuffer);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "szHostNameNew '%s' should be hostbuffer '%s'.\n", szHostNameNew, hostbuffer);

    /* Reset the original registry entry */
    cbData = lstrlenA(szHostNameOld) + 1;

    Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ, (LPBYTE)szHostNameOld, cbData);
    ok(Error == ERROR_SUCCESS, "Error resetting new registry value back (%ld).\n", GetLastError());

    /* Retrieve gethostname() */
    hnError = gethostname(hostbuffer, sizeof(hostbuffer));
    ok(!hnError, "Winsock gethostname() Error is '%d'.\n", WSAGetLastError());

    /* Display results */
    if (!hnError)
        printf("Winsock gethostname() is '%s'.\n", hostbuffer);

    pos = strcmp(szHostNameOld, hostbuffer);
    printf("The test results were '%s'.\n", pos==0 ? "good" : "bad");
    ok(pos == 0, "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer);

Cleanup:
    iResult = WSACleanup();
    ok(iResult == 0, "WSACleanup error occurred ending Winsock");

    Error = RegCloseKey(hKeyHN);
    ok(Error == ERROR_SUCCESS, "Error closing registry key (%ld).\n", GetLastError());

    return;
}