Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include <windows.h> |
michael@0 | 6 | |
michael@0 | 7 | #include "base/win/scoped_handle.h" |
michael@0 | 8 | #include "sandbox/win/src/win_utils.h" |
michael@0 | 9 | #include "sandbox/win/tests/common/test_utils.h" |
michael@0 | 10 | #include "testing/gtest/include/gtest/gtest.h" |
michael@0 | 11 | |
michael@0 | 12 | TEST(WinUtils, IsReparsePoint) { |
michael@0 | 13 | using sandbox::IsReparsePoint; |
michael@0 | 14 | |
michael@0 | 15 | // Create a temp file because we need write access to it. |
michael@0 | 16 | wchar_t temp_directory[MAX_PATH]; |
michael@0 | 17 | wchar_t my_folder[MAX_PATH]; |
michael@0 | 18 | ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
michael@0 | 19 | ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); |
michael@0 | 20 | |
michael@0 | 21 | // Delete the file and create a directory instead. |
michael@0 | 22 | ASSERT_TRUE(::DeleteFile(my_folder)); |
michael@0 | 23 | ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); |
michael@0 | 24 | |
michael@0 | 25 | bool result = true; |
michael@0 | 26 | EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(my_folder, &result)); |
michael@0 | 27 | EXPECT_FALSE(result); |
michael@0 | 28 | |
michael@0 | 29 | // We have to fix Bug 32224 to pass this test. |
michael@0 | 30 | std::wstring not_found = std::wstring(my_folder) + L"\\foo\\bar"; |
michael@0 | 31 | // EXPECT_EQ(ERROR_PATH_NOT_FOUND, IsReparsePoint(not_found, &result)); |
michael@0 | 32 | |
michael@0 | 33 | std::wstring new_file = std::wstring(my_folder) + L"\\foo"; |
michael@0 | 34 | EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); |
michael@0 | 35 | EXPECT_FALSE(result); |
michael@0 | 36 | |
michael@0 | 37 | // Replace the directory with a reparse point to %temp%. |
michael@0 | 38 | HANDLE dir = ::CreateFile(my_folder, FILE_ALL_ACCESS, |
michael@0 | 39 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
michael@0 | 40 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
michael@0 | 41 | EXPECT_NE(INVALID_HANDLE_VALUE, dir); |
michael@0 | 42 | |
michael@0 | 43 | std::wstring temp_dir_nt = std::wstring(L"\\??\\") + temp_directory; |
michael@0 | 44 | EXPECT_TRUE(SetReparsePoint(dir, temp_dir_nt.c_str())); |
michael@0 | 45 | |
michael@0 | 46 | EXPECT_EQ(ERROR_SUCCESS, IsReparsePoint(new_file, &result)); |
michael@0 | 47 | EXPECT_TRUE(result); |
michael@0 | 48 | |
michael@0 | 49 | EXPECT_TRUE(DeleteReparsePoint(dir)); |
michael@0 | 50 | EXPECT_TRUE(::CloseHandle(dir)); |
michael@0 | 51 | EXPECT_TRUE(::RemoveDirectory(my_folder)); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | TEST(WinUtils, SameObject) { |
michael@0 | 55 | using sandbox::SameObject; |
michael@0 | 56 | |
michael@0 | 57 | // Create a temp file because we need write access to it. |
michael@0 | 58 | wchar_t temp_directory[MAX_PATH]; |
michael@0 | 59 | wchar_t my_folder[MAX_PATH]; |
michael@0 | 60 | ASSERT_NE(::GetTempPath(MAX_PATH, temp_directory), 0u); |
michael@0 | 61 | ASSERT_NE(::GetTempFileName(temp_directory, L"test", 0, my_folder), 0u); |
michael@0 | 62 | |
michael@0 | 63 | // Delete the file and create a directory instead. |
michael@0 | 64 | ASSERT_TRUE(::DeleteFile(my_folder)); |
michael@0 | 65 | ASSERT_TRUE(::CreateDirectory(my_folder, NULL)); |
michael@0 | 66 | |
michael@0 | 67 | std::wstring folder(my_folder); |
michael@0 | 68 | std::wstring file_name = folder + L"\\foo.txt"; |
michael@0 | 69 | const ULONG kSharing = FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE; |
michael@0 | 70 | base::win::ScopedHandle file(CreateFile( |
michael@0 | 71 | file_name.c_str(), GENERIC_WRITE, kSharing, NULL, CREATE_ALWAYS, |
michael@0 | 72 | FILE_FLAG_DELETE_ON_CLOSE, NULL)); |
michael@0 | 73 | |
michael@0 | 74 | EXPECT_TRUE(file.IsValid()); |
michael@0 | 75 | std::wstring file_name_nt1 = std::wstring(L"\\??\\") + file_name; |
michael@0 | 76 | std::wstring file_name_nt2 = std::wstring(L"\\??\\") + folder + L"\\FOO.txT"; |
michael@0 | 77 | EXPECT_TRUE(SameObject(file.Get(), file_name_nt1.c_str())); |
michael@0 | 78 | EXPECT_TRUE(SameObject(file.Get(), file_name_nt2.c_str())); |
michael@0 | 79 | |
michael@0 | 80 | file.Close(); |
michael@0 | 81 | EXPECT_TRUE(::RemoveDirectory(my_folder)); |
michael@0 | 82 | } |