| Index: components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc
|
| diff --git a/components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc b/components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc
|
| index 756fd96393f83cc17146dabf028ef3e061ca7ac8..1a5533f2e691da7c41d582dadfc7cef0edcb35b1 100644
|
| --- a/components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc
|
| +++ b/components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler_unittest.cc
|
| @@ -5,161 +5,137 @@
|
|
|
| #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
|
|
|
| +#include "base/md5.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/strings/string16.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "base/time/time.h"
|
| +#include "components/data_reduction_proxy/browser/data_reduction_proxy_params_test_utils.h"
|
| #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h"
|
| #include "net/base/auth.h"
|
| +#include "net/base/host_port_pair.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| +#include "url/gurl.h"
|
|
|
| -namespace data_reduction_proxy {
|
| -
|
| -namespace {
|
|
|
| -const char kInvalidTestRealm[] = "invalid-test-realm";
|
| -const char kTestChallenger[] = "https://test-challenger.com:443";
|
| -const char kTestRealm[] = "test-realm";
|
| -const char kTestToken[] = "test-token";
|
| -const char kTestUser[] = "fw-cookie";
|
|
|
| +namespace {
|
| +const char kChromeProxyHeader[] = "chrome-proxy";
|
| +const char kOtherProxy[] = "testproxy:17";
|
| +
|
| +const char kTestKey[] = "test-key";
|
| +const char kVersion[] = "0";
|
| +const char kExpectedCredentials[] = "96bd72ec4a050ba60981743d41787768";
|
| +const char kExpectedSession[] = "0-1633771873-1633771873-1633771873";
|
| +
|
| +const char kTestKey2[] = "test-key2";
|
| +const char kClient2[] = "foo";
|
| +const char kVersion2[] = "2";
|
| +const char kExpectedCredentials2[] = "c911fdb402f578787562cf7f00eda972";
|
| +const char kExpectedSession2[] = "0-1633771873-1633771873-1633771873";
|
| +const char kExpectedHeader2[] =
|
| + "ps=0-1633771873-1633771873-1633771873, "
|
| + "sid=c911fdb402f578787562cf7f00eda972, v=2, c=foo";
|
| +
|
| +const char kDataReductionProxyKey[] = "12345";
|
| } // namespace
|
|
|
| -// Test class that overrides underlying calls to see if an auth challenge is
|
| -// acceptible for the data reduction proxy, and to get a valid token if so.
|
| +
|
| +namespace data_reduction_proxy {
|
| +namespace {
|
| class TestDataReductionProxyAuthRequestHandler
|
| : public DataReductionProxyAuthRequestHandler {
|
| public:
|
| - TestDataReductionProxyAuthRequestHandler(int time_step_ms,
|
| - int64 initial_time_ms,
|
| - DataReductionProxySettings* settings)
|
| - : DataReductionProxyAuthRequestHandler(settings),
|
| - time_step_ms_(time_step_ms),
|
| - now_(base::TimeTicks() +
|
| - base::TimeDelta::FromMilliseconds(initial_time_ms)) {}
|
| - protected:
|
| - // Test implementation.
|
| - virtual bool IsAcceptableAuthChallenge(
|
| - net::AuthChallengeInfo* auth_info) OVERRIDE {
|
| - if (net::HostPortPair::FromString(
|
| - kTestChallenger).Equals(auth_info->challenger) &&
|
| - auth_info->realm == kTestRealm) {
|
| - return true;
|
| - }
|
| - return false;
|
| - }
|
| -
|
| - // Test implementation.
|
| - virtual base::string16 GetTokenForAuthChallenge(
|
| - net::AuthChallengeInfo* auth_info) OVERRIDE {
|
| - return base::ASCIIToUTF16(kTestToken);
|
| - }
|
| -
|
| - virtual base::TimeTicks Now() OVERRIDE {
|
| - now_ += base::TimeDelta::FromMilliseconds(time_step_ms_);
|
| - return now_;
|
| - }
|
| - int time_step_ms_;
|
| - base::TimeTicks now_;
|
| -};
|
| -
|
| -class DataReductionProxyAuthRequestHandlerTest : public testing::Test {
|
| - public:
|
| + TestDataReductionProxyAuthRequestHandler(
|
| + DataReductionProxyParams* params)
|
| + : DataReductionProxyAuthRequestHandler(params) {}
|
|
|
| - virtual void SetUp() OVERRIDE {
|
| - DataReductionProxySettingsTestBase::AddTestProxyToCommandLine();
|
| - settings_.reset(
|
| - new MockDataReductionProxySettings<DataReductionProxySettings>(
|
| - DataReductionProxyParams::kAllowed |
|
| - DataReductionProxyParams::kFallbackAllowed |
|
| - DataReductionProxyParams::kPromoAllowed));
|
| + virtual std::string GetDefaultKey() const OVERRIDE {
|
| + return kTestKey;
|
| }
|
|
|
| - // Checks that |PROCEED| was returned with expected user and password.
|
| - void ExpectProceed(
|
| - DataReductionProxyAuthRequestHandler::TryHandleResult result,
|
| - const base::string16& user,
|
| - const base::string16& password) {
|
| - base::string16 expected_user = base::ASCIIToUTF16(kTestUser);
|
| - base::string16 expected_password = base::ASCIIToUTF16(kTestToken);
|
| - EXPECT_EQ(DataReductionProxyAuthRequestHandler::TRY_HANDLE_RESULT_PROCEED,
|
| - result);
|
| - EXPECT_EQ(expected_user, user);
|
| - EXPECT_EQ(expected_password, password);
|
| + virtual base::Time Now() const OVERRIDE {
|
| + return base::Time::UnixEpoch();
|
| }
|
|
|
| - // Checks that |CANCEL| was returned.
|
| - void ExpectCancel(
|
| - DataReductionProxyAuthRequestHandler::TryHandleResult result,
|
| - const base::string16& user,
|
| - const base::string16& password) {
|
| - EXPECT_EQ(DataReductionProxyAuthRequestHandler::TRY_HANDLE_RESULT_CANCEL,
|
| - result);
|
| - EXPECT_EQ(base::string16(), user);
|
| - EXPECT_EQ(base::string16(), password);
|
| + virtual void RandBytes(void* output, size_t length) OVERRIDE {
|
| + char* c = static_cast<char*>(output);
|
| + for (size_t i = 0; i < length; ++i) {
|
| + c[i] = 'a';
|
| + }
|
| }
|
| +};
|
|
|
| - // Checks that |IGNORE| was returned.
|
| - void ExpectIgnore(
|
| - DataReductionProxyAuthRequestHandler::TryHandleResult result,
|
| - const base::string16& user,
|
| - const base::string16& password) {
|
| - EXPECT_EQ(DataReductionProxyAuthRequestHandler::TRY_HANDLE_RESULT_IGNORE,
|
| - result);
|
| - EXPECT_EQ(base::string16(), user);
|
| - EXPECT_EQ(base::string16(), password);
|
| - }
|
| +} // namespace
|
|
|
| - scoped_ptr<DataReductionProxySettings> settings_;
|
| +class DataReductionProxyAuthRequestHandlerTest : public testing::Test {
|
| };
|
|
|
| -TEST_F(DataReductionProxyAuthRequestHandlerTest,
|
| - CancelAfterSuccessiveAuthAttempts) {
|
| - DataReductionProxyAuthRequestHandler::auth_request_timestamp_ = 0;
|
| - DataReductionProxyAuthRequestHandler::back_to_back_failure_count_ = 0;
|
| - DataReductionProxyAuthRequestHandler::auth_token_invalidation_timestamp_ = 0;
|
| - scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
|
| - auth_info->realm = kTestRealm;
|
| - auth_info->challenger = net::HostPortPair::FromString(kTestChallenger);
|
| - TestDataReductionProxyAuthRequestHandler handler(
|
| - 499, 3600001, settings_.get());
|
| - base::string16 user, password;
|
| - DataReductionProxyAuthRequestHandler::TryHandleResult result =
|
| - handler.TryHandleAuthentication(auth_info.get(), &user, &password);
|
| - ExpectProceed(result, user, password);
|
| -
|
| - // Successive retries should also succeed up to a maximum count.
|
| - for (int i = 0; i < 5; ++i) {
|
| - user = base::string16();
|
| - password = base::string16();
|
| - result = handler.TryHandleAuthentication(auth_info.get(), &user, &password);
|
| - ExpectProceed(result, user, password);
|
| - }
|
| -
|
| - // Then another retry should fail.
|
| - user = base::string16();
|
| - password = base::string16();
|
| - result = handler.TryHandleAuthentication(auth_info.get(), &user, &password);
|
| - ExpectCancel(result, user, password);
|
| -
|
| - // After canceling, the next one should proceed.
|
| - user = base::string16();
|
| - password = base::string16();
|
| - result = handler.TryHandleAuthentication(auth_info.get(), &user, &password);
|
| - ExpectProceed(result, user, password);
|
| +TEST_F(DataReductionProxyAuthRequestHandlerTest, Authorization) {
|
| + scoped_ptr<TestDataReductionProxyParams> params;
|
| + params.reset(
|
| + new TestDataReductionProxyParams(
|
| + DataReductionProxyParams::kAllowed |
|
| + DataReductionProxyParams::kFallbackAllowed |
|
| + DataReductionProxyParams::kPromoAllowed,
|
| + TestDataReductionProxyParams::HAS_EVERYTHING &
|
| + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN));
|
| + TestDataReductionProxyAuthRequestHandler auth_handler(params.get());
|
| + auth_handler.Init();
|
| +#if defined(OS_ANDROID)
|
| + EXPECT_EQ(auth_handler.client_, "android");
|
| +#elif defined(OS_IOS)
|
| + EXPECT_EQ(auth_handler.client_, "ios");
|
| +else
|
| + EXPECT_EQ(auth_handler.client_, "");
|
| +#endif
|
| + EXPECT_EQ(kVersion, auth_handler.version_);
|
| + EXPECT_EQ(auth_handler.key_, kTestKey);
|
| + EXPECT_EQ(kExpectedCredentials, auth_handler.credentials_);
|
| + EXPECT_EQ(kExpectedSession, auth_handler.session_);
|
| +
|
| + // Now set a key.
|
| + auth_handler.SetKey(kTestKey2, kClient2, kVersion2);
|
| + EXPECT_EQ(kClient2, auth_handler.client_);
|
| + EXPECT_EQ(kVersion2, auth_handler.version_);
|
| + EXPECT_EQ(kTestKey2, auth_handler.key_);
|
| + EXPECT_EQ(kExpectedCredentials2, auth_handler.credentials_);
|
| + EXPECT_EQ(kExpectedSession2, auth_handler.session_);
|
| +
|
| +
|
| + // Don't write headers if the proxy is invalid.
|
| + net::HttpRequestHeaders headers;
|
| + auth_handler.MaybeAddRequestHeader(NULL, net::ProxyServer(), &headers);
|
| + EXPECT_FALSE(headers.HasHeader(kChromeProxyHeader));
|
| +
|
| + // Don't write headers with a valid proxy, that's not a data reduction proxy.
|
| + auth_handler.MaybeAddRequestHeader(
|
| + NULL,
|
| + net::ProxyServer::FromURI(kOtherProxy, net::ProxyServer::SCHEME_HTTP),
|
| + &headers);
|
| + EXPECT_FALSE(headers.HasHeader(kChromeProxyHeader));
|
| +
|
| + // Write headers with a valid data reduction proxy;
|
| + auth_handler.MaybeAddRequestHeader(
|
| + NULL,
|
| + net::ProxyServer::FromURI(
|
| + net::HostPortPair::FromURL(GURL(params->DefaultOrigin())).ToString(),
|
| + net::ProxyServer::SCHEME_HTTP),
|
| + &headers);
|
| + EXPECT_TRUE(headers.HasHeader(kChromeProxyHeader));
|
| + std::string header_value;
|
| + headers.GetHeader(kChromeProxyHeader, &header_value);
|
| + EXPECT_EQ(kExpectedHeader2, header_value);
|
| }
|
|
|
| -TEST_F(DataReductionProxyAuthRequestHandlerTest, Ignore) {
|
| - scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
|
| - auth_info->realm = kInvalidTestRealm;
|
| - auth_info->challenger = net::HostPortPair::FromString(kTestChallenger);
|
| - TestDataReductionProxyAuthRequestHandler handler(
|
| - 100, 3600001, settings_.get());
|
| - base::string16 user, password;
|
| - DataReductionProxyAuthRequestHandler::TryHandleResult result =
|
| - handler.TryHandleAuthentication(auth_info.get(), &user, &password);
|
| - ExpectIgnore(result, user, password);
|
| +TEST_F(DataReductionProxyAuthRequestHandlerTest, AuthHashForSalt) {
|
| + std::string salt = "8675309"; // Jenny's number to test the hash generator.
|
| + std::string salted_key = salt + kDataReductionProxyKey + salt;
|
| + base::string16 expected_hash = base::UTF8ToUTF16(base::MD5String(salted_key));
|
| + EXPECT_EQ(expected_hash,
|
| + DataReductionProxyAuthRequestHandler::AuthHashForSalt(
|
| + 8675309, kDataReductionProxyKey));
|
| }
|
|
|
| } // namespace data_reduction_proxy
|
|
|