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