levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [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 | |
thakis@chromium.org | 927d728 | 2011-05-20 08:34:17 +0900 | [diff] [blame] | 5 | #include "base/scoped_native_library.h" |
thestig@chromium.org | 6b2f4ab | 2010-08-19 12:49:38 +0900 | [diff] [blame] | 6 | #if defined(OS_WIN) |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig@chromium.org | 6b2f4ab | 2010-08-19 12:49:38 +0900 | [diff] [blame] | 8 | #endif |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 9 | |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
brettw@chromium.org | 9e0aee4 | 2013-02-19 14:47:10 +0900 | [diff] [blame] | 12 | namespace base { |
| 13 | |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 14 | // Tests whether or not a function pointer retrieved via ScopedNativeLibrary |
| 15 | // is available only in a scope. |
| 16 | TEST(ScopedNativeLibrary, Basic) { |
| 17 | #if defined(OS_WIN) |
| 18 | // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it |
| 19 | // is valid only in this scope. |
| 20 | // FreeLibrary() doesn't actually unload a DLL until its reference count |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 21 | // becomes zero, i.e. function pointer is still valid if the DLL used |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 22 | // in this test is also used by another part of this executable. |
| 23 | // So, this test uses "ddraw.dll", which is not used by Chrome at all but |
| 24 | // installed on all versions of Windows. |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 25 | const char kFunctionName[] = "DirectDrawCreate"; |
| 26 | NativeLibrary native_library; |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 27 | { |
brettw@chromium.org | 9e0aee4 | 2013-02-19 14:47:10 +0900 | [diff] [blame] | 28 | FilePath path(GetNativeLibraryName(L"ddraw")); |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 29 | native_library = LoadNativeLibrary(path, NULL); |
| 30 | ScopedNativeLibrary library(native_library); |
wittman | a7f3ea2 | 2015-11-03 07:38:40 +0900 | [diff] [blame] | 31 | EXPECT_TRUE(library.is_valid()); |
| 32 | EXPECT_EQ(native_library, library.get()); |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 33 | FARPROC test_function = |
| 34 | reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName)); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 35 | EXPECT_EQ(0, IsBadCodePtr(test_function)); |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 36 | EXPECT_EQ( |
| 37 | GetFunctionPointerFromNativeLibrary(native_library, kFunctionName), |
| 38 | test_function); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 39 | } |
tmoniuszko@opera.com | f898562 | 2013-11-01 05:28:47 +0900 | [diff] [blame] | 40 | EXPECT_EQ(NULL, |
| 41 | GetFunctionPointerFromNativeLibrary(native_library, kFunctionName)); |
hbono@chromium.org | 83bc0b6 | 2010-03-10 15:23:35 +0900 | [diff] [blame] | 42 | #endif |
| 43 | } |
brettw@chromium.org | 9e0aee4 | 2013-02-19 14:47:10 +0900 | [diff] [blame] | 44 | |
| 45 | } // namespace base |