15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/scoped_native_library.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base { 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests whether or not a function pointer retrieved via ScopedNativeLibrary 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is available only in a scope. 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(ScopedNativeLibrary, Basic) { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // is valid only in this scope. 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FreeLibrary() doesn't actually unload a DLL until its reference count 210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // becomes zero, i.e. function pointer is still valid if the DLL used 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in this test is also used by another part of this executable. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // So, this test uses "ddraw.dll", which is not used by Chrome at all but 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // installed on all versions of Windows. 250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const char kFunctionName[] = "DirectDrawCreate"; 260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) NativeLibrary native_library; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) { 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FilePath path(GetNativeLibraryName(L"ddraw")); 290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) native_library = LoadNativeLibrary(path, NULL); 300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ScopedNativeLibrary library(native_library); 310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) FARPROC test_function = 320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName)); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(0, IsBadCodePtr(test_function)); 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_EQ( 350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) GetFunctionPointerFromNativeLibrary(native_library, kFunctionName), 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) test_function); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_EQ(NULL, 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) GetFunctionPointerFromNativeLibrary(native_library, kFunctionName)); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace base 44