io_win32_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: laszlocsomor@google.com (Laszlo Csomor)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
35 // Unit tests for long-path-aware open/mkdir/access/etc. on Windows, as well as
36 // for the supporting utility functions.
37 //
38 // This file is only used on Windows, it's empty on other platforms.
39 
40 #if defined(_WIN32)
41 
42 #define WIN32_LEAN_AND_MEAN
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/stat.h>
48 #include <sys/types.h>
49 #include <wchar.h>
50 #include <windows.h>
51 
53 #include <gtest/gtest.h>
54 
55 #include <memory>
56 #include <sstream>
57 #include <string>
58 
59 namespace google {
60 namespace protobuf {
61 namespace io {
62 namespace win32 {
63 namespace {
64 
65 const char kUtf8Text[] = {
66  'h', 'i', ' ',
67  // utf-8: 11010000 10011111, utf-16: 100 0001 1111 = 0x041F
68  static_cast<char>(0xd0), static_cast<char>(0x9f),
69  // utf-8: 11010001 10000000, utf-16: 100 0100 0000 = 0x0440
70  static_cast<char>(0xd1), static_cast<char>(0x80),
71  // utf-8: 11010000 10111000, utf-16: 100 0011 1000 = 0x0438
72  static_cast<char>(0xd0), static_cast<char>(0xb8),
73  // utf-8: 11010000 10110010, utf-16: 100 0011 0010 = 0x0432
74  static_cast<char>(0xd0), static_cast<char>(0xb2),
75  // utf-8: 11010000 10110101, utf-16: 100 0011 0101 = 0x0435
76  static_cast<char>(0xd0), static_cast<char>(0xb5),
77  // utf-8: 11010001 10000010, utf-16: 100 0100 0010 = 0x0442
78  static_cast<char>(0xd1), static_cast<char>(0x82), 0
79 };
80 
81 const wchar_t kUtf16Text[] = {
82  L'h', L'i', L' ',
83  L'\x41f', L'\x440', L'\x438', L'\x432', L'\x435', L'\x442', 0
84 };
85 
86 using std::string;
87 using std::wstring;
88 
89 class IoWin32Test : public ::testing::Test {
90  public:
91  void SetUp();
92  void TearDown();
93 
94  protected:
95  bool CreateAllUnder(wstring path);
96  bool DeleteAllUnder(wstring path);
97 
98  WCHAR working_directory[MAX_PATH];
99  string test_tmpdir;
100  wstring wtest_tmpdir;
101 };
102 
103 #define ASSERT_INITIALIZED \
104  { \
105  EXPECT_FALSE(test_tmpdir.empty()); \
106  EXPECT_FALSE(wtest_tmpdir.empty()); \
107  }
108 
109 namespace {
110 void StripTrailingSlashes(string* str) {
111  int i = str->size() - 1;
112  for (; i >= 0 && ((*str)[i] == '/' || (*str)[i] == '\\'); --i) {}
113  str->resize(i+1);
114 }
115 
116 bool GetEnvVarAsUtf8(const WCHAR* name, string* result) {
117  DWORD size = ::GetEnvironmentVariableW(name, nullptr, 0);
118  if (size > 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
119  std::unique_ptr<WCHAR[]> wcs(new WCHAR[size]);
120  ::GetEnvironmentVariableW(name, wcs.get(), size);
121  // GetEnvironmentVariableA retrieves an Active-Code-Page-encoded text which
122  // we'd first need to convert to UTF-16 then to UTF-8, because there seems
123  // to be no API function to do that conversion directly.
124  // GetEnvironmentVariableW retrieves an UTF-16-encoded text, which we need
125  // to convert to UTF-8.
126  return strings::wcs_to_utf8(wcs.get(), result);
127  } else {
128  return false;
129  }
130 }
131 
132 bool GetCwdAsUtf8(string* result) {
133  DWORD size = ::GetCurrentDirectoryW(0, nullptr);
134  if (size > 0) {
135  std::unique_ptr<WCHAR[]> wcs(new WCHAR[size]);
136  ::GetCurrentDirectoryW(size, wcs.get());
137  // GetCurrentDirectoryA retrieves an Active-Code-Page-encoded text which
138  // we'd first need to convert to UTF-16 then to UTF-8, because there seems
139  // to be no API function to do that conversion directly.
140  // GetCurrentDirectoryW retrieves an UTF-16-encoded text, which we need
141  // to convert to UTF-8.
142  return strings::wcs_to_utf8(wcs.get(), result);
143  } else {
144  return false;
145  }
146 }
147 
148 } // namespace
149 
150 void IoWin32Test::SetUp() {
151  test_tmpdir.clear();
152  wtest_tmpdir.clear();
153  EXPECT_GT(::GetCurrentDirectoryW(MAX_PATH, working_directory), 0);
154 
155  string tmp;
156  bool ok = false;
157  if (!ok) {
158  // Bazel sets this environment variable when it runs tests.
159  ok = GetEnvVarAsUtf8(L"TEST_TMPDIR", &tmp);
160  }
161  if (!ok) {
162  // Bazel 0.8.0 sets this environment for every build and test action.
163  ok = GetEnvVarAsUtf8(L"TEMP", &tmp);
164  }
165  if (!ok) {
166  // Bazel 0.8.0 sets this environment for every build and test action.
167  ok = GetEnvVarAsUtf8(L"TMP", &tmp);
168  }
169  if (!ok) {
170  // Fall back to using the current directory.
171  ok = GetCwdAsUtf8(&tmp);
172  }
173  if (!ok || tmp.empty()) {
174  FAIL() << "Cannot find a temp directory.";
175  }
176 
177  StripTrailingSlashes(&tmp);
178  std::stringstream result;
179  // Deleting files and directories is asynchronous on Windows, and if TearDown
180  // just deleted the previous temp directory, sometimes we cannot recreate the
181  // same directory.
182  // Use a counter so every test method gets its own temp directory.
183  static unsigned int counter = 0;
184  result << tmp << "\\w32tst" << counter++ << ".tmp";
185  test_tmpdir = result.str();
186  wtest_tmpdir = testonly_utf8_to_winpath(test_tmpdir.c_str());
187  ASSERT_FALSE(wtest_tmpdir.empty());
188  ASSERT_TRUE(DeleteAllUnder(wtest_tmpdir));
189  ASSERT_TRUE(CreateAllUnder(wtest_tmpdir));
190 }
191 
192 void IoWin32Test::TearDown() {
193  if (!wtest_tmpdir.empty()) {
194  DeleteAllUnder(wtest_tmpdir);
195  }
196  ::SetCurrentDirectoryW(working_directory);
197 }
198 
199 bool IoWin32Test::CreateAllUnder(wstring path) {
200  // Prepend UNC prefix if the path doesn't have it already. Don't bother
201  // checking if the path is shorter than MAX_PATH, let's just do it
202  // unconditionally.
203  if (path.find(L"\\\\?\\") != 0) {
204  path = wstring(L"\\\\?\\") + path;
205  }
206  if (::CreateDirectoryW(path.c_str(), nullptr) ||
207  GetLastError() == ERROR_ALREADY_EXISTS ||
208  GetLastError() == ERROR_ACCESS_DENIED) {
209  return true;
210  }
211  if (GetLastError() == ERROR_PATH_NOT_FOUND) {
212  size_t pos = path.find_last_of(L'\\');
213  if (pos != wstring::npos) {
214  wstring parent(path, 0, pos);
215  if (CreateAllUnder(parent) && CreateDirectoryW(path.c_str(), nullptr)) {
216  return true;
217  }
218  }
219  }
220  return false;
221 }
222 
223 bool IoWin32Test::DeleteAllUnder(wstring path) {
224  static const wstring kDot(L".");
225  static const wstring kDotDot(L"..");
226 
227  // Prepend UNC prefix if the path doesn't have it already. Don't bother
228  // checking if the path is shorter than MAX_PATH, let's just do it
229  // unconditionally.
230  if (path.find(L"\\\\?\\") != 0) {
231  path = wstring(L"\\\\?\\") + path;
232  }
233  // Append "\" if necessary.
234  if (path[path.size() - 1] != L'\\') {
235  path.push_back(L'\\');
236  }
237 
238  WIN32_FIND_DATAW metadata;
239  HANDLE handle = ::FindFirstFileW((path + L"*").c_str(), &metadata);
240  if (handle == INVALID_HANDLE_VALUE) {
241  return true; // directory doesn't exist
242  }
243 
244  bool result = true;
245  do {
246  wstring childname = metadata.cFileName;
247  if (kDot != childname && kDotDot != childname) {
248  wstring childpath = path + childname;
249  if ((metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
250  // If this is not a junction, delete its contents recursively.
251  // Finally delete this directory/junction too.
252  if (((metadata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0 &&
253  !DeleteAllUnder(childpath)) ||
254  !::RemoveDirectoryW(childpath.c_str())) {
255  result = false;
256  break;
257  }
258  } else {
259  if (!::DeleteFileW(childpath.c_str())) {
260  result = false;
261  break;
262  }
263  }
264  }
265  } while (::FindNextFileW(handle, &metadata));
266  ::FindClose(handle);
267  return result;
268 }
269 
270 TEST_F(IoWin32Test, AccessTest) {
271  ASSERT_INITIALIZED;
272 
273  string path = test_tmpdir;
274  while (path.size() < MAX_PATH - 30) {
275  path += "\\accesstest";
276  EXPECT_EQ(mkdir(path.c_str(), 0644), 0);
277  }
278  string file = path + "\\file.txt";
279  int fd = open(file.c_str(), O_CREAT | O_WRONLY, 0644);
280  if (fd > 0) {
281  EXPECT_EQ(close(fd), 0);
282  } else {
283  EXPECT_TRUE(false);
284  }
285 
286  EXPECT_EQ(access(test_tmpdir.c_str(), F_OK), 0);
287  EXPECT_EQ(access(path.c_str(), F_OK), 0);
288  EXPECT_EQ(access(path.c_str(), W_OK), 0);
289  EXPECT_EQ(access(file.c_str(), F_OK | W_OK), 0);
290  EXPECT_NE(access((file + ".blah").c_str(), F_OK), 0);
291  EXPECT_NE(access((file + ".blah").c_str(), W_OK), 0);
292 
293  EXPECT_EQ(access(".", F_OK), 0);
294  EXPECT_EQ(access(".", W_OK), 0);
295  EXPECT_EQ(access((test_tmpdir + "/accesstest").c_str(), F_OK | W_OK), 0);
296  ASSERT_EQ(access((test_tmpdir + "/./normalize_me/.././accesstest").c_str(),
297  F_OK | W_OK),
298  0);
299  EXPECT_NE(access("io_win32_unittest.AccessTest.nonexistent", F_OK), 0);
300  EXPECT_NE(access("io_win32_unittest.AccessTest.nonexistent", W_OK), 0);
301 
302  ASSERT_EQ(access("c:bad", F_OK), -1);
303  ASSERT_EQ(errno, ENOENT);
304  ASSERT_EQ(access("/tmp/bad", F_OK), -1);
305  ASSERT_EQ(errno, ENOENT);
306  ASSERT_EQ(access("\\bad", F_OK), -1);
307  ASSERT_EQ(errno, ENOENT);
308 }
309 
310 TEST_F(IoWin32Test, OpenTest) {
311  ASSERT_INITIALIZED;
312 
313  string path = test_tmpdir;
314  while (path.size() < MAX_PATH) {
315  path += "\\opentest";
316  EXPECT_EQ(mkdir(path.c_str(), 0644), 0);
317  }
318  string file = path + "\\file.txt";
319  int fd = open(file.c_str(), O_CREAT | O_WRONLY, 0644);
320  if (fd > 0) {
321  EXPECT_EQ(write(fd, "hello", 5), 5);
322  EXPECT_EQ(close(fd), 0);
323  } else {
324  EXPECT_TRUE(false);
325  }
326 
327  ASSERT_EQ(open("c:bad.txt", O_CREAT | O_WRONLY, 0644), -1);
328  ASSERT_EQ(errno, ENOENT);
329  ASSERT_EQ(open("/tmp/bad.txt", O_CREAT | O_WRONLY, 0644), -1);
330  ASSERT_EQ(errno, ENOENT);
331  ASSERT_EQ(open("\\bad.txt", O_CREAT | O_WRONLY, 0644), -1);
332  ASSERT_EQ(errno, ENOENT);
333 }
334 
335 TEST_F(IoWin32Test, MkdirTest) {
336  ASSERT_INITIALIZED;
337 
338  string path = test_tmpdir;
339  do {
340  path += "\\mkdirtest";
341  ASSERT_EQ(mkdir(path.c_str(), 0644), 0);
342  } while (path.size() <= MAX_PATH);
343 
344  ASSERT_EQ(mkdir("c:bad", 0644), -1);
345  ASSERT_EQ(errno, ENOENT);
346  ASSERT_EQ(mkdir("/tmp/bad", 0644), -1);
347  ASSERT_EQ(errno, ENOENT);
348  ASSERT_EQ(mkdir("\\bad", 0644), -1);
349  ASSERT_EQ(errno, ENOENT);
350 }
351 
352 TEST_F(IoWin32Test, MkdirTestNonAscii) {
353  ASSERT_INITIALIZED;
354 
355  // Create a non-ASCII path.
356  // Ensure that we can create the directory using SetCurrentDirectoryW.
357  EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1").c_str(), nullptr));
358  EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1\\" + kUtf16Text).c_str(), nullptr));
359  // Ensure that we can create a very similarly named directory using mkdir.
360  // We don't attemp to delete and recreate the same directory, because on
361  // Windows, deleting files and directories seems to be asynchronous.
362  EXPECT_EQ(mkdir((test_tmpdir + "\\2").c_str(), 0644), 0);
363  EXPECT_EQ(mkdir((test_tmpdir + "\\2\\" + kUtf8Text).c_str(), 0644), 0);
364 }
365 
366 TEST_F(IoWin32Test, ChdirTest) {
367  string path("C:\\");
368  EXPECT_EQ(access(path.c_str(), F_OK), 0);
369  ASSERT_EQ(chdir(path.c_str()), 0);
370 
371  // Do not try to chdir into the test_tmpdir, it may already contain directory
372  // names with trailing dots.
373  // Instead test here with an obviously dot-trailed path. If the win32_chdir
374  // function would not convert the path to absolute and prefix with "\\?\" then
375  // the Win32 API would ignore the trailing dot, but because of the prefixing
376  // there'll be no path processing done, so we'll actually attempt to chdir
377  // into "C:\some\path\foo."
378  path = test_tmpdir + "/foo.";
379  EXPECT_EQ(mkdir(path.c_str(), 644), 0);
380  EXPECT_EQ(access(path.c_str(), F_OK), 0);
381  ASSERT_NE(chdir(path.c_str()), 0);
382 }
383 
384 TEST_F(IoWin32Test, ChdirTestNonAscii) {
385  ASSERT_INITIALIZED;
386 
387  // Create a directory with a non-ASCII path and ensure we can cd into it.
388  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
389  string nonAscii;
390  EXPECT_TRUE(strings::wcs_to_utf8(wNonAscii.c_str(), &nonAscii));
391  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
392  WCHAR cwd[MAX_PATH];
393  EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
394  // Ensure that we can cd into the path using SetCurrentDirectoryW.
395  EXPECT_TRUE(SetCurrentDirectoryW(wNonAscii.c_str()));
396  EXPECT_TRUE(SetCurrentDirectoryW(cwd));
397  // Ensure that we can cd into the path using chdir.
398  ASSERT_EQ(chdir(nonAscii.c_str()), 0);
399  // Ensure that the GetCurrentDirectoryW returns the desired path.
400  EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
401  ASSERT_EQ(wNonAscii, cwd);
402 }
403 
404 TEST_F(IoWin32Test, AsWindowsPathTest) {
405  DWORD size = GetCurrentDirectoryW(0, nullptr);
406  std::unique_ptr<wchar_t[]> cwd_str(new wchar_t[size]);
407  EXPECT_GT(GetCurrentDirectoryW(size, cwd_str.get()), 0);
408  wstring cwd = wstring(L"\\\\?\\") + cwd_str.get();
409 
410  ASSERT_EQ(testonly_utf8_to_winpath("relative_mkdirtest"),
411  cwd + L"\\relative_mkdirtest");
412  ASSERT_EQ(testonly_utf8_to_winpath("preserve//\\trailing///"),
413  cwd + L"\\preserve\\trailing\\");
414  ASSERT_EQ(testonly_utf8_to_winpath("./normalize_me\\/../blah"),
415  cwd + L"\\blah");
416  std::ostringstream relpath;
417  for (wchar_t* p = cwd_str.get(); *p; ++p) {
418  if (*p == '/' || *p == '\\') {
419  relpath << "../";
420  }
421  }
422  relpath << ".\\/../\\./beyond-toplevel";
423  ASSERT_EQ(testonly_utf8_to_winpath(relpath.str().c_str()),
424  wstring(L"\\\\?\\") + cwd_str.get()[0] + L":\\beyond-toplevel");
425 
426  // Absolute unix paths lack drive letters, driveless absolute windows paths
427  // do too. Neither can be converted to a drive-specifying absolute Windows
428  // path.
429  ASSERT_EQ(testonly_utf8_to_winpath("/absolute/unix/path"), L"");
430  // Though valid on Windows, we also don't support UNC paths (\\UNC\\blah).
431  ASSERT_EQ(testonly_utf8_to_winpath("\\driveless\\absolute"), L"");
432  // Though valid in cmd.exe, drive-relative paths are not supported.
433  ASSERT_EQ(testonly_utf8_to_winpath("c:foo"), L"");
434  ASSERT_EQ(testonly_utf8_to_winpath("c:/foo"), L"\\\\?\\c:\\foo");
435  ASSERT_EQ(testonly_utf8_to_winpath("\\\\?\\C:\\foo"), L"\\\\?\\C:\\foo");
436 }
437 
438 TEST_F(IoWin32Test, Utf8Utf16ConversionTest) {
439  string mbs;
440  wstring wcs;
441  ASSERT_TRUE(strings::utf8_to_wcs(kUtf8Text, &wcs));
442  ASSERT_TRUE(strings::wcs_to_utf8(kUtf16Text, &mbs));
443  ASSERT_EQ(wcs, kUtf16Text);
444  ASSERT_EQ(mbs, kUtf8Text);
445 }
446 
447 } // namespace
448 } // namespace win32
449 } // namespace io
450 } // namespace protobuf
451 } // namespace google
452 
453 #endif // defined(_WIN32)
454 
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: gtest.h:1998
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: gtest.h:2086
FAIL
#define FAIL()
Definition: gtest.h:1952
gtest.h
benchmarks.util.result_uploader.metadata
def metadata
Definition: result_uploader.py:97
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: glog/src/googletest.h:157
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
errno
int errno
testing::Test
Definition: gtest.h:415
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:2082
ok
ROSCPP_DECL bool ok()
testing::internal::wstring
::std::wstring wstring
Definition: gtest-port.h:887
access
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:4266
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: glog/src/googletest.h:156
update_failure_list.str
str
Definition: update_failure_list.py:41
p
const char * p
Definition: gmock-matchers_test.cc:3863
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: gtest.h:1995
i
int i
Definition: gmock-matchers_test.cc:764
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::TEST_F
TEST_F(DynamicMessageTest, Descriptor)
Definition: dynamic_message_unittest.cc:126
HANDLE
void * HANDLE
Definition: wepoll.c:70
io_win32.h
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54