blob: ba0114a8b708b22fb88ae174fc7b3c8029546a43 [file] [log] [blame]
// Copyright 2019 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 "components/safe_browsing/realtime/url_lookup_service.h"
#include "base/test/task_environment.h"
#include "content/public/test/browser_task_environment.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/platform_test.h"
namespace safe_browsing {
class RealTimeUrlLookupServiceTest : public PlatformTest {
public:
RealTimeUrlLookupServiceTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
void SetUp() override {
PlatformTest::SetUp();
test_shared_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
rt_service_ =
std::make_unique<RealTimeUrlLookupService>(test_shared_loader_factory_);
}
void HandleLookupError() { rt_service_->HandleLookupError(); }
void HandleLookupSuccess() { rt_service_->HandleLookupSuccess(); }
bool IsInBackoffMode() { return rt_service_->IsInBackoffMode(); }
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
std::unique_ptr<RealTimeUrlLookupService> rt_service_;
content::BrowserTaskEnvironment task_environment_;
};
TEST_F(RealTimeUrlLookupServiceTest, TestBackoffAndTimerReset) {
// Not in backoff at the beginning.
ASSERT_FALSE(IsInBackoffMode());
// Failure 1: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Failure 2: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Failure 3: Entered backoff.
HandleLookupError();
EXPECT_TRUE(IsInBackoffMode());
// Backoff not reset after 1 second.
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(1));
EXPECT_TRUE(IsInBackoffMode());
// Backoff not reset after 299 seconds.
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(298));
EXPECT_TRUE(IsInBackoffMode());
// Backoff should have been reset after 300 seconds.
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(1));
EXPECT_FALSE(IsInBackoffMode());
}
TEST_F(RealTimeUrlLookupServiceTest, TestBackoffAndLookupSuccessReset) {
// Not in backoff at the beginning.
ASSERT_FALSE(IsInBackoffMode());
// Failure 1: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Lookup success resets the backoff counter.
HandleLookupSuccess();
EXPECT_FALSE(IsInBackoffMode());
// Failure 1: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Failure 2: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Lookup success resets the backoff counter.
HandleLookupSuccess();
EXPECT_FALSE(IsInBackoffMode());
// Failure 1: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Failure 2: No backoff.
HandleLookupError();
EXPECT_FALSE(IsInBackoffMode());
// Failure 3: Entered backoff.
HandleLookupError();
EXPECT_TRUE(IsInBackoffMode());
// Lookup success resets the backoff counter.
HandleLookupSuccess();
EXPECT_FALSE(IsInBackoffMode());
}
TEST_F(RealTimeUrlLookupServiceTest, TestGetSBThreatTypeForRTThreatType) {
EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE,
RealTimeUrlLookupService::GetSBThreatTypeForRTThreatType(
RTLookupResponse::ThreatInfo::WEB_MALWARE));
EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING,
RealTimeUrlLookupService::GetSBThreatTypeForRTThreatType(
RTLookupResponse::ThreatInfo::SOCIAL_ENGINEERING));
EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED,
RealTimeUrlLookupService::GetSBThreatTypeForRTThreatType(
RTLookupResponse::ThreatInfo::UNWANTED_SOFTWARE));
EXPECT_EQ(SB_THREAT_TYPE_BILLING,
RealTimeUrlLookupService::GetSBThreatTypeForRTThreatType(
RTLookupResponse::ThreatInfo::UNCLEAR_BILLING));
}
} // namespace safe_browsing