Add error handing for bad enivronment and bad parameter
This commit is contained in:
parent
fb5279ad1e
commit
7a85d035c0
|
@ -8,10 +8,10 @@ include_directories(../ext-src/gtest/fused-src impl)
|
||||||
link_directories(${monad_native_BINARY_DIR})
|
link_directories(${monad_native_BINARY_DIR})
|
||||||
|
|
||||||
# source file definitions
|
# 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_SOURCE_FILES host/common/coreclrutil.cpp)
|
||||||
set(HOST_COMMON_TEST_SOURCE_FILES tests/host/test-hostutil.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(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})
|
SET(HOST_CMDLINE_SOURCE_FILES host/cmdline/main.cpp ${HOST_COMMON_SOURCE_FILES})
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,28 @@
|
||||||
#include "getcomputername.h"
|
#include "getcomputername.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <string>
|
||||||
|
#include <langinfo.h>
|
||||||
|
|
||||||
BOOL GetComputerName(LPTSTR name, LPDWORD len)
|
BOOL GetComputerNameW(LPTSTR name, LPDWORD len)
|
||||||
{
|
{
|
||||||
|
const std::string utf8 = "UTF-8";
|
||||||
errno = 0;
|
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;
|
size_t len2 = *len;
|
||||||
int host = gethostname(name, len2);
|
int host = gethostname(name, len2);
|
||||||
if(host == 0)
|
if(host == 0)
|
||||||
|
@ -17,3 +35,4 @@ BOOL GetComputerName(LPTSTR name, LPDWORD len)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,6 @@
|
||||||
|
|
||||||
PAL_BEGIN_EXTERNC
|
PAL_BEGIN_EXTERNC
|
||||||
|
|
||||||
BOOL GetComputerName(LPTSTR name, LPDWORD len);
|
BOOL GetComputerNameW(LPTSTR name, LPDWORD len);
|
||||||
|
|
||||||
PAL_END_EXTERNC
|
PAL_END_EXTERNC
|
|
@ -63,8 +63,7 @@
|
||||||
typedef void *PVOID;
|
typedef void *PVOID;
|
||||||
typedef PVOID HANDLE;
|
typedef PVOID HANDLE;
|
||||||
typedef char TCHAR;
|
typedef char TCHAR;
|
||||||
typedef TCHAR *LPTSTR;
|
typedef char *LPTSTR;
|
||||||
typedef DWORD *LPDWORD;
|
|
||||||
#define NO_ERROR 0
|
#define NO_ERROR 0
|
||||||
#define INFINITE 0xFFFFFFFF
|
#define INFINITE 0xFFFFFFFF
|
||||||
#define WINAPI
|
#define WINAPI
|
||||||
|
@ -74,6 +73,7 @@
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
#define ERROR_INVALID_PARAMETER 87
|
#define ERROR_INVALID_PARAMETER 87
|
||||||
#define ERROR_OUTOFMEMORY 14
|
#define ERROR_OUTOFMEMORY 14
|
||||||
|
#define ERROR_BAD_ENVIRONMENT 10;
|
||||||
#define ERROR_BUFFER_OVERFLOW 111
|
#define ERROR_BUFFER_OVERFLOW 111
|
||||||
#define MAX_PATH 0x00000104
|
#define MAX_PATH 0x00000104
|
||||||
typedef unsigned long long uint64;
|
typedef unsigned long long uint64;
|
||||||
|
|
|
@ -3,37 +3,26 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
TEST(GetComputerName,simple)
|
TEST(GetComputerName,simple)
|
||||||
{
|
{
|
||||||
char hostname[HOST_NAME_MAX];
|
char hostname[HOST_NAME_MAX];
|
||||||
std::string hostnameFunctionTest;
|
std::string hostnameFunctionTest(LOGIN_NAME_MAX, '\0');;
|
||||||
DWORD hostSize = HOST_NAME_MAX;
|
DWORD hostSize = HOST_NAME_MAX;
|
||||||
BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize);
|
BOOL getComputerName = GetComputerNameW(&hostnameFunctionTest[0], &hostSize);
|
||||||
BOOL host = gethostname(hostname, sizeof hostname);
|
BOOL host = gethostname(hostname, sizeof hostname);
|
||||||
|
|
||||||
if(host == 0)
|
|
||||||
{
|
|
||||||
host = TRUE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
host = FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string hostnameString(hostname);
|
std::string hostnameString(hostname);
|
||||||
std::string hostnameStringTest(&hostnameFunctionTest[0]);
|
std::string hostnameStringTest(&hostnameFunctionTest[0]);
|
||||||
|
|
||||||
ASSERT_TRUE(getComputerName == TRUE);
|
ASSERT_TRUE(getComputerName == TRUE);
|
||||||
ASSERT_EQ(host,TRUE);
|
ASSERT_EQ(host,0);
|
||||||
ASSERT_EQ(hostnameString,hostnameStringTest);
|
ASSERT_EQ(hostnameString,hostnameStringTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(GetComputerName,bufferttoosmall)
|
TEST(GetComputerName,bufferttoosmall)
|
||||||
{
|
{
|
||||||
char hostname[HOST_NAME_MAX];
|
|
||||||
std::string hostnameFunctionTest;
|
std::string hostnameFunctionTest;
|
||||||
DWORD hostSize = 0;
|
DWORD hostSize = 0;
|
||||||
BOOL getComputerName = GetComputerName(&hostnameFunctionTest[0], &hostSize);
|
BOOL getComputerName = GetComputerNameW(&hostnameFunctionTest[0], &hostSize);
|
||||||
ASSERT_TRUE(getComputerName == 0);
|
ASSERT_TRUE(getComputerName == 0);
|
||||||
char buffer[ 256 ];
|
|
||||||
EXPECT_EQ(errno, ERROR_BUFFER_OVERFLOW);
|
EXPECT_EQ(errno, ERROR_BUFFER_OVERFLOW);
|
||||||
}
|
}
|
Loading…
Reference in a new issue