mock_sspi_library_win.h 5.4 KB

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