blob: 204aadb983e07cfe51ae8dd0f07d93690d31d84b [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[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
[email protected]864b1362010-08-19 03:49:3810#if defined(OS_WIN)
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]864b1362010-08-19 03:49:3812#endif
[email protected]da2566e12010-03-10 06:23:3513
[email protected]5257bcf2013-02-19 05:47:1014namespace base {
15
[email protected]da2566e12010-03-10 06:23:3516// Tests whether or not a function pointer retrieved via ScopedNativeLibrary
17// is available only in a scope.
18TEST(ScopedNativeLibrary, Basic) {
19#if defined(OS_WIN)
20 // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it
21 // is valid only in this scope.
22 // FreeLibrary() doesn't actually unload a DLL until its reference count
[email protected]cbd119a2013-10-31 20:28:4723 // becomes zero, i.e. function pointer is still valid if the DLL used
[email protected]da2566e12010-03-10 06:23:3524 // in this test is also used by another part of this executable.
25 // So, this test uses "ddraw.dll", which is not used by Chrome at all but
26 // installed on all versions of Windows.
[email protected]cbd119a2013-10-31 20:28:4727 const char kFunctionName[] = "DirectDrawCreate";
28 NativeLibrary native_library;
[email protected]da2566e12010-03-10 06:23:3529 {
[email protected]5257bcf2013-02-19 05:47:1030 FilePath path(GetNativeLibraryName(L"ddraw"));
[email protected]cbd119a2013-10-31 20:28:4731 native_library = LoadNativeLibrary(path, NULL);
32 ScopedNativeLibrary library(native_library);
wittmanf2d56442015-11-02 22:38:4033 EXPECT_TRUE(library.is_valid());
34 EXPECT_EQ(native_library, library.get());
[email protected]cbd119a2013-10-31 20:28:4735 FARPROC test_function =
36 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
[email protected]da2566e12010-03-10 06:23:3537 EXPECT_EQ(0, IsBadCodePtr(test_function));
[email protected]cbd119a2013-10-31 20:28:4738 EXPECT_EQ(
39 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
40 test_function);
[email protected]da2566e12010-03-10 06:23:3541 }
[email protected]cbd119a2013-10-31 20:28:4742 EXPECT_EQ(NULL,
43 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
[email protected]da2566e12010-03-10 06:23:3544#endif
45}
[email protected]5257bcf2013-02-19 05:47:1046
47} // namespace base