xref: /aosp_15_r20/external/cronet/net/http/mock_sspi_library_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2010 The Chromium Authors
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 
13 namespace net {
14 
15 // The MockSSPILibrary class is intended for unit tests which want to bypass
16 // the system SSPI library calls.
17 class MockSSPILibrary : public SSPILibrary {
18  public:
19   explicit MockSSPILibrary(const wchar_t* package);
20   ~MockSSPILibrary() override;
21 
22   // Default max token length regardless of package name returned by
23   // QuerySecurityPackageInfo() if no expectations are set.
24   static constexpr unsigned long kDefaultMaxTokenLength = 1024;
25 
26   // SSPILibrary methods:
27 
28   // AcquireCredentialsHandle() returns a handle that must be freed using
29   // FreeCredentialsHandle(). The credentials handle records the principal name.
30   //
31   // On return ptsExpiry is set to a constant.
32   SECURITY_STATUS AcquireCredentialsHandle(LPWSTR pszPrincipal,
33                                            unsigned long fCredentialUse,
34                                            void* pvLogonId,
35                                            void* pvAuthData,
36                                            SEC_GET_KEY_FN pGetKeyFn,
37                                            void* pvGetKeyArgument,
38                                            PCredHandle phCredential,
39                                            PTimeStamp ptsExpiry) override;
40 
41   // InitializeSecurityContext() returns a handle in phContext that must be
42   // freed via FreeContextBuffer() or by passing it into another
43   // InitializeSecurityContext() call.
44   //
45   // On return ptsExpiry is set to a constant.
46   //
47   // The output buffer will contain a token consisting of the ASCII string:
48   //
49   //   "<source principal>'s token #<n> for <target principal>"
50   //
51   // <source principal> is the security principal derived from explicit
52   // credentials that were passed to a prior AcquireCredentialsHandle() call, or
53   // the string "<Default>" if ambient credentials were requested.
54   //
55   // <n> is the 1-based invocation counter for InitializeSecurityContext() for
56   // the same context.
57   //
58   // <target principal> is the contents of the pszTargetName. Note that the
59   // function expects the same target name on every invocation.
60   SECURITY_STATUS InitializeSecurityContext(PCredHandle phCredential,
61                                             PCtxtHandle phContext,
62                                             SEC_WCHAR* pszTargetName,
63                                             unsigned long fContextReq,
64                                             unsigned long Reserved1,
65                                             unsigned long TargetDataRep,
66                                             PSecBufferDesc pInput,
67                                             unsigned long Reserved2,
68                                             PCtxtHandle phNewContext,
69                                             PSecBufferDesc pOutput,
70                                             unsigned long* contextAttr,
71                                             PTimeStamp ptsExpiry) override;
72 
73   // QueryContextAttributesEx() supports querying the same attributes as
74   // required by HttpAuthSSPI.
75   SECURITY_STATUS QueryContextAttributesEx(PCtxtHandle phContext,
76                                            ULONG ulAttribute,
77                                            PVOID pBuffer,
78                                            ULONG cbBuffer) override;
79 
80   SECURITY_STATUS QuerySecurityPackageInfo(PSecPkgInfoW* pkgInfo) override;
81   SECURITY_STATUS FreeCredentialsHandle(PCredHandle phCredential) override;
82   SECURITY_STATUS DeleteSecurityContext(PCtxtHandle phContext) override;
83   SECURITY_STATUS FreeContextBuffer(PVOID pvContextBuffer) override;
84 
85   // Establishes an expectation for a |QuerySecurityPackageInfo()| call.
86   //
87   // Each expectation established by |ExpectSecurityQueryPackageInfo()| must be
88   // matched by a call to |QuerySecurityPackageInfo()| during the lifetime of
89   // the MockSSPILibrary. The expectations establish an explicit ordering.
90   //
91   // |response_code| is used as the return value for
92   // |QuerySecurityPackageInfo()|. If |response_code| is SEC_E_OK,
93   // an expectation is also set for a call to |FreeContextBuffer()| after
94   // the matching |QuerySecurityPackageInfo()| is called.
95   //
96   // |package_info| is assigned to |*pkgInfo| in |QuerySecurityPackageInfo|.
97   // The lifetime of |*package_info| should last at least until the matching
98   // |QuerySecurityPackageInfo()| is called.
99   void ExpectQuerySecurityPackageInfo(SECURITY_STATUS response_code,
100                                       PSecPkgInfoW package_info);
101 
102  private:
103   struct PackageQuery {
104     SECURITY_STATUS response_code;
105     PSecPkgInfoW package_info;
106   };
107 
108   // expected_package_queries contains an ordered list of expected
109   // |QuerySecurityPackageInfo()| calls and the return values for those
110   // calls.
111   std::list<PackageQuery> expected_package_queries_;
112 
113   // Set of packages which should be freed.
114   std::set<PSecPkgInfoW> expected_freed_packages_;
115 
116   // These sets keep track of active credentials and contexts.
117   std::set<CredHandle> active_credentials_;
118   std::set<CtxtHandle> active_contexts_;
119 };
120 
121 using MockAuthLibrary = MockSSPILibrary;
122 
123 }  // namespace net
124 
125 #endif  // NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_
126