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