Commit 57a01c7f authored by Laszlo Csomor's avatar Laszlo Csomor

io_win32: add more encoding-related tests

parent 65da9fd9
......@@ -210,16 +210,20 @@ bool as_windows_path(const char* path, wstring* result) {
result->clear();
return true;
}
if (is_separator(path[0]) || is_drive_relative(path)) {
return false;
}
wstring wpath;
if (!strings::utf8_to_wcs(path, &wpath)) {
return false;
}
if (has_longpath_prefix(wpath.c_str())) {
*result = wpath;
return true;
}
if (is_separator(path[0]) || is_drive_relative(path)) {
return false;
}
if (!is_path_absolute(wpath.c_str()) && !has_longpath_prefix(wpath.c_str())) {
if (!is_path_absolute(wpath.c_str())) {
int size = ::GetCurrentDirectoryW(0, NULL);
if (size == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return false;
......@@ -396,6 +400,10 @@ bool utf8_to_wcs(const char* input, wstring* out) {
return mbs_to_wcs(input, out, true);
}
bool wcs_to_utf8(const wchar_t* input, string* out) {
return wcs_to_mbs(input, out, true);
}
} // namespace strings
} // namespace win32
} // namespace internal
......
......@@ -84,6 +84,9 @@ LIBPROTOBUF_EXPORT bool mbs_to_wcs(
// Convert from UTF-8-encoded text to UTF-16.
LIBPROTOBUF_EXPORT bool utf8_to_wcs(const char* input, std::wstring* out);
// Convert from UTF-16-encoded text to UTF-8.
LIBPROTOBUF_EXPORT bool wcs_to_utf8(const wchar_t* input, std::string* out);
} // namespace strings
} // namespace win32
......
......@@ -61,6 +61,27 @@ namespace internal {
namespace win32 {
namespace {
const char kUtf8Text[] = {
'h', 'i', ' ',
// utf-8: 11010000 10011111, utf-16: 100 0001 1111 = 0x041F
0xd0, 0x9f,
// utf-8: 11010001 10000000, utf-16: 100 0100 0000 = 0x0440
0xd1, 0x80,
// utf-8: 11010000 10111000, utf-16: 100 0011 1000 = 0x0438
0xd0, 0xb8,
// utf-8: 11010000 10110010, utf-16: 100 0011 0010 = 0x0432
0xd0, 0xb2,
// utf-8: 11010000 10110101, utf-16: 100 0011 0101 = 0x0435
0xd0, 0xb5,
// utf-8: 11010001 10000010, utf-16: 100 0100 0010 = 0x0442
0xd1, 0x82, 0
};
const wchar_t kUtf16Text[] = {
L'h', L'i', L' ',
L'\x41f', L'\x440', L'\x438', L'\x432', L'\x435', L'\x442', 0
};
using std::string;
using std::wstring;
......@@ -73,6 +94,7 @@ class IoWin32Test : public ::testing::Test {
bool CreateAllUnder(wstring path);
bool DeleteAllUnder(wstring path);
WCHAR working_directory[MAX_PATH];
string test_tmpdir;
wstring wtest_tmpdir;
};
......@@ -111,6 +133,7 @@ bool GetEnvVarAsUtf8(const WCHAR* name, string* result) {
void IoWin32Test::SetUp() {
test_tmpdir.clear();
wtest_tmpdir.clear();
EXPECT_GT(::GetCurrentDirectoryW(MAX_PATH, working_directory), 0);
string tmp;
bool ok = false;
......@@ -128,7 +151,14 @@ void IoWin32Test::SetUp() {
}
StripTrailingSlashes(&tmp);
test_tmpdir = tmp + "\\io_win32_unittest.tmp";
std::stringstream result;
// Deleting files and directories is asynchronous on Windows, and if TearDown
// just deleted the previous temp directory, sometimes we cannot recreate the
// same directory.
// Use a counter so every test method gets its own temp directory.
static int counter = 0;
result << tmp << "\\io_win32_test" << counter++ << ".tmp";
test_tmpdir = result.str();
wtest_tmpdir = testonly_utf8_to_winpath(test_tmpdir.c_str());
ASSERT_FALSE(wtest_tmpdir.empty());
ASSERT_TRUE(DeleteAllUnder(wtest_tmpdir));
......@@ -139,6 +169,7 @@ void IoWin32Test::TearDown() {
if (!wtest_tmpdir.empty()) {
DeleteAllUnder(wtest_tmpdir);
}
::SetCurrentDirectoryW(working_directory);
}
bool IoWin32Test::CreateAllUnder(wstring path) {
......@@ -294,13 +325,24 @@ TEST_F(IoWin32Test, MkdirTest) {
ASSERT_EQ(errno, ENOENT);
}
TEST_F(IoWin32Test, MkdirTestNonAscii) {
ASSERT_INITIALIZED;
// Create a non-ASCII path.
// Ensure that we can create the directory using SetCurrentDirectoryW.
EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1").c_str(), NULL));
EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1\\" + kUtf16Text).c_str(), NULL));
// Ensure that we can create a very similarly named directory using mkdir.
// We don't attemp to delete and recreate the same directory, because on
// Windows, deleting files and directories seems to be asynchronous.
EXPECT_EQ(mkdir((test_tmpdir + "\\2").c_str(), 0644), 0);
EXPECT_EQ(mkdir((test_tmpdir + "\\2\\" + kUtf8Text).c_str(), 0644), 0);
}
TEST_F(IoWin32Test, ChdirTest) {
WCHAR owd[MAX_PATH];
EXPECT_GT(::GetCurrentDirectoryW(MAX_PATH, owd), 0);
string path("C:\\");
EXPECT_EQ(access(path.c_str(), F_OK), 0);
ASSERT_EQ(chdir(path.c_str()), 0);
EXPECT_TRUE(::SetCurrentDirectoryW(owd));
// Do not try to chdir into the test_tmpdir, it may already contain directory
// names with trailing dots.
......@@ -315,6 +357,26 @@ TEST_F(IoWin32Test, ChdirTest) {
ASSERT_NE(chdir(path.c_str()), 0);
}
TEST_F(IoWin32Test, ChdirTestNonAscii) {
ASSERT_INITIALIZED;
// Create a directory with a non-ASCII path and ensure we can cd into it.
wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
string nonAscii;
EXPECT_TRUE(strings::wcs_to_utf8(wNonAscii.c_str(), &nonAscii));
EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), NULL));
WCHAR cwd[MAX_PATH];
EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
// Ensure that we can cd into the path using SetCurrentDirectoryW.
EXPECT_TRUE(SetCurrentDirectoryW(wNonAscii.c_str()));
EXPECT_TRUE(SetCurrentDirectoryW(cwd));
// Ensure that we can cd into the path using chdir.
ASSERT_EQ(chdir(nonAscii.c_str()), 0);
// Ensure that the GetCurrentDirectoryW returns the desired path.
EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
ASSERT_EQ(wNonAscii, cwd);
}
TEST_F(IoWin32Test, AsWindowsPathTest) {
DWORD size = GetCurrentDirectoryW(0, NULL);
scoped_array<wchar_t> cwd_str(new wchar_t[size]);
......@@ -346,31 +408,16 @@ TEST_F(IoWin32Test, AsWindowsPathTest) {
// Though valid in cmd.exe, drive-relative paths are not supported.
ASSERT_EQ(testonly_utf8_to_winpath("c:foo"), L"");
ASSERT_EQ(testonly_utf8_to_winpath("c:/foo"), L"\\\\?\\c:\\foo");
ASSERT_EQ(testonly_utf8_to_winpath("\\\\?\\C:\\foo"), L"\\\\?\\C:\\foo");
}
TEST_F(IoWin32Test, Utf8ToUtf16Test) {
const char hi_utf8[] = {
'h', 'i', ' ',
// utf-8: 11010000 10011111, utf-16: 100 0001 1111 = 0x041F
0xd0, 0x9f,
// utf-8: 11010001 10000000, utf-16: 100 0100 0000 = 0x0440
0xd1, 0x80,
// utf-8: 11010000 10111000, utf-16: 100 0011 1000 = 0x0438
0xd0, 0xb8,
// utf-8: 11010000 10110010, utf-16: 100 0011 0010 = 0x0432
0xd0, 0xb2,
// utf-8: 11010000 10110101, utf-16: 100 0011 0101 = 0x0435
0xd0, 0xb5,
// utf-8: 11010001 10000010, utf-16: 100 0100 0010 = 0x0442
0xd1, 0x82, 0
};
const wchar_t hi_utf16[] = {
L'h', L'i', L' ', 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0
};
TEST_F(IoWin32Test, Utf8Utf16ConversionTest) {
string mbs;
wstring wcs;
ASSERT_TRUE(strings::utf8_to_wcs(hi_utf8, &wcs));
ASSERT_EQ(wcs, hi_utf16);
ASSERT_TRUE(strings::utf8_to_wcs(kUtf8Text, &wcs));
ASSERT_TRUE(strings::wcs_to_utf8(kUtf16Text, &mbs));
ASSERT_EQ(wcs, kUtf16Text);
ASSERT_EQ(mbs, kUtf8Text);
}
} // namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment