1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
4ffe3c632Sopenharmony_ci//
5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
7ffe3c632Sopenharmony_ci// met:
8ffe3c632Sopenharmony_ci//
9ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
11ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
14ffe3c632Sopenharmony_ci// distribution.
15ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
17ffe3c632Sopenharmony_ci// this software without specific prior written permission.
18ffe3c632Sopenharmony_ci//
19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ffe3c632Sopenharmony_ci
31ffe3c632Sopenharmony_ci// Author: laszlocsomor@google.com (Laszlo Csomor)
32ffe3c632Sopenharmony_ci//  Based on original Protocol Buffers design by
33ffe3c632Sopenharmony_ci//  Sanjay Ghemawat, Jeff Dean, and others.
34ffe3c632Sopenharmony_ci
35ffe3c632Sopenharmony_ci// Unit tests for long-path-aware open/mkdir/access/etc. on Windows, as well as
36ffe3c632Sopenharmony_ci// for the supporting utility functions.
37ffe3c632Sopenharmony_ci//
38ffe3c632Sopenharmony_ci// This file is only used on Windows, it's empty on other platforms.
39ffe3c632Sopenharmony_ci
40ffe3c632Sopenharmony_ci#if defined(_WIN32)
41ffe3c632Sopenharmony_ci
42ffe3c632Sopenharmony_ci#define WIN32_LEAN_AND_MEAN
43ffe3c632Sopenharmony_ci#include <google/protobuf/io/io_win32.h>
44ffe3c632Sopenharmony_ci
45ffe3c632Sopenharmony_ci#include <errno.h>
46ffe3c632Sopenharmony_ci#include <fcntl.h>
47ffe3c632Sopenharmony_ci#include <stdlib.h>
48ffe3c632Sopenharmony_ci#include <string.h>
49ffe3c632Sopenharmony_ci#include <sys/stat.h>
50ffe3c632Sopenharmony_ci#include <sys/types.h>
51ffe3c632Sopenharmony_ci#include <wchar.h>
52ffe3c632Sopenharmony_ci#include <windows.h>
53ffe3c632Sopenharmony_ci
54ffe3c632Sopenharmony_ci#include <memory>
55ffe3c632Sopenharmony_ci#include <sstream>
56ffe3c632Sopenharmony_ci#include <string>
57ffe3c632Sopenharmony_ci#include <vector>
58ffe3c632Sopenharmony_ci
59ffe3c632Sopenharmony_ci#include <gtest/gtest.h>
60ffe3c632Sopenharmony_ci
61ffe3c632Sopenharmony_cinamespace google {
62ffe3c632Sopenharmony_cinamespace protobuf {
63ffe3c632Sopenharmony_cinamespace io {
64ffe3c632Sopenharmony_cinamespace win32 {
65ffe3c632Sopenharmony_cinamespace {
66ffe3c632Sopenharmony_ci
67ffe3c632Sopenharmony_ciconst char kUtf8Text[] = {
68ffe3c632Sopenharmony_ci    'h', 'i', ' ',
69ffe3c632Sopenharmony_ci    // utf-8: 11010000 10011111, utf-16: 100 0001 1111 = 0x041F
70ffe3c632Sopenharmony_ci    static_cast<char>(0xd0), static_cast<char>(0x9f),
71ffe3c632Sopenharmony_ci    // utf-8: 11010001 10000000, utf-16: 100 0100 0000 = 0x0440
72ffe3c632Sopenharmony_ci    static_cast<char>(0xd1), static_cast<char>(0x80),
73ffe3c632Sopenharmony_ci    // utf-8: 11010000 10111000, utf-16: 100 0011 1000 = 0x0438
74ffe3c632Sopenharmony_ci    static_cast<char>(0xd0), static_cast<char>(0xb8),
75ffe3c632Sopenharmony_ci    // utf-8: 11010000 10110010, utf-16: 100 0011 0010 = 0x0432
76ffe3c632Sopenharmony_ci    static_cast<char>(0xd0), static_cast<char>(0xb2),
77ffe3c632Sopenharmony_ci    // utf-8: 11010000 10110101, utf-16: 100 0011 0101 = 0x0435
78ffe3c632Sopenharmony_ci    static_cast<char>(0xd0), static_cast<char>(0xb5),
79ffe3c632Sopenharmony_ci    // utf-8: 11010001 10000010, utf-16: 100 0100 0010 = 0x0442
80ffe3c632Sopenharmony_ci    static_cast<char>(0xd1), static_cast<char>(0x82), 0
81ffe3c632Sopenharmony_ci};
82ffe3c632Sopenharmony_ci
83ffe3c632Sopenharmony_ciconst wchar_t kUtf16Text[] = {
84ffe3c632Sopenharmony_ci  L'h', L'i', L' ',
85ffe3c632Sopenharmony_ci  L'\x41f', L'\x440', L'\x438', L'\x432', L'\x435', L'\x442', 0
86ffe3c632Sopenharmony_ci};
87ffe3c632Sopenharmony_ci
88ffe3c632Sopenharmony_ciusing std::string;
89ffe3c632Sopenharmony_ciusing std::vector;
90ffe3c632Sopenharmony_ciusing std::wstring;
91ffe3c632Sopenharmony_ci
92ffe3c632Sopenharmony_ciclass IoWin32Test : public ::testing::Test {
93ffe3c632Sopenharmony_ci public:
94ffe3c632Sopenharmony_ci  void SetUp();
95ffe3c632Sopenharmony_ci  void TearDown();
96ffe3c632Sopenharmony_ci
97ffe3c632Sopenharmony_ci protected:
98ffe3c632Sopenharmony_ci  bool CreateAllUnder(wstring path);
99ffe3c632Sopenharmony_ci  bool DeleteAllUnder(wstring path);
100ffe3c632Sopenharmony_ci
101ffe3c632Sopenharmony_ci  WCHAR working_directory[MAX_PATH];
102ffe3c632Sopenharmony_ci  string test_tmpdir;
103ffe3c632Sopenharmony_ci  wstring wtest_tmpdir;
104ffe3c632Sopenharmony_ci};
105ffe3c632Sopenharmony_ci
106ffe3c632Sopenharmony_ci#define ASSERT_INITIALIZED              \
107ffe3c632Sopenharmony_ci  {                                     \
108ffe3c632Sopenharmony_ci    EXPECT_FALSE(test_tmpdir.empty());  \
109ffe3c632Sopenharmony_ci    EXPECT_FALSE(wtest_tmpdir.empty()); \
110ffe3c632Sopenharmony_ci  }
111ffe3c632Sopenharmony_ci
112ffe3c632Sopenharmony_cinamespace {
113ffe3c632Sopenharmony_civoid StripTrailingSlashes(string* str) {
114ffe3c632Sopenharmony_ci  int i = str->size() - 1;
115ffe3c632Sopenharmony_ci  for (; i >= 0 && ((*str)[i] == '/' || (*str)[i] == '\\'); --i) {}
116ffe3c632Sopenharmony_ci  str->resize(i+1);
117ffe3c632Sopenharmony_ci}
118ffe3c632Sopenharmony_ci
119ffe3c632Sopenharmony_cibool GetEnvVarAsUtf8(const WCHAR* name, string* result) {
120ffe3c632Sopenharmony_ci  DWORD size = ::GetEnvironmentVariableW(name, nullptr, 0);
121ffe3c632Sopenharmony_ci  if (size > 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
122ffe3c632Sopenharmony_ci    std::unique_ptr<WCHAR[]> wcs(new WCHAR[size]);
123ffe3c632Sopenharmony_ci    ::GetEnvironmentVariableW(name, wcs.get(), size);
124ffe3c632Sopenharmony_ci    // GetEnvironmentVariableA retrieves an Active-Code-Page-encoded text which
125ffe3c632Sopenharmony_ci    // we'd first need to convert to UTF-16 then to UTF-8, because there seems
126ffe3c632Sopenharmony_ci    // to be no API function to do that conversion directly.
127ffe3c632Sopenharmony_ci    // GetEnvironmentVariableW retrieves an UTF-16-encoded text, which we need
128ffe3c632Sopenharmony_ci    // to convert to UTF-8.
129ffe3c632Sopenharmony_ci    return strings::wcs_to_utf8(wcs.get(), result);
130ffe3c632Sopenharmony_ci  } else {
131ffe3c632Sopenharmony_ci    return false;
132ffe3c632Sopenharmony_ci  }
133ffe3c632Sopenharmony_ci}
134ffe3c632Sopenharmony_ci
135ffe3c632Sopenharmony_cibool GetCwdAsUtf8(string* result) {
136ffe3c632Sopenharmony_ci  DWORD size = ::GetCurrentDirectoryW(0, nullptr);
137ffe3c632Sopenharmony_ci  if (size > 0) {
138ffe3c632Sopenharmony_ci    std::unique_ptr<WCHAR[]> wcs(new WCHAR[size]);
139ffe3c632Sopenharmony_ci    ::GetCurrentDirectoryW(size, wcs.get());
140ffe3c632Sopenharmony_ci    // GetCurrentDirectoryA retrieves an Active-Code-Page-encoded text which
141ffe3c632Sopenharmony_ci    // we'd first need to convert to UTF-16 then to UTF-8, because there seems
142ffe3c632Sopenharmony_ci    // to be no API function to do that conversion directly.
143ffe3c632Sopenharmony_ci    // GetCurrentDirectoryW retrieves an UTF-16-encoded text, which we need
144ffe3c632Sopenharmony_ci    // to convert to UTF-8.
145ffe3c632Sopenharmony_ci    return strings::wcs_to_utf8(wcs.get(), result);
146ffe3c632Sopenharmony_ci  } else {
147ffe3c632Sopenharmony_ci    return false;
148ffe3c632Sopenharmony_ci  }
149ffe3c632Sopenharmony_ci}
150ffe3c632Sopenharmony_ci
151ffe3c632Sopenharmony_cibool CreateEmptyFile(const wstring& path) {
152ffe3c632Sopenharmony_ci  HANDLE h = CreateFileW(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
153ffe3c632Sopenharmony_ci                         FILE_ATTRIBUTE_NORMAL, NULL);
154ffe3c632Sopenharmony_ci  if (h == INVALID_HANDLE_VALUE) {
155ffe3c632Sopenharmony_ci    return false;
156ffe3c632Sopenharmony_ci  }
157ffe3c632Sopenharmony_ci  CloseHandle(h);
158ffe3c632Sopenharmony_ci  return true;
159ffe3c632Sopenharmony_ci}
160ffe3c632Sopenharmony_ci
161ffe3c632Sopenharmony_ci}  // namespace
162ffe3c632Sopenharmony_ci
163ffe3c632Sopenharmony_civoid IoWin32Test::SetUp() {
164ffe3c632Sopenharmony_ci  test_tmpdir.clear();
165ffe3c632Sopenharmony_ci  wtest_tmpdir.clear();
166ffe3c632Sopenharmony_ci  DWORD size = ::GetCurrentDirectoryW(MAX_PATH, working_directory);
167ffe3c632Sopenharmony_ci  EXPECT_GT(size, 0);
168ffe3c632Sopenharmony_ci  EXPECT_LT(size, MAX_PATH);
169ffe3c632Sopenharmony_ci
170ffe3c632Sopenharmony_ci  string tmp;
171ffe3c632Sopenharmony_ci  bool ok = false;
172ffe3c632Sopenharmony_ci  if (!ok) {
173ffe3c632Sopenharmony_ci    // Bazel sets this environment variable when it runs tests.
174ffe3c632Sopenharmony_ci    ok = GetEnvVarAsUtf8(L"TEST_TMPDIR", &tmp);
175ffe3c632Sopenharmony_ci  }
176ffe3c632Sopenharmony_ci  if (!ok) {
177ffe3c632Sopenharmony_ci    // Bazel 0.8.0 sets this environment for every build and test action.
178ffe3c632Sopenharmony_ci    ok = GetEnvVarAsUtf8(L"TEMP", &tmp);
179ffe3c632Sopenharmony_ci  }
180ffe3c632Sopenharmony_ci  if (!ok) {
181ffe3c632Sopenharmony_ci    // Bazel 0.8.0 sets this environment for every build and test action.
182ffe3c632Sopenharmony_ci    ok = GetEnvVarAsUtf8(L"TMP", &tmp);
183ffe3c632Sopenharmony_ci  }
184ffe3c632Sopenharmony_ci  if (!ok) {
185ffe3c632Sopenharmony_ci    // Fall back to using the current directory.
186ffe3c632Sopenharmony_ci    ok = GetCwdAsUtf8(&tmp);
187ffe3c632Sopenharmony_ci  }
188ffe3c632Sopenharmony_ci  if (!ok || tmp.empty()) {
189ffe3c632Sopenharmony_ci    FAIL() << "Cannot find a temp directory.";
190ffe3c632Sopenharmony_ci  }
191ffe3c632Sopenharmony_ci
192ffe3c632Sopenharmony_ci  StripTrailingSlashes(&tmp);
193ffe3c632Sopenharmony_ci  std::stringstream result;
194ffe3c632Sopenharmony_ci  // Deleting files and directories is asynchronous on Windows, and if TearDown
195ffe3c632Sopenharmony_ci  // just deleted the previous temp directory, sometimes we cannot recreate the
196ffe3c632Sopenharmony_ci  // same directory.
197ffe3c632Sopenharmony_ci  // Use a counter so every test method gets its own temp directory.
198ffe3c632Sopenharmony_ci  static unsigned int counter = 0;
199ffe3c632Sopenharmony_ci  result << tmp << "\\w32tst" << counter++ << ".tmp";
200ffe3c632Sopenharmony_ci  test_tmpdir = result.str();
201ffe3c632Sopenharmony_ci  wtest_tmpdir = testonly_utf8_to_winpath(test_tmpdir.c_str());
202ffe3c632Sopenharmony_ci  ASSERT_FALSE(wtest_tmpdir.empty());
203ffe3c632Sopenharmony_ci  ASSERT_TRUE(DeleteAllUnder(wtest_tmpdir));
204ffe3c632Sopenharmony_ci  ASSERT_TRUE(CreateAllUnder(wtest_tmpdir));
205ffe3c632Sopenharmony_ci}
206ffe3c632Sopenharmony_ci
207ffe3c632Sopenharmony_civoid IoWin32Test::TearDown() {
208ffe3c632Sopenharmony_ci  if (!wtest_tmpdir.empty()) {
209ffe3c632Sopenharmony_ci    DeleteAllUnder(wtest_tmpdir);
210ffe3c632Sopenharmony_ci  }
211ffe3c632Sopenharmony_ci  ::SetCurrentDirectoryW(working_directory);
212ffe3c632Sopenharmony_ci}
213ffe3c632Sopenharmony_ci
214ffe3c632Sopenharmony_cibool IoWin32Test::CreateAllUnder(wstring path) {
215ffe3c632Sopenharmony_ci  // Prepend UNC prefix if the path doesn't have it already. Don't bother
216ffe3c632Sopenharmony_ci  // checking if the path is shorter than MAX_PATH, let's just do it
217ffe3c632Sopenharmony_ci  // unconditionally.
218ffe3c632Sopenharmony_ci  if (path.find(L"\\\\?\\") != 0) {
219ffe3c632Sopenharmony_ci    path = wstring(L"\\\\?\\") + path;
220ffe3c632Sopenharmony_ci  }
221ffe3c632Sopenharmony_ci  if (::CreateDirectoryW(path.c_str(), nullptr) ||
222ffe3c632Sopenharmony_ci      GetLastError() == ERROR_ALREADY_EXISTS ||
223ffe3c632Sopenharmony_ci      GetLastError() == ERROR_ACCESS_DENIED) {
224ffe3c632Sopenharmony_ci    return true;
225ffe3c632Sopenharmony_ci  }
226ffe3c632Sopenharmony_ci  if (GetLastError() == ERROR_PATH_NOT_FOUND) {
227ffe3c632Sopenharmony_ci    size_t pos = path.find_last_of(L'\\');
228ffe3c632Sopenharmony_ci    if (pos != wstring::npos) {
229ffe3c632Sopenharmony_ci      wstring parent(path, 0, pos);
230ffe3c632Sopenharmony_ci      if (CreateAllUnder(parent) && CreateDirectoryW(path.c_str(), nullptr)) {
231ffe3c632Sopenharmony_ci        return true;
232ffe3c632Sopenharmony_ci      }
233ffe3c632Sopenharmony_ci    }
234ffe3c632Sopenharmony_ci  }
235ffe3c632Sopenharmony_ci  return false;
236ffe3c632Sopenharmony_ci}
237ffe3c632Sopenharmony_ci
238ffe3c632Sopenharmony_cibool IoWin32Test::DeleteAllUnder(wstring path) {
239ffe3c632Sopenharmony_ci  static const wstring kDot(L".");
240ffe3c632Sopenharmony_ci  static const wstring kDotDot(L"..");
241ffe3c632Sopenharmony_ci
242ffe3c632Sopenharmony_ci  // Prepend UNC prefix if the path doesn't have it already. Don't bother
243ffe3c632Sopenharmony_ci  // checking if the path is shorter than MAX_PATH, let's just do it
244ffe3c632Sopenharmony_ci  // unconditionally.
245ffe3c632Sopenharmony_ci  if (path.find(L"\\\\?\\") != 0) {
246ffe3c632Sopenharmony_ci    path = wstring(L"\\\\?\\") + path;
247ffe3c632Sopenharmony_ci  }
248ffe3c632Sopenharmony_ci  // Append "\" if necessary.
249ffe3c632Sopenharmony_ci  if (path[path.size() - 1] != L'\\') {
250ffe3c632Sopenharmony_ci    path.push_back(L'\\');
251ffe3c632Sopenharmony_ci  }
252ffe3c632Sopenharmony_ci
253ffe3c632Sopenharmony_ci  WIN32_FIND_DATAW metadata;
254ffe3c632Sopenharmony_ci  HANDLE handle = ::FindFirstFileW((path + L"*").c_str(), &metadata);
255ffe3c632Sopenharmony_ci  if (handle == INVALID_HANDLE_VALUE) {
256ffe3c632Sopenharmony_ci    return true;  // directory doesn't exist
257ffe3c632Sopenharmony_ci  }
258ffe3c632Sopenharmony_ci
259ffe3c632Sopenharmony_ci  bool result = true;
260ffe3c632Sopenharmony_ci  do {
261ffe3c632Sopenharmony_ci    wstring childname = metadata.cFileName;
262ffe3c632Sopenharmony_ci    if (kDot != childname && kDotDot != childname) {
263ffe3c632Sopenharmony_ci      wstring childpath = path + childname;
264ffe3c632Sopenharmony_ci      if ((metadata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
265ffe3c632Sopenharmony_ci        // If this is not a junction, delete its contents recursively.
266ffe3c632Sopenharmony_ci        // Finally delete this directory/junction too.
267ffe3c632Sopenharmony_ci        if (((metadata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0 &&
268ffe3c632Sopenharmony_ci             !DeleteAllUnder(childpath)) ||
269ffe3c632Sopenharmony_ci            !::RemoveDirectoryW(childpath.c_str())) {
270ffe3c632Sopenharmony_ci          result = false;
271ffe3c632Sopenharmony_ci          break;
272ffe3c632Sopenharmony_ci        }
273ffe3c632Sopenharmony_ci      } else {
274ffe3c632Sopenharmony_ci        if (!::DeleteFileW(childpath.c_str())) {
275ffe3c632Sopenharmony_ci          result = false;
276ffe3c632Sopenharmony_ci          break;
277ffe3c632Sopenharmony_ci        }
278ffe3c632Sopenharmony_ci      }
279ffe3c632Sopenharmony_ci    }
280ffe3c632Sopenharmony_ci  } while (::FindNextFileW(handle, &metadata));
281ffe3c632Sopenharmony_ci  ::FindClose(handle);
282ffe3c632Sopenharmony_ci  return result;
283ffe3c632Sopenharmony_ci}
284ffe3c632Sopenharmony_ci
285ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, AccessTest) {
286ffe3c632Sopenharmony_ci  ASSERT_INITIALIZED;
287ffe3c632Sopenharmony_ci
288ffe3c632Sopenharmony_ci  string path = test_tmpdir;
289ffe3c632Sopenharmony_ci  while (path.size() < MAX_PATH - 30) {
290ffe3c632Sopenharmony_ci    path += "\\accesstest";
291ffe3c632Sopenharmony_ci    EXPECT_EQ(mkdir(path.c_str(), 0644), 0);
292ffe3c632Sopenharmony_ci  }
293ffe3c632Sopenharmony_ci  string file = path + "\\file.txt";
294ffe3c632Sopenharmony_ci  int fd = open(file.c_str(), O_CREAT | O_WRONLY, 0644);
295ffe3c632Sopenharmony_ci  if (fd > 0) {
296ffe3c632Sopenharmony_ci    EXPECT_EQ(close(fd), 0);
297ffe3c632Sopenharmony_ci  } else {
298ffe3c632Sopenharmony_ci    EXPECT_TRUE(false);
299ffe3c632Sopenharmony_ci  }
300ffe3c632Sopenharmony_ci
301ffe3c632Sopenharmony_ci  EXPECT_EQ(access(test_tmpdir.c_str(), F_OK), 0);
302ffe3c632Sopenharmony_ci  EXPECT_EQ(access(path.c_str(), F_OK), 0);
303ffe3c632Sopenharmony_ci  EXPECT_EQ(access(path.c_str(), W_OK), 0);
304ffe3c632Sopenharmony_ci  EXPECT_EQ(access(file.c_str(), F_OK | W_OK), 0);
305ffe3c632Sopenharmony_ci  EXPECT_NE(access((file + ".blah").c_str(), F_OK), 0);
306ffe3c632Sopenharmony_ci  EXPECT_NE(access((file + ".blah").c_str(), W_OK), 0);
307ffe3c632Sopenharmony_ci
308ffe3c632Sopenharmony_ci  EXPECT_EQ(access(".", F_OK), 0);
309ffe3c632Sopenharmony_ci  EXPECT_EQ(access(".", W_OK), 0);
310ffe3c632Sopenharmony_ci  EXPECT_EQ(access((test_tmpdir + "/accesstest").c_str(), F_OK | W_OK), 0);
311ffe3c632Sopenharmony_ci  ASSERT_EQ(access((test_tmpdir + "/./normalize_me/.././accesstest").c_str(),
312ffe3c632Sopenharmony_ci                   F_OK | W_OK),
313ffe3c632Sopenharmony_ci            0);
314ffe3c632Sopenharmony_ci  EXPECT_NE(access("io_win32_unittest.AccessTest.nonexistent", F_OK), 0);
315ffe3c632Sopenharmony_ci  EXPECT_NE(access("io_win32_unittest.AccessTest.nonexistent", W_OK), 0);
316ffe3c632Sopenharmony_ci
317ffe3c632Sopenharmony_ci  ASSERT_EQ(access("c:bad", F_OK), -1);
318ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
319ffe3c632Sopenharmony_ci  ASSERT_EQ(access("/tmp/bad", F_OK), -1);
320ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
321ffe3c632Sopenharmony_ci  ASSERT_EQ(access("\\bad", F_OK), -1);
322ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
323ffe3c632Sopenharmony_ci}
324ffe3c632Sopenharmony_ci
325ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, OpenTest) {
326ffe3c632Sopenharmony_ci  ASSERT_INITIALIZED;
327ffe3c632Sopenharmony_ci
328ffe3c632Sopenharmony_ci  string path = test_tmpdir;
329ffe3c632Sopenharmony_ci  while (path.size() < MAX_PATH) {
330ffe3c632Sopenharmony_ci    path += "\\opentest";
331ffe3c632Sopenharmony_ci    EXPECT_EQ(mkdir(path.c_str(), 0644), 0);
332ffe3c632Sopenharmony_ci  }
333ffe3c632Sopenharmony_ci  string file = path + "\\file.txt";
334ffe3c632Sopenharmony_ci  int fd = open(file.c_str(), O_CREAT | O_WRONLY, 0644);
335ffe3c632Sopenharmony_ci  if (fd > 0) {
336ffe3c632Sopenharmony_ci    EXPECT_EQ(write(fd, "hello", 5), 5);
337ffe3c632Sopenharmony_ci    EXPECT_EQ(close(fd), 0);
338ffe3c632Sopenharmony_ci  } else {
339ffe3c632Sopenharmony_ci    EXPECT_TRUE(false);
340ffe3c632Sopenharmony_ci  }
341ffe3c632Sopenharmony_ci
342ffe3c632Sopenharmony_ci  ASSERT_EQ(open("c:bad.txt", O_CREAT | O_WRONLY, 0644), -1);
343ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
344ffe3c632Sopenharmony_ci  ASSERT_EQ(open("/tmp/bad.txt", O_CREAT | O_WRONLY, 0644), -1);
345ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
346ffe3c632Sopenharmony_ci  ASSERT_EQ(open("\\bad.txt", O_CREAT | O_WRONLY, 0644), -1);
347ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
348ffe3c632Sopenharmony_ci}
349ffe3c632Sopenharmony_ci
350ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, MkdirTest) {
351ffe3c632Sopenharmony_ci  ASSERT_INITIALIZED;
352ffe3c632Sopenharmony_ci
353ffe3c632Sopenharmony_ci  string path = test_tmpdir;
354ffe3c632Sopenharmony_ci  do {
355ffe3c632Sopenharmony_ci    path += "\\mkdirtest";
356ffe3c632Sopenharmony_ci    ASSERT_EQ(mkdir(path.c_str(), 0644), 0);
357ffe3c632Sopenharmony_ci  } while (path.size() <= MAX_PATH);
358ffe3c632Sopenharmony_ci
359ffe3c632Sopenharmony_ci  ASSERT_EQ(mkdir("c:bad", 0644), -1);
360ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
361ffe3c632Sopenharmony_ci  ASSERT_EQ(mkdir("/tmp/bad", 0644), -1);
362ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
363ffe3c632Sopenharmony_ci  ASSERT_EQ(mkdir("\\bad", 0644), -1);
364ffe3c632Sopenharmony_ci  ASSERT_EQ(errno, ENOENT);
365ffe3c632Sopenharmony_ci}
366ffe3c632Sopenharmony_ci
367ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, MkdirTestNonAscii) {
368ffe3c632Sopenharmony_ci  ASSERT_INITIALIZED;
369ffe3c632Sopenharmony_ci
370ffe3c632Sopenharmony_ci  // Create a non-ASCII path.
371ffe3c632Sopenharmony_ci  // Ensure that we can create the directory using CreateDirectoryW.
372ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1").c_str(), nullptr));
373ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW((wtest_tmpdir + L"\\1\\" + kUtf16Text).c_str(), nullptr));
374ffe3c632Sopenharmony_ci  // Ensure that we can create a very similarly named directory using mkdir.
375ffe3c632Sopenharmony_ci  // We don't attempt to delete and recreate the same directory, because on
376ffe3c632Sopenharmony_ci  // Windows, deleting files and directories seems to be asynchronous.
377ffe3c632Sopenharmony_ci  EXPECT_EQ(mkdir((test_tmpdir + "\\2").c_str(), 0644), 0);
378ffe3c632Sopenharmony_ci  EXPECT_EQ(mkdir((test_tmpdir + "\\2\\" + kUtf8Text).c_str(), 0644), 0);
379ffe3c632Sopenharmony_ci}
380ffe3c632Sopenharmony_ci
381ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ChdirTest) {
382ffe3c632Sopenharmony_ci  string path("C:\\");
383ffe3c632Sopenharmony_ci  EXPECT_EQ(access(path.c_str(), F_OK), 0);
384ffe3c632Sopenharmony_ci  ASSERT_EQ(chdir(path.c_str()), 0);
385ffe3c632Sopenharmony_ci
386ffe3c632Sopenharmony_ci  // Do not try to chdir into the test_tmpdir, it may already contain directory
387ffe3c632Sopenharmony_ci  // names with trailing dots.
388ffe3c632Sopenharmony_ci  // Instead test here with an obviously dot-trailed path. If the win32_chdir
389ffe3c632Sopenharmony_ci  // function would not convert the path to absolute and prefix with "\\?\" then
390ffe3c632Sopenharmony_ci  // the Win32 API would ignore the trailing dot, but because of the prefixing
391ffe3c632Sopenharmony_ci  // there'll be no path processing done, so we'll actually attempt to chdir
392ffe3c632Sopenharmony_ci  // into "C:\some\path\foo."
393ffe3c632Sopenharmony_ci  path = test_tmpdir + "/foo.";
394ffe3c632Sopenharmony_ci  EXPECT_EQ(mkdir(path.c_str(), 644), 0);
395ffe3c632Sopenharmony_ci  EXPECT_EQ(access(path.c_str(), F_OK), 0);
396ffe3c632Sopenharmony_ci  ASSERT_NE(chdir(path.c_str()), 0);
397ffe3c632Sopenharmony_ci}
398ffe3c632Sopenharmony_ci
399ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ChdirTestNonAscii) {
400ffe3c632Sopenharmony_ci  ASSERT_INITIALIZED;
401ffe3c632Sopenharmony_ci
402ffe3c632Sopenharmony_ci  // Create a directory with a non-ASCII path and ensure we can cd into it.
403ffe3c632Sopenharmony_ci  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
404ffe3c632Sopenharmony_ci  string nonAscii;
405ffe3c632Sopenharmony_ci  EXPECT_TRUE(strings::wcs_to_utf8(wNonAscii.c_str(), &nonAscii));
406ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
407ffe3c632Sopenharmony_ci  WCHAR cwd[MAX_PATH];
408ffe3c632Sopenharmony_ci  EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
409ffe3c632Sopenharmony_ci  // Ensure that we can cd into the path using SetCurrentDirectoryW.
410ffe3c632Sopenharmony_ci  EXPECT_TRUE(SetCurrentDirectoryW(wNonAscii.c_str()));
411ffe3c632Sopenharmony_ci  EXPECT_TRUE(SetCurrentDirectoryW(cwd));
412ffe3c632Sopenharmony_ci  // Ensure that we can cd into the path using chdir.
413ffe3c632Sopenharmony_ci  ASSERT_EQ(chdir(nonAscii.c_str()), 0);
414ffe3c632Sopenharmony_ci  // Ensure that the GetCurrentDirectoryW returns the desired path.
415ffe3c632Sopenharmony_ci  EXPECT_TRUE(GetCurrentDirectoryW(MAX_PATH, cwd));
416ffe3c632Sopenharmony_ci  ASSERT_EQ(wNonAscii, cwd);
417ffe3c632Sopenharmony_ci}
418ffe3c632Sopenharmony_ci
419ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ExpandWildcardsInRelativePathTest) {
420ffe3c632Sopenharmony_ci  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
421ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
422ffe3c632Sopenharmony_ci  // Create mock files we will test pattern matching on.
423ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_a.proto"));
424ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_b.proto"));
425ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\bar.proto"));
426ffe3c632Sopenharmony_ci  // `cd` into `wtest_tmpdir`.
427ffe3c632Sopenharmony_ci  EXPECT_TRUE(SetCurrentDirectoryW(wtest_tmpdir.c_str()));
428ffe3c632Sopenharmony_ci
429ffe3c632Sopenharmony_ci  int found_a = 0;
430ffe3c632Sopenharmony_ci  int found_b = 0;
431ffe3c632Sopenharmony_ci  vector<string> found_bad;
432ffe3c632Sopenharmony_ci  // Assert matching a relative path pattern. Results should also be relative.
433ffe3c632Sopenharmony_ci  ExpandWildcardsResult result =
434ffe3c632Sopenharmony_ci      ExpandWildcards(string(kUtf8Text) + "\\foo*.proto",
435ffe3c632Sopenharmony_ci                      [&found_a, &found_b, &found_bad](const string& p) {
436ffe3c632Sopenharmony_ci                        if (p == string(kUtf8Text) + "\\foo_a.proto") {
437ffe3c632Sopenharmony_ci                          found_a++;
438ffe3c632Sopenharmony_ci                        } else if (p == string(kUtf8Text) + "\\foo_b.proto") {
439ffe3c632Sopenharmony_ci                          found_b++;
440ffe3c632Sopenharmony_ci                        } else {
441ffe3c632Sopenharmony_ci                          found_bad.push_back(p);
442ffe3c632Sopenharmony_ci                        }
443ffe3c632Sopenharmony_ci                      });
444ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
445ffe3c632Sopenharmony_ci  EXPECT_EQ(found_a, 1);
446ffe3c632Sopenharmony_ci  EXPECT_EQ(found_b, 1);
447ffe3c632Sopenharmony_ci  if (!found_bad.empty()) {
448ffe3c632Sopenharmony_ci    FAIL() << found_bad[0];
449ffe3c632Sopenharmony_ci  }
450ffe3c632Sopenharmony_ci
451ffe3c632Sopenharmony_ci  // Assert matching the exact filename.
452ffe3c632Sopenharmony_ci  found_a = 0;
453ffe3c632Sopenharmony_ci  found_bad.clear();
454ffe3c632Sopenharmony_ci  result = ExpandWildcards(string(kUtf8Text) + "\\foo_a.proto",
455ffe3c632Sopenharmony_ci                           [&found_a, &found_bad](const string& p) {
456ffe3c632Sopenharmony_ci                             if (p == string(kUtf8Text) + "\\foo_a.proto") {
457ffe3c632Sopenharmony_ci                               found_a++;
458ffe3c632Sopenharmony_ci                             } else {
459ffe3c632Sopenharmony_ci                               found_bad.push_back(p);
460ffe3c632Sopenharmony_ci                             }
461ffe3c632Sopenharmony_ci                           });
462ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
463ffe3c632Sopenharmony_ci  EXPECT_EQ(found_a, 1);
464ffe3c632Sopenharmony_ci  if (!found_bad.empty()) {
465ffe3c632Sopenharmony_ci    FAIL() << found_bad[0];
466ffe3c632Sopenharmony_ci  }
467ffe3c632Sopenharmony_ci}
468ffe3c632Sopenharmony_ci
469ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ExpandWildcardsInAbsolutePathTest) {
470ffe3c632Sopenharmony_ci  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
471ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
472ffe3c632Sopenharmony_ci  // Create mock files we will test pattern matching on.
473ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_a.proto"));
474ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_b.proto"));
475ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\bar.proto"));
476ffe3c632Sopenharmony_ci
477ffe3c632Sopenharmony_ci  int found_a = 0;
478ffe3c632Sopenharmony_ci  int found_b = 0;
479ffe3c632Sopenharmony_ci  vector<string> found_bad;
480ffe3c632Sopenharmony_ci  // Assert matching an absolute path. The results should also use absolute
481ffe3c632Sopenharmony_ci  // path.
482ffe3c632Sopenharmony_ci  ExpandWildcardsResult result =
483ffe3c632Sopenharmony_ci      ExpandWildcards(string(test_tmpdir) + "\\" + kUtf8Text + "\\foo*.proto",
484ffe3c632Sopenharmony_ci                      [this, &found_a, &found_b, &found_bad](const string& p) {
485ffe3c632Sopenharmony_ci                        if (p == string(this->test_tmpdir) + "\\" + kUtf8Text +
486ffe3c632Sopenharmony_ci                                     "\\foo_a.proto") {
487ffe3c632Sopenharmony_ci                          found_a++;
488ffe3c632Sopenharmony_ci                        } else if (p == string(this->test_tmpdir) + "\\" +
489ffe3c632Sopenharmony_ci                                            kUtf8Text + "\\foo_b.proto") {
490ffe3c632Sopenharmony_ci                          found_b++;
491ffe3c632Sopenharmony_ci                        } else {
492ffe3c632Sopenharmony_ci                          found_bad.push_back(p);
493ffe3c632Sopenharmony_ci                        }
494ffe3c632Sopenharmony_ci                      });
495ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
496ffe3c632Sopenharmony_ci  EXPECT_EQ(found_a, 1);
497ffe3c632Sopenharmony_ci  EXPECT_EQ(found_b, 1);
498ffe3c632Sopenharmony_ci  if (!found_bad.empty()) {
499ffe3c632Sopenharmony_ci    FAIL() << found_bad[0];
500ffe3c632Sopenharmony_ci  }
501ffe3c632Sopenharmony_ci
502ffe3c632Sopenharmony_ci  // Assert matching the exact filename.
503ffe3c632Sopenharmony_ci  found_a = 0;
504ffe3c632Sopenharmony_ci  found_bad.clear();
505ffe3c632Sopenharmony_ci  result =
506ffe3c632Sopenharmony_ci      ExpandWildcards(string(test_tmpdir) + "\\" + kUtf8Text + "\\foo_a.proto",
507ffe3c632Sopenharmony_ci                      [this, &found_a, &found_bad](const string& p) {
508ffe3c632Sopenharmony_ci                        if (p == string(this->test_tmpdir) + "\\" + kUtf8Text +
509ffe3c632Sopenharmony_ci                                     "\\foo_a.proto") {
510ffe3c632Sopenharmony_ci                          found_a++;
511ffe3c632Sopenharmony_ci                        } else {
512ffe3c632Sopenharmony_ci                          found_bad.push_back(p);
513ffe3c632Sopenharmony_ci                        }
514ffe3c632Sopenharmony_ci                      });
515ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
516ffe3c632Sopenharmony_ci  EXPECT_EQ(found_a, 1);
517ffe3c632Sopenharmony_ci  if (!found_bad.empty()) {
518ffe3c632Sopenharmony_ci    FAIL() << found_bad[0];
519ffe3c632Sopenharmony_ci  }
520ffe3c632Sopenharmony_ci}
521ffe3c632Sopenharmony_ci
522ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ExpandWildcardsIgnoresDirectoriesTest) {
523ffe3c632Sopenharmony_ci  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
524ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
525ffe3c632Sopenharmony_ci  // Create mock files we will test pattern matching on.
526ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_a.proto"));
527ffe3c632Sopenharmony_ci  EXPECT_TRUE(
528ffe3c632Sopenharmony_ci      CreateDirectoryW((wNonAscii + L"\\foo_b.proto").c_str(), nullptr));
529ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_c.proto"));
530ffe3c632Sopenharmony_ci  // `cd` into `wtest_tmpdir`.
531ffe3c632Sopenharmony_ci  EXPECT_TRUE(SetCurrentDirectoryW(wtest_tmpdir.c_str()));
532ffe3c632Sopenharmony_ci
533ffe3c632Sopenharmony_ci  int found_a = 0;
534ffe3c632Sopenharmony_ci  int found_c = 0;
535ffe3c632Sopenharmony_ci  vector<string> found_bad;
536ffe3c632Sopenharmony_ci  // Assert that the pattern matches exactly the expected files, and using the
537ffe3c632Sopenharmony_ci  // absolute path as did the input pattern.
538ffe3c632Sopenharmony_ci  ExpandWildcardsResult result =
539ffe3c632Sopenharmony_ci      ExpandWildcards(string(kUtf8Text) + "\\foo*.proto",
540ffe3c632Sopenharmony_ci                      [&found_a, &found_c, &found_bad](const string& p) {
541ffe3c632Sopenharmony_ci                        if (p == string(kUtf8Text) + "\\foo_a.proto") {
542ffe3c632Sopenharmony_ci                          found_a++;
543ffe3c632Sopenharmony_ci                        } else if (p == string(kUtf8Text) + "\\foo_c.proto") {
544ffe3c632Sopenharmony_ci                          found_c++;
545ffe3c632Sopenharmony_ci                        } else {
546ffe3c632Sopenharmony_ci                          found_bad.push_back(p);
547ffe3c632Sopenharmony_ci                        }
548ffe3c632Sopenharmony_ci                      });
549ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
550ffe3c632Sopenharmony_ci  EXPECT_EQ(found_a, 1);
551ffe3c632Sopenharmony_ci  EXPECT_EQ(found_c, 1);
552ffe3c632Sopenharmony_ci  if (!found_bad.empty()) {
553ffe3c632Sopenharmony_ci    FAIL() << found_bad[0];
554ffe3c632Sopenharmony_ci  }
555ffe3c632Sopenharmony_ci}
556ffe3c632Sopenharmony_ci
557ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, ExpandWildcardsFailsIfNoFileMatchesTest) {
558ffe3c632Sopenharmony_ci  wstring wNonAscii(wtest_tmpdir + L"\\" + kUtf16Text);
559ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateDirectoryW(wNonAscii.c_str(), nullptr));
560ffe3c632Sopenharmony_ci  // Create mock files we will test pattern matching on.
561ffe3c632Sopenharmony_ci  EXPECT_TRUE(CreateEmptyFile(wNonAscii + L"\\foo_a.proto"));
562ffe3c632Sopenharmony_ci  // `cd` into `wtest_tmpdir`.
563ffe3c632Sopenharmony_ci  EXPECT_TRUE(SetCurrentDirectoryW(wtest_tmpdir.c_str()));
564ffe3c632Sopenharmony_ci
565ffe3c632Sopenharmony_ci  // Control test: should match foo*.proto
566ffe3c632Sopenharmony_ci  ExpandWildcardsResult result =
567ffe3c632Sopenharmony_ci      ExpandWildcards(string(kUtf8Text) + "\\foo*.proto", [](const string&) {});
568ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
569ffe3c632Sopenharmony_ci
570ffe3c632Sopenharmony_ci  // Control test: should match foo_a.proto
571ffe3c632Sopenharmony_ci  result = ExpandWildcards(string(kUtf8Text) + "\\foo_a.proto",
572ffe3c632Sopenharmony_ci                           [](const string&) {});
573ffe3c632Sopenharmony_ci  EXPECT_EQ(result, ExpandWildcardsResult::kSuccess);
574ffe3c632Sopenharmony_ci
575ffe3c632Sopenharmony_ci  // Actual test: should not match anything.
576ffe3c632Sopenharmony_ci  result =
577ffe3c632Sopenharmony_ci      ExpandWildcards(string(kUtf8Text) + "\\bar*.proto", [](const string&) {});
578ffe3c632Sopenharmony_ci  ASSERT_EQ(result, ExpandWildcardsResult::kErrorNoMatchingFile);
579ffe3c632Sopenharmony_ci}
580ffe3c632Sopenharmony_ci
581ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, AsWindowsPathTest) {
582ffe3c632Sopenharmony_ci  DWORD size = GetCurrentDirectoryW(0, nullptr);
583ffe3c632Sopenharmony_ci  std::unique_ptr<wchar_t[]> cwd_str(new wchar_t[size]);
584ffe3c632Sopenharmony_ci  EXPECT_GT(GetCurrentDirectoryW(size, cwd_str.get()), 0);
585ffe3c632Sopenharmony_ci  wstring cwd = wstring(L"\\\\?\\") + cwd_str.get();
586ffe3c632Sopenharmony_ci
587ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("relative_mkdirtest"),
588ffe3c632Sopenharmony_ci            cwd + L"\\relative_mkdirtest");
589ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("preserve//\\trailing///"),
590ffe3c632Sopenharmony_ci            cwd + L"\\preserve\\trailing\\");
591ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("./normalize_me\\/../blah"),
592ffe3c632Sopenharmony_ci            cwd + L"\\blah");
593ffe3c632Sopenharmony_ci  std::ostringstream relpath;
594ffe3c632Sopenharmony_ci  for (wchar_t* p = cwd_str.get(); *p; ++p) {
595ffe3c632Sopenharmony_ci    if (*p == '/' || *p == '\\') {
596ffe3c632Sopenharmony_ci      relpath << "../";
597ffe3c632Sopenharmony_ci    }
598ffe3c632Sopenharmony_ci  }
599ffe3c632Sopenharmony_ci  relpath << ".\\/../\\./beyond-toplevel";
600ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath(relpath.str().c_str()),
601ffe3c632Sopenharmony_ci            wstring(L"\\\\?\\") + cwd_str.get()[0] + L":\\beyond-toplevel");
602ffe3c632Sopenharmony_ci
603ffe3c632Sopenharmony_ci  // Absolute unix paths lack drive letters, driveless absolute windows paths
604ffe3c632Sopenharmony_ci  // do too. Neither can be converted to a drive-specifying absolute Windows
605ffe3c632Sopenharmony_ci  // path.
606ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("/absolute/unix/path"), L"");
607ffe3c632Sopenharmony_ci  // Though valid on Windows, we also don't support UNC paths (\\UNC\\blah).
608ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("\\driveless\\absolute"), L"");
609ffe3c632Sopenharmony_ci  // Though valid in cmd.exe, drive-relative paths are not supported.
610ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("c:foo"), L"");
611ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("c:/foo"), L"\\\\?\\c:\\foo");
612ffe3c632Sopenharmony_ci  ASSERT_EQ(testonly_utf8_to_winpath("\\\\?\\C:\\foo"), L"\\\\?\\C:\\foo");
613ffe3c632Sopenharmony_ci}
614ffe3c632Sopenharmony_ci
615ffe3c632Sopenharmony_ciTEST_F(IoWin32Test, Utf8Utf16ConversionTest) {
616ffe3c632Sopenharmony_ci  string mbs;
617ffe3c632Sopenharmony_ci  wstring wcs;
618ffe3c632Sopenharmony_ci  ASSERT_TRUE(strings::utf8_to_wcs(kUtf8Text, &wcs));
619ffe3c632Sopenharmony_ci  ASSERT_TRUE(strings::wcs_to_utf8(kUtf16Text, &mbs));
620ffe3c632Sopenharmony_ci  ASSERT_EQ(wcs, kUtf16Text);
621ffe3c632Sopenharmony_ci  ASSERT_EQ(mbs, kUtf8Text);
622ffe3c632Sopenharmony_ci}
623ffe3c632Sopenharmony_ci
624ffe3c632Sopenharmony_ci}  // namespace
625ffe3c632Sopenharmony_ci}  // namespace win32
626ffe3c632Sopenharmony_ci}  // namespace io
627ffe3c632Sopenharmony_ci}  // namespace protobuf
628ffe3c632Sopenharmony_ci}  // namespace google
629ffe3c632Sopenharmony_ci
630ffe3c632Sopenharmony_ci#endif  // defined(_WIN32)
631ffe3c632Sopenharmony_ci
632