diff options
author | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-20 17:25:09 +0000 |
---|---|---|
committer | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-20 17:25:09 +0000 |
commit | 5c2471b3621992761ea946b3929c7fde86fb24ce (patch) | |
tree | 10e876bca6a07c90155d008e94ace2162b11e66d | |
parent | dd224e13b5371d1d313424247eea37f5b0d20acb (diff) | |
download | chromium_src-5c2471b3621992761ea946b3929c7fde86fb24ce.zip chromium_src-5c2471b3621992761ea946b3929c7fde86fb24ce.tar.gz chromium_src-5c2471b3621992761ea946b3929c7fde86fb24ce.tar.bz2 |
This CL moves the MockSSPILibrary out of http_auth_sspi_win_unittests.cc and into it's own file at mock_http_auth_sspi_win.[h|cc].
I did this in preparation of another CL which uses the mock SSPI library in http_auth_handler_negotiate_win_unittest.cc.
There are no behavior changes introduced in this CL.
BUG=None
TEST=net_unittests.exe --gtest_filter="*HttpAuthSSPI*"
Review URL: http://codereview.chromium.org/1600032
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@45046 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/http/http_auth_sspi_win_unittest.cc | 161 | ||||
-rw-r--r-- | net/http/mock_sspi_library_win.cc | 92 | ||||
-rw-r--r-- | net/http/mock_sspi_library_win.h | 111 | ||||
-rw-r--r-- | net/net.gyp | 2 |
4 files changed, 209 insertions, 157 deletions
diff --git a/net/http/http_auth_sspi_win_unittest.cc b/net/http/http_auth_sspi_win_unittest.cc index 7690664..fdef793 100644 --- a/net/http/http_auth_sspi_win_unittest.cc +++ b/net/http/http_auth_sspi_win_unittest.cc @@ -1,13 +1,11 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <list> -#include <set> - #include "base/basictypes.h" #include "net/base/net_errors.h" #include "net/http/http_auth_sspi_win.h" +#include "net/http/mock_sspi_library_win.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { @@ -24,157 +22,6 @@ void MatchDomainUserAfterSplit(const std::wstring& combined, EXPECT_EQ(expected_user, actual_user); } -class MockSSPILibrary : public SSPILibrary { - public: - MockSSPILibrary() {} - virtual ~MockSSPILibrary() {} - - virtual SECURITY_STATUS AcquireCredentialsHandle(LPWSTR pszPrincipal, - LPWSTR pszPackage, - unsigned long fCredentialUse, - void* pvLogonId, - void* pvAuthData, - SEC_GET_KEY_FN pGetKeyFn, - void* pvGetKeyArgument, - PCredHandle phCredential, - PTimeStamp ptsExpiry) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } - - virtual SECURITY_STATUS InitializeSecurityContext(PCredHandle phCredential, - PCtxtHandle phContext, - SEC_WCHAR* pszTargetName, - unsigned long fContextReq, - unsigned long Reserved1, - unsigned long TargetDataRep, - PSecBufferDesc pInput, - unsigned long Reserved2, - PCtxtHandle phNewContext, - PSecBufferDesc pOutput, - unsigned long* contextAttr, - PTimeStamp ptsExpiry) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } - - virtual SECURITY_STATUS QuerySecurityPackageInfo(LPWSTR pszPackageName, - PSecPkgInfoW *pkgInfo) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } - - virtual SECURITY_STATUS FreeCredentialsHandle(PCredHandle phCredential) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } - - virtual SECURITY_STATUS DeleteSecurityContext(PCtxtHandle phContext) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } - - virtual SECURITY_STATUS FreeContextBuffer(PVOID pvContextBuffer) { - ADD_FAILURE(); - return ERROR_CALL_NOT_IMPLEMENTED; - } -}; - -class ExpectedPackageQuerySSPILibrary : public MockSSPILibrary { - public: - ExpectedPackageQuerySSPILibrary() {} - virtual ~ExpectedPackageQuerySSPILibrary() { - EXPECT_TRUE(expected_package_queries_.empty()); - EXPECT_TRUE(expected_freed_packages_.empty()); - } - - // Establishes an expectation for a |QuerySecurityPackageInfo()| call. - // - // Each expectation established by |ExpectSecurityQueryPackageInfo()| must be - // matched by a call to |QuerySecurityPackageInfo()| during the lifetime of - // the MockSSPILibrary. The |expected_package| argument must equal the - // |*pszPackageName| argument to |QuerySecurityPackageInfo()| for there to be - // a match. The expectations also establish an explicit ordering. - // - // For example, this sequence will be successful. - // ExpectedPackageQuerySSPILibrary lib; - // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) - // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) - // lib.QuerySecurityPackageInfo(L"NTLM", ...) - // lib.QuerySecurityPackageInfo(L"Negotiate", ...) - // - // This sequence will fail since the queries do not occur in the order - // established by the expectations. - // ExpectedPackageQuerySSPILibrary lib; - // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) - // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) - // lib.QuerySecurityPackageInfo(L"Negotiate", ...) - // lib.QuerySecurityPackageInfo(L"NTLM", ...) - // - // This sequence will fail because there were not enough queries. - // ExpectedPackageQuerySSPILibrary lib; - // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) - // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) - // lib.QuerySecurityPackageInfo(L"NTLM", ...) - // - // |response_code| is used as the return value for - // |QuerySecurityPackageInfo()|. If |response_code| is SEC_E_OK, - // an expectation is also set for a call to |FreeContextBuffer()| after - // the matching |QuerySecurityPackageInfo()| is called. - // - // |package_info| is assigned to |*pkgInfo| in |QuerySecurityPackageInfo|. - // The lifetime of |*package_info| should last at least until the matching - // |QuerySecurityPackageInfo()| is called. - void ExpectQuerySecurityPackageInfo(const std::wstring& expected_package, - SECURITY_STATUS response_code, - PSecPkgInfoW package_info) { - PackageQuery package_query = {expected_package, response_code, - package_info}; - expected_package_queries_.push_back(package_query); - } - - // Queries security package information. This must be an expected call, - // see |ExpectQuerySecurityPackageInfo()| for more information. - virtual SECURITY_STATUS QuerySecurityPackageInfo(LPWSTR pszPackageName, - PSecPkgInfoW* pkgInfo) { - EXPECT_TRUE(!expected_package_queries_.empty()); - PackageQuery package_query = expected_package_queries_.front(); - expected_package_queries_.pop_front(); - std::wstring actual_package(pszPackageName); - EXPECT_EQ(package_query.expected_package, actual_package); - *pkgInfo = package_query.package_info; - if (package_query.response_code == SEC_E_OK) - expected_freed_packages_.insert(package_query.package_info); - return package_query.response_code; - } - - // Frees the context buffer. This should be called after a successful call - // of |QuerySecurityPackageInfo()|. - virtual SECURITY_STATUS FreeContextBuffer(PVOID pvContextBuffer) { - PSecPkgInfoW package_info = static_cast<PSecPkgInfoW>(pvContextBuffer); - std::set<PSecPkgInfoW>::iterator it = expected_freed_packages_.find( - package_info); - EXPECT_TRUE(it != expected_freed_packages_.end()); - expected_freed_packages_.erase(it); - return SEC_E_OK; - } - - private: - struct PackageQuery { - std::wstring expected_package; - SECURITY_STATUS response_code; - PSecPkgInfoW package_info; - }; - - // expected_package_queries contains an ordered list of expected - // |QuerySecurityPackageInfo()| calls and the return values for those - // calls. - std::list<PackageQuery> expected_package_queries_; - - // Set of packages which should be freed. - std::set<PSecPkgInfoW> expected_freed_packages_; -}; - } // namespace TEST(HttpAuthSSPITest, SplitUserAndDomain) { @@ -187,7 +34,7 @@ TEST(HttpAuthSSPITest, DetermineMaxTokenLength_Normal) { memset(&package_info, 0x0, sizeof(package_info)); package_info.cbMaxToken = 1337; - ExpectedPackageQuerySSPILibrary mock_library; + MockSSPILibrary mock_library; mock_library.ExpectQuerySecurityPackageInfo(L"NTLM", SEC_E_OK, &package_info); ULONG max_token_length = 100; int rv = DetermineMaxTokenLength(&mock_library, L"NTLM", &max_token_length); @@ -196,7 +43,7 @@ TEST(HttpAuthSSPITest, DetermineMaxTokenLength_Normal) { } TEST(HttpAuthSSPITest, DetermineMaxTokenLength_InvalidPackage) { - ExpectedPackageQuerySSPILibrary mock_library; + MockSSPILibrary mock_library; mock_library.ExpectQuerySecurityPackageInfo(L"Foo", SEC_E_SECPKG_NOT_FOUND, NULL); ULONG max_token_length = 100; diff --git a/net/http/mock_sspi_library_win.cc b/net/http/mock_sspi_library_win.cc new file mode 100644 index 0000000..de8f828 --- /dev/null +++ b/net/http/mock_sspi_library_win.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/http/mock_sspi_library_win.h" + +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { + +MockSSPILibrary::MockSSPILibrary() { +} + +MockSSPILibrary::~MockSSPILibrary() { + EXPECT_TRUE(expected_package_queries_.empty()); + EXPECT_TRUE(expected_freed_packages_.empty()); +} + +SECURITY_STATUS MockSSPILibrary::AcquireCredentialsHandle( + LPWSTR pszPrincipal, + LPWSTR pszPackage, + unsigned long fCredentialUse, + void* pvLogonId, + void* pvAuthData, + SEC_GET_KEY_FN pGetKeyFn, + void* pvGetKeyArgument, + PCredHandle phCredential, + PTimeStamp ptsExpiry) { + ADD_FAILURE(); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +SECURITY_STATUS MockSSPILibrary::InitializeSecurityContext( + PCredHandle phCredential, + PCtxtHandle phContext, + SEC_WCHAR* pszTargetName, + unsigned long fContextReq, + unsigned long Reserved1, + unsigned long TargetDataRep, + PSecBufferDesc pInput, + unsigned long Reserved2, + PCtxtHandle phNewContext, + PSecBufferDesc pOutput, + unsigned long* contextAttr, + PTimeStamp ptsExpiry) { + ADD_FAILURE(); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +SECURITY_STATUS MockSSPILibrary::QuerySecurityPackageInfo( + LPWSTR pszPackageName, PSecPkgInfoW *pkgInfo) { + EXPECT_TRUE(!expected_package_queries_.empty()); + PackageQuery package_query = expected_package_queries_.front(); + expected_package_queries_.pop_front(); + std::wstring actual_package(pszPackageName); + EXPECT_EQ(package_query.expected_package, actual_package); + *pkgInfo = package_query.package_info; + if (package_query.response_code == SEC_E_OK) + expected_freed_packages_.insert(package_query.package_info); + return package_query.response_code; +} + +SECURITY_STATUS MockSSPILibrary::FreeCredentialsHandle( + PCredHandle phCredential) { + ADD_FAILURE(); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +SECURITY_STATUS MockSSPILibrary::DeleteSecurityContext(PCtxtHandle phContext) { + ADD_FAILURE(); + return ERROR_CALL_NOT_IMPLEMENTED; +} + +SECURITY_STATUS MockSSPILibrary::FreeContextBuffer(PVOID pvContextBuffer) { + PSecPkgInfoW package_info = static_cast<PSecPkgInfoW>(pvContextBuffer); + std::set<PSecPkgInfoW>::iterator it = expected_freed_packages_.find( + package_info); + EXPECT_TRUE(it != expected_freed_packages_.end()); + expected_freed_packages_.erase(it); + return SEC_E_OK; +} + +void MockSSPILibrary::ExpectQuerySecurityPackageInfo( + const std::wstring& expected_package, + SECURITY_STATUS response_code, + PSecPkgInfoW package_info) { + PackageQuery package_query = {expected_package, response_code, + package_info}; + expected_package_queries_.push_back(package_query); +} + +} // namespace net diff --git a/net/http/mock_sspi_library_win.h b/net/http/mock_sspi_library_win.h new file mode 100644 index 0000000..eca3eb5 --- /dev/null +++ b/net/http/mock_sspi_library_win.h @@ -0,0 +1,111 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_ +#define NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_ + +#include <list> +#include <set> + +#include "net/http/http_auth_sspi_win.h" + +namespace net { + +// The MockSSPILibrary class is intended for unit tests which want to bypass +// the system SSPI library calls. +class MockSSPILibrary : public SSPILibrary { + public: + MockSSPILibrary(); + virtual ~MockSSPILibrary(); + + // TODO(cbentzel): Only QuerySecurityPackageInfo and FreeContextBuffer + // are properly handled currently. + // SSPILibrary methods: + virtual SECURITY_STATUS AcquireCredentialsHandle(LPWSTR pszPrincipal, + LPWSTR pszPackage, + unsigned long fCredentialUse, + void* pvLogonId, + void* pvAuthData, + SEC_GET_KEY_FN pGetKeyFn, + void* pvGetKeyArgument, + PCredHandle phCredential, + PTimeStamp ptsExpiry); + virtual SECURITY_STATUS InitializeSecurityContext(PCredHandle phCredential, + PCtxtHandle phContext, + SEC_WCHAR* pszTargetName, + unsigned long fContextReq, + unsigned long Reserved1, + unsigned long TargetDataRep, + PSecBufferDesc pInput, + unsigned long Reserved2, + PCtxtHandle phNewContext, + PSecBufferDesc pOutput, + unsigned long* contextAttr, + PTimeStamp ptsExpiry); + virtual SECURITY_STATUS QuerySecurityPackageInfo(LPWSTR pszPackageName, + PSecPkgInfoW *pkgInfo); + virtual SECURITY_STATUS FreeCredentialsHandle(PCredHandle phCredential); + virtual SECURITY_STATUS DeleteSecurityContext(PCtxtHandle phContext); + virtual SECURITY_STATUS FreeContextBuffer(PVOID pvContextBuffer); + + // Establishes an expectation for a |QuerySecurityPackageInfo()| call. + // + // Each expectation established by |ExpectSecurityQueryPackageInfo()| must be + // matched by a call to |QuerySecurityPackageInfo()| during the lifetime of + // the MockSSPILibrary. The |expected_package| argument must equal the + // |*pszPackageName| argument to |QuerySecurityPackageInfo()| for there to be + // a match. The expectations also establish an explicit ordering. + // + // For example, this sequence will be successful. + // MockSSPILibrary lib; + // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) + // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) + // lib.QuerySecurityPackageInfo(L"NTLM", ...) + // lib.QuerySecurityPackageInfo(L"Negotiate", ...) + // + // This sequence will fail since the queries do not occur in the order + // established by the expectations. + // MockSSPILibrary lib; + // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) + // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) + // lib.QuerySecurityPackageInfo(L"Negotiate", ...) + // lib.QuerySecurityPackageInfo(L"NTLM", ...) + // + // This sequence will fail because there were not enough queries. + // MockSSPILibrary lib; + // lib.ExpectQuerySecurityPackageInfo(L"NTLM", ...) + // lib.ExpectQuerySecurityPackageInfo(L"Negotiate", ...) + // lib.QuerySecurityPackageInfo(L"NTLM", ...) + // + // |response_code| is used as the return value for + // |QuerySecurityPackageInfo()|. If |response_code| is SEC_E_OK, + // an expectation is also set for a call to |FreeContextBuffer()| after + // the matching |QuerySecurityPackageInfo()| is called. + // + // |package_info| is assigned to |*pkgInfo| in |QuerySecurityPackageInfo|. + // The lifetime of |*package_info| should last at least until the matching + // |QuerySecurityPackageInfo()| is called. + void ExpectQuerySecurityPackageInfo(const std::wstring& expected_package, + SECURITY_STATUS response_code, + PSecPkgInfoW package_info); + + private: + struct PackageQuery { + std::wstring expected_package; + SECURITY_STATUS response_code; + PSecPkgInfoW package_info; + }; + + // expected_package_queries contains an ordered list of expected + // |QuerySecurityPackageInfo()| calls and the return values for those + // calls. + std::list<PackageQuery> expected_package_queries_; + + // Set of packages which should be freed. + std::set<PSecPkgInfoW> expected_freed_packages_; +}; + +} // namespace net + +#endif // NET_HTTP_MOCK_SSPI_LIBRARY_WIN_H_ diff --git a/net/net.gyp b/net/net.gyp index f0a632b..2a5dc4d 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -687,6 +687,8 @@ 'http/http_transaction_unittest.h', 'http/http_util_unittest.cc', 'http/http_vary_data_unittest.cc', + 'http/mock_sspi_library_win.h', + 'http/mock_sspi_library_win.cc', 'proxy/init_proxy_resolver_unittest.cc', 'proxy/mock_proxy_resolver.h', 'proxy/proxy_bypass_rules_unittest.cc', |