mock_random.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. // Mocks are used to test sad paths by forcing error responses
  9. //
  10. #include <boost/core/ignore_unused.hpp>
  11. #include <boost/uuid/detail/random_provider_detect_platform.hpp>
  12. #if defined(BOOST_UUID_TEST_RANDOM_MOCK)
  13. #if defined(BOOST_UUID_RANDOM_PROVIDER_WINCRYPT) || defined(BOOST_UUID_RANDOM_PROVIDER_POSIX)
  14. #define BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE BOOST_SYMBOL_IMPORT
  15. #else
  16. #define BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE
  17. #endif
  18. //! \returns true if the provider can be mocked - if not then the test
  19. //! should skip negative testing
  20. BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE bool expectations_capable();
  21. //! Ensure all expectations for calls were consumed. This means the number
  22. //! of expected calls was met.
  23. //! \returns true if all expectations were met
  24. BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE bool expectations_met();
  25. //! Set the response of the next mocked random/crypto call - builds up
  26. //! a queue of responses. If the queue empties and another call is made,
  27. //! the test will core.
  28. //! \param[in] success true for success response, false for failure
  29. BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE void expect_next_call_success(bool success);
  30. //! \returns true if the provider acquires a context
  31. BOOST_UUID_TEST_RANDOM_MOCK_LINKAGE bool provider_acquires_context();
  32. #if defined(BOOST_UUID_RANDOM_PROVIDER_ARC4RANDOM)
  33. // arc4random cannot fail therefore it needs no mocking at all!
  34. bool expectations_capable()
  35. {
  36. return false;
  37. }
  38. bool expectations_met()
  39. {
  40. throw std::logic_error("expectations not supported");
  41. }
  42. void expect_next_call_success(bool success)
  43. {
  44. boost::ignore_unused(success);
  45. throw std::logic_error("expectations not supported");
  46. }
  47. bool provider_acquires_context()
  48. {
  49. throw std::logic_error("expectations not supported");
  50. }
  51. #elif defined(BOOST_UUID_RANDOM_PROVIDER_BCRYPT)
  52. #include <boost/winapi/bcrypt.hpp>
  53. #include <deque>
  54. std::deque<boost::winapi::NTSTATUS_> bcrypt_next_result;
  55. bool expectations_capable()
  56. {
  57. return true;
  58. }
  59. bool expectations_met()
  60. {
  61. return bcrypt_next_result.empty();
  62. }
  63. void expect_next_call_success(bool success)
  64. {
  65. bcrypt_next_result.push_back(success ? 0 : 17);
  66. }
  67. bool provider_acquires_context()
  68. {
  69. return true;
  70. }
  71. boost::winapi::NTSTATUS_ BOOST_WINAPI_WINAPI_CC
  72. BCryptOpenAlgorithmProvider(
  73. boost::winapi::BCRYPT_ALG_HANDLE_ *phAlgorithm,
  74. boost::winapi::LPCWSTR_ pszAlgId,
  75. boost::winapi::LPCWSTR_ pszImplementation,
  76. boost::winapi::DWORD_ dwFlags
  77. )
  78. {
  79. boost::ignore_unused(phAlgorithm);
  80. boost::ignore_unused(pszAlgId);
  81. boost::ignore_unused(pszImplementation);
  82. boost::ignore_unused(dwFlags);
  83. boost::winapi::NTSTATUS_ result = bcrypt_next_result.front();
  84. bcrypt_next_result.pop_front();
  85. return result;
  86. }
  87. boost::winapi::NTSTATUS_ BOOST_WINAPI_WINAPI_CC
  88. BCryptGenRandom(
  89. boost::winapi::BCRYPT_ALG_HANDLE_ hAlgorithm,
  90. boost::winapi::PUCHAR_ pbBuffer,
  91. boost::winapi::ULONG_ cbBuffer,
  92. boost::winapi::ULONG_ dwFlags
  93. )
  94. {
  95. boost::ignore_unused(hAlgorithm);
  96. boost::ignore_unused(pbBuffer);
  97. boost::ignore_unused(cbBuffer);
  98. boost::ignore_unused(dwFlags);
  99. boost::winapi::NTSTATUS_ result = bcrypt_next_result.front();
  100. bcrypt_next_result.pop_front();
  101. return result;
  102. }
  103. // the implementation ignores the result of close because it
  104. // happens in a destructor
  105. boost::winapi::NTSTATUS_ BOOST_WINAPI_WINAPI_CC
  106. BCryptCloseAlgorithmProvider(
  107. boost::winapi::BCRYPT_ALG_HANDLE_ hAlgorithm,
  108. boost::winapi::ULONG_ dwFlags
  109. )
  110. {
  111. boost::ignore_unused(hAlgorithm);
  112. boost::ignore_unused(dwFlags);
  113. return 0;
  114. }
  115. #elif defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM)
  116. #include <deque>
  117. #include <unistd.h>
  118. std::deque<bool> getrandom_next_result;
  119. bool expectations_capable()
  120. {
  121. return true;
  122. }
  123. bool expectations_met()
  124. {
  125. return getrandom_next_result.empty();
  126. }
  127. void expect_next_call_success(bool success)
  128. {
  129. getrandom_next_result.push_back(success);
  130. }
  131. bool provider_acquires_context()
  132. {
  133. return false;
  134. }
  135. ssize_t mock_getrandom(void *buffer, size_t length, unsigned int flags)
  136. {
  137. boost::ignore_unused(buffer);
  138. boost::ignore_unused(length);
  139. boost::ignore_unused(flags);
  140. bool success = getrandom_next_result.front();
  141. getrandom_next_result.pop_front();
  142. return success ? static_cast< ssize_t >(length) : static_cast< ssize_t >(-1);
  143. }
  144. #define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM ::mock_getrandom
  145. #elif defined(BOOST_UUID_RANDOM_PROVIDER_GETENTROPY)
  146. //
  147. // This stubbing technique works on unix because of how the loader resolves
  148. // functions. Locally defined functions resolve first.
  149. //
  150. #include <deque>
  151. #include <unistd.h>
  152. std::deque<int> getentropy_next_result;
  153. bool expectations_capable()
  154. {
  155. return true;
  156. }
  157. bool expectations_met()
  158. {
  159. return getentropy_next_result.empty();
  160. }
  161. void expect_next_call_success(bool success)
  162. {
  163. getentropy_next_result.push_back(success ? 0 : -1);
  164. }
  165. bool provider_acquires_context()
  166. {
  167. return false;
  168. }
  169. int getentropy(void *buffer, size_t length)
  170. {
  171. boost::ignore_unused(buffer);
  172. boost::ignore_unused(length);
  173. int result = getentropy_next_result.front();
  174. getentropy_next_result.pop_front();
  175. return result;
  176. }
  177. #elif defined(BOOST_UUID_RANDOM_PROVIDER_POSIX)
  178. #include <boost/numeric/conversion/cast.hpp>
  179. #include <deque>
  180. #include <stdexcept>
  181. std::deque<bool> posix_next_result; // bool success
  182. bool expectations_capable()
  183. {
  184. return true;
  185. }
  186. bool expectations_met()
  187. {
  188. return posix_next_result.empty();
  189. }
  190. void expect_next_call_success(bool success)
  191. {
  192. posix_next_result.push_back(success);
  193. }
  194. bool provider_acquires_context()
  195. {
  196. return true;
  197. }
  198. int mockopen(const char *fname, int flags)
  199. {
  200. boost::ignore_unused(fname);
  201. boost::ignore_unused(flags);
  202. bool success = posix_next_result.front();
  203. posix_next_result.pop_front();
  204. return success ? 17 : -1;
  205. }
  206. ssize_t mockread(int fd, void *buf, size_t siz)
  207. {
  208. boost::ignore_unused(fd);
  209. boost::ignore_unused(buf);
  210. // first call siz is 4, in a success case we return 1
  211. // forcing a second loop to come through size 3
  212. if (siz < 4) { return boost::numeric_cast<ssize_t>(siz); }
  213. if (siz > 4) { throw std::logic_error("unexpected siz"); }
  214. bool success = posix_next_result.front();
  215. posix_next_result.pop_front();
  216. return success ? 1 : -1;
  217. }
  218. #define BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_OPEN mockopen
  219. #define BOOST_UUID_RANDOM_PROVIDER_POSIX_IMPL_READ mockread
  220. #elif defined(BOOST_UUID_RANDOM_PROVIDER_WINCRYPT)
  221. // Nothing to declare, since the expectation methods were already
  222. // defined as import, we will link against a mock library
  223. #else
  224. #error support needed here for testing
  225. #endif
  226. #endif // BOOST_UUID_TEST_RANDOM_MOCK