mock_random.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) 2017 James E. King III
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // https://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // The contents of this file are compiled into a loadable
  9. // library that is used for mocking purposes so that the error
  10. // paths in the random_provider implementations are exercised.
  11. //
  12. #include <boost/config.hpp>
  13. #include <boost/core/ignore_unused.hpp>
  14. #if defined(BOOST_WINDOWS)
  15. #include <boost/winapi/basic_types.hpp>
  16. // WinAPI is not currently set up well for building mocks, as
  17. // the definitions of wincrypt APIs all use BOOST_SYMBOL_IMPORT
  18. // therefore we cannot include it, but we need some of the types
  19. // so they are defined here...
  20. namespace boost {
  21. namespace winapi {
  22. typedef ULONG_PTR_ HCRYPTPROV_;
  23. }
  24. }
  25. // wincrypt has to be mocked through a DLL pretending to be
  26. // the real thing as the official APIs use __declspec(dllimport)
  27. #include <deque>
  28. std::deque<boost::winapi::BOOL_> wincrypt_next_result;
  29. BOOST_SYMBOL_EXPORT bool expectations_capable()
  30. {
  31. return true;
  32. }
  33. BOOST_SYMBOL_EXPORT bool expectations_met()
  34. {
  35. return wincrypt_next_result.empty();
  36. }
  37. BOOST_SYMBOL_EXPORT void expect_next_call_success(bool success)
  38. {
  39. wincrypt_next_result.push_back(success ? 1 : 0);
  40. }
  41. BOOST_SYMBOL_EXPORT bool provider_acquires_context()
  42. {
  43. return true;
  44. }
  45. extern "C" {
  46. BOOST_SYMBOL_EXPORT
  47. boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  48. CryptAcquireContextW(
  49. boost::winapi::HCRYPTPROV_ *phProv,
  50. boost::winapi::LPCWSTR_ szContainer,
  51. boost::winapi::LPCWSTR_ szProvider,
  52. boost::winapi::DWORD_ dwProvType,
  53. boost::winapi::DWORD_ dwFlags)
  54. {
  55. boost::ignore_unused(phProv);
  56. boost::ignore_unused(szContainer);
  57. boost::ignore_unused(szProvider);
  58. boost::ignore_unused(dwProvType);
  59. boost::ignore_unused(dwFlags);
  60. boost::winapi::BOOL_ result = wincrypt_next_result.front();
  61. wincrypt_next_result.pop_front();
  62. return result;
  63. }
  64. BOOST_SYMBOL_EXPORT
  65. boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  66. CryptGenRandom(
  67. boost::winapi::HCRYPTPROV_ hProv,
  68. boost::winapi::DWORD_ dwLen,
  69. boost::winapi::BYTE_ *pbBuffer)
  70. {
  71. boost::ignore_unused(hProv);
  72. boost::ignore_unused(dwLen);
  73. boost::ignore_unused(pbBuffer);
  74. boost::winapi::BOOL_ result = wincrypt_next_result.front();
  75. wincrypt_next_result.pop_front();
  76. return result;
  77. }
  78. // the implementation ignores the result of close because it
  79. // happens in a destructor
  80. BOOST_SYMBOL_EXPORT
  81. boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  82. CryptReleaseContext(
  83. boost::winapi::HCRYPTPROV_ hProv,
  84. #if defined(_MSC_VER) && (_MSC_VER+0) >= 1500 && (_MSC_VER+0) < 1900 && BOOST_USE_NTDDI_VERSION < BOOST_WINAPI_NTDDI_WINXP
  85. // see winapi crypt.hpp for more details on why these differ...
  86. boost::winapi::ULONG_PTR_ dwFlags
  87. #else
  88. boost::winapi::DWORD_ dwFlags
  89. #endif
  90. )
  91. {
  92. boost::ignore_unused(hProv);
  93. boost::ignore_unused(dwFlags);
  94. return true;
  95. }
  96. } // end extern "C"
  97. #endif