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#include "net/http/mock_sspi_library_win.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace net {
10
11MockSSPILibrary::MockSSPILibrary() {
12}
13
14MockSSPILibrary::~MockSSPILibrary() {
15  EXPECT_TRUE(expected_package_queries_.empty());
16  EXPECT_TRUE(expected_freed_packages_.empty());
17}
18
19SECURITY_STATUS MockSSPILibrary::AcquireCredentialsHandle(
20    LPWSTR pszPrincipal,
21    LPWSTR pszPackage,
22    unsigned long fCredentialUse,
23    void* pvLogonId,
24    void* pvAuthData,
25    SEC_GET_KEY_FN pGetKeyFn,
26    void* pvGetKeyArgument,
27    PCredHandle phCredential,
28    PTimeStamp ptsExpiry) {
29  // Fill in phCredential with arbitrary value.
30  phCredential->dwLower = phCredential->dwUpper = ((ULONG_PTR) ((INT_PTR)0));
31  return SEC_E_OK;
32}
33
34SECURITY_STATUS MockSSPILibrary::InitializeSecurityContext(
35    PCredHandle phCredential,
36    PCtxtHandle phContext,
37    SEC_WCHAR* pszTargetName,
38    unsigned long fContextReq,
39    unsigned long Reserved1,
40    unsigned long TargetDataRep,
41    PSecBufferDesc pInput,
42    unsigned long Reserved2,
43    PCtxtHandle phNewContext,
44    PSecBufferDesc pOutput,
45    unsigned long* contextAttr,
46    PTimeStamp ptsExpiry) {
47  // Fill in the outbound buffer with garbage data.
48  PSecBuffer out_buffer = pOutput->pBuffers;
49  out_buffer->cbBuffer = 2;
50  uint8* buf = reinterpret_cast<uint8 *>(out_buffer->pvBuffer);
51  buf[0] = 0xAB;
52  buf[1] = 0xBA;
53
54  // Fill in phNewContext with arbitrary value if it's invalid.
55  if (phNewContext != phContext)
56    phNewContext->dwLower = phNewContext->dwUpper = ((ULONG_PTR) ((INT_PTR)0));
57  return SEC_E_OK;
58}
59
60SECURITY_STATUS MockSSPILibrary::QuerySecurityPackageInfo(
61    LPWSTR pszPackageName, PSecPkgInfoW *pkgInfo) {
62  EXPECT_TRUE(!expected_package_queries_.empty());
63  PackageQuery package_query = expected_package_queries_.front();
64  expected_package_queries_.pop_front();
65  std::wstring actual_package(pszPackageName);
66  EXPECT_EQ(package_query.expected_package, actual_package);
67  *pkgInfo = package_query.package_info;
68  if (package_query.response_code == SEC_E_OK)
69    expected_freed_packages_.insert(package_query.package_info);
70  return package_query.response_code;
71}
72
73SECURITY_STATUS MockSSPILibrary::FreeCredentialsHandle(
74    PCredHandle phCredential) {
75  EXPECT_TRUE(phCredential->dwLower == ((ULONG_PTR) ((INT_PTR) 0)));
76  EXPECT_TRUE(phCredential->dwUpper == ((ULONG_PTR) ((INT_PTR) 0)));
77  SecInvalidateHandle(phCredential);
78  return SEC_E_OK;
79}
80
81SECURITY_STATUS MockSSPILibrary::DeleteSecurityContext(PCtxtHandle phContext) {
82  EXPECT_TRUE(phContext->dwLower == ((ULONG_PTR) ((INT_PTR) 0)));
83  EXPECT_TRUE(phContext->dwUpper == ((ULONG_PTR) ((INT_PTR) 0)));
84  SecInvalidateHandle(phContext);
85  return SEC_E_OK;
86}
87
88SECURITY_STATUS MockSSPILibrary::FreeContextBuffer(PVOID pvContextBuffer) {
89  PSecPkgInfoW package_info = static_cast<PSecPkgInfoW>(pvContextBuffer);
90  std::set<PSecPkgInfoW>::iterator it = expected_freed_packages_.find(
91      package_info);
92  EXPECT_TRUE(it != expected_freed_packages_.end());
93  expected_freed_packages_.erase(it);
94  return SEC_E_OK;
95}
96
97void MockSSPILibrary::ExpectQuerySecurityPackageInfo(
98    const std::wstring& expected_package,
99    SECURITY_STATUS response_code,
100    PSecPkgInfoW package_info) {
101  PackageQuery package_query = {expected_package, response_code,
102                                package_info};
103  expected_package_queries_.push_back(package_query);
104}
105
106}  // namespace net
107