blob: c7e44892d06e63780d2dab1b1b7d8e1cc140108d [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
hbono@chromium.org83bc0b62010-03-10 15:23:35 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thakis@chromium.org927d7282011-05-20 08:34:17 +09005#include "base/scoped_native_library.h"
thestig@chromium.org6b2f4ab2010-08-19 12:49:38 +09006#if defined(OS_WIN)
brettw@chromium.org59eef1f2013-02-24 14:40:52 +09007#include "base/files/file_path.h"
thestig@chromium.org6b2f4ab2010-08-19 12:49:38 +09008#endif
hbono@chromium.org83bc0b62010-03-10 15:23:35 +09009
10#include "testing/gtest/include/gtest/gtest.h"
11
brettw@chromium.org9e0aee42013-02-19 14:47:10 +090012namespace base {
13
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090014// Tests whether or not a function pointer retrieved via ScopedNativeLibrary
15// is available only in a scope.
16TEST(ScopedNativeLibrary, Basic) {
17#if defined(OS_WIN)
18 // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it
19 // is valid only in this scope.
20 // FreeLibrary() doesn't actually unload a DLL until its reference count
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090021 // becomes zero, i.e. function pointer is still valid if the DLL used
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090022 // in this test is also used by another part of this executable.
23 // So, this test uses "ddraw.dll", which is not used by Chrome at all but
24 // installed on all versions of Windows.
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090025 const char kFunctionName[] = "DirectDrawCreate";
26 NativeLibrary native_library;
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090027 {
brettw@chromium.org9e0aee42013-02-19 14:47:10 +090028 FilePath path(GetNativeLibraryName(L"ddraw"));
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090029 native_library = LoadNativeLibrary(path, NULL);
30 ScopedNativeLibrary library(native_library);
wittmana7f3ea22015-11-03 07:38:40 +090031 EXPECT_TRUE(library.is_valid());
32 EXPECT_EQ(native_library, library.get());
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090033 FARPROC test_function =
34 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090035 EXPECT_EQ(0, IsBadCodePtr(test_function));
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090036 EXPECT_EQ(
37 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
38 test_function);
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090039 }
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090040 EXPECT_EQ(NULL,
41 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090042#endif
43}
brettw@chromium.org9e0aee42013-02-19 14:47:10 +090044
45} // namespace base