diff --git a/CMakeLists.txt b/CMakeLists.txt index 05be91e26..97b14a942 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,10 +8,10 @@ include_directories(../ext-src/gtest/fused-src impl) link_directories(${monad_native_BINARY_DIR}) # source file definitions -set(LIB_SOURCE_FILES impl/getcurrentprocessorid.cpp impl/getusername.cpp impl/terminal.cpp) +set(LIB_SOURCE_FILES impl/getcurrentprocessorid.cpp impl/getusername.cpp impl/terminal.cpp impl/getcomputername.cpp) set(HOST_COMMON_SOURCE_FILES host/common/coreclrutil.cpp) set(HOST_COMMON_TEST_SOURCE_FILES tests/host/test-hostutil.cpp) -set(TEST_SOURCE_FILES tests/test-getcurrentprocessid.cpp ${HOST_COMMON_SOURCE_FILES} ${HOST_COMMON_TEST_SOURCE_FILES}) +set(TEST_SOURCE_FILES tests/test-getcurrentprocessid.cpp tests/test-getcomputername ${HOST_COMMON_SOURCE_FILES} ${HOST_COMMON_TEST_SOURCE_FILES}) set(SOURCE_FILES main.cpp ../ext-src/gtest/fused-src/gtest/gtest-all.cc) SET(HOST_CMDLINE_SOURCE_FILES host/cmdline/main.cpp ${HOST_COMMON_SOURCE_FILES}) diff --git a/impl/getcomputername.cpp b/impl/getcomputername.cpp index c8a7c9719..5946851b2 100644 --- a/impl/getcomputername.cpp +++ b/impl/getcomputername.cpp @@ -1,10 +1,28 @@ #include "getcomputername.h" #include #include +#include +#include -BOOL GetComputerName(LPTSTR name, LPDWORD len) +BOOL GetComputerNameW(LPTSTR name, LPDWORD len) { + const std::string utf8 = "UTF-8"; errno = 0; + + // Check parameters + if (!name || !len) { + errno = ERROR_INVALID_PARAMETER; + return 0; + } + + // Select locale from environment + setlocale(LC_ALL, ""); + // Check that locale is UTF-8 + if (nl_langinfo(CODESET) != utf8) { + errno = ERROR_BAD_ENVIRONMENT; + return 0; + } + size_t len2 = *len; int host = gethostname(name, len2); if(host == 0) @@ -17,3 +35,4 @@ BOOL GetComputerName(LPTSTR name, LPDWORD len) return FALSE; } } + diff --git a/impl/getcomputername.h b/impl/getcomputername.h index 7f8dd8eb5..3c83030df 100644 --- a/impl/getcomputername.h +++ b/impl/getcomputername.h @@ -4,6 +4,6 @@ PAL_BEGIN_EXTERNC -BOOL GetComputerName(LPTSTR name, LPDWORD len); +BOOL GetComputerNameW(LPTSTR name, LPDWORD len); PAL_END_EXTERNC \ No newline at end of file diff --git a/impl/pal.h b/impl/pal.h index 645974e33..196e0314a 100644 --- a/impl/pal.h +++ b/impl/pal.h @@ -63,8 +63,7 @@ typedef void *PVOID; typedef PVOID HANDLE; typedef char TCHAR; - typedef TCHAR *LPTSTR; - typedef DWORD *LPDWORD; + typedef char *LPTSTR; #define NO_ERROR 0 #define INFINITE 0xFFFFFFFF #define WINAPI @@ -74,6 +73,7 @@ #define FALSE 0 #define ERROR_INVALID_PARAMETER 87 #define ERROR_OUTOFMEMORY 14 + #define ERROR_BAD_ENVIRONMENT 10; #define ERROR_BUFFER_OVERFLOW 111 #define MAX_PATH 0x00000104 typedef unsigned long long uint64; diff --git a/tests/test-getcomputername.cpp b/tests/test-getcomputername.cpp index ffce53881..e76e88df4 100644 --- a/tests/test-getcomputername.cpp +++ b/tests/test-getcomputername.cpp @@ -3,37 +3,26 @@ #include TEST(GetComputerName,simple) -{ +{ char hostname[HOST_NAME_MAX]; - std::string hostnameFunctionTest; + std::string hostnameFunctionTest(LOGIN_NAME_MAX, '\0');; DWORD hostSize = HOST_NAME_MAX; - BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize); + BOOL getComputerName = GetComputerNameW(&hostnameFunctionTest[0], &hostSize); BOOL host = gethostname(hostname, sizeof hostname); - if(host == 0) - { - host = TRUE; - } - else - { - host = FALSE; - } - std::string hostnameString(hostname); std::string hostnameStringTest(&hostnameFunctionTest[0]); ASSERT_TRUE(getComputerName == TRUE); - ASSERT_EQ(host,TRUE); + ASSERT_EQ(host,0); ASSERT_EQ(hostnameString,hostnameStringTest); } TEST(GetComputerName,bufferttoosmall) { - char hostname[HOST_NAME_MAX]; std::string hostnameFunctionTest; DWORD hostSize = 0; - BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize); + BOOL getComputerName = GetComputerNameW(&hostnameFunctionTest[0], &hostSize); ASSERT_TRUE(getComputerName == 0); - char buffer[ 256 ]; EXPECT_EQ(errno, ERROR_BUFFER_OVERFLOW); } \ No newline at end of file