[email protected] | 5c2471b | 2010-04-20 17:25:09 | [diff] [blame] | 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 | |
| 9 | namespace net { |
| 10 | |
| 11 | MockSSPILibrary::MockSSPILibrary() { |
| 12 | } |
| 13 | |
| 14 | MockSSPILibrary::~MockSSPILibrary() { |
| 15 | EXPECT_TRUE(expected_package_queries_.empty()); |
| 16 | EXPECT_TRUE(expected_freed_packages_.empty()); |
| 17 | } |
| 18 | |
| 19 | SECURITY_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) { |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 29 | // Fill in phCredential with arbitrary value. |
| 30 | phCredential->dwLower = phCredential->dwUpper = ((ULONG_PTR) ((INT_PTR)0)); |
| 31 | return SEC_E_OK; |
[email protected] | 5c2471b | 2010-04-20 17:25:09 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | SECURITY_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) { |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 47 | // Fill in the outbound buffer with garbage data. |
| 48 | PSecBuffer out_buffer = pOutput->pBuffers; |
| 49 | out_buffer->cbBuffer = 2; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 50 | uint8_t* buf = reinterpret_cast<uint8_t*>(out_buffer->pvBuffer); |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 51 | buf[0] = 0xAB; |
| 52 | buf[1] = 0xBA; |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 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)); |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 57 | return SEC_E_OK; |
[email protected] | 5c2471b | 2010-04-20 17:25:09 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | SECURITY_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 | |
| 73 | SECURITY_STATUS MockSSPILibrary::FreeCredentialsHandle( |
| 74 | PCredHandle phCredential) { |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 75 | EXPECT_TRUE(phCredential->dwLower == ((ULONG_PTR) ((INT_PTR) 0))); |
[email protected] | 60c36363 | 2014-07-24 11:21:44 | [diff] [blame] | 76 | EXPECT_TRUE(phCredential->dwUpper == ((ULONG_PTR) ((INT_PTR) 0))); |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 77 | SecInvalidateHandle(phCredential); |
| 78 | return SEC_E_OK; |
[email protected] | 5c2471b | 2010-04-20 17:25:09 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | SECURITY_STATUS MockSSPILibrary::DeleteSecurityContext(PCtxtHandle phContext) { |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 82 | EXPECT_TRUE(phContext->dwLower == ((ULONG_PTR) ((INT_PTR) 0))); |
[email protected] | 60c36363 | 2014-07-24 11:21:44 | [diff] [blame] | 83 | EXPECT_TRUE(phContext->dwUpper == ((ULONG_PTR) ((INT_PTR) 0))); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 84 | SecInvalidateHandle(phContext); |
| 85 | return SEC_E_OK; |
[email protected] | 5c2471b | 2010-04-20 17:25:09 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SECURITY_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 | |
| 97 | void 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 |