• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include <boost/config.hpp>
14 #include <boost/core/ignore_unused.hpp>
15 
16 #if defined(BOOST_WINDOWS)
17 
18 #include <boost/winapi/basic_types.hpp>
19 
20 // WinAPI is not currently set up well for building mocks, as
21 // the definitions of wincrypt APIs all use BOOST_SYMBOL_IMPORT
22 // therefore we cannot include it, but we need some of the types
23 // so they are defined here...
24 namespace boost {
25 namespace winapi {
26     typedef ULONG_PTR_ HCRYPTPROV_;
27 }
28 }
29 
30 // wincrypt has to be mocked through a DLL pretending to be
31 // the real thing as the official APIs use __declspec(dllimport)
32 
33 #include <deque>
34 std::deque<boost::winapi::BOOL_> wincrypt_next_result;
35 
expectations_capable()36 BOOST_SYMBOL_EXPORT bool expectations_capable()
37 {
38     return true;
39 }
40 
expectations_met()41 BOOST_SYMBOL_EXPORT bool expectations_met()
42 {
43     return wincrypt_next_result.empty();
44 }
45 
expect_next_call_success(bool success)46 BOOST_SYMBOL_EXPORT void expect_next_call_success(bool success)
47 {
48     wincrypt_next_result.push_back(success ? 1 : 0);
49 }
50 
provider_acquires_context()51 BOOST_SYMBOL_EXPORT bool provider_acquires_context()
52 {
53     return true;
54 }
55 
56 extern "C" {
57 
58 BOOST_SYMBOL_EXPORT
59 boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
CryptAcquireContextW(boost::winapi::HCRYPTPROV_ * phProv,boost::winapi::LPCWSTR_ szContainer,boost::winapi::LPCWSTR_ szProvider,boost::winapi::DWORD_ dwProvType,boost::winapi::DWORD_ dwFlags)60 CryptAcquireContextW(
61     boost::winapi::HCRYPTPROV_ *phProv,
62     boost::winapi::LPCWSTR_ szContainer,
63     boost::winapi::LPCWSTR_ szProvider,
64     boost::winapi::DWORD_ dwProvType,
65     boost::winapi::DWORD_ dwFlags)
66 {
67     boost::ignore_unused(phProv);
68     boost::ignore_unused(szContainer);
69     boost::ignore_unused(szProvider);
70     boost::ignore_unused(dwProvType);
71     boost::ignore_unused(dwFlags);
72 
73     boost::winapi::BOOL_ result = wincrypt_next_result.front();
74     wincrypt_next_result.pop_front();
75     return result;
76 }
77 
78 BOOST_SYMBOL_EXPORT
79 boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
CryptGenRandom(boost::winapi::HCRYPTPROV_ hProv,boost::winapi::DWORD_ dwLen,boost::winapi::BYTE_ * pbBuffer)80 CryptGenRandom(
81     boost::winapi::HCRYPTPROV_ hProv,
82     boost::winapi::DWORD_ dwLen,
83     boost::winapi::BYTE_ *pbBuffer)
84 {
85     boost::ignore_unused(hProv);
86     boost::ignore_unused(dwLen);
87     boost::ignore_unused(pbBuffer);
88 
89     boost::winapi::BOOL_ result = wincrypt_next_result.front();
90     wincrypt_next_result.pop_front();
91     return result;
92 }
93 
94 // the implementation ignores the result of close because it
95 // happens in a destructor
96 BOOST_SYMBOL_EXPORT
97 boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
CryptReleaseContext(boost::winapi::HCRYPTPROV_ hProv,boost::winapi::ULONG_PTR_ dwFlags)98 CryptReleaseContext(
99     boost::winapi::HCRYPTPROV_ hProv,
100 #if defined(_MSC_VER) && (_MSC_VER+0) >= 1500 && (_MSC_VER+0) < 1900 && BOOST_USE_NTDDI_VERSION < BOOST_WINAPI_NTDDI_WINXP
101     // see winapi crypt.hpp for more details on why these differ...
102     boost::winapi::ULONG_PTR_ dwFlags
103 #else
104     boost::winapi::DWORD_ dwFlags
105 #endif
106 )
107 {
108     boost::ignore_unused(hProv);
109     boost::ignore_unused(dwFlags);
110     return true;
111 }
112 
113 } // end extern "C"
114 
115 #endif
116