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