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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
content / services / auction_worklet / public / cpp / auction_downloader_unittest.cc [blame]
// 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 "content/services/auction_worklet/public/cpp/auction_downloader.h"
#include <cstddef>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "content/services/auction_worklet/worklet_test_util.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/devtools_observer_util.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_response_head.mojom-forward.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace auction_worklet {
namespace {
const char kAsciiResponseBody[] = "ASCII response body.";
const char kUtf8ResponseBody[] = "\xc3\x9f\xc3\x9e";
const char kNonUtf8ResponseBody[] = "\xc3";
const char kAsciiCharset[] = "us-ascii";
const char kUtf8Charset[] = "utf-8";
class AuctionDownloaderTest
: public testing::TestWithParam<AuctionDownloader::DownloadMode> {
public:
AuctionDownloaderTest() = default;
~AuctionDownloaderTest() override = default;
AuctionDownloader::DownloadMode download_mode() { return GetParam(); }
class TestDelegate : public AuctionDownloader::NetworkEventsDelegate {
public:
TestDelegate(network::URLLoaderCompletionStatus& completetion_status,
std::optional<GURL>& response_url,
std::optional<std::string>& request_id,
std::optional<GURL>& request_url,
std::optional<network::mojom::URLResponseHeadPtr>& head)
: request_url_ref_(request_url),
head_ref_(head),
request_id_ref_(request_id),
response_url_ref_(response_url),
completetion_status_ref_(completetion_status) {}
~TestDelegate() override = default;
void OnNetworkSendRequest(network::ResourceRequest& request) override {
*request_url_ref_ = request.url;
*request_id_ref_ = request.devtools_request_id;
}
void OnNetworkResponseReceived(
const GURL& url,
const network::mojom::URLResponseHead& head) override {
*response_url_ref_ = url;
*head_ref_ = head.Clone();
}
void OnNetworkRequestComplete(
const network::URLLoaderCompletionStatus& status) override {
*completetion_status_ref_ = status;
}
private:
raw_ref<std::optional<GURL>> request_url_ref_;
raw_ref<std::optional<network::mojom::URLResponseHeadPtr>> head_ref_;
raw_ref<std::optional<std::string>> request_id_ref_;
raw_ref<std::optional<GURL>> response_url_ref_;
raw_ref<network::URLLoaderCompletionStatus> completetion_status_ref_;
};
std::unique_ptr<std::string> RunRequest(
std::optional<std::string> post_body = std::nullopt,
std::optional<std::string> content_type = std::nullopt) {
DCHECK(!run_loop_);
// reset values
observed_request_id_ = std::nullopt;
observed_request_url_ = std::nullopt;
observed_request_content_type_ = std::nullopt;
observed_response_url_ = std::nullopt;
observed_completion_status_ =
network::URLLoaderCompletionStatus(net::Error());
observed_response_head_ = std::nullopt;
auto test_network_events_delegate = std::make_unique<TestDelegate>(
observed_completion_status_, observed_response_url_,
observed_request_id_, observed_request_url_, observed_response_head_);
url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
[this](const network::ResourceRequest& request) {
EXPECT_TRUE(request.devtools_request_id);
observed_request_post_body_ = request.request_body;
observed_request_content_type_ =
request.headers.GetHeader(net::HttpRequestHeaders::kContentType);
observed_request_method_ = request.method;
}));
AuctionDownloader downloader(
&url_loader_factory_, url_, download_mode(), mime_type_,
std::move(post_body), std::move(content_type),
response_started_callback_,
base::BindOnce(&AuctionDownloaderTest::DownloadCompleteCallback,
base::Unretained(this)),
std::move(test_network_events_delegate));
// Populate `run_loop_` after starting the download, since API guarantees
// callback will not be invoked synchronously.
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
run_loop_.reset();
return std::move(body_);
}
std::string EmptyIfSimulated(std::string in) {
return download_mode() ==
AuctionDownloader::DownloadMode::kSimulatedDownload
? std::string()
: in;
}
// Helper to avoid checking has_value all over the place.
std::string last_error_msg() const {
return error_.value_or("Not an error.");
}
protected:
void DownloadCompleteCallback(std::unique_ptr<std::string> body,
scoped_refptr<net::HttpResponseHeaders> headers,
std::optional<std::string> error) {
DCHECK(!body_);
DCHECK(run_loop_);
body_ = std::move(body);
headers_ = std::move(headers);
error_ = std::move(error);
EXPECT_EQ(error_.has_value(), !body_);
run_loop_->Quit();
}
base::test::TaskEnvironment task_environment_;
GURL url_ = GURL("https://url.test/script.js");
AuctionDownloader::MimeType mime_type_ =
AuctionDownloader::MimeType::kJavascript;
std::unique_ptr<base::RunLoop> run_loop_;
std::unique_ptr<std::string> body_;
scoped_refptr<net::HttpResponseHeaders> headers_;
std::optional<std::string> error_;
network::TestURLLoaderFactory url_loader_factory_;
std::optional<GURL> observed_request_url_;
std::optional<std::string> observed_request_id_;
scoped_refptr<network::ResourceRequestBody> observed_request_post_body_;
std::optional<std::string> observed_request_content_type_;
std::optional<std::string> observed_request_method_;
std::optional<GURL> observed_response_url_;
std::optional<network::mojom::URLResponseHeadPtr> observed_response_head_;
network::URLLoaderCompletionStatus observed_completion_status_;
base::RepeatingCallback<void(const network::mojom::URLResponseHead&)>
response_started_callback_;
};
TEST_P(AuctionDownloaderTest, NetworkError) {
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_FAILED;
url_loader_factory_.AddResponse(url_, /*head=*/nullptr, kAsciiResponseBody,
status);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Failed to load https://url.test/script.js error = net::ERR_FAILED.",
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code, net::ERR_FAILED);
}
TEST_P(AuctionDownloaderTest, NetworkErrorTruncatesUrl) {
network::URLLoaderCompletionStatus status;
status.error_code = net::ERR_FAILED;
std::string almost_too_long_url_base = "https://url.test/";
almost_too_long_url_base += std::string(
AuctionDownloader::kMaxErrorUrlLength - almost_too_long_url_base.size(),
'1');
GURL almost_too_long_url = GURL(almost_too_long_url_base);
GURL too_long_url = GURL(almost_too_long_url_base + "2");
url_ = almost_too_long_url;
url_loader_factory_.AddResponse(url_, /*head=*/nullptr, kAsciiResponseBody,
status);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(base::StringPrintf("Failed to load %s error = net::ERR_FAILED.",
almost_too_long_url.spec().c_str()),
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code, net::ERR_FAILED);
url_ = too_long_url;
url_loader_factory_.AddResponse(url_, /*head=*/nullptr, kAsciiResponseBody,
status);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(base::StringPrintf(
"Failed to load %s... error = net::ERR_FAILED.",
too_long_url.spec()
.substr(0, AuctionDownloader::kMaxErrorUrlLength - 3)
.c_str()),
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code, net::ERR_FAILED);
}
// HTTP 404 responses are treated as failures.
TEST_P(AuctionDownloaderTest, HttpError) {
// This is an unlikely response for an error case, but should fail if it ever
// happens.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, kAllowFledgeHeader, net::HTTP_NOT_FOUND);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Failed to load https://url.test/script.js HTTP status = 404 Not Found.",
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code,
net::ERR_HTTP_RESPONSE_CODE_FAILURE);
}
TEST_P(AuctionDownloaderTest, Timeout) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, kAllowFledgeHeader,
net::HTTP_REQUEST_TIMEOUT);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Failed to load https://url.test/script.js HTTP status = 408 Request "
"Timeout.",
last_error_msg());
}
TEST_P(AuctionDownloaderTest, AllowAdAuction) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "X-Allow-FLEDGE: true");
EXPECT_TRUE(RunRequest());
EXPECT_EQ(observed_request_url_, observed_response_url_);
ASSERT_TRUE(observed_response_head_.has_value());
const scoped_refptr<::net::HttpResponseHeaders> observed_header =
observed_response_head_.value()->headers;
EXPECT_TRUE(observed_header->GetNormalizedHeader("X-Allow-FLEDGE"));
EXPECT_EQ(observed_completion_status_.error_code, net::OK);
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "x-aLLow-fLeDgE: true");
EXPECT_TRUE(RunRequest());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "aD-aUCtioN-alloWeD: true");
EXPECT_TRUE(RunRequest());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "X-Allow-FLEDGE: false");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code, net::ERR_ABORTED);
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Ad-Auction-Allowed: true");
EXPECT_TRUE(RunRequest());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Ad-Auction-Allowed: false");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody,
"Ad-Auction-Allowed: true\nX-Allow-FLEDGE: true");
EXPECT_TRUE(RunRequest());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody,
"Ad-Auction-Allowed: false\nX-Allow-FLEDGE: false");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Ad-Auction-Allowed: sometimes");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Ad-Auction-Allowed: ");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "X-Allow-Hats: true");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "");
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, std::nullopt);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
}
TEST_P(AuctionDownloaderTest, PassesHeaders) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Ad-Auction-Allowed: true");
EXPECT_TRUE(RunRequest()) << last_error_msg();
EXPECT_EQ(headers_->GetNormalizedHeader("Ad-Auction-Allowed"), "true");
EXPECT_FALSE(headers_->GetNormalizedHeader("Data-Version"));
mime_type_ = AuctionDownloader::MimeType::kJson;
AddVersionedJsonResponse(&url_loader_factory_, url_, kAsciiResponseBody, 10u);
EXPECT_TRUE(RunRequest()) << last_error_msg();
EXPECT_EQ(headers_->GetNormalizedHeader("Ad-Auction-Allowed"), "true");
EXPECT_EQ(headers_->GetNormalizedHeader("Data-Version"), "10");
AddVersionedJsonResponse(&url_loader_factory_, url_, kAsciiResponseBody, 5u);
EXPECT_TRUE(RunRequest()) << last_error_msg();
EXPECT_EQ(headers_->GetNormalizedHeader("Ad-Auction-Allowed"), "true");
EXPECT_EQ(headers_->GetNormalizedHeader("Data-Version"), "5");
AddJsonResponse(&url_loader_factory_, url_, kAsciiResponseBody);
EXPECT_TRUE(RunRequest()) << last_error_msg();
EXPECT_EQ(headers_->GetNormalizedHeader("Ad-Auction-Allowed"), "true");
EXPECT_FALSE(headers_->GetNormalizedHeader("Data-Version"));
}
TEST_P(AuctionDownloaderTest, ResponseStartedCallback) {
bool called = false;
response_started_callback_ = base::BindLambdaForTesting(
[&](const network::mojom::URLResponseHead& response_head) {
EXPECT_FALSE(called);
// If this callback is called, it's called before the result one.
EXPECT_TRUE(run_loop_);
called = true;
ASSERT_TRUE(response_head.headers);
EXPECT_EQ(response_head.headers->GetNormalizedHeader("Test-Header"),
"test-val");
});
// Since response doesn't have Ad-Auction-Allowed, this will fail and not
// get the `response_started_callback_` invoked, either.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, "Test-Header: test-val");
EXPECT_FALSE(RunRequest());
EXPECT_FALSE(called);
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to lack of "
"Ad-Auction-Allowed: true (or the deprecated X-Allow-FLEDGE: true).",
last_error_msg());
// This will succeed and invoke the callback as well.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody,
"Test-Header: test-val\r\nAd-Auction-Allowed: true");
EXPECT_TRUE(RunRequest());
EXPECT_TRUE(called);
}
// Redirect responses are treated as failures.
TEST_P(AuctionDownloaderTest, Redirect) {
// None of these fields actually matter for this test, but a bit strange for
// them not to be populated.
net::RedirectInfo redirect_info;
redirect_info.status_code = net::HTTP_MOVED_PERMANENTLY;
redirect_info.new_url = url_;
redirect_info.new_method = "GET";
network::TestURLLoaderFactory::Redirects redirects;
redirects.push_back(
std::make_pair(redirect_info, network::mojom::URLResponseHead::New()));
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody, kAllowFledgeHeader, net::HTTP_OK,
std::move(redirects));
EXPECT_FALSE(RunRequest());
EXPECT_EQ("Unexpected redirect on https://url.test/script.js.",
last_error_msg());
EXPECT_EQ(observed_completion_status_.error_code, net::ERR_ABORTED);
}
TEST_P(AuctionDownloaderTest, Success) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
}
TEST_P(AuctionDownloaderTest, SuccessWithPostBody) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
const std::string kPostBody = "TEST BODY";
const std::string kContentType = "text/javascript";
std::unique_ptr<std::string> body =
RunRequest(std::move(kPostBody), std::move(kContentType));
ASSERT_TRUE(body);
ASSERT_TRUE(observed_request_post_body_);
ASSERT_EQ(1u, observed_request_post_body_->elements()->size());
const network::DataElement& elem =
observed_request_post_body_->elements()->at(0);
ASSERT_EQ(network::DataElement::Tag::kBytes, elem.type());
const network::DataElementBytes& byte_elem =
elem.As<network::DataElementBytes>();
EXPECT_EQ(kPostBody, byte_elem.AsStringPiece());
EXPECT_EQ(observed_request_method_, "POST");
EXPECT_EQ(observed_request_content_type_, kContentType);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
}
// Test `AuctionDownloader::MimeType` values work as expected.
TEST_P(AuctionDownloaderTest, MimeType) {
// Javascript request, JSON response type.
AddResponse(&url_loader_factory_, url_, kJsonMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// Javascript request, no response type.
AddResponse(&url_loader_factory_, url_, std::nullopt, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// Javascript request, empty response type.
AddResponse(&url_loader_factory_, url_, "", kUtf8Charset, kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// Javascript request, unknown response type.
AddResponse(&url_loader_factory_, url_, "blobfish", kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// JSON request, Javascript response type.
mime_type_ = AuctionDownloader::MimeType::kJson;
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// JSON request, no response type.
AddResponse(&url_loader_factory_, url_, std::nullopt, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// JSON request, empty response type.
AddResponse(&url_loader_factory_, url_, "", kUtf8Charset, kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// JSON request, unknown response type.
AddResponse(&url_loader_factory_, url_, "blobfish", kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// JSON request, JSON response type.
mime_type_ = AuctionDownloader::MimeType::kJson;
AddResponse(&url_loader_factory_, url_, kJsonMimeType, kUtf8Charset,
kAsciiResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
}
TEST_P(AuctionDownloaderTest, MimeTypeWasm) {
mime_type_ = AuctionDownloader::MimeType::kWebAssembly;
// WASM request, Javascript response type.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// WASM request, no response type.
AddResponse(&url_loader_factory_, url_, /*mime_type=*/std::nullopt,
/*charset=*/std::nullopt, kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// WASM request, JSON response type.
AddResponse(&url_loader_factory_, url_, kJsonMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// WASM request, WASM response type.
AddResponse(&url_loader_factory_, url_, kWasmMimeType,
/*charset=*/std::nullopt, kNonUtf8ResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kNonUtf8ResponseBody), *body);
// Mimetypes are case insensitive.
AddResponse(&url_loader_factory_, url_, "Application/WasM",
/*charset=*/std::nullopt, kNonUtf8ResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kNonUtf8ResponseBody), *body);
// No charset should be used (it's a binary format, after all).
AddResponse(&url_loader_factory_, url_, kWasmMimeType,
/*charset=*/kUtf8Charset, kUtf8ResponseBody);
body = RunRequest();
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// Even an empty parameter list is to be rejected.
AddResponse(&url_loader_factory_, url_, "application/wasm;",
/*charset=*/std::nullopt, kNonUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
}
TEST_P(AuctionDownloaderTest, MimeTypeTrustedSignals) {
mime_type_ = AuctionDownloader::MimeType::kAdAuctionTrustedSignals;
// AdAuctionTrustedSignals request, Javascript response type.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// AdAuctionTrustedSignals request, no response type.
AddResponse(&url_loader_factory_, url_, /*mime_type=*/std::nullopt,
/*charset=*/std::nullopt, kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// AdAuctionTrustedSignals request, JSON response type.
AddResponse(&url_loader_factory_, url_, kJsonMimeType, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
// AdAuctionTrustedSignals request, AdAuctionTrustedSignals response type.
AddResponse(&url_loader_factory_, url_, kAdAuctionTrustedSignalsMimeType,
/*charset=*/std::nullopt, kUtf8ResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kUtf8ResponseBody), *body);
}
// Test all Javascript and JSON MIME type strings.
TEST_P(AuctionDownloaderTest, MimeTypeVariants) {
// All supported Javascript MIME types, copied from blink's mime_util.cc.
const char* kJavascriptMimeTypes[] = {
"application/ecmascript",
"application/javascript",
"application/x-ecmascript",
"application/x-javascript",
"text/ecmascript",
"text/javascript",
"text/javascript1.0",
"text/javascript1.1",
"text/javascript1.2",
"text/javascript1.3",
"text/javascript1.4",
"text/javascript1.5",
"text/jscript",
"text/livescript",
"text/x-ecmascript",
"text/x-javascript",
};
// Some MIME types (there are some wild cards in the matcher).
const char* kJsonMimeTypes[] = {
"application/json", "text/json",
"application/goat+json", "application/javascript+json",
"application/+json",
};
for (const char* javascript_type : kJavascriptMimeTypes) {
mime_type_ = AuctionDownloader::MimeType::kJavascript;
AddResponse(&url_loader_factory_, url_, javascript_type, kUtf8Charset,
kAsciiResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
mime_type_ = AuctionDownloader::MimeType::kJson;
AddResponse(&url_loader_factory_, url_, javascript_type, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
}
for (const char* json_type : kJsonMimeTypes) {
mime_type_ = AuctionDownloader::MimeType::kJavascript;
AddResponse(&url_loader_factory_, url_, json_type, kUtf8Charset,
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected MIME "
"type.",
last_error_msg());
mime_type_ = AuctionDownloader::MimeType::kJson;
AddResponse(&url_loader_factory_, url_, json_type, kUtf8Charset,
kAsciiResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
}
}
TEST_P(AuctionDownloaderTest, Charset) {
mime_type_ = AuctionDownloader::MimeType::kJson;
// Unknown/unsupported charsets should result in failure.
AddResponse(&url_loader_factory_, url_, kJsonMimeType, "fred",
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected charset.",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJsonMimeType, "iso-8859-1",
kAsciiResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected charset.",
last_error_msg());
// ASCII charset should restrict response bodies to ASCII characters.
// (Not relevant in kSimulatedDownload since that doesn't have a body).
mime_type_ = AuctionDownloader::MimeType::kJavascript;
if (download_mode() != AuctionDownloader::DownloadMode::kSimulatedDownload) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kAsciiCharset,
kUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
}
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kAsciiCharset,
kAsciiResponseBody);
std::unique_ptr<std::string> body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
// (Not relevant in kSimulatedDownload since that doesn't have a body).
if (download_mode() != AuctionDownloader::DownloadMode::kSimulatedDownload) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kAsciiCharset,
kUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kAsciiCharset,
kNonUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
}
// UTF-8 charset should restrict response bodies to valid UTF-8 characters.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kAsciiResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kUtf8ResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kUtf8ResponseBody), *body);
// (Not relevant in kSimulatedDownload since that doesn't have a body).
if (download_mode() != AuctionDownloader::DownloadMode::kSimulatedDownload) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, kUtf8Charset,
kNonUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
}
// Null charset should act like UTF-8.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, std::nullopt,
kAsciiResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, std::nullopt,
kUtf8ResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kUtf8ResponseBody), *body);
// (Not relevant in kSimulatedDownload since that doesn't have a body).
if (download_mode() != AuctionDownloader::DownloadMode::kSimulatedDownload) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, std::nullopt,
kNonUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
}
// Empty charset should act like UTF-8.
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, "",
kAsciiResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kAsciiResponseBody), *body);
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, "",
kUtf8ResponseBody);
body = RunRequest();
ASSERT_TRUE(body);
EXPECT_EQ(EmptyIfSimulated(kUtf8ResponseBody), *body);
// (Not relevant in kSimulatedDownload since that doesn't have a body).
if (download_mode() != AuctionDownloader::DownloadMode::kSimulatedDownload) {
AddResponse(&url_loader_factory_, url_, kJavascriptMimeType, "",
kNonUtf8ResponseBody);
EXPECT_FALSE(RunRequest());
EXPECT_EQ(
"Rejecting load of https://url.test/script.js due to unexpected "
"charset.",
last_error_msg());
}
}
INSTANTIATE_TEST_SUITE_P(
/* no label */,
AuctionDownloaderTest,
testing::Values(AuctionDownloader::DownloadMode::kActualDownload,
AuctionDownloader::DownloadMode::kSimulatedDownload));
} // namespace
} // namespace auction_worklet