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_SSPI_LIBRARY_WIN_H_
6#define NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_
7
8#include <list>
9#include <set>
10
11#include "net/http/http_auth_sspi_win.h"
12
13namespace net {
14
15// The MockSSPILibrary class is intended for unit tests which want to bypass
16// the system SSPI library calls.
17class MockSSPILibrary : public SSPILibrary {
18 public:
19  MockSSPILibrary();
20  virtual ~MockSSPILibrary();
21
22  // TODO(cbentzel): Only QuerySecurityPackageInfo and FreeContextBuffer
23  //                 are properly handled currently.
24  // SSPILibrary methods:
25  virtual SECURITY_STATUS AcquireCredentialsHandle(LPWSTR pszPrincipal,
26                                                   LPWSTR pszPackage,
27                                                   unsigned long fCredentialUse,
28                                                   void* pvLogonId,
29                                                   void* pvAuthData,
30                                                   SEC_GET_KEY_FN pGetKeyFn,
31                                                   void* pvGetKeyArgument,
32                                                   PCredHandle phCredential,
33                                                   PTimeStamp ptsExpiry);
34  virtual SECURITY_STATUS InitializeSecurityContext(PCredHandle phCredential,
35                                                    PCtxtHandle phContext,
36                                                    SEC_WCHAR* pszTargetName,
37                                                    unsigned long fContextReq,
38                                                    unsigned long Reserved1,
39                                                    unsigned long TargetDataRep,
40                                                    PSecBufferDesc pInput,
41                                                    unsigned long Reserved2,
42                                                    PCtxtHandle phNewContext,
43                                                    PSecBufferDesc pOutput,
44                                                    unsigned long* contextAttr,
45                                                    PTimeStamp ptsExpiry);
46  virtual SECURITY_STATUS QuerySecurityPackageInfo(LPWSTR pszPackageName,
47                                                   PSecPkgInfoW *pkgInfo);
48  virtual SECURITY_STATUS FreeCredentialsHandle(PCredHandle phCredential);
49  virtual SECURITY_STATUS DeleteSecurityContext(PCtxtHandle phContext);
50  virtual SECURITY_STATUS FreeContextBuffer(PVOID pvContextBuffer);
51
52  // Establishes an expectation for a |QuerySecurityPackageInfo()| call.
53  //
54  // Each expectation established by |ExpectSecurityQueryPackageInfo()| must be
55  // matched by a call to |QuerySecurityPackageInfo()| during the lifetime of
56  // the MockSSPILibrary. The |expected_package| argument must equal the
57  // |*pszPackageName| argument to |QuerySecurityPackageInfo()| for there to be
58  // a match. The expectations also establish an explicit ordering.
59  //
60  // For example, this sequence will be successful.
61  //   MockSSPILibrary lib;
62  //   lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...)
63  //   lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...)
64  //   lib.QuerySecurityPackageInfo(L"NTLM", ...)
65  //   lib.QuerySecurityPackageInfo(L"Negotiate", ...)
66  //
67  // This sequence will fail since the queries do not occur in the order
68  // established by the expectations.
69  //   MockSSPILibrary lib;
70  //   lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...)
71  //   lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...)
72  //   lib.QuerySecurityPackageInfo(L"Negotiate", ...)
73  //   lib.QuerySecurityPackageInfo(L"NTLM", ...)
74  //
75  // This sequence will fail because there were not enough queries.
76  //   MockSSPILibrary lib;
77  //   lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...)
78  //   lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...)
79  //   lib.QuerySecurityPackageInfo(L"NTLM", ...)
80  //
81  // |response_code| is used as the return value for
82  // |QuerySecurityPackageInfo()|. If |response_code| is SEC_E_OK,
83  // an expectation is also set for a call to |FreeContextBuffer()| after
84  // the matching |QuerySecurityPackageInfo()| is called.
85  //
86  // |package_info| is assigned to |*pkgInfo| in |QuerySecurityPackageInfo|.
87  // The lifetime of |*package_info| should last at least until the matching
88  // |QuerySecurityPackageInfo()| is called.
89  void ExpectQuerySecurityPackageInfo(const std::wstring& expected_package,
90                                      SECURITY_STATUS response_code,
91                                      PSecPkgInfoW package_info);
92
93 private:
94  struct PackageQuery {
95    std::wstring expected_package;
96    SECURITY_STATUS response_code;
97    PSecPkgInfoW package_info;
98  };
99
100  // expected_package_queries contains an ordered list of expected
101  // |QuerySecurityPackageInfo()| calls and the return values for those
102  // calls.
103  std::list<PackageQuery> expected_package_queries_;
104
105  // Set of packages which should be freed.
106  std::set<PSecPkgInfoW> expected_freed_packages_;
107};
108
109}  // namespace net
110
111#endif  // NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_
112