chrome_create_file_unittest.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome_elf/create_file/chrome_create_file.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <windows.h>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <bitset>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/base_paths_win.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/file_util.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/file_path.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/scoped_temp_dir.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/path_service.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/threading/platform_thread.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/win/iat_patch_function.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/win/scoped_handle.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/win/windows_version.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome_elf/chrome_elf_constants.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome_elf/ntdll_cache.h"
2323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "sandbox/win/src/interception_internal.h"
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "sandbox/win/src/nt_internals.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/platform_test.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Test fixtures -------------------------------------------------------------
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ChromeCreateFileTest : public PlatformTest {
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct NtCreateFileParams {
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ACCESS_MASK desired_access;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OBJECT_ATTRIBUTES object_attributes;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PLARGE_INTEGER allocation_size;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ULONG file_attributes;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ULONG share_access;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ULONG create_disposition;
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ULONG create_options;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PVOID ea_buffer;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ULONG ea_length;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum CallPath {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ELF,
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    KERNEL
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  template<CallPath path>
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static NTSTATUS WINAPI FakeNtCreateFile(
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PHANDLE file_handle,
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ACCESS_MASK desired_access,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      POBJECT_ATTRIBUTES object_attributes,
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PIO_STATUS_BLOCK io_status_block,
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PLARGE_INTEGER allocation_size,
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG file_attributes,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG share_access,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_disposition,
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_options,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PVOID ea_buffer,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG ea_length) {
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return self_->HandleCreateFileCall(file_handle,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        desired_access,
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        object_attributes,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        io_status_block,
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        allocation_size,
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        file_attributes,
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        share_access,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        create_disposition,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        create_options,
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ea_buffer,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ea_length,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        path);
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    original_thread_ = base::PlatformThread::CurrentId();
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    InitCache();
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PlatformTest::SetUp();
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::FilePath user_data_dir;
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PathService::Get(base::DIR_LOCAL_APP_DATA, &user_data_dir);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(user_data_dir));
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ASSERT_TRUE(temp_dir2_.CreateUniqueTempDir());
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self_ = this;
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void UnsetThunkStorage() {
9223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    DWORD old_protect = 0;
9323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage,
9423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 sizeof(g_nt_thunk_storage),
9523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 PAGE_EXECUTE_READWRITE,
9623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 &old_protect));
9723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    memset(&g_nt_thunk_storage, 0, sizeof(g_nt_thunk_storage));
9823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
9923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    EXPECT_TRUE(::VirtualProtect(&g_nt_thunk_storage,
10023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 sizeof(g_nt_thunk_storage),
10123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 PAGE_EXECUTE_READ,
10223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 &old_protect));
10323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
10423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RedirectNtCreateFileCalls() {
10623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    UnsetThunkStorage();
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    old_func_ptr_ =
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        reinterpret_cast<NtCreateFileFunction>(g_ntdll_lookup["NtCreateFile"]);
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // KernelBase.dll only exists for Win7 and later, prior to that, kernel32
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // imports from ntdll directly.
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (base::win::GetVersion() < base::win::VERSION_WIN7) {
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      patcher_.Patch(L"kernel32.dll", "ntdll.dll", "NtCreateFile",
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>));
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      patcher_.Patch(L"kernelbase.dll", "ntdll.dll", "NtCreateFile",
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          reinterpret_cast<void(*)()>(&FakeNtCreateFile<KERNEL>));
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    g_ntdll_lookup["NtCreateFile"] = reinterpret_cast<void(*)()>(
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        &ChromeCreateFileTest::FakeNtCreateFile<ELF>);
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ResetNtCreateFileCalls() {
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    g_ntdll_lookup["NtCreateFile"] = reinterpret_cast<void*>(old_func_ptr_);
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    patcher_.Unpatch();
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NTSTATUS HandleCreateFileCall(PHANDLE file_handle,
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ACCESS_MASK desired_access,
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      POBJECT_ATTRIBUTES object_attributes,
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PIO_STATUS_BLOCK io_status_block,
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PLARGE_INTEGER allocation_size,
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG file_attributes,
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG share_access,
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_disposition,
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_options,
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PVOID ea_buffer,
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG ea_length,
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      CallPath call_path) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (original_thread_ == base::PlatformThread::CurrentId()) {
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      SetParams(desired_access,
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          object_attributes,
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          allocation_size,
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          file_attributes,
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          share_access,
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          create_disposition,
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          create_options,
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ea_buffer,
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ea_length,
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          call_path == ELF ? &elf_params_ : &kernel_params_);
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Forward the call to the real NTCreateFile.
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return old_func_ptr_(file_handle,
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        desired_access,
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        object_attributes,
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        io_status_block,
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        allocation_size,
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        file_attributes,
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        share_access,
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        create_disposition,
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        create_options,
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ea_buffer,
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ea_length);
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetParams(ACCESS_MASK desired_access,
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      POBJECT_ATTRIBUTES object_attributes,
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PLARGE_INTEGER allocation_size,
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG file_attributes,
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG share_access,
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_disposition,
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG create_options,
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PVOID ea_buffer,
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      ULONG ea_length,
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      NtCreateFileParams* params) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->desired_access = desired_access;
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.Length = object_attributes->Length;
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.ObjectName = object_attributes->ObjectName;
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.RootDirectory = object_attributes->RootDirectory;
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.Attributes = object_attributes->Attributes;
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.SecurityDescriptor =
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        object_attributes->SecurityDescriptor;
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->object_attributes.SecurityQualityOfService =
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        object_attributes->SecurityQualityOfService;
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->allocation_size = allocation_size;
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->file_attributes = file_attributes;
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->share_access = share_access;
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->create_disposition = create_disposition;
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->create_options = create_options;
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->ea_buffer = ea_buffer;
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params->ea_length = ea_length;
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void CheckParams() {
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::bitset<32> elf((int) elf_params_.desired_access);
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    std::bitset<32> ker((int) kernel_params_.desired_access);
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.desired_access, elf_params_.desired_access)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        << elf << "\n" << ker;
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.object_attributes.Length,
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              elf_params_.object_attributes.Length);
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.object_attributes.RootDirectory,
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              elf_params_.object_attributes.RootDirectory);
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.object_attributes.Attributes,
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              elf_params_.object_attributes.Attributes);
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.object_attributes.SecurityDescriptor,
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              elf_params_.object_attributes.SecurityDescriptor);
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.allocation_size, elf_params_.allocation_size);
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.file_attributes, elf_params_.file_attributes);
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.share_access, elf_params_.share_access);
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.create_disposition,
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              elf_params_.create_disposition);
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.create_options, elf_params_.create_options);
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.ea_buffer, elf_params_.ea_buffer);
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(kernel_params_.ea_length, elf_params_.ea_length);
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void DoWriteCheck(const base::FilePath& path, DWORD flag, bool is_system) {
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::win::ScopedHandle file_handle;
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const char kTestData[] = "0123456789";
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int buffer_size = sizeof(kTestData) - 1;
2245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DWORD bytes_written;
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (is_system) {
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      file_handle.Set(::CreateFileW(path.value().c_str(),
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    GENERIC_WRITE,
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    FILE_SHARE_READ,
2305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    NULL,
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    CREATE_ALWAYS,
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    FILE_ATTRIBUTE_NORMAL | flag,
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    NULL));
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      file_handle.Set(CreateFileNTDLL(path.value().c_str(),
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      GENERIC_WRITE,
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      FILE_SHARE_READ,
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      NULL,
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      CREATE_ALWAYS,
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      FILE_ATTRIBUTE_NORMAL | flag,
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      NULL));
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(file_handle == INVALID_HANDLE_VALUE);
2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ::WriteFile(file_handle, kTestData, buffer_size, &bytes_written, NULL);
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(buffer_size, bytes_written);
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void DoReadCheck(const base::FilePath& path, DWORD flag, bool is_system) {
2515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::win::ScopedHandle file_handle;
2525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const char kTestData[] = "0123456789";
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int buffer_size = sizeof(kTestData) - 1;
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DWORD bytes_read;
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    char read_buffer[10];
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (is_system) {
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      file_handle.Set(::CreateFileW(path.value().c_str(),
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    GENERIC_READ,
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    0,
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    NULL,
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    OPEN_ALWAYS,
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    FILE_ATTRIBUTE_NORMAL | flag,
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    NULL));
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      file_handle.Set(CreateFileNTDLL(path.value().c_str(),
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      GENERIC_READ,
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      0,
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      NULL,
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      OPEN_ALWAYS,
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      FILE_ATTRIBUTE_NORMAL | flag,
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      NULL));
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_FALSE(file_handle == INVALID_HANDLE_VALUE);
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ::ReadFile(file_handle, read_buffer, buffer_size, &bytes_read, NULL);
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(buffer_size, bytes_read);
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(0, memcmp(kTestData, read_buffer, bytes_read));
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void RunChecks(DWORD flag, bool check_reads) {
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Make sure we can write to this file handle when called via the system.
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::FilePath junk_path_1 = temp_dir_.path().Append(L"junk_1.txt");
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::FilePath junk_path_2 = temp_dir_.path().Append(L"junk_2.txt");
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DoWriteCheck(junk_path_1, flag, true);
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DoWriteCheck(junk_path_2, flag, false);
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CheckParams();
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (check_reads) {
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      // Make sure we can read from this file handle when called via the system.
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DoReadCheck(junk_path_1, flag, true);
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DoReadCheck(junk_path_2, flag, false);
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      CheckParams();
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DeleteFile(junk_path_1, false);
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DeleteFile(junk_path_2, false);
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static ChromeCreateFileTest* self_;
3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NtCreateFileFunction old_func_ptr_;
3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ScopedTempDir temp_dir_;
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ScopedTempDir temp_dir2_;
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::win::IATPatchFunction patcher_;
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NtCreateFileParams kernel_params_;
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NtCreateFileParams elf_params_;
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::PlatformThreadId original_thread_;
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ChromeCreateFileTest* ChromeCreateFileTest::self_ = NULL;
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests ---------------------------------------------------------------------
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_ATTRIBUTE_NORMAL) {
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_ATTRIBUTE_NORMAL, true);
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_WRITE_THROUGH) {
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_WRITE_THROUGH, true);
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_RANDOM_ACCESS) {
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_RANDOM_ACCESS, true);
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_SEQUENTIAL_SCAN) {
3335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_SEQUENTIAL_SCAN, true);
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_DELETE_ON_CLOSE) {
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_DELETE_ON_CLOSE, false);
3415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_BACKUP_SEMANTICS) {
3455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_BACKUP_SEMANTICS, true);
3475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_OPEN_REPARSE_POINT) {
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_OPEN_REPARSE_POINT, true);
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, CheckParams_FILE_FLAG_OPEN_NO_RECALL) {
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RedirectNtCreateFileCalls();
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RunChecks(FILE_FLAG_OPEN_NO_RECALL, true);
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ResetNtCreateFileCalls();
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(ChromeCreateFileTest, BypassTest) {
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::wstring UNC_filepath_file(L"\\\\.\\some_file.txt");
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath local_path;
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PathService::Get(base::DIR_LOCAL_APP_DATA, &local_path);
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath local_prefs_path = local_path.Append(kAppDataDirName).Append(
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      kUserDataDirName).Append(L"default\\Preferences");
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath local_state_path = local_path.Append(kAppDataDirName).Append(
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      kUserDataDirName).Append(L"ninja\\Local State");
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath local_junk_path = local_path.Append(kAppDataDirName).Append(
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      kUserDataDirName).Append(L"default\\Junk");
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath desktop_path;
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PathService::Get(base::DIR_USER_DESKTOP, &desktop_path);
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath desktop_junk_path =
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      desktop_path.Append(L"Downloads\\junk.txt");
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath desktop_prefs_path =
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      desktop_path.Append(L"Downloads\\Preferences");
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Don't redirect UNC files.
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(ShouldBypass(UNC_filepath_file.c_str()));
3845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Don't redirect if file is not in UserData directory.
3865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(ShouldBypass(desktop_junk_path.value().c_str()));
3875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(ShouldBypass(desktop_prefs_path.value().c_str()));
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Only redirect "Preferences" and "Local State" files.
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ShouldBypass(local_prefs_path.value().c_str()));
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(ShouldBypass(local_state_path.value().c_str()));
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(ShouldBypass(local_junk_path.value().c_str()));
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
39523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)TEST_F(ChromeCreateFileTest, ReadWriteFromNtDll) {
39623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  UnsetThunkStorage();
39723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt");
39823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
39923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
40223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)TEST_F(ChromeCreateFileTest, ReadWriteFromThunk) {
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::FilePath file_name = temp_dir_.path().Append(L"some_file.txt");
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DoWriteCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DoReadCheck(file_name, FILE_ATTRIBUTE_NORMAL, false);
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
409