blob: 76e24a75b6cdabaa2433f630c138bb3d1d50fb11 [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)
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
thestig02c965b2016-06-14 18:52:2312#include "base/strings/utf_string_conversions.h"
[email protected]864b1362010-08-19 03:49:3813#endif
[email protected]da2566e12010-03-10 06:23:3514
[email protected]5257bcf2013-02-19 05:47:1015namespace base {
16
[email protected]da2566e12010-03-10 06:23:3517// Tests whether or not a function pointer retrieved via ScopedNativeLibrary
18// is available only in a scope.
19TEST(ScopedNativeLibrary, Basic) {
Xiaohan Wang38e4ebb2022-01-19 06:57:4320#if BUILDFLAG(IS_WIN)
[email protected]da2566e12010-03-10 06:23:3521 // 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]cbd119a2013-10-31 20:28:4724 // becomes zero, i.e. function pointer is still valid if the DLL used
[email protected]da2566e12010-03-10 06:23:3525 // 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]cbd119a2013-10-31 20:28:4728 const char kFunctionName[] = "DirectDrawCreate";
29 NativeLibrary native_library;
[email protected]da2566e12010-03-10 06:23:3530 {
thestig02c965b2016-06-14 18:52:2331 FilePath path(FilePath::FromUTF8Unsafe(GetNativeLibraryName("ddraw")));
32 native_library = LoadNativeLibrary(path, nullptr);
[email protected]cbd119a2013-10-31 20:28:4733 ScopedNativeLibrary library(native_library);
wittmanf2d56442015-11-02 22:38:4034 EXPECT_TRUE(library.is_valid());
35 EXPECT_EQ(native_library, library.get());
[email protected]cbd119a2013-10-31 20:28:4736 FARPROC test_function =
37 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
[email protected]da2566e12010-03-10 06:23:3538 EXPECT_EQ(0, IsBadCodePtr(test_function));
[email protected]cbd119a2013-10-31 20:28:4739 EXPECT_EQ(
40 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
41 test_function);
[email protected]da2566e12010-03-10 06:23:3542 }
thestig02c965b2016-06-14 18:52:2343 EXPECT_FALSE(
44 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
[email protected]da2566e12010-03-10 06:23:3545#endif
46}
[email protected]5257bcf2013-02-19 05:47:1047
48} // namespace base