OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 #include <DelayIMP.h> | |
7 | |
8 #include "base/compiler_specific.h" | |
9 #include "base/scoped_native_library.h" | |
10 #include "chrome/app/delay_load_hook_win.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace { | |
14 | |
15 class ChromeDelayLoadHookTest : public testing::Test { | |
16 public: | |
17 ChromeDelayLoadHookTest() : proc_ptr_(NULL) { | |
18 } | |
19 | |
20 void SetUp() override { | |
21 SetupInfo("kernel32.dll"); | |
22 } | |
23 | |
24 void SetupInfo(const char* dll_name) { | |
25 info_.cb = sizeof(info_); | |
26 info_.pidd = NULL; | |
27 info_.ppfn = &proc_ptr_; | |
28 info_.szDll = dll_name; | |
29 info_.dlp.fImportByName = TRUE; | |
30 info_.dlp.szProcName = "CreateFileA"; | |
31 info_.hmodCur = NULL; | |
32 info_.pfnCur = NULL; | |
33 info_.dwLastError = 0; | |
34 } | |
35 | |
36 FARPROC proc_ptr_; | |
37 DelayLoadInfo info_; | |
38 }; | |
39 | |
40 } // namespace | |
41 | |
42 TEST_F(ChromeDelayLoadHookTest, HooksAreSetAtLinkTime) { | |
43 // This test verifies that referencing the ChromeDelayLoadHook at link | |
44 // time results in overriding the delay loader's hook instances in the CRT | |
45 // ropriately. | |
46 EXPECT_TRUE(__pfnDliNotifyHook2 == ChromeDelayLoadHook); | |
47 EXPECT_TRUE(__pfnDliFailureHook2 == ChromeDelayLoadHook); | |
48 } | |
49 | |
50 TEST_F(ChromeDelayLoadHookTest, NonSuffixedDllsAreNotHandled) { | |
51 // The hook should ignore non-suffixed DLLs. | |
52 SetupInfo("kernel32.dll"); | |
53 | |
54 HMODULE none = reinterpret_cast<HMODULE>( | |
55 ChromeDelayLoadHook(dliNotePreLoadLibrary, &info_)); | |
56 // Make sure the library is released on exit. | |
57 base::ScopedNativeLibrary lib_holder(none); | |
58 | |
59 ASSERT_TRUE(none == NULL); | |
60 } | |
61 | |
62 TEST_F(ChromeDelayLoadHookTest, SuffixedDllsAreRedirected) { | |
63 // Check that a DLL name of the form "foo-delay.dll" gets redirected to | |
64 // the "foo.dll". | |
65 SetupInfo("kernel32-delay.dll"); | |
66 HMODULE kernel32 = reinterpret_cast<HMODULE>( | |
67 ChromeDelayLoadHook(dliNotePreLoadLibrary, &info_)); | |
68 | |
69 // Make sure the library is released on exit. | |
70 base::ScopedNativeLibrary lib_holder(kernel32); | |
71 | |
72 ASSERT_TRUE(kernel32 == ::GetModuleHandle(L"kernel32.dll")); | |
73 } | |
74 | |
75 TEST_F(ChromeDelayLoadHookTest, IgnoresUnhandledNotifications) { | |
76 SetupInfo("kernel32-delay.dll"); | |
77 | |
78 // The hook should ignore all notifications but the preload notifications. | |
79 EXPECT_TRUE(ChromeDelayLoadHook(dliNoteStartProcessing, &info_) == NULL); | |
80 EXPECT_TRUE(ChromeDelayLoadHook(dliNotePreGetProcAddress, &info_) == NULL); | |
81 EXPECT_TRUE(ChromeDelayLoadHook(dliNoteEndProcessing, &info_) == NULL); | |
82 EXPECT_TRUE(ChromeDelayLoadHook(dliFailLoadLib, &info_) == NULL); | |
83 EXPECT_TRUE(ChromeDelayLoadHook(dliFailGetProc, &info_) == NULL); | |
84 } | |
OLD | NEW |