1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/win/src/win_utils_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include <windows.h> 1.9 + 1.10 +#include "base/win/scoped_handle.h" 1.11 +#include "sandbox/win/src/win_utils.h" 1.12 +#include "sandbox/win/tests/common/test_utils.h" 1.13 +#include "testing/gtest/include/gtest/gtest.h" 1.14 + 1.15 +TEST(WinUtils, IsReparsePoint) { 1.16 + using sandbox::IsReparsePoint; 1.17 + 1.18 + // Create a temp file because we need write access to it. 1.19 + wchar_t temp_directory[MAX_PATH]; 1.20 + wchar_t my_folder[MAX_PATH]; 1.21 + ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); 1.22 + ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); 1.23 + 1.24 + // Delete the file and create a directory instead. 1.25 + ASSERT_TRUE(::DeleteFile(my_folder)); 1.26 + ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); 1.27 + 1.28 + bool result = true; 1.29 + EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(my_folder, &result)); 1.30 + EXPECT_FALSE(result); 1.31 + 1.32 + // We have to fix Bug 32224 to pass this test. 1.33 + std::wstring not_found = std::wstring(my_folder) + L"\\foo\\bar"; 1.34 + // EXPECT_EQ(ERROR_PATH_NOT_FOUND, IsReparsePoint(not_found, &result)); 1.35 + 1.36 + std::wstring new_file = std::wstring(my_folder) + L"\\foo"; 1.37 + EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); 1.38 + EXPECT_FALSE(result); 1.39 + 1.40 + // Replace the directory with a reparse point to %temp%. 1.41 + HANDLE dir = ::CreateFile(my_folder, FILE_ALL_ACCESS, 1.42 + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 1.43 + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1.44 + EXPECT_NE(INVALID_HANDLE_VALUE, dir); 1.45 + 1.46 + std::wstring temp_dir_nt = std::wstring(L"\\??\\") + temp_directory; 1.47 + EXPECT_TRUE(SetReparsePoint(dir, temp_dir_nt.c_str())); 1.48 + 1.49 + EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); 1.50 + EXPECT_TRUE(result); 1.51 + 1.52 + EXPECT_TRUE(DeleteReparsePoint(dir)); 1.53 + EXPECT_TRUE(::CloseHandle(dir)); 1.54 + EXPECT_TRUE(::RemoveDirectory(my_folder)); 1.55 +} 1.56 + 1.57 +TEST(WinUtils, SameObject) { 1.58 + using sandbox::SameObject; 1.59 + 1.60 + // Create a temp file because we need write access to it. 1.61 + wchar_t temp_directory[MAX_PATH]; 1.62 + wchar_t my_folder[MAX_PATH]; 1.63 + ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); 1.64 + ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); 1.65 + 1.66 + // Delete the file and create a directory instead. 1.67 + ASSERT_TRUE(::DeleteFile(my_folder)); 1.68 + ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); 1.69 + 1.70 + std::wstring folder(my_folder); 1.71 + std::wstring file_name = folder + L"\\foo.txt"; 1.72 + const ULONG kSharing = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE; 1.73 + base::win::ScopedHandle file(CreateFile( 1.74 + file_name.c_str(), GENERIC_WRITE, kSharing, NULL, CREATE_ALWAYS, 1.75 + FILE_FLAG_DELETE_ON_CLOSE, NULL)); 1.76 + 1.77 + EXPECT_TRUE(file.IsValid()); 1.78 + std::wstring file_name_nt1 = std::wstring(L"\\??\\") + file_name; 1.79 + std::wstring file_name_nt2 = std::wstring(L"\\??\\") + folder + L"\\FOO.txT"; 1.80 + EXPECT_TRUE(SameObject(file.Get(), file_name_nt1.c_str())); 1.81 + EXPECT_TRUE(SameObject(file.Get(), file_name_nt2.c_str())); 1.82 + 1.83 + file.Close(); 1.84 + EXPECT_TRUE(::RemoveDirectory(my_folder)); 1.85 +}