1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/reporting/client/report_queue_factory.h"
#include <memory>
#include <string>
#include <utility>
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/test/task_environment.h"
#include "components/reporting/client/mock_report_queue.h"
#include "components/reporting/client/mock_report_queue_provider.h"
#include "components/reporting/client/report_queue.h"
#include "components/reporting/client/report_queue_provider_test_helper.h"
#include "components/reporting/util/rate_limiter_interface.h"
#include "components/reporting/util/test_support_callbacks.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::HasSubstr;
using ::testing::IsNull;
using ::testing::NiceMock;
using ::testing::NotNull;
using ::testing::Return;
namespace reporting {
class MockRateLimiter : public RateLimiterInterface {
public:
MOCK_METHOD(bool, Acquire, (size_t event_size), (override));
};
class MockReportQueueConsumer {
public:
MockReportQueueConsumer() = default;
void SetReportQueue(test::TestCallbackWaiter* waiter,
std::unique_ptr<ReportQueue> report_queue) {
report_queue_ = std::move(report_queue);
if (waiter) {
waiter->Signal();
}
}
base::OnceCallback<void(std::unique_ptr<ReportQueue>)> GetReportQueueSetter(
test::TestCallbackWaiter* waiter) {
return base::BindOnce(&MockReportQueueConsumer::SetReportQueue,
weak_factory_.GetWeakPtr(), base::Unretained(waiter));
}
ReportQueue* GetReportQueue() const { return report_queue_.get(); }
private:
std::unique_ptr<ReportQueue> report_queue_;
base::WeakPtrFactory<MockReportQueueConsumer> weak_factory_{this};
};
class ReportQueueFactoryTest : public ::testing::Test {
protected:
ReportQueueFactoryTest() = default;
void SetUp() override {
consumer_ = std::make_unique<MockReportQueueConsumer>();
helper_ = std::make_unique<test::ReportQueueProviderTestHelper>();
}
void TearDown() override { helper_.reset(); }
const Destination destination_ = Destination::UPLOAD_EVENTS;
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<MockReportQueueConsumer> consumer_;
std::unique_ptr<test::ReportQueueProviderTestHelper> helper_;
};
TEST_F(ReportQueueFactoryTest, CreateAndGetQueue) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice, .destination = destination_}),
consumer_->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(1);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateQueueWithInvalidConfig) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice,
.destination = Destination::UNDEFINED_DESTINATION}),
consumer_->GetReportQueueSetter(nullptr));
// Expect failure before it gets to the report queue provider
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(0);
// We do not expect the report queue to be existing in the consumer.
EXPECT_FALSE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateAndGetQueueWithRateLimiter) {
EXPECT_FALSE(consumer_->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice, .destination = destination_})
.SetRateLimiter(std::make_unique<MockRateLimiter>()),
consumer_->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(1);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateAndGetQueueWithValidReservedSpace) {
EXPECT_FALSE(consumer_->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
ReportQueueFactory::Create(
ReportQueueConfiguration::Create({.event_type = EventType::kDevice,
.destination = destination_,
.reserved_space = 12345L}),
consumer_->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(1);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateQueueWithInvalidReservedSpace) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
ReportQueueFactory::Create(
ReportQueueConfiguration::Create({.event_type = EventType::kDevice,
.destination = destination_,
.reserved_space = -1L}),
consumer_->GetReportQueueSetter(nullptr));
// Expect failure before it gets to the report queue provider
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(0);
// We do not expect the report queue to be existing in the consumer.
EXPECT_FALSE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateAndGetQueueWithSource) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
SourceInfo source_info;
source_info.set_source(SourceInfo::ASH);
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)),
consumer_->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(1);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateAndGetQueueWithSourceVersion) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
SourceInfo source_info;
source_info.set_source(SourceInfo::ASH);
source_info.set_source_version("1.0.0");
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)),
consumer_->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(1);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateQueueWithSourceVersionAndInvalidSource) {
// Initially the queue must be an uninitialized unique_ptr
EXPECT_FALSE(consumer_->GetReportQueue());
SourceInfo source_info;
source_info.set_source(SourceInfo::SOURCE_UNSPECIFIED);
source_info.set_source_version("1.0.0");
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)),
consumer_->GetReportQueueSetter(nullptr));
// Expect failure before it gets to the report queue provider
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(0);
// We do not expect the report queue to be existing in the consumer.
EXPECT_FALSE(consumer_->GetReportQueue());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueue) {
// Mock internal implementation to use a MockReportQueue
helper_->mock_provider()
->ExpectCreateNewSpeculativeQueueAndReturnNewMockQueue(1);
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice, .destination = destination_}));
EXPECT_THAT(report_queue, NotNull());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithValidReservedSpace) {
// Mock internal implementation to use a MockReportQueue
helper_->mock_provider()
->ExpectCreateNewSpeculativeQueueAndReturnNewMockQueue(1);
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create({.event_type = EventType::kDevice,
.destination = destination_,
.reserved_space = 12345L}));
EXPECT_THAT(report_queue, NotNull());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithInvalidReservedSpace) {
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create({.event_type = EventType::kDevice,
.destination = destination_,
.reserved_space = -1L}));
EXPECT_THAT(report_queue, IsNull());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithInvalidConfig) {
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice,
.destination = Destination::UNDEFINED_DESTINATION}));
EXPECT_THAT(report_queue, IsNull());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithSource) {
// Mock internal implementation to use a MockReportQueue
helper_->mock_provider()
->ExpectCreateNewSpeculativeQueueAndReturnNewMockQueue(1);
SourceInfo source_info;
source_info.set_source(SourceInfo::ASH);
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)));
EXPECT_THAT(report_queue, NotNull());
}
TEST_F(ReportQueueFactoryTest, CreateSpeculativeQueueWithSourceVersion) {
// Mock internal implementation to use a MockReportQueue
helper_->mock_provider()
->ExpectCreateNewSpeculativeQueueAndReturnNewMockQueue(1);
SourceInfo source_info;
source_info.set_source(SourceInfo::ASH);
source_info.set_source_version("1.0.0");
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)));
EXPECT_THAT(report_queue, NotNull());
}
TEST_F(ReportQueueFactoryTest,
CreateSpeculativeQueueWithSourceVersionAndInvalidSource) {
SourceInfo source_info;
source_info.set_source(SourceInfo::SOURCE_UNSPECIFIED);
source_info.set_source_version("1.0.0");
const auto report_queue = ReportQueueFactory::CreateSpeculativeReportQueue(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_})
.SetSourceInfo(std::move(source_info)));
EXPECT_THAT(report_queue, IsNull());
}
// Tests if two consumers use the same provider and create two queues.
TEST_F(ReportQueueFactoryTest, SameProviderForMultipleThreads) {
auto consumer2 = std::make_unique<MockReportQueueConsumer>();
EXPECT_FALSE(consumer_->GetReportQueue());
EXPECT_FALSE(consumer2->GetReportQueue());
{
test::TestCallbackAutoWaiter set_waiter;
set_waiter.Attach();
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kDevice, .destination = destination_}),
consumer_->GetReportQueueSetter(&set_waiter));
ReportQueueFactory::Create(
ReportQueueConfiguration::Create(
{.event_type = EventType::kUser, .destination = destination_}),
consumer2->GetReportQueueSetter(&set_waiter));
EXPECT_CALL(*helper_->mock_provider(), OnInitCompletedMock()).Times(1);
helper_->mock_provider()->ExpectCreateNewQueueAndReturnNewMockQueue(2);
}
// We expect the report queue to be existing in the consumer.
EXPECT_TRUE(consumer_->GetReportQueue());
// And for the 2nd consumer
EXPECT_TRUE(consumer2->GetReportQueue());
}
} // namespace reporting
|