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.
5 #include "base/scoped_native_library.h"
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
11 #include "base/files/file_path.h"
12 #include "base/strings/utf_string_conversions.h"
17 // Tests whether or not a function pointer retrieved via ScopedNativeLibrary
18 // is available only in a scope.
19 TEST(ScopedNativeLibrary, Basic) {
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;
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));
40 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
44 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));