From 316cabbdcf367d409eb2ab281be02ced3ea08e65 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 6 Aug 2015 10:16:13 -0700 Subject: [PATCH] Got rid of tests for UTF7, added error checking, adding test to check for null paramater for codepage --- CMakeLists.txt | 4 ++-- impl/getcpinfo.cpp | 52 ++++++++++++++++++++++++++++------------ impl/getcpinfo.h | 2 +- impl/pal.h | 2 ++ tests/test-getcpinfo.cpp | 34 +++++++++++++++----------- 5 files changed, 62 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5d27e7bf..9fce473e1 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/getcpinfo.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-getcpinfo.cpp ${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/getcpinfo.cpp b/impl/getcpinfo.cpp index 857b00de7..419c4c5f6 100644 --- a/impl/getcpinfo.cpp +++ b/impl/getcpinfo.cpp @@ -1,28 +1,50 @@ #include "getcpinfo.h" -#include +#include #include +#include -BOOL GetCPInfo(UINT codepage, CPINFO &cpinfo) + +// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx +// Sets errno to: +// ERROR_INVALID_PARAMETER - parameter is not valid +// ERROR_BAD_ENVIRONMENT - locale is not UTF-8 +// +// Returns: +// TRUE - succeeded +// FALSE - failed + +BOOL GetCPInfoW(UINT codepage, CPINFO &cpinfo) { - std::string test; - switch(codepage) - { - case 65000: - cpinfo.DefaultChar[0] = '?'; - cpinfo.DefaultChar[1] = '0'; - cpinfo.LeadByte[0] = '0'; - cpinfo.LeadByte[1] = '0'; - cpinfo.MaxCharSize = 5; - return TRUE; - case 65001: + + const std::string utf8 = "UTF-8"; + errno = 0; + + //Check that codepage is not null + if(!codepage) { + errno = ERROR_INVALID_PARAMETER; + return FALSE; + } + + // Select locale from environment + setlocale(LC_ALL, ""); + // Check that locale is UTF-8 + if (nl_langinfo(CODESET) != utf8) { + errno = ERROR_BAD_ENVIRONMENT; + return 0; + } + + //if codepage is utf8 + if(codepage == 65001) { cpinfo.DefaultChar[0] = '?'; cpinfo.DefaultChar[1] = '0'; cpinfo.LeadByte[0] = '0'; cpinfo.LeadByte[1] = '0'; cpinfo.MaxCharSize = 4; return TRUE; - default: + } + else{ + errno = ERROR_INVALID_PARAMETER; return FALSE; - } + } } diff --git a/impl/getcpinfo.h b/impl/getcpinfo.h index 0e5d57462..e1b203bc1 100644 --- a/impl/getcpinfo.h +++ b/impl/getcpinfo.h @@ -4,6 +4,6 @@ PAL_BEGIN_EXTERNC -BOOL GetCPInfo(UINT codepage, CPINFO &cpinfo); +BOOL GetCPInfoW(UINT codepage, CPINFO &cpinfo); PAL_END_EXTERNC diff --git a/impl/pal.h b/impl/pal.h index 46ae8feb5..5d398c8b8 100644 --- a/impl/pal.h +++ b/impl/pal.h @@ -75,6 +75,8 @@ #define ERROR_INVALID_PARAMETER 87 #define ERROR_OUTOFMEMORY 14 #define MAX_PATH 0x00000104 + #define UTF8 65001 + #define ERROR_BAD_ENVIRONMENT 0x0000000A typedef unsigned long long uint64; #endif diff --git a/tests/test-getcpinfo.cpp b/tests/test-getcpinfo.cpp index 0a8d110e9..341c5279d 100644 --- a/tests/test-getcpinfo.cpp +++ b/tests/test-getcpinfo.cpp @@ -1,14 +1,14 @@ #include #include "getcpinfo.h" -// This is a very simple test case to show how tests can be written -TEST(GetCPInfo,utf8) +// This test is with correct parameters +TEST(GetCPInfo,Utf8) { CPINFO cpinfo; - BOOL utf8 = GetCPInfo(65001, cpinfo); + BOOL result = GetCPInfoW(UTF8, cpinfo); // first make sure that the function worked - ASSERT_TRUE(utf8 == TRUE); + ASSERT_TRUE(result == TRUE); // now compare the actual values ASSERT_EQ(cpinfo.DefaultChar[0],'?'); @@ -18,18 +18,24 @@ TEST(GetCPInfo,utf8) ASSERT_EQ(cpinfo.MaxCharSize,4); } -TEST(GetCPInfo,utf7) +// This test is with codepage being null +TEST(GetCPInfo, NullForCodePageUINTButNotCpinfo) { CPINFO cpinfo; - BOOL utf7 = GetCPInfo(65000, cpinfo); + BOOL result = GetCPInfoW(NULL, cpinfo); - // first make sure that the function worked - ASSERT_TRUE(utf7 == TRUE); + ASSERT_TRUE(result == FALSE); + EXPECT_EQ(errno, ERROR_INVALID_PARAMETER); + +} - // now compare the actual values - ASSERT_EQ(cpinfo.DefaultChar[0],'?'); - ASSERT_EQ(cpinfo.DefaultChar[1],'0'); - ASSERT_EQ(cpinfo.LeadByte[0],'0'); - ASSERT_EQ(cpinfo.LeadByte[1],'0'); - ASSERT_EQ(cpinfo.MaxCharSize,5); +// This test is with codepage not being utf8 +TEST(GetCPInfo, CodePageNotUTF8) +{ + CPINFO cpinfo; + BOOL result = GetCPInfoW(65000, cpinfo); + + ASSERT_TRUE(result == FALSE); + EXPECT_EQ(errno, ERROR_INVALID_PARAMETER); + } \ No newline at end of file