diff --git a/modules/rostests/apitests/ws2_32/CMakeLists.txt b/modules/rostests/apitests/ws2_32/CMakeLists.txt index cae491a2fb..d9ad4135c4 100644 --- a/modules/rostests/apitests/ws2_32/CMakeLists.txt +++ b/modules/rostests/apitests/ws2_32/CMakeLists.txt @@ -3,6 +3,7 @@ list(APPEND SOURCE bind.c close.c getaddrinfo.c + gethostname.c getnameinfo.c getservbyname.c getservbyport.c @@ -22,6 +23,6 @@ list(APPEND SOURCE add_executable(ws2_32_apitest ${SOURCE} testlist.c) target_link_libraries(ws2_32_apitest wine ${PSEH_LIB}) set_module_type(ws2_32_apitest win32cui) -add_importlibs(ws2_32_apitest ws2_32 msvcrt iphlpapi kernel32 ntdll) +add_importlibs(ws2_32_apitest advapi32 iphlpapi ws2_32 msvcrt kernel32 ntdll) add_pch(ws2_32_apitest ws2_32.h SOURCE) add_rostests_file(TARGET ws2_32_apitest) diff --git a/modules/rostests/apitests/ws2_32/gethostname.c b/modules/rostests/apitests/ws2_32/gethostname.c new file mode 100644 index 0000000000..1f9dcc65f6 --- /dev/null +++ b/modules/rostests/apitests/ws2_32/gethostname.c @@ -0,0 +1,319 @@ +/* + * 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 + * + */ + +#include +#include +#include +#include +#include + +START_TEST(gethostname) +{ + CHAR file_name[25] = {"HostName123.txt"}; + CHAR cmdline[100] = {"hostname > "}; + FILE *fp; + CHAR mystring [100] = {0}; + CHAR *myreturn; + INT len, pos; + HKEY hKeyHN; + DWORD cbData; + LONG Error; + CHAR szHostNameOld[MAX_PATH]; + CHAR szHostNameNew[MAX_PATH] = {0}; + CHAR hostbuffer[1024] = {0}; + INT hnerror; + INT iResult; + WSADATA wsaData; + +HKEY OpenHostNameKey(void) +{ + static const WCHAR RegHostNameKey[] = L"System\\CurrentControlSet\\Services\\Tcpip\\Parameters"; + HKEY hKey = NULL; + LONG Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegHostNameKey, 0, KEY_ALL_ACCESS, &hKey); + if (!Error) + return hKey; + return NULL; +} + +HKEY OpenComputerNameKey(void) +{ + static const WCHAR + RegComputerNameKey[] = L"System\\CurrentControlSet\\Control\\ComputerName\\ComputerName"; + HKEY hKey = NULL; + LONG Error = RegOpenKeyExW(HKEY_LOCAL_MACHINE, RegComputerNameKey, 0, KEY_ALL_ACCESS, &hKey); + if (!Error) + return hKey; + return NULL; +} + + fp = fopen(file_name, "r"); // read mode + + ok(fp == NULL, + "Temp file already exists.\n"); + + if (fp != NULL) + { + return; + } + + strcat(cmdline, file_name); + + system(cmdline); + + fp = fopen(file_name, "r"); // read mode + + ok(fp != NULL, + "Error while opening the file.\n"); + + if (fp == NULL) + { + exit(EXIT_FAILURE); + } + + myreturn = fgets(mystring , 100 , fp); + + ok(myreturn != NULL, + "Error while reading the file.\n"); + + if (myreturn == NULL) + { + exit(EXIT_FAILURE); + } + + len = strlen(mystring); + + /* Delete the expected ending line feed character */ + if(mystring[len-1] == 10) mystring[len-1]=0; + + len = strlen(mystring); + + printf("The Hostname from command is '%s'.\n", mystring); + + iResult = fclose(fp); + + ok(iResult == 0, + "Error while closing the file.\n"); + + if (iResult != 0) + { + exit(EXIT_FAILURE); + } + + iResult = remove(file_name); + + ok(iResult == 0, + "Following error occurred deleting file"); + + hKeyHN = OpenHostNameKey(); + + /* Get Old Hostname */ + szHostNameOld[0] = UNICODE_NULL; + cbData = sizeof(szHostNameOld); + Error = RegQueryValueExA(hKeyHN, "Hostname", NULL, NULL, (LPBYTE)szHostNameOld, &cbData); + + printf("Hostname from Registry is '%s'.\n", szHostNameOld); + + pos = strcmp(szHostNameOld, mystring); + + printf("The test results were '%s'.\n", pos==0 ? "good" : "bad"); + + ok(strcmp(szHostNameOld, mystring) == 0, + "mystring '%s' should have been szHostNameOld '%s'.\n", mystring, szHostNameOld); + + /* Change Hostname in the Registry */ + szHostNameNew[0] = UNICODE_NULL; + strcat(szHostNameNew, "ROSHOSTNAME1"); + cbData = strlen(szHostNameNew) + 1; + + Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ , (LPBYTE)szHostNameNew, cbData); + + if(Error != ERROR_SUCCESS) + printf("Error setting new registry value (%ld).\n", GetLastError()); + + ok(Error == ERROR_SUCCESS, + "Error setting new registry value (%ld).\n", GetLastError()); + + fp = fopen(file_name, "r"); // read mode + + ok(fp == NULL, + "Temp file already exists.\n"); + + if (fp != NULL) + { + return; + } + + iResult = system(cmdline); + + ok(iResult != -1, + "Following error occurred executing system command"); + + if (iResult == -1) + { + return; + } + + fp = fopen(file_name, "r"); // read mode + + if (fp == NULL) + { + perror("Error while opening the file.\n"); + exit(EXIT_FAILURE); + } + + myreturn = fgets(mystring , 100 , fp); + + ok(myreturn != NULL, + "Error while reading the file.\n"); + + if (myreturn == NULL) + { + exit(EXIT_FAILURE); + } + + len = strlen(mystring); + + /* Delete the expected ending line feed character */ + if(mystring[len-1] == 10) mystring[len-1]=0; + + len = strlen(mystring); + + printf("The Hostname from command is '%s'.\n", mystring); + + iResult = fclose(fp); + + ok(iResult == 0, + "Error while closing the file.\n"); + + if (iResult != 0) + { + exit(EXIT_FAILURE); + } + + iResult = remove(file_name); + + ok(iResult == 0, + "Following error occurred deleting file"); + + pos = strcmp(szHostNameNew, mystring); + + printf("The test results were '%s'.\n", pos==0 ? "good" : "bad"); + + ok(strcmp(szHostNameNew, mystring) == 0, + "mystring '%s' should be szHostNameNew '%s'.\n", mystring, szHostNameNew); + + /* Reset the original registry entry */ + cbData = strlen(szHostNameOld) + 1; + + Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ , (LPBYTE)szHostNameOld, cbData); + if(Error != ERROR_SUCCESS) + printf("Error resetting registry value back (%ld).\n", GetLastError()); + + 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) + { + return; + } + + /* 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(strcmp(szHostNameOld, hostbuffer) == 0, + "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer); + + /* Change Hostname in the Registry */ + szHostNameNew[0] = UNICODE_NULL; + strcat(szHostNameNew, "ROSHOSTNAME1"); + cbData = strlen(szHostNameNew) + 1; + + Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ , (LPBYTE)szHostNameNew, cbData); + + if(Error != ERROR_SUCCESS) + printf("Error setting new registry value (%ld).\n", GetLastError()); + + 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(strcmp(szHostNameNew, hostbuffer) == 0, + "szHostNameNew '%s' should be hostbuffer '%s'.\n", szHostNameNew, hostbuffer); + + /* Reset the original registry entry */ + cbData = strlen(szHostNameOld) + 1; + + Error = RegSetValueExA(hKeyHN, "Hostname", 0, REG_SZ , (LPBYTE)szHostNameOld, cbData); + if(Error != ERROR_SUCCESS) + printf("Error resetting registry value back (%ld).\n", GetLastError()); + + 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(strcmp(szHostNameOld, hostbuffer) == 0, + "szHostNameOld '%s' should be hostbuffer '%s'.\n", szHostNameOld, hostbuffer); + + Error = RegCloseKey(hKeyHN); + + ok(Error == ERROR_SUCCESS, + "Error closing registry key (%ld).\n", GetLastError()); + + iResult = WSACleanup(); + + ok(iResult == 0, + "WSACleanup error occurred ending Winsock"); + + return; + +} diff --git a/modules/rostests/apitests/ws2_32/testlist.c b/modules/rostests/apitests/ws2_32/testlist.c index 8e33e8daa3..acf4feaf59 100644 --- a/modules/rostests/apitests/ws2_32/testlist.c +++ b/modules/rostests/apitests/ws2_32/testlist.c @@ -6,6 +6,7 @@ extern void func_bind(void); extern void func_close(void); extern void func_getaddrinfo(void); +extern void func_gethostname(void); extern void func_getnameinfo(void); extern void func_getservbyname(void); extern void func_getservbyport(void); @@ -25,6 +26,7 @@ const struct test winetest_testlist[] = { "bind", func_bind }, { "close", func_close }, { "getaddrinfo", func_getaddrinfo }, + { "gethostname", func_gethostname }, { "getnameinfo", func_getnameinfo }, { "getservbyname", func_getservbyname }, { "getservbyport", func_getservbyport },