blob: 42613edebeb52a7958924b61f6dc194f768e3e0f [file] [log] [blame]
// Copyright 2017 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/reporting/reporting_service.h"
#include <memory>
#include <string>
#include "base/memory/ptr_util.h"
#include "base/time/tick_clock.h"
#include "base/values.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_policy.h"
#include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_service.h"
#include "net/reporting/reporting_test_util.h"
#include "net/test/test_with_scoped_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class ReportingServiceTest : public TestWithScopedTaskEnvironment {
protected:
const GURL kUrl_ = GURL("https://origin/path");
const url::Origin kOrigin_ = url::Origin::Create(kUrl_);
const GURL kEndpoint_ = GURL("https://endpoint/");
const std::string kUserAgent_ = "Mozilla/1.0";
const std::string kGroup_ = "group";
const std::string kType_ = "type";
ReportingServiceTest()
: context_(
new TestReportingContext(&clock_, &tick_clock_, ReportingPolicy())),
service_(
ReportingService::CreateForTesting(base::WrapUnique(context_))) {}
TestReportingContext* context() { return context_; }
ReportingService* service() { return service_.get(); }
private:
base::SimpleTestClock clock_;
base::SimpleTestTickClock tick_clock_;
TestReportingContext* context_;
std::unique_ptr<ReportingService> service_;
};
TEST_F(ReportingServiceTest, QueueReport) {
service()->QueueReport(kUrl_, kUserAgent_, kGroup_, kType_,
std::make_unique<base::DictionaryValue>(), 0);
std::vector<const ReportingReport*> reports;
context()->cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl_, reports[0]->url);
EXPECT_EQ(kUserAgent_, reports[0]->user_agent);
EXPECT_EQ(kGroup_, reports[0]->group);
EXPECT_EQ(kType_, reports[0]->type);
}
TEST_F(ReportingServiceTest, ProcessHeader) {
service()->ProcessHeader(kUrl_, "{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400}");
const ReportingClient* client =
FindClientInCache(context()->cache(), kOrigin_, kEndpoint_);
ASSERT_TRUE(client != nullptr);
EXPECT_EQ(kOrigin_, client->origin);
EXPECT_EQ(kEndpoint_, client->endpoint);
EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains);
EXPECT_EQ(kGroup_, client->group);
EXPECT_EQ(context()->tick_clock()->NowTicks() + base::TimeDelta::FromDays(1),
client->expires);
}
TEST_F(ReportingServiceTest, ProcessHeader_TooLong) {
const std::string header_too_long =
"{\"endpoints\":[{\"url\":\"" + kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400," +
"\"junk\":\"" + std::string(32 * 1024, 'a') + "\"}";
service()->ProcessHeader(kUrl_, header_too_long);
const ReportingClient* client =
FindClientInCache(context()->cache(), kOrigin_, kEndpoint_);
EXPECT_FALSE(client);
}
TEST_F(ReportingServiceTest, ProcessHeader_TooDeep) {
const std::string header_too_deep = "{\"endpoints\":[{\"url\":\"" +
kEndpoint_.spec() +
"\"}],"
"\"group\":\"" +
kGroup_ +
"\","
"\"max_age\":86400," +
"\"junk\":[[[[[[[[[[]]]]]]]]]]}";
service()->ProcessHeader(kUrl_, header_too_deep);
const ReportingClient* client =
FindClientInCache(context()->cache(), kOrigin_, kEndpoint_);
EXPECT_FALSE(client);
}
} // namespace
} // namespace net