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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media/webrtc/webrtc_logging_controller.h"
#include <list>
#include <map>
#include <string>
#include <utility>
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/testing_pref_store.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_render_process_host.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 webrtc_text_log {
using ::testing::_;
using BrowserContext = content::BrowserContext;
using MockRenderProcessHost = content::MockRenderProcessHost;
using RenderProcessHost = content::RenderProcessHost;
class WebRtcLoggingControllerTest : public ::testing::Test {
public:
WebRtcLoggingControllerTest()
: browser_context_(nullptr),
test_shared_url_loader_factory_(
test_url_loader_factory_.GetSafeWeakWrapper()) {
TestingBrowserProcess::GetGlobal()->SetSharedURLLoaderFactory(
test_shared_url_loader_factory_);
EXPECT_TRUE(profiles_dir_.CreateUniqueTempDir());
}
WebRtcLoggingControllerTest(const WebRtcLoggingControllerTest&) = delete;
WebRtcLoggingControllerTest& operator=(const WebRtcLoggingControllerTest&) =
delete;
~WebRtcLoggingControllerTest() override {
webrtc_logging_controller_ = nullptr;
if (browser_context_) {
UnloadMainTestProfile();
}
}
void LoadMainTestProfile(std::optional<bool> text_log_collection_allowed) {
browser_context_ = CreateBrowserContext("browser_context_", true,
text_log_collection_allowed);
CreateRenderHost();
}
void UnloadMainTestProfile() {
TestingBrowserProcess::GetGlobal()->webrtc_log_uploader()->Shutdown();
TestingBrowserProcess::GetGlobal()->SetWebRtcLogUploader(nullptr);
rph_.reset();
browser_context_.reset();
}
void CreateRenderHost() {
rph_ = std::make_unique<MockRenderProcessHost>(browser_context_.get());
auto webrtc_log_uploader = std::make_unique<WebRtcLogUploader>();
TestingBrowserProcess::GetGlobal()->SetWebRtcLogUploader(
std::move(webrtc_log_uploader));
WebRtcLoggingController::AttachToRenderProcessHost(rph_.get());
webrtc_logging_controller_ =
WebRtcLoggingController::FromRenderProcessHost(rph_.get());
}
void CreateUnManagedProfile() {
browser_context_ =
CreateBrowserContext("browser_context_", false, std::nullopt);
CreateRenderHost();
}
std::unique_ptr<TestingProfile> CreateBrowserContext(
std::string profile_name,
bool is_managed_profile,
std::optional<bool> text_log_collection_allowed) {
// If profile name not specified, select a unique name.
if (profile_name.empty()) {
static size_t index = 0;
profile_name = base::NumberToString(++index);
}
// Set a directory for the profile, derived from its name, so that
// recreating the profile will get the same directory.
const base::FilePath profile_path =
profiles_dir_.GetPath().AppendASCII(profile_name);
if (base::PathExists(profile_path)) {
EXPECT_TRUE(base::DirectoryExists(profile_path));
} else {
EXPECT_TRUE(base::CreateDirectory(profile_path));
}
// Prepare to specify preferences for the profile.
sync_preferences::PrefServiceMockFactory factory;
factory.set_user_prefs(base::WrapRefCounted(new TestingPrefStore()));
scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
new user_prefs::PrefRegistrySyncable);
sync_preferences::PrefServiceSyncable* regular_prefs =
factory.CreateSyncable(registry.get()).release();
// Set the preference associated with the policy for
// WebRtcTextLogCollectionAllowed
RegisterUserProfilePrefs(registry.get());
if (text_log_collection_allowed.has_value()) {
regular_prefs->SetBoolean(prefs::kWebRtcTextLogCollectionAllowed,
text_log_collection_allowed.value());
}
// Build the profile.
TestingProfile::Builder profile_builder;
profile_builder.SetProfileName(profile_name);
profile_builder.SetPath(profile_path);
profile_builder.SetPrefService(base::WrapUnique(regular_prefs));
profile_builder.OverridePolicyConnectorIsManagedForTesting(
is_managed_profile);
std::unique_ptr<TestingProfile> profile = profile_builder.Build();
return profile;
}
// The directory which will contain all profiles.
base::ScopedTempDir profiles_dir_;
// Default BrowserContext
std::unique_ptr<TestingProfile> browser_context_;
std::unique_ptr<MockRenderProcessHost> rph_;
// Class under test.
raw_ptr<WebRtcLoggingController> webrtc_logging_controller_ = nullptr;
// Testing utilities.
content::BrowserTaskEnvironment task_environment_;
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory>
test_shared_url_loader_factory_;
};
TEST_F(WebRtcLoggingControllerTest, ManagedProfileWithTruePolicy) {
LoadMainTestProfile(true);
EXPECT_TRUE(webrtc_logging_controller_->IsWebRtcTextLogAllowed(
browser_context_.get()));
}
TEST_F(WebRtcLoggingControllerTest, ManagedProfileWithFalsePolicy) {
LoadMainTestProfile(false);
EXPECT_FALSE(webrtc_logging_controller_->IsWebRtcTextLogAllowed(
browser_context_.get()));
}
TEST_F(WebRtcLoggingControllerTest, ManagedProfileWithUnsetPolicy) {
LoadMainTestProfile(std::nullopt);
EXPECT_TRUE(webrtc_logging_controller_->IsWebRtcTextLogAllowed(
browser_context_.get()));
}
TEST_F(WebRtcLoggingControllerTest, IncognitoWithUnsetPolicy) {
LoadMainTestProfile(std::nullopt);
Profile* incognito_profile =
browser_context_->GetPrimaryOTRProfile(/*create_if_needed=*/true);
EXPECT_TRUE(
webrtc_logging_controller_->IsWebRtcTextLogAllowed(incognito_profile));
browser_context_->DestroyOffTheRecordProfile(incognito_profile);
}
TEST_F(WebRtcLoggingControllerTest, UnmanagedProfileWithUnsetPolicy) {
CreateUnManagedProfile();
EXPECT_TRUE(webrtc_logging_controller_->IsWebRtcTextLogAllowed(
browser_context_.get()));
}
TEST_F(WebRtcLoggingControllerTest, NullBrowserContext) {
EXPECT_TRUE(webrtc_logging_controller_->IsWebRtcTextLogAllowed(nullptr));
}
} // namespace webrtc_text_log
|