blob: 035faa03f741119029ecafc278a979b30d6e53d3 [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);
31 FARPROC test_function =
32 reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090033 EXPECT_EQ(0, IsBadCodePtr(test_function));
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090034 EXPECT_EQ(
35 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
36 test_function);
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090037 }
tmoniuszko@opera.comf8985622013-11-01 05:28:47 +090038 EXPECT_EQ(NULL,
39 GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
hbono@chromium.org83bc0b62010-03-10 15:23:35 +090040#endif
41}
brettw@chromium.org9e0aee42013-02-19 14:47:10 +090042
43} // namespace base