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 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_command_line.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_private_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager.h"
#include "chrome/browser/media/webrtc/webrtc_event_log_manager_common.h"
#include "chrome/browser/media/webrtc/webrtc_log_uploader.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/policy_constants.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/common/extension_builder.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "third_party/zlib/google/compression_utils.h"
using compression::GzipUncompress;
using extensions::Extension;
using extensions::WebrtcLoggingPrivateDiscardFunction;
using extensions::WebrtcLoggingPrivateSetMetaDataFunction;
using extensions::WebrtcLoggingPrivateStartAudioDebugRecordingsFunction;
using extensions::WebrtcLoggingPrivateStartEventLoggingFunction;
using extensions::WebrtcLoggingPrivateStartFunction;
using extensions::WebrtcLoggingPrivateStartRtpDumpFunction;
using extensions::WebrtcLoggingPrivateStopAudioDebugRecordingsFunction;
using extensions::WebrtcLoggingPrivateStopFunction;
using extensions::WebrtcLoggingPrivateStopRtpDumpFunction;
using extensions::WebrtcLoggingPrivateStoreFunction;
using extensions::WebrtcLoggingPrivateUploadFunction;
using extensions::WebrtcLoggingPrivateUploadStoredFunction;
using webrtc_event_logging::kMaxOutputPeriodMs;
using webrtc_event_logging::kMaxRemoteLogFileSizeBytes;
using webrtc_event_logging::kStartRemoteLoggingFailureAlreadyLogging;
using webrtc_event_logging::kStartRemoteLoggingFailureFeatureDisabled;
using webrtc_event_logging::kStartRemoteLoggingFailureMaxSizeTooLarge;
using webrtc_event_logging::kStartRemoteLoggingFailureMaxSizeTooSmall;
using webrtc_event_logging::kStartRemoteLoggingFailureOutputPeriodMsTooLarge;
using webrtc_event_logging::
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
using webrtc_event_logging::kStartRemoteLoggingFailureUnlimitedSizeDisallowed;
using webrtc_event_logging::kWebRtcEventLogManagerUnlimitedFileSize;
using webrtc_event_logging::WebRtcEventLogManager;
namespace utils = extensions::api_test_utils;
namespace {
static const char kTestLoggingSessionIdKey[] = "app_session_id";
static const char kTestLoggingSessionIdValue[] = "0123456789abcdef";
static const char kTestLoggingUrl[] = "dummy url string";
constexpr int kWebAppId = 15; // Arbitrary.
constexpr char kTestUploadUrlPath[] = "/upload_webrtc_log";
constexpr char kTestReportId[] = "report_id";
std::string ParamsToString(const base::Value::List& parameters) {
std::string parameter_string;
EXPECT_TRUE(base::JSONWriter::Write(parameters, ¶meter_string));
return parameter_string;
}
void InitializeTestMetaData(base::Value::List& parameters) {
base::Value::Dict meta_data_entry;
meta_data_entry.Set("key", kTestLoggingSessionIdKey);
meta_data_entry.Set("value", kTestLoggingSessionIdValue);
base::Value::List meta_data;
meta_data.Append(std::move(meta_data_entry));
base::Value::Dict meta_data_entry2;
meta_data_entry2.Set("key", "url");
meta_data_entry2.Set("value", kTestLoggingUrl);
meta_data.Append(std::move(meta_data_entry2));
parameters.Append(std::move(meta_data));
}
class WebrtcLoggingPrivateApiTest : public extensions::ExtensionApiTest {
protected:
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
extension_ = extensions::ExtensionBuilder("Test").Build();
}
template<typename T>
scoped_refptr<T> CreateFunction() {
scoped_refptr<T> function(new T());
function->set_extension(extension_.get());
function->set_has_callback(true);
return function;
}
// Overriding can use incognito session instead, etc.
virtual Browser* GetBrowser() { return browser(); }
content::WebContents* web_contents() {
return GetBrowser()->tab_strip_model()->GetActiveWebContents();
}
bool SetupTestServerLogUploading() {
embedded_test_server()->RegisterRequestHandler(
base::BindRepeating(&WebrtcLoggingPrivateApiTest::HandleServerRequest,
base::Unretained(this)));
const bool start_result = StartEmbeddedTestServer();
g_browser_process->webrtc_log_uploader()->SetUploadUrlForTesting(
embedded_test_server()->GetURL(kTestUploadUrlPath));
return start_result;
}
std::unique_ptr<net::test_server::HttpResponse> HandleServerRequest(
const net::test_server::HttpRequest& request) {
if (request.relative_url == kTestUploadUrlPath) {
upload_request_content_ = request.content;
std::unique_ptr<net::test_server::BasicHttpResponse> response(
new net::test_server::BasicHttpResponse);
response->set_code(net::HTTP_OK);
response->set_content(kTestReportId);
return std::move(response);
}
return nullptr;
}
void AppendTabIdAndUrl(base::Value::List& parameters) {
base::Value::Dict request_info;
request_info.Set("tabId",
extensions::ExtensionTabUtil::GetTabId(web_contents()));
parameters.Append(std::move(request_info));
parameters.Append(web_contents()
->GetLastCommittedURL()
.DeprecatedGetOriginAsURL()
.spec());
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns the value (NOT whether it had succeeded or failed).
// TODO(crbug.com/41381060): Return success/failure of the executed function.
template <typename Function>
std::optional<base::Value> RunFunction(const base::Value::List& parameters) {
scoped_refptr<Function> function(CreateFunction<Function>());
std::optional<base::Value> result = utils::RunFunctionAndReturnSingleResult(
function.get(), ParamsToString(parameters), GetBrowser()->profile());
return result;
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns the value (NOT whether it had succeeded or failed).
// TODO(crbug.com/41381060): Return success/failure of the executed function.
template <typename Function>
std::optional<base::Value> RunNoArgsFunction() {
base::Value::List params;
AppendTabIdAndUrl(params);
scoped_refptr<Function> function(CreateFunction<Function>());
std::optional<base::Value> result = utils::RunFunctionAndReturnSingleResult(
function.get(), ParamsToString(params), GetBrowser()->profile());
return result;
}
template <typename Function>
void RunFunctionAndExpectError(const base::Value::List& parameters,
const std::string& expected_error) {
DCHECK(!expected_error.empty());
scoped_refptr<Function> function(CreateFunction<Function>());
const std::string error_message = utils::RunFunctionAndReturnError(
function.get(), ParamsToString(parameters), GetBrowser()->profile());
EXPECT_EQ(error_message, expected_error);
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StartLogging() {
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunNoArgsFunction<WebrtcLoggingPrivateStartFunction>();
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StopLogging() {
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunNoArgsFunction<WebrtcLoggingPrivateStopFunction>();
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool DiscardLog() {
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunNoArgsFunction<WebrtcLoggingPrivateDiscardFunction>();
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool UploadLog(std::string* report_id) {
constexpr bool value_expected = true;
std::optional<base::Value> value =
RunNoArgsFunction<WebrtcLoggingPrivateUploadFunction>();
const bool value_returned = value.has_value();
if (value_returned) {
*report_id = *value->GetDict().FindString("reportId");
}
return value_expected == value_returned;
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool SetMetaData(const base::Value::List& data) {
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateSetMetaDataFunction>(data);
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StartRtpDump(bool incoming, bool outgoing) {
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(incoming);
params.Append(outgoing);
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateStartRtpDumpFunction>(params);
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StopRtpDump(bool incoming, bool outgoing) {
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(incoming);
params.Append(outgoing);
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateStopRtpDumpFunction>(params);
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StoreLog(const std::string& log_id) {
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(log_id);
constexpr bool value_expected = false;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateStoreFunction>(params);
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool UploadStoredLog(const std::string& log_id, std::string* report_id) {
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(log_id);
constexpr bool value_expected = true;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateUploadStoredFunction>(params);
const bool value_returned = value.has_value();
if (value_returned) {
*report_id = *value->GetDict().FindString("reportId");
}
return value_expected == value_returned;
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StartAudioDebugRecordings(int seconds) {
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(seconds);
constexpr bool value_expected = true;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateStartAudioDebugRecordingsFunction>(
params);
return value_expected == value.has_value();
}
// This function implicitly expects the function to succeed (test failure
// initiated otherwise).
// Returns whether the function that was run returned a value, or avoided
// returning a value, according to expectation.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
bool StopAudioDebugRecordings() {
base::Value::List params;
AppendTabIdAndUrl(params);
constexpr bool value_expected = true;
std::optional<base::Value> value =
RunFunction<WebrtcLoggingPrivateStopAudioDebugRecordingsFunction>(
params);
return value_expected == value.has_value();
}
// This function expects the function to succeed or fail according to
// |expect_success| (test failure initiated otherwise). It also implicitly
// expects that no value would be returned.
// TODO(crbug.com/41381060): Return success/failure of the executed function.
void StartEventLogging(const std::string& session_id,
int max_log_size_bytes,
int output_period_ms,
int web_app_id,
bool expect_success,
const std::string& expected_error = std::string()) {
DCHECK_EQ(expect_success, expected_error.empty());
base::Value::List params;
AppendTabIdAndUrl(params);
params.Append(session_id);
params.Append(max_log_size_bytes);
params.Append(output_period_ms);
params.Append(web_app_id);
if (expect_success) {
scoped_refptr<WebrtcLoggingPrivateStartEventLoggingFunction> function(
CreateFunction<WebrtcLoggingPrivateStartEventLoggingFunction>());
std::optional<base::Value> result =
utils::RunFunctionAndReturnSingleResult(
function.get(), ParamsToString(params), GetBrowser()->profile());
ASSERT_TRUE(result);
ASSERT_TRUE(result->is_dict());
const base::Value::Dict& result_dict = result->GetDict();
ASSERT_EQ(result_dict.size(), 1u);
const std::string* log_id = result_dict.FindString("logId");
ASSERT_TRUE(log_id);
EXPECT_EQ(log_id->size(), 32u);
EXPECT_EQ(log_id->find_first_not_of("0123456789ABCDEF"),
std::string::npos);
} else {
RunFunctionAndExpectError<WebrtcLoggingPrivateStartEventLoggingFunction>(
params, expected_error);
}
}
void SetUpPeerConnection(const std::string& session_id = "") {
auto* manager = WebRtcEventLogManager::GetInstance();
content::RenderFrameHost* render_frame_host =
web_contents()->GetPrimaryMainFrame();
const content::GlobalRenderFrameHostId frame_id =
render_frame_host->GetGlobalId();
const base::ProcessId pid =
render_frame_host->GetProcess()->GetProcess().Pid();
const int lid = 0;
manager->OnPeerConnectionAdded(frame_id, lid, pid, /*url=*/std::string(),
/*rtc_configuration=*/std::string());
if (!session_id.empty()) {
manager->OnPeerConnectionSessionIdSet(frame_id, lid, session_id);
}
}
base::test::ScopedCommandLine scoped_command_line_;
scoped_refptr<const Extension> extension_;
// The content of the upload request that reached the test server.
std::string upload_request_content_;
};
} // namespace
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopDiscard) {
EXPECT_TRUE(StartLogging());
EXPECT_TRUE(StopLogging());
EXPECT_TRUE(DiscardLog());
}
// Tests WebRTC diagnostic logging. Sets up the browser to save the multipart
// contents to a buffer instead of uploading it, then verifies it after a calls.
// Example of multipart contents:
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="prod"
//
// Chrome_Linux
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="ver"
//
// 30.0.1554.0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="guid"
//
// 0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="type"
//
// webrtc_log
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="app_session_id"
//
// 0123456789abcdef
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="url"
//
// http://127.0.0.1:43213/webrtc/webrtc_jsep01_test.html
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="webrtc_log"; filename="webrtc_log.gz"
// Content-Type: application/gzip
//
// <compressed data (zip)>
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**------
//
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopUpload) {
ASSERT_TRUE(SetupTestServerLogUploading());
base::Value::List parameters;
AppendTabIdAndUrl(parameters);
InitializeTestMetaData(parameters);
std::string report_id;
SetMetaData(parameters);
StartLogging();
StopLogging();
UploadLog(&report_id);
ASSERT_FALSE(upload_request_content_.empty());
EXPECT_STREQ(kTestReportId, report_id.c_str());
// Check multipart data.
const char boundary[] = "------**--yradnuoBgoLtrapitluMklaTelgooG--**----";
// Move the compressed data to its own string, since it may contain "\r\n" and
// it makes the tests below easier.
const char zip_content_type[] = "Content-Type: application/gzip";
size_t zip_pos = upload_request_content_.find(&zip_content_type[0]);
ASSERT_NE(std::string::npos, zip_pos);
// Move pos to where the zip begins. - 1 to remove '\0', + 4 for two "\r\n".
zip_pos += sizeof(zip_content_type) + 3;
size_t zip_length = upload_request_content_.find(boundary, zip_pos);
ASSERT_NE(std::string::npos, zip_length);
// Calculate length, adjust for a "\r\n".
zip_length -= zip_pos + 2;
ASSERT_GT(zip_length, 0u);
std::string log_part = upload_request_content_.substr(zip_pos, zip_length);
upload_request_content_.erase(zip_pos, zip_length);
// Uncompress log and verify contents.
EXPECT_TRUE(GzipUncompress(log_part, &log_part));
EXPECT_GT(log_part.length(), 0u);
// Verify that meta data exists.
EXPECT_NE(std::string::npos, log_part.find(base::StringPrintf("%s: %s",
kTestLoggingSessionIdKey, kTestLoggingSessionIdValue)));
// Verify that the basic info generated at logging startup exists.
EXPECT_NE(std::string::npos, log_part.find("Chrome version:"));
EXPECT_NE(std::string::npos, log_part.find("Cpu brand:"));
// Check the multipart contents.
std::vector<std::string> multipart_lines =
base::SplitStringUsingSubstr(upload_request_content_, "\r\n",
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
ASSERT_EQ(31, static_cast<int>(multipart_lines.size()));
EXPECT_STREQ(&boundary[0], multipart_lines[0].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"prod\"",
multipart_lines[1].c_str());
EXPECT_TRUE(multipart_lines[2].empty());
EXPECT_NE(std::string::npos, multipart_lines[3].find("Chrome"));
EXPECT_STREQ(&boundary[0], multipart_lines[4].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"ver\"",
multipart_lines[5].c_str());
EXPECT_TRUE(multipart_lines[6].empty());
// Just check that the version contains a dot.
EXPECT_NE(std::string::npos, multipart_lines[7].find('.'));
EXPECT_STREQ(&boundary[0], multipart_lines[8].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"guid\"",
multipart_lines[9].c_str());
EXPECT_TRUE(multipart_lines[10].empty());
EXPECT_STREQ("0", multipart_lines[11].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[12].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"type\"",
multipart_lines[13].c_str());
EXPECT_TRUE(multipart_lines[14].empty());
EXPECT_STREQ("webrtc_log", multipart_lines[15].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[16].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"app_session_id\"",
multipart_lines[17].c_str());
EXPECT_TRUE(multipart_lines[18].empty());
EXPECT_STREQ(kTestLoggingSessionIdValue, multipart_lines[19].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[20].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"url\"",
multipart_lines[21].c_str());
EXPECT_TRUE(multipart_lines[22].empty());
EXPECT_STREQ(kTestLoggingUrl, multipart_lines[23].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[24].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"webrtc_log\";"
" filename=\"webrtc_log.gz\"",
multipart_lines[25].c_str());
EXPECT_STREQ("Content-Type: application/gzip",
multipart_lines[26].c_str());
EXPECT_TRUE(multipart_lines[27].empty());
EXPECT_TRUE(multipart_lines[28].empty()); // The removed zip part.
std::string final_delimiter = boundary;
final_delimiter += "--";
EXPECT_STREQ(final_delimiter.c_str(), multipart_lines[29].c_str());
EXPECT_TRUE(multipart_lines[30].empty());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopRtpDump) {
// TODO(tommi): As is, these tests are missing verification of the actual
// RTP dump data. We should fix that, e.g. use SetDumpWriterForTesting.
StartRtpDump(true, true);
StopRtpDump(true, true);
}
// Tests trying to store a log when a log is not being captured.
// We should get a failure callback in this case.
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStoreWithoutLog) {
base::Value::List parameters;
AppendTabIdAndUrl(parameters);
parameters.Append("MyLogId");
scoped_refptr<WebrtcLoggingPrivateStoreFunction> store(
CreateFunction<WebrtcLoggingPrivateStoreFunction>());
const std::string error = utils::RunFunctionAndReturnError(
store.get(), ParamsToString(parameters), GetBrowser()->profile());
ASSERT_FALSE(error.empty());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest, TestStartStopStore) {
ASSERT_TRUE(StartLogging());
ASSERT_TRUE(StopLogging());
EXPECT_TRUE(StoreLog("MyLogID"));
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
TestStartStopStoreAndUpload) {
ASSERT_TRUE(SetupTestServerLogUploading());
static const char kLogId[] = "TestStartStopStoreAndUpload";
ASSERT_TRUE(StartLogging());
ASSERT_TRUE(StopLogging());
ASSERT_TRUE(StoreLog(kLogId));
std::string report_id;
EXPECT_TRUE(UploadStoredLog(kLogId, &report_id));
EXPECT_NE(std::string::npos,
upload_request_content_.find("filename=\"webrtc_log.gz\""));
EXPECT_STREQ(kTestReportId, report_id.c_str());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
TestStartStopStoreAndUploadWithRtp) {
ASSERT_TRUE(SetupTestServerLogUploading());
static const char kLogId[] = "TestStartStopStoreAndUploadWithRtp";
ASSERT_TRUE(StartLogging());
ASSERT_TRUE(StartRtpDump(true, true));
ASSERT_TRUE(StopLogging());
ASSERT_TRUE(StopRtpDump(true, true));
ASSERT_TRUE(StoreLog(kLogId));
std::string report_id;
EXPECT_TRUE(UploadStoredLog(kLogId, &report_id));
EXPECT_NE(std::string::npos,
upload_request_content_.find("filename=\"webrtc_log.gz\""));
EXPECT_STREQ(kTestReportId, report_id.c_str());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
TestStartStopStoreAndUploadWithMetaData) {
ASSERT_TRUE(SetupTestServerLogUploading());
static const char kLogId[] = "TestStartStopStoreAndUploadWithRtp";
ASSERT_TRUE(StartLogging());
base::Value::List parameters;
AppendTabIdAndUrl(parameters);
InitializeTestMetaData(parameters);
SetMetaData(parameters);
ASSERT_TRUE(StopLogging());
ASSERT_TRUE(StoreLog(kLogId));
std::string report_id;
EXPECT_TRUE(UploadStoredLog(kLogId, &report_id));
EXPECT_NE(std::string::npos,
upload_request_content_.find("filename=\"webrtc_log.gz\""));
EXPECT_NE(std::string::npos, upload_request_content_.find(kTestLoggingUrl));
EXPECT_STREQ(kTestReportId, report_id.c_str());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
TestStartStopAudioDebugRecordings) {
// TODO(guidou): These tests are missing verification of the actual AEC dump
// data. This will be fixed with a separate browser test.
// See crbug.com/569957.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableAudioDebugRecordingsFromExtension);
ASSERT_TRUE(StartAudioDebugRecordings(0));
ASSERT_TRUE(StopAudioDebugRecordings());
}
IN_PROC_BROWSER_TEST_F(WebrtcLoggingPrivateApiTest,
TestStartTimedAudioDebugRecordings) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableAudioDebugRecordingsFromExtension);
ASSERT_TRUE(StartAudioDebugRecordings(1));
}
#if !BUILDFLAG(IS_ANDROID)
// Fixture for various tests over StartEventLogging. Intended to be sub-classed
// to test different scenarios.
class WebrtcLoggingPrivateApiStartEventLoggingTestBase
: public WebrtcLoggingPrivateApiTest {
public:
~WebrtcLoggingPrivateApiStartEventLoggingTestBase() override = default;
protected:
void SetUpInProcessBrowserTestFixture() override {
provider_.SetDefaultReturns(
/*is_initialization_complete_return=*/true,
/*is_first_policy_load_complete_return=*/true);
policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
policy::PolicyMap values;
values.Set(policy::key::kWebRtcEventLogCollectionAllowed,
policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
policy::POLICY_SOURCE_ENTERPRISE_DEFAULT,
base::Value(WebRtcEventLogCollectionPolicy()), nullptr);
provider_.UpdateChromePolicy(values);
}
// Whether the test should simulate running on a user profile which
// has the kWebRtcEventLogCollectionAllowed policy configured or not.
virtual bool WebRtcEventLogCollectionPolicy() const = 0;
private:
testing::NiceMock<policy::MockConfigurationPolicyProvider> provider_;
};
// Test StartEventLogging's behavior when the feature is active (non-Android,
// working in a profile where the policy is configured).
class WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled
: public WebrtcLoggingPrivateApiStartEventLoggingTestBase,
public testing::WithParamInterface<bool> {
public:
~WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled() override =
default;
bool WebRtcEventLogCollectionPolicy() const override { return true; }
};
// Also covers StartEventLoggingForLegalWebAppIdSucceeds scenario.
IN_PROC_BROWSER_TEST_P(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForKnownPeerConnectionSucceeds) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = true;
const int output_period_ms = GetParam() ? kMaxOutputPeriodMs : 0;
StartEventLogging(session_id, max_size_bytes, output_period_ms, kWebAppId,
expect_success);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingWithUnlimitedSizeFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kWebRtcEventLogManagerUnlimitedFileSize;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnlimitedSizeDisallowed;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingWithTooSmallMaxSize) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = 1;
constexpr bool expect_success = false;
const std::string error_message = kStartRemoteLoggingFailureMaxSizeTooSmall;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingWithExcessiveMaxSizeFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes + 1;
constexpr bool expect_success = false;
const std::string error_message = kStartRemoteLoggingFailureMaxSizeTooLarge;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingWithTooLargeOutputPeriodMsFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int output_period_ms = kMaxOutputPeriodMs + 1;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureOutputPeriodMsTooLarge;
StartEventLogging(session_id, kMaxRemoteLogFileSizeBytes, output_period_ms,
kWebAppId, expect_success, error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForNeverAddedPeerConnectionFails) {
// Note that manager->OnPeerConnectionAdded() is not called.
const std::string session_id = "id";
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForWrongSessionIdFails) {
const std::string session_id_1 = "id1";
const std::string session_id_2 = "id2";
SetUpPeerConnection(session_id_1);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
StartEventLogging(session_id_2, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingIfSessionIdNeverSetFails) {
SetUpPeerConnection(); // Note lack of session ID.
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
StartEventLogging("session_id", max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingIfSessionIdNeverSetFailsForEmptySessionId) {
SetUpPeerConnection(); // Note lack of session ID.
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
StartEventLogging("", max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLogginWithEmptySessionIdFails) {
SetUpPeerConnection("session_id");
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message =
kStartRemoteLoggingFailureUnknownOrInactivePeerConnection;
StartEventLogging("", max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForAlreadyLoggedPeerConnectionFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
// First call succeeds.
{
constexpr bool expect_success = true;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success);
}
// Second call fails.
{
constexpr bool expect_success = false;
const std::string error_message = kStartRemoteLoggingFailureAlreadyLogging;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForTooLowWebAppIdFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
const size_t web_app_id =
webrtc_event_logging::kMinWebRtcEventLogWebAppId - 1;
ASSERT_LT(web_app_id, webrtc_event_logging::kMinWebRtcEventLogWebAppId);
constexpr bool expect_success = false;
const std::string error_message =
webrtc_event_logging::kStartRemoteLoggingFailureIllegalWebAppId;
StartEventLogging(session_id, max_size_bytes, 0, web_app_id, expect_success,
error_message);
}
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
StartEventLoggingForTooHighWebAppIdFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
const size_t web_app_id =
webrtc_event_logging::kMaxWebRtcEventLogWebAppId + 1;
ASSERT_GT(web_app_id, webrtc_event_logging::kMaxWebRtcEventLogWebAppId);
constexpr bool expect_success = false;
const std::string error_message =
webrtc_event_logging::kStartRemoteLoggingFailureIllegalWebAppId;
StartEventLogging(session_id, max_size_bytes, 0, web_app_id, expect_success,
error_message);
}
INSTANTIATE_TEST_SUITE_P(
_,
WebrtcLoggingPrivateApiStartEventLoggingTestPolicyEnabled,
::testing::Bool());
// Testing with the policy enabled/disabled.
class WebrtcLoggingPrivateApiStartEventLoggingTestPolicy
: public WebrtcLoggingPrivateApiStartEventLoggingTestBase,
public ::testing::WithParamInterface<bool> {
public:
WebrtcLoggingPrivateApiStartEventLoggingTestPolicy()
: policy_enabled_(GetParam()) {}
~WebrtcLoggingPrivateApiStartEventLoggingTestPolicy() override = default;
protected:
bool WebRtcEventLogCollectionPolicy() const override {
return policy_enabled_;
}
private:
const bool policy_enabled_;
};
IN_PROC_BROWSER_TEST_P(WebrtcLoggingPrivateApiStartEventLoggingTestPolicy,
StartEventLogging) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
bool expect_success = WebRtcEventLogCollectionPolicy();
const std::string error_message =
expect_success ? "" : kStartRemoteLoggingFailureFeatureDisabled;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
INSTANTIATE_TEST_SUITE_P(PolicyEnabled,
WebrtcLoggingPrivateApiStartEventLoggingTestPolicy,
::testing::Bool());
// Make sure that, even if both the feature and the policy enable remote-bound
// event logging, it will be blocked for incognito sessions.
class WebrtcLoggingPrivateApiStartEventLoggingTestInIncognitoMode
: public WebrtcLoggingPrivateApiStartEventLoggingTestBase {
public:
~WebrtcLoggingPrivateApiStartEventLoggingTestInIncognitoMode() override =
default;
protected:
Browser* GetBrowser() override {
if (!browser_) {
browser_ = CreateIncognitoBrowser();
}
return browser_;
}
bool WebRtcEventLogCollectionPolicy() const override { return true; }
private:
raw_ptr<Browser, AcrossTasksDanglingUntriaged> browser_{
nullptr}; // Does not own the object.
};
IN_PROC_BROWSER_TEST_F(
WebrtcLoggingPrivateApiStartEventLoggingTestInIncognitoMode,
StartEventLoggingFails) {
const std::string session_id = "id";
SetUpPeerConnection(session_id);
const int max_size_bytes = kMaxRemoteLogFileSizeBytes;
constexpr bool expect_success = false;
const std::string error_message = kStartRemoteLoggingFailureFeatureDisabled;
StartEventLogging(session_id, max_size_bytes, 0, kWebAppId, expect_success,
error_message);
}
#endif // !BUILDFLAG(IS_ANDROID)
|