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
|
// Copyright 2020 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/enterprise/remote_commands/clear_browsing_data_job.h"
#include "base/json/json_writer.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/nacl_host/nacl_browser_delegate_impl.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/nacl/browser/nacl_browser.h"
#include "components/nacl/common/buildflags.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace enterprise_commands {
namespace {
const char kProfileName[] = "Test";
const char kProfileNameLowerCase[] = "test";
const policy::RemoteCommandJob::UniqueIDType kUniqueID = 123456789;
const char kTestProfilePath[] = "/Path/To/Profile";
const char kProfilePathField[] = "profile_path";
const char kClearCacheField[] = "clear_cache";
const char kClearCookiesField[] = "clear_cookies";
enterprise_management::RemoteCommand CreateCommandProto(
const std::string& profile_path,
bool clear_cache,
bool clear_cookies) {
enterprise_management::RemoteCommand command_proto;
command_proto.set_type(
enterprise_management::RemoteCommand_Type_BROWSER_CLEAR_BROWSING_DATA);
command_proto.set_command_id(kUniqueID);
base::Value::Dict root;
root.Set(kProfilePathField, profile_path);
root.Set(kClearCacheField, clear_cache);
root.Set(kClearCookiesField, clear_cookies);
std::string payload;
base::JSONWriter::Write(root, &payload);
command_proto.set_payload(payload);
return command_proto;
}
void InitJob(ClearBrowsingDataJob* job,
const enterprise_management::RemoteCommand& command_proto) {
EXPECT_TRUE(job->Init(base::TimeTicks::Now(), command_proto,
enterprise_management::SignedData{}));
EXPECT_EQ(kUniqueID, job->unique_id());
EXPECT_EQ(policy::RemoteCommandJob::NOT_STARTED, job->status());
}
std::unique_ptr<ClearBrowsingDataJob> CreateJob(
const enterprise_management::RemoteCommand& command_proto,
ProfileManager* profile_manager) {
auto job = std::make_unique<ClearBrowsingDataJob>(profile_manager);
InitJob(job.get(), command_proto);
return job;
}
std::unique_ptr<ClearBrowsingDataJob> CreateJob(
const enterprise_management::RemoteCommand& command_proto,
Profile* profile) {
auto job = std::make_unique<ClearBrowsingDataJob>(profile);
InitJob(job.get(), command_proto);
return job;
}
} // namespace
class ClearBrowsingDataJobTest : public ::testing::Test {
protected:
void SetUp() override {
::testing::Test::SetUp();
task_environment_ = std::make_unique<content::BrowserTaskEnvironment>();
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
EXPECT_TRUE(profile_manager_->SetUp());
#if BUILDFLAG(ENABLE_NACL)
// Clearing Cache will clear PNACL cache, which needs this delegate set.
nacl::NaClBrowser::SetDelegate(
std::make_unique<NaClBrowserDelegateImpl>(profile_manager()));
#endif
}
void TearDown() override {
profile_manager_.reset();
task_environment_.reset();
#if BUILDFLAG(ENABLE_NACL)
// Clearing Cache will clear PNACL cache, which needs this delegate set.
nacl::NaClBrowser::ClearAndDeleteDelegate();
#endif
::testing::Test::TearDown();
}
ProfileManager* profile_manager() {
return profile_manager_->profile_manager();
}
void RunUntilIdle() { task_environment_->RunUntilIdle(); }
TestingProfile* AddTestingProfile() {
return profile_manager_->CreateTestingProfile(kProfileName);
}
base::FilePath GetTestProfilePath() {
return profile_manager_->profiles_dir().AppendASCII(kProfileName);
}
base::FilePath GetTestProfilePathLowerCase() {
return profile_manager_->profiles_dir().AppendASCII(kProfileNameLowerCase);
}
private:
std::unique_ptr<content::BrowserTaskEnvironment> task_environment_;
std::unique_ptr<TestingProfileManager> profile_manager_;
};
TEST_F(ClearBrowsingDataJobTest, CanParseWithMissingDataTypes) {
base::RunLoop run_loop;
enterprise_management::RemoteCommand command_proto;
command_proto.set_type(
enterprise_management::RemoteCommand_Type_BROWSER_CLEAR_BROWSING_DATA);
command_proto.set_command_id(kUniqueID);
base::Value::Dict root;
root.Set(kProfilePathField, kTestProfilePath);
std::string payload;
base::JSONWriter::Write(root, &payload);
command_proto.set_payload(payload);
auto job = std::make_unique<ClearBrowsingDataJob>(profile_manager());
EXPECT_TRUE(job->Init(base::TimeTicks::Now(), command_proto,
enterprise_management::SignedData{}));
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::FAILED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, DontInitWhenMissingProfilePath) {
enterprise_management::RemoteCommand command_proto;
command_proto.set_type(
enterprise_management::RemoteCommand_Type_BROWSER_CLEAR_BROWSING_DATA);
command_proto.set_command_id(kUniqueID);
base::Value::Dict root;
root.Set(kClearCacheField, true);
root.Set(kClearCookiesField, true);
std::string payload;
base::JSONWriter::Write(root, &payload);
command_proto.set_payload(payload);
auto job = std::make_unique<ClearBrowsingDataJob>(profile_manager());
EXPECT_FALSE(job->Init(base::TimeTicks::Now(), command_proto,
enterprise_management::SignedData{}));
}
TEST_F(ClearBrowsingDataJobTest, FailureWhenProfileDoesntExist) {
base::RunLoop run_loop;
auto job =
CreateJob(CreateCommandProto(kTestProfilePath, /* clear_cache= */ true,
/* clear_cookies= */ true),
profile_manager());
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::FAILED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, SuccessClearCookies) {
base::RunLoop run_loop;
AddTestingProfile();
auto job = CreateJob(CreateCommandProto(GetTestProfilePath().AsUTF8Unsafe(),
/* clear_cache= */ false,
/* clear_cookies= */ true),
profile_manager());
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::SUCCEEDED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, SuccessClearBoth) {
base::RunLoop run_loop;
AddTestingProfile();
auto job = CreateJob(CreateCommandProto(GetTestProfilePath().AsUTF8Unsafe(),
/* clear_cache= */ true,
/* clear_cookies= */ false),
profile_manager());
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::SUCCEEDED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, SuccessClearCache) {
base::RunLoop run_loop;
AddTestingProfile();
auto job = CreateJob(CreateCommandProto(GetTestProfilePath().AsUTF8Unsafe(),
/* clear_cache= */ true,
/* clear_cookies= */ false),
profile_manager());
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::SUCCEEDED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, SuccessClearNeither) {
base::RunLoop run_loop;
AddTestingProfile();
auto job = CreateJob(CreateCommandProto(GetTestProfilePath().AsUTF8Unsafe(),
/* clear_cache= */ false,
/* clear_cookies= */ false),
profile_manager());
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::SUCCEEDED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
TEST_F(ClearBrowsingDataJobTest, SucessClearWithProfile) {
base::RunLoop run_loop;
TestingProfile* profile = AddTestingProfile();
auto job = CreateJob(CreateCommandProto(GetTestProfilePath().AsUTF8Unsafe(),
/* clear_cache= */ true,
/* clear_cookies= */ false),
profile);
bool done = false;
// Run should return true because the command will be successfully posted,
// but status of the command will be |FAILED| when |finished_callback| is
// invoked.
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(policy::RemoteCommandJob::SUCCEEDED,
job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
// For Windows machines, the path that Chrome reports for the profile is
// "Normalized" to all lower-case on the reporting server. This means that
// when the server sends the command, the path will be all lower case and
// the profile manager won't be able to use it as a key.
// Because of that, the code for this job is slightly different on Windows,
// and this test verifies that behavior.
TEST_F(ClearBrowsingDataJobTest, HandleProfilPathCaseSensitivity) {
base::RunLoop run_loop;
AddTestingProfile();
auto job =
CreateJob(CreateCommandProto(GetTestProfilePathLowerCase().AsUTF8Unsafe(),
/* clear_cache= */ false,
/* clear_cookies= */ true),
profile_manager());
bool done = false;
#if BUILDFLAG(IS_WIN)
// On windows, paths are case-insensitive so passing a lowercase path should
// still result in success.
auto expected = policy::RemoteCommandJob::SUCCEEDED;
#else
// On other platforms, paths are case-sensitive, so passing a lower case path
// will result in the profile not being found.
auto expected = policy::RemoteCommandJob::FAILED;
#endif // BUILDFLAG(IS_WIN)
EXPECT_TRUE(job->Run(base::Time::Now(), base::TimeTicks::Now(),
base::BindLambdaForTesting([&] {
EXPECT_EQ(expected, job->status());
done = true;
run_loop.Quit();
})));
run_loop.Run();
EXPECT_TRUE(done);
}
} // namespace enterprise_commands
|