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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2025 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/affiliations/core/browser/affiliation_fetcher_manager.h"
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/test/gmock_move_support.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "components/affiliations/core/browser/affiliation_api.pb.h"
#include "components/affiliations/core/browser/affiliation_fetcher_factory_impl.h"
#include "components/affiliations/core/browser/affiliation_fetcher_interface.h"
#include "components/affiliations/core/browser/fake_affiliation_api.h"
#include "components/affiliations/core/browser/hash_affiliation_fetcher.h"
#include "components/variations/scoped_variations_ids_provider.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace affiliations {
namespace {
constexpr char kNotExampleAndroidFacetURI[] =
"android://hash1234@com.example.not";
constexpr char kNotExampleWebFacetURI[] = "https://not.example.com";
constexpr char kExampleWebFacet1URI[] = "https://www.example.com";
constexpr char kExampleWebFacet2URI[] = "https://www.example.org";
constexpr char kExampleAndroidFacetURI[] = "android://hash@com.example";
constexpr AffiliationFetcherInterface::RequestInfo kRequestInfo{
.branding_info = true,
.change_password_info = true};
// Fetcher factory that will create |HashAffiliationFetcher| but avoid checking
// API keys since they aren't always available in unit tests. Instead, to test
// failed fetcher creation, this class allows setting a bool that will disallow
// fetcher creation.
class KeylessFetcherFactory : public AffiliationFetcherFactory {
public:
KeylessFetcherFactory();
~KeylessFetcherFactory() override;
std::unique_ptr<AffiliationFetcherInterface> CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
override;
void SetCanCreateFetcher(bool can_create_fetcher) {
can_create_fetcher_ = can_create_fetcher;
}
bool CanCreateFetcher() const override { return can_create_fetcher_; }
private:
bool can_create_fetcher_ = true;
};
KeylessFetcherFactory::KeylessFetcherFactory() = default;
KeylessFetcherFactory::~KeylessFetcherFactory() = default;
std::unique_ptr<AffiliationFetcherInterface>
KeylessFetcherFactory::CreateInstance(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
if (can_create_fetcher_) {
return std::make_unique<HashAffiliationFetcher>(
std::move(url_loader_factory));
}
return nullptr;
}
} // namespace
class AffiliationFetcherManagerTest : public testing::Test {
public:
AffiliationFetcherManagerTest() = default;
protected:
GURL interception_url() const {
return HashAffiliationFetcher::BuildQueryURL();
}
// Setup a complex response from Affiliation Server to make sure it is passed
// down to |AffiliationFetcherManager|.
std::string GetSuccessfulAffiliationResponse() const {
affiliation_pb::LookupAffiliationByHashPrefixResponse test_response;
affiliation_pb::Affiliation* eq_class1 = test_response.add_affiliations();
eq_class1->add_facet()->set_id(kExampleWebFacet1URI);
eq_class1->add_facet()->set_id(kExampleWebFacet2URI);
eq_class1->add_facet()->set_id(kExampleAndroidFacetURI);
affiliation_pb::Affiliation* eq_class2 = test_response.add_affiliations();
eq_class2->add_facet()->set_id(kNotExampleWebFacetURI);
eq_class2->add_facet()->set_id(kNotExampleAndroidFacetURI);
return test_response.SerializeAsString();
}
void SetupSuccessfulResponse(const std::string& response) {
test_url_loader_factory_.ClearResponses();
test_url_loader_factory_.AddResponse(interception_url().spec(), response,
net::HTTP_OK);
}
void SetupServerErrorResponse() {
test_url_loader_factory_.ClearResponses();
test_url_loader_factory_.AddResponse(interception_url().spec(), "",
net::HTTP_INTERNAL_SERVER_ERROR);
}
int GetNumPendingRequests() { return test_url_loader_factory_.NumPending(); }
AffiliationFetcherManager* manager() { return manager_.get(); }
void DisallowFetcherCreation() {
keyless_fetcher_factory_->SetCanCreateFetcher(false);
}
void DestroyManager() {
keyless_fetcher_factory_ = nullptr;
manager_.reset();
}
private:
// testing::Test:
void SetUp() override {
manager_ = std::make_unique<AffiliationFetcherManager>(
test_shared_loader_factory_);
auto fetcher_factory = std::make_unique<KeylessFetcherFactory>();
keyless_fetcher_factory_ = fetcher_factory.get();
manager_->SetFetcherFactoryForTesting(std::move(fetcher_factory));
}
base::test::SingleThreadTaskEnvironment task_environment_;
variations::ScopedVariationsIdsProvider scoped_variations_ids_provider_{
variations::VariationsIdsProvider::Mode::kUseSignedInState};
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_);
std::unique_ptr<AffiliationFetcherManager> manager_;
// Owned by |manager_|.
raw_ptr<KeylessFetcherFactory> keyless_fetcher_factory_ = nullptr;
};
TEST_F(AffiliationFetcherManagerTest, OneFetchCallCreatesOneFetcher) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
manager()->Fetch(requested_uris, kRequestInfo, base::DoNothing());
EXPECT_EQ(1u, manager()->GetFetchersForTesting()->size());
EXPECT_EQ(1, GetNumPendingRequests());
}
TEST_F(AffiliationFetcherManagerTest,
MultipleFetchCallsCreateMultipleFetchers) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
manager()->Fetch(requested_uris, kRequestInfo, base::DoNothing());
manager()->Fetch(requested_uris, kRequestInfo, base::DoNothing());
manager()->Fetch(requested_uris, kRequestInfo, base::DoNothing());
EXPECT_EQ(3u, manager()->GetFetchersForTesting()->size());
EXPECT_EQ(3, GetNumPendingRequests());
}
TEST_F(AffiliationFetcherManagerTest,
ImmediatelyInvokeCallbackIfFetcherCreationFailed) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback;
DisallowFetcherCreation();
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback.GetCallback());
EXPECT_FALSE(manager()->IsFetchPossible());
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(completion_callback.IsReady());
}
TEST_F(AffiliationFetcherManagerTest,
CompletionClosureInvokedOnFetchCompletion) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback;
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback.GetCallback());
SetupSuccessfulResponse(GetSuccessfulAffiliationResponse());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(completion_callback.Take().data);
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
}
TEST_F(AffiliationFetcherManagerTest,
CompletionClosureInvokedOnMultipleFetchCompletions) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_1;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_2;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_3;
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_1.GetCallback());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_2.GetCallback());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_3.GetCallback());
// All requests have the same URL, so this will serve all of them
SetupSuccessfulResponse(GetSuccessfulAffiliationResponse());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(completion_callback_1.Take().data);
EXPECT_TRUE(completion_callback_2.Take().data);
EXPECT_TRUE(completion_callback_3.Take().data);
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
}
// This test mimics |HashAffiliationFetcherTest::BasicRequestAndResponse| to
// makes sure that the response from |HashAffiliationFetcher| is passed down to
// |AffiliationFetcherManger|.
TEST_F(AffiliationFetcherManagerTest, DelegateInvokedOnFetchSuccess) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback;
SetupSuccessfulResponse(GetSuccessfulAffiliationResponse());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback.GetCallback());
auto affiliation_response = completion_callback.Take().data;
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(affiliation_response);
ASSERT_EQ(2u, affiliation_response->affiliations.size());
EXPECT_THAT(affiliation_response->affiliations[0],
testing::UnorderedElementsAre(
Facet{FacetURI::FromCanonicalSpec(kExampleWebFacet1URI)},
Facet{FacetURI::FromCanonicalSpec(kExampleWebFacet2URI)},
Facet{FacetURI::FromCanonicalSpec(kExampleAndroidFacetURI)}));
EXPECT_THAT(
affiliation_response->affiliations[1],
testing::UnorderedElementsAre(
Facet{FacetURI::FromCanonicalSpec(kNotExampleWebFacetURI)},
Facet{FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI)}));
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
}
TEST_F(AffiliationFetcherManagerTest, CallbackInvokedOnFetchFailed) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback;
SetupServerErrorResponse();
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback.GetCallback());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(completion_callback.IsReady());
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
}
TEST_F(AffiliationFetcherManagerTest, CallbackInvokedOnMalformedResponse) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback;
SetupSuccessfulResponse("gibberish");
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback.GetCallback());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_TRUE(completion_callback.IsReady());
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
}
// The structure of this test is awkward because |TestURLLoaderFactory| seeds
// responses per URL, not per ResourceRequest. Since all URLs that we request
// are the same, we can't seed different responses for different requests. In
// this test we seed and serve the responses one-by-one to work around this,
// which is not perfect.
// However the test case is still valuable to check that consecutive calls can
// produce different results.
TEST_F(AffiliationFetcherManagerTest, DelegateInvokedOnAllPossibleResponses) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_1;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_2;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_3;
// Successful response
SetupSuccessfulResponse(GetSuccessfulAffiliationResponse());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_1.GetCallback());
// Failing response
SetupServerErrorResponse();
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_2.GetCallback());
// Malformed response
SetupSuccessfulResponse("gibberish");
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_3.GetCallback());
EXPECT_EQ(0, GetNumPendingRequests());
EXPECT_EQ(0u, manager()->GetFetchersForTesting()->size());
// First response
auto result_1 = completion_callback_1.Take().data;
ASSERT_TRUE(result_1);
EXPECT_EQ(2u, result_1->affiliations.size());
// Second response
auto result_2 = completion_callback_2.Take();
EXPECT_FALSE(result_2.data);
EXPECT_EQ(net::HTTP_INTERNAL_SERVER_ERROR, result_2.http_status_code);
// Third response
auto result_3 = completion_callback_3.Take();
EXPECT_FALSE(result_3.data);
EXPECT_EQ(net::HTTP_OK, result_3.http_status_code);
EXPECT_EQ(0, result_3.network_status);
}
TEST_F(AffiliationFetcherManagerTest, CallbackIsCalledOnManagerDestruction) {
std::vector<FacetURI> requested_uris;
requested_uris.push_back(FacetURI::FromCanonicalSpec(kExampleWebFacet1URI));
requested_uris.push_back(
FacetURI::FromCanonicalSpec(kNotExampleAndroidFacetURI));
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_1;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_2;
base::test::TestFuture<HashAffiliationFetcher::FetchResult>
completion_callback_3;
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_1.GetCallback());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_2.GetCallback());
manager()->Fetch(requested_uris, kRequestInfo,
completion_callback_3.GetCallback());
DestroyManager();
EXPECT_TRUE(completion_callback_1.IsReady());
EXPECT_TRUE(completion_callback_2.IsReady());
EXPECT_TRUE(completion_callback_3.IsReady());
EXPECT_EQ(0, GetNumPendingRequests());
}
} // namespace affiliations
|