Add error handing for bad enivronment and bad parameter

This commit is contained in:
Aaron 2015-08-03 10:48:46 -07:00
parent fb5279ad1e
commit 7a85d035c0
5 changed files with 30 additions and 22 deletions

View file

@ -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})

View file

@ -1,10 +1,28 @@
#include "getcomputername.h"
#include <unistd.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;
// 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;
}
}

View file

@ -4,6 +4,6 @@
PAL_BEGIN_EXTERNC
BOOL GetComputerName(LPTSTR name, LPDWORD len);
BOOL GetComputerNameW(LPTSTR name, LPDWORD len);
PAL_END_EXTERNC

View file

@ -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;

View file

@ -3,37 +3,26 @@
#include <iostream>
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);
}