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