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