Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 5 | #include "base/scoped_native_library.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 6 | |
| 7 | #include "build/build_config.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 10 | #if BUILDFLAG(IS_WIN) |
Peter Kasting | cab7461 | 2025-01-21 20:13:23 | [diff] [blame] | 11 | #include <windows.h> |
| 12 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 15 | #endif |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 16 | |
[email protected] | 5257bcf | 2013-02-19 05:47:10 | [diff] [blame] | 17 | namespace base { |
| 18 | |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 19 | // Tests whether or not a function pointer retrieved via ScopedNativeLibrary |
| 20 | // is available only in a scope. |
| 21 | TEST(ScopedNativeLibrary, Basic) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 22 | #if BUILDFLAG(IS_WIN) |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 23 | // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it |
| 24 | // is valid only in this scope. |
| 25 | // FreeLibrary() doesn't actually unload a DLL until its reference count |
[email protected] | cbd119a | 2013-10-31 20:28:47 | [diff] [blame] | 26 | // becomes zero, i.e. function pointer is still valid if the DLL used |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 27 | // in this test is also used by another part of this executable. |
| 28 | // So, this test uses "ddraw.dll", which is not used by Chrome at all but |
| 29 | // installed on all versions of Windows. |
[email protected] | cbd119a | 2013-10-31 20:28:47 | [diff] [blame] | 30 | const char kFunctionName[] = "DirectDrawCreate"; |
| 31 | NativeLibrary native_library; |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 32 | { |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 33 | FilePath path(FilePath::FromUTF8Unsafe(GetNativeLibraryName("ddraw"))); |
| 34 | native_library = LoadNativeLibrary(path, nullptr); |
[email protected] | cbd119a | 2013-10-31 20:28:47 | [diff] [blame] | 35 | ScopedNativeLibrary library(native_library); |
wittman | f2d5644 | 2015-11-02 22:38:40 | [diff] [blame] | 36 | EXPECT_TRUE(library.is_valid()); |
| 37 | EXPECT_EQ(native_library, library.get()); |
[email protected] | cbd119a | 2013-10-31 20:28:47 | [diff] [blame] | 38 | FARPROC test_function = |
| 39 | reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName)); |
Peter Kasting | cab7461 | 2025-01-21 20:13:23 | [diff] [blame] | 40 | EXPECT_EQ(0, ::IsBadCodePtr(test_function)); |
[email protected] | cbd119a | 2013-10-31 20:28:47 | [diff] [blame] | 41 | EXPECT_EQ( |
| 42 | GetFunctionPointerFromNativeLibrary(native_library, kFunctionName), |
| 43 | test_function); |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 44 | } |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 45 | EXPECT_FALSE( |
| 46 | GetFunctionPointerFromNativeLibrary(native_library, kFunctionName)); |
[email protected] | da2566e1 | 2010-03-10 06:23:35 | [diff] [blame] | 47 | #endif |
| 48 | } |
[email protected] | 5257bcf | 2013-02-19 05:47:10 | [diff] [blame] | 49 | |
| 50 | } // namespace base |