mock_gssapi_library_posix.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef NET_HTTP_MOCK_GSSAPI_LIBRARY_POSIX_H_
  5. #define NET_HTTP_MOCK_GSSAPI_LIBRARY_POSIX_H_
  6. #include <list>
  7. #include <string>
  8. #include "base/gtest_prod_util.h"
  9. #include "net/http/http_auth_gssapi_posix.h"
  10. namespace net {
  11. namespace test {
  12. class GssContextMockImpl {
  13. public:
  14. GssContextMockImpl();
  15. GssContextMockImpl(const GssContextMockImpl& other);
  16. GssContextMockImpl(const char* src_name,
  17. const char* targ_name,
  18. OM_uint32 lifetime_rec,
  19. const gss_OID_desc& mech_type,
  20. OM_uint32 ctx_flags,
  21. int locally_initiated,
  22. int open);
  23. ~GssContextMockImpl();
  24. void Assign(const GssContextMockImpl& other);
  25. std::string src_name;
  26. std::string targ_name;
  27. OM_uint32 lifetime_rec;
  28. gss_OID_desc mech_type;
  29. OM_uint32 ctx_flags;
  30. int locally_initiated;
  31. int open;
  32. };
  33. // The MockGSSAPILibrary class is intended for unit tests which want to bypass
  34. // the system GSSAPI library calls.
  35. class MockGSSAPILibrary : public GSSAPILibrary {
  36. public:
  37. // Unit tests need access to this. "Friend"ing didn't help.
  38. struct SecurityContextQuery {
  39. SecurityContextQuery();
  40. SecurityContextQuery(const std::string& expected_package,
  41. OM_uint32 response_code,
  42. OM_uint32 minor_response_code,
  43. const test::GssContextMockImpl& context_info,
  44. const char* expected_input_token,
  45. const char* output_token);
  46. SecurityContextQuery(const SecurityContextQuery& other);
  47. ~SecurityContextQuery();
  48. std::string expected_package;
  49. OM_uint32 response_code;
  50. OM_uint32 minor_response_code;
  51. test::GssContextMockImpl context_info;
  52. gss_buffer_desc expected_input_token;
  53. gss_buffer_desc output_token;
  54. };
  55. MockGSSAPILibrary();
  56. ~MockGSSAPILibrary() override;
  57. // Establishes an expectation for a |init_sec_context()| call.
  58. //
  59. // Each expectation established by |ExpectSecurityContext()| must be
  60. // matched by a call to |init_sec_context()| during the lifetime of
  61. // the MockGSSAPILibrary. The |expected_package| argument must equal the
  62. // value associated with the |target_name| argument to |init_sec_context()|
  63. // for there to be a match. The expectations also establish an explicit
  64. // ordering.
  65. //
  66. // For example, this sequence will be successful.
  67. // MockGSSAPILibrary lib;
  68. // lib.ExpectSecurityContext("NTLM", ...)
  69. // lib.ExpectSecurityContext("Negotiate", ...)
  70. // lib.init_sec_context("NTLM", ...)
  71. // lib.init_sec_context("Negotiate", ...)
  72. //
  73. // This sequence will fail since the queries do not occur in the order
  74. // established by the expectations.
  75. // MockGSSAPILibrary lib;
  76. // lib.ExpectSecurityContext("NTLM", ...)
  77. // lib.ExpectSecurityContext("Negotiate", ...)
  78. // lib.init_sec_context("Negotiate", ...)
  79. // lib.init_sec_context("NTLM", ...)
  80. //
  81. // This sequence will fail because there were not enough queries.
  82. // MockGSSAPILibrary lib;
  83. // lib.ExpectSecurityContext("NTLM", ...)
  84. // lib.ExpectSecurityContext("Negotiate", ...)
  85. // lib.init_sec_context("NTLM", ...)
  86. //
  87. // |response_code| is used as the return value for |init_sec_context()|.
  88. // If |response_code| is GSS_S_COMPLETE,
  89. //
  90. // |context_info| is the expected value of the |**context_handle| in after
  91. // |init_sec_context()| returns.
  92. void ExpectSecurityContext(const std::string& expected_package,
  93. OM_uint32 response_code,
  94. OM_uint32 minor_response_code,
  95. const test::GssContextMockImpl& context_info,
  96. const gss_buffer_desc& expected_input_token,
  97. const gss_buffer_desc& output_token);
  98. // GSSAPILibrary methods:
  99. // Initializes the library, including any necessary dynamic libraries.
  100. // This is done separately from construction (which happens at startup time)
  101. // in order to delay work until the class is actually needed.
  102. bool Init(const NetLogWithSource& net_log) override;
  103. // These methods match the ones in the GSSAPI library.
  104. OM_uint32 import_name(OM_uint32* minor_status,
  105. const gss_buffer_t input_name_buffer,
  106. const gss_OID input_name_type,
  107. gss_name_t* output_name) override;
  108. OM_uint32 release_name(OM_uint32* minor_status,
  109. gss_name_t* input_name) override;
  110. OM_uint32 release_buffer(OM_uint32* minor_status,
  111. gss_buffer_t buffer) override;
  112. OM_uint32 display_name(OM_uint32* minor_status,
  113. const gss_name_t input_name,
  114. gss_buffer_t output_name_buffer,
  115. gss_OID* output_name_type) override;
  116. // These special status values can be used to trigger specific behavior in
  117. // |display_status()|.
  118. enum class DisplayStatusSpecials : OM_uint32 {
  119. // A multiline status message.
  120. MultiLine = 128,
  121. // Multiline, execept there's no ending message.
  122. InfiniteLines,
  123. // Causes |display_status()| to fail.
  124. Fail,
  125. // Returns an empty message.
  126. EmptyMessage,
  127. // Returns successfully without modifying |status_string|.
  128. UninitalizedBuffer,
  129. // Returns a message that's invalid UTF-8.
  130. InvalidUtf8
  131. };
  132. OM_uint32 display_status(OM_uint32* minor_status,
  133. OM_uint32 status_value,
  134. int status_type,
  135. const gss_OID mech_type,
  136. OM_uint32* message_contex,
  137. gss_buffer_t status_string) override;
  138. OM_uint32 init_sec_context(OM_uint32* minor_status,
  139. const gss_cred_id_t initiator_cred_handle,
  140. gss_ctx_id_t* context_handle,
  141. const gss_name_t target_name,
  142. const gss_OID mech_type,
  143. OM_uint32 req_flags,
  144. OM_uint32 time_req,
  145. const gss_channel_bindings_t input_chan_bindings,
  146. const gss_buffer_t input_token,
  147. gss_OID* actual_mech_type,
  148. gss_buffer_t output_token,
  149. OM_uint32* ret_flags,
  150. OM_uint32* time_rec) override;
  151. OM_uint32 wrap_size_limit(OM_uint32* minor_status,
  152. const gss_ctx_id_t context_handle,
  153. int conf_req_flag,
  154. gss_qop_t qop_req,
  155. OM_uint32 req_output_size,
  156. OM_uint32* max_input_size) override;
  157. OM_uint32 delete_sec_context(OM_uint32* minor_status,
  158. gss_ctx_id_t* context_handle,
  159. gss_buffer_t output_token) override;
  160. OM_uint32 inquire_context(OM_uint32* minor_status,
  161. const gss_ctx_id_t context_handle,
  162. gss_name_t* src_name,
  163. gss_name_t* targ_name,
  164. OM_uint32* lifetime_rec,
  165. gss_OID* mech_type,
  166. OM_uint32* ctx_flags,
  167. int* locally_initiated,
  168. int* open) override;
  169. const std::string& GetLibraryNameForTesting() override;
  170. private:
  171. FRIEND_TEST_ALL_PREFIXES(HttpAuthGSSAPIPOSIXTest, GSSAPICycle);
  172. // |expected_security_queries| contains an ordered list of expected
  173. // |init_sec_context()| calls and the return values for those
  174. // calls.
  175. std::list<SecurityContextQuery> expected_security_queries_;
  176. // Empty string. Enables GetLibraryNameForTesting() to return a reference.
  177. std::string library_name_;
  178. };
  179. } // namespace test
  180. using MockAuthLibrary = test::MockGSSAPILibrary;
  181. } // namespace net
  182. #endif // NET_HTTP_MOCK_GSSAPI_LIBRARY_POSIX_H_