michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include michael@0: michael@0: #include "base/win/scoped_handle.h" michael@0: #include "sandbox/win/src/win_utils.h" michael@0: #include "sandbox/win/tests/common/test_utils.h" michael@0: #include "testing/gtest/include/gtest/gtest.h" michael@0: michael@0: TEST(WinUtils, IsReparsePoint) { michael@0: using sandbox::IsReparsePoint; michael@0: michael@0: // Create a temp file because we need write access to it. michael@0: wchar_t temp_directory[MAX_PATH]; michael@0: wchar_t my_folder[MAX_PATH]; michael@0: ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); michael@0: ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); michael@0: michael@0: // Delete the file and create a directory instead. michael@0: ASSERT_TRUE(::DeleteFile(my_folder)); michael@0: ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); michael@0: michael@0: bool result = true; michael@0: EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(my_folder, &result)); michael@0: EXPECT_FALSE(result); michael@0: michael@0: // We have to fix Bug 32224 to pass this test. michael@0: std::wstring not_found = std::wstring(my_folder) + L"\\foo\\bar"; michael@0: // EXPECT_EQ(ERROR_PATH_NOT_FOUND, IsReparsePoint(not_found, &result)); michael@0: michael@0: std::wstring new_file = std::wstring(my_folder) + L"\\foo"; michael@0: EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); michael@0: EXPECT_FALSE(result); michael@0: michael@0: // Replace the directory with a reparse point to %temp%. michael@0: HANDLE dir = ::CreateFile(my_folder, FILE_ALL_ACCESS, michael@0: FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, michael@0: OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); michael@0: EXPECT_NE(INVALID_HANDLE_VALUE, dir); michael@0: michael@0: std::wstring temp_dir_nt = std::wstring(L"\\??\\") + temp_directory; michael@0: EXPECT_TRUE(SetReparsePoint(dir, temp_dir_nt.c_str())); michael@0: michael@0: EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); michael@0: EXPECT_TRUE(result); michael@0: michael@0: EXPECT_TRUE(DeleteReparsePoint(dir)); michael@0: EXPECT_TRUE(::CloseHandle(dir)); michael@0: EXPECT_TRUE(::RemoveDirectory(my_folder)); michael@0: } michael@0: michael@0: TEST(WinUtils, SameObject) { michael@0: using sandbox::SameObject; michael@0: michael@0: // Create a temp file because we need write access to it. michael@0: wchar_t temp_directory[MAX_PATH]; michael@0: wchar_t my_folder[MAX_PATH]; michael@0: ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); michael@0: ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); michael@0: michael@0: // Delete the file and create a directory instead. michael@0: ASSERT_TRUE(::DeleteFile(my_folder)); michael@0: ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); michael@0: michael@0: std::wstring folder(my_folder); michael@0: std::wstring file_name = folder + L"\\foo.txt"; michael@0: const ULONG kSharing = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE; michael@0: base::win::ScopedHandle file(CreateFile( michael@0: file_name.c_str(), GENERIC_WRITE, kSharing, NULL, CREATE_ALWAYS, michael@0: FILE_FLAG_DELETE_ON_CLOSE, NULL)); michael@0: michael@0: EXPECT_TRUE(file.IsValid()); michael@0: std::wstring file_name_nt1 = std::wstring(L"\\??\\") + file_name; michael@0: std::wstring file_name_nt2 = std::wstring(L"\\??\\") + folder + L"\\FOO.txT"; michael@0: EXPECT_TRUE(SameObject(file.Get(), file_name_nt1.c_str())); michael@0: EXPECT_TRUE(SameObject(file.Get(), file_name_nt2.c_str())); michael@0: michael@0: file.Close(); michael@0: EXPECT_TRUE(::RemoveDirectory(my_folder)); michael@0: }