blob: 36aaa7638f936c85152250846403c0426686b5c7 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]da2566e12010-03-10 06:23:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e0785902011-05-19 23:34:175#include "base/scoped_native_library.h"
avi9b6f42932015-12-26 22:15:146
7#include "build/build_config.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
Xiaohan Wang38e4ebb2022-01-19 06:57:4310#if BUILDFLAG(IS_WIN)
Peter Kastingcab74612025-01-21 20:13:2311#include <windows.h>
12
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
thestig02c965b2016-06-14 18:52:2314#include "base/strings/utf_string_conversions.h"
[email protected]864b1362010-08-19 03:49:3815#endif
[email protected]da2566e12010-03-10 06:23:3516
[email protected]5257bcf2013-02-19 05:47:1017namespace base {
18
[email protected]da2566e12010-03-10 06:23:3519// Tests whether or not a function pointer retrieved via ScopedNativeLibrary
20// is available only in a scope.
21TEST(ScopedNativeLibrary, Basic) {
Xiaohan Wang38e4ebb2022-01-19 06:57:4322#if BUILDFLAG(IS_WIN)
[email protected]da2566e12010-03-10 06:23:3523 // 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]cbd119a2013-10-31 20:28:4726 // becomes zero, i.e. function pointer is still valid if the DLL used
[email protected]da2566e12010-03-10 06:23:3527 // 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]cbd119a2013-10-31 20:28:4730 const char kFunctionName[] = "DirectDrawCreate";
31 NativeLibrary native_library;
[email protected]da2566e12010-03-10 06:23:3532 {
thestig02c965b2016-06-14 18:52:2333 FilePath path(FilePath::FromUTF8Unsafe(GetNativeLibraryName("ddraw")));
34 native_library = LoadNativeLibrary(path, nullptr);
[email protected]cbd119a2013-10-31 20:28:4735 ScopedNativeLibrary library(native_library);
wittmanf2d56442015-11-02 22:38:4036 EXPECT_TRUE(library.is_valid());
37 EXPECT_EQ(native_library, library.get());
[email protected]cbd119a2013-10-31 20:28:4738 FARPROC test_function =
39 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
Peter Kastingcab74612025-01-21 20:13:2340 EXPECT_EQ(0, ::IsBadCodePtr(test_function));
[email protected]cbd119a2013-10-31 20:28:4741 EXPECT_EQ(
42 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
43 test_function);
[email protected]da2566e12010-03-10 06:23:3544 }
thestig02c965b2016-06-14 18:52:2345 EXPECT_FALSE(
46 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
[email protected]da2566e12010-03-10 06:23:3547#endif
48}
[email protected]5257bcf2013-02-19 05:47:1049
50} // namespace base