1 // Copyright (c) 2011 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 "base/scoped_native_library.h"
6
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 #if defined(OS_WIN)
11 #include "base/files/file_path.h"
12 #include "base/strings/utf_string_conversions.h"
13 #endif
14
15 namespace base {
16
17 // Tests whether or not a function pointer retrieved via ScopedNativeLibrary
18 // is available only in a scope.
TEST(ScopedNativeLibrary,Basic)19 TEST(ScopedNativeLibrary, Basic) {
20 #if defined(OS_WIN)
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
24 // becomes zero, i.e. function pointer is still valid if the DLL used
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.
28 const char kFunctionName[] = "DirectDrawCreate";
29 NativeLibrary native_library;
30 {
31 FilePath path(FilePath::FromUTF8Unsafe(GetNativeLibraryName("ddraw")));
32 native_library = LoadNativeLibrary(path, nullptr);
33 ScopedNativeLibrary library(native_library);
34 EXPECT_TRUE(library.is_valid());
35 EXPECT_EQ(native_library, library.get());
36 FARPROC test_function =
37 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
38 EXPECT_EQ(0, IsBadCodePtr(test_function));
39 EXPECT_EQ(
40 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
41 test_function);
42 }
43 EXPECT_FALSE(
44 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
45 #endif
46 }
47
48 } // namespace base
49