scoped_native_library_unittest.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/scoped_native_library.h"
  5. #include "build/build_config.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #if BUILDFLAG(IS_WIN)
  8. #include "base/files/file_path.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #endif
  11. namespace base {
  12. // Tests whether or not a function pointer retrieved via ScopedNativeLibrary
  13. // is available only in a scope.
  14. TEST(ScopedNativeLibrary, Basic) {
  15. #if BUILDFLAG(IS_WIN)
  16. // Get the pointer to DirectDrawCreate() from "ddraw.dll" and verify it
  17. // is valid only in this scope.
  18. // FreeLibrary() doesn't actually unload a DLL until its reference count
  19. // becomes zero, i.e. function pointer is still valid if the DLL used
  20. // in this test is also used by another part of this executable.
  21. // So, this test uses "ddraw.dll", which is not used by Chrome at all but
  22. // installed on all versions of Windows.
  23. const char kFunctionName[] = "DirectDrawCreate";
  24. NativeLibrary native_library;
  25. {
  26. FilePath path(FilePath::FromUTF8Unsafe(GetNativeLibraryName("ddraw")));
  27. native_library = LoadNativeLibrary(path, nullptr);
  28. ScopedNativeLibrary library(native_library);
  29. EXPECT_TRUE(library.is_valid());
  30. EXPECT_EQ(native_library, library.get());
  31. FARPROC test_function =
  32. reinterpret_cast<FARPROC>(library.GetFunctionPointer(kFunctionName));
  33. EXPECT_EQ(0, IsBadCodePtr(test_function));
  34. EXPECT_EQ(
  35. GetFunctionPointerFromNativeLibrary(native_library, kFunctionName),
  36. test_function);
  37. }
  38. EXPECT_FALSE(
  39. GetFunctionPointerFromNativeLibrary(native_library, kFunctionName));
  40. #endif
  41. }
  42. } // namespace base