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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/diagnostics/diagnostics_log_controller.h"
#include <memory>
#include <string>
#include <vector>
#include "ash/public/cpp/session/session_types.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/diagnostics/diagnostics_browser_delegate.h"
#include "ash/system/diagnostics/fake_diagnostics_browser_delegate.h"
#include "ash/system/diagnostics/keyboard_input_log.h"
#include "ash/system/diagnostics/log_test_helpers.h"
#include "ash/system/diagnostics/networking_log.h"
#include "ash/system/diagnostics/routine_log.h"
#include "ash/system/diagnostics/telemetry_log.h"
#include "ash/test/ash_test_base.h"
#include "ash/webui/diagnostics_ui/mojom/network_health_provider.mojom-forward.h"
#include "ash/webui/diagnostics_ui/mojom/network_health_provider.mojom-shared.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "components/user_manager/user_type.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace diagnostics {
namespace {
const char kTestSessionLogFileName[] = "test_session_log.txt";
const char kDiangosticsDirName[] = "diagnostics";
const char kTmpDiagnosticsDir[] = "/tmp/diagnostics";
const char kTestUserEmail[] = "test-user@gmail.com";
const char kFakeUserDir[] = "fake-user";
// Log headers content.
const char kRoutineLogSubsectionHeader[] = "--- Test Routines ---";
const char kSystemLogSectionHeader[] = "=== System ===";
const char kNetworkingLogSectionHeader[] = "=== Networking ===";
const char kNetworkingLogNetworkInfoHeader[] = "--- Network Info ---";
const char kNetworkingLogNetworkEventsHeader[] = "--- Network Events ---";
const char kKeyboardLogSectionHeader[] = "=== Keyboard ===";
} // namespace
class DiagnosticsLogControllerTest : public NoSessionAshTestBase {
public:
DiagnosticsLogControllerTest() = default;
DiagnosticsLogControllerTest(DiagnosticsLogControllerTest&) = delete;
DiagnosticsLogControllerTest& operator=(DiagnosticsLogControllerTest&) =
delete;
~DiagnosticsLogControllerTest() override = default;
void SetUp() override { NoSessionAshTestBase::SetUp(); }
protected:
base::FilePath GetSessionLogPath() {
EXPECT_TRUE(save_dir_.CreateUniqueTempDir());
return save_dir_.GetPath().Append(kTestSessionLogFileName);
}
void ResetLogBasePath() {
return DiagnosticsLogController::Get()->ResetLogBasePath();
}
base::FilePath log_base_path() {
return DiagnosticsLogController::Get()->log_base_path_;
}
void SetBrowserDelegate(
std::unique_ptr<DiagnosticsBrowserDelegate> delegate) {
DiagnosticsLogController::Get()->delegate_ = std::move(delegate);
}
void InitializeWithFakeDelegate() {
std::unique_ptr<DiagnosticsBrowserDelegate> delegate =
std::make_unique<FakeDiagnosticsBrowserDelegate>();
DiagnosticsLogController::Initialize(std::move(delegate));
}
void SimulateLockScreen() {
DCHECK(!Shell::Get()->session_controller()->IsScreenLocked());
Shell::Get()->session_controller()->LockScreen();
task_environment()->RunUntilIdle();
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
}
void SimulateUnlockScreen() {
DCHECK(Shell::Get()->session_controller()->IsScreenLocked());
SessionInfo info;
info.state = session_manager::SessionState::ACTIVE;
Shell::Get()->session_controller()->SetSessionInfo(std::move(info));
task_environment()->RunUntilIdle();
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
}
void SimulateLogoutActiveUser() {
Shell::Get()->session_controller()->RequestSignOut();
task_environment()->RunUntilIdle();
EXPECT_FALSE(
Shell::Get()->session_controller()->IsActiveUserSessionStarted());
}
private:
base::ScopedTempDir save_dir_;
};
TEST_F(DiagnosticsLogControllerTest,
ShellProvidesControllerWhenFeatureEnabled) {
EXPECT_NO_FATAL_FAILURE(DiagnosticsLogController::Get());
EXPECT_NE(nullptr, DiagnosticsLogController::Get());
}
TEST_F(DiagnosticsLogControllerTest, IsInitializedAfterDelegateProvided) {
EXPECT_NE(nullptr, DiagnosticsLogController::Get());
EXPECT_FALSE(DiagnosticsLogController::IsInitialized());
InitializeWithFakeDelegate();
EXPECT_TRUE(DiagnosticsLogController::IsInitialized());
}
TEST_F(DiagnosticsLogControllerTest, GenerateSessionString) {
base::ScopedTempDir scoped_diagnostics_log_dir;
EXPECT_TRUE(scoped_diagnostics_log_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_diagnostics_log_dir.GetPath().Append(kFakeUserDir));
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
// Create keyboard input log.
KeyboardInputLog& keyboard_input_log =
DiagnosticsLogController::Get()->GetKeyboardInputLog();
keyboard_input_log.AddKeyboard(/*id=*/1, "internal keyboard");
keyboard_input_log.CreateLogAndRemoveKeyboard(/*id=*/1);
task_environment()->RunUntilIdle();
const std::string contents =
DiagnosticsLogController::Get()->GenerateSessionStringOnBlockingPool();
const std::vector<std::string> log_lines = GetLogLines(contents);
EXPECT_EQ(10u, log_lines.size());
EXPECT_EQ(kSystemLogSectionHeader, log_lines[0]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[1]);
const std::string expected_no_routine_msg =
"No routines of this type were run in the session.";
EXPECT_EQ(expected_no_routine_msg, log_lines[2]);
EXPECT_EQ(kNetworkingLogSectionHeader, log_lines[3]);
EXPECT_EQ(kNetworkingLogNetworkInfoHeader, log_lines[4]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[5]);
EXPECT_EQ(expected_no_routine_msg, log_lines[6]);
EXPECT_EQ(kNetworkingLogNetworkEventsHeader, log_lines[7]);
EXPECT_EQ(kKeyboardLogSectionHeader, log_lines[8]);
}
TEST_F(DiagnosticsLogControllerTest, GenerateSessionLogOnBlockingPoolFile) {
base::ScopedTempDir scoped_diagnostics_log_dir;
EXPECT_TRUE(scoped_diagnostics_log_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_diagnostics_log_dir.GetPath().Append(kFakeUserDir));
const base::FilePath expected_diagnostics_log_path =
expected_path_regular_user.Append(kDiangosticsDirName);
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
// Create keyboard input log.
KeyboardInputLog& keyboard_input_log =
DiagnosticsLogController::Get()->GetKeyboardInputLog();
keyboard_input_log.AddKeyboard(/*id=*/1, "internal keyboard");
keyboard_input_log.CreateLogAndRemoveKeyboard(/*id=*/1);
task_environment()->RunUntilIdle();
const base::FilePath save_file_path = GetSessionLogPath();
EXPECT_TRUE(DiagnosticsLogController::Get()->GenerateSessionLogOnBlockingPool(
save_file_path));
EXPECT_TRUE(base::PathExists(save_file_path));
std::string contents;
EXPECT_TRUE(base::ReadFileToString(save_file_path, &contents));
const std::vector<std::string> log_lines = GetLogLines(contents);
EXPECT_EQ(10u, log_lines.size());
EXPECT_EQ(kSystemLogSectionHeader, log_lines[0]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[1]);
const std::string expected_no_routine_msg =
"No routines of this type were run in the session.";
EXPECT_EQ(expected_no_routine_msg, log_lines[2]);
EXPECT_EQ(kNetworkingLogSectionHeader, log_lines[3]);
EXPECT_EQ(kNetworkingLogNetworkInfoHeader, log_lines[4]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[5]);
EXPECT_EQ(expected_no_routine_msg, log_lines[6]);
EXPECT_EQ(kNetworkingLogNetworkEventsHeader, log_lines[7]);
EXPECT_EQ(kKeyboardLogSectionHeader, log_lines[8]);
}
TEST_F(DiagnosticsLogControllerTest,
GenerateWithRoutinesSessionLogOnBlockingPoolFile) {
base::ScopedTempDir scoped_diagnostics_log_dir;
EXPECT_TRUE(scoped_diagnostics_log_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_diagnostics_log_dir.GetPath().Append(kFakeUserDir));
const base::FilePath expected_diagnostics_log_path =
expected_path_regular_user.Append(kDiangosticsDirName);
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
RoutineLog& routine_log = DiagnosticsLogController::Get()->GetRoutineLog();
routine_log.LogRoutineCancelled(mojom::RoutineType::kArcHttp);
routine_log.LogRoutineCancelled(mojom::RoutineType::kBatteryCharge);
task_environment()->RunUntilIdle();
// Create keyboard input log.
KeyboardInputLog& keyboard_input_log =
DiagnosticsLogController::Get()->GetKeyboardInputLog();
keyboard_input_log.AddKeyboard(/*id=*/1, "internal keyboard");
keyboard_input_log.CreateLogAndRemoveKeyboard(/*id=*/1);
task_environment()->RunUntilIdle();
// Generate log file at test path.
const base::FilePath save_file_path = GetSessionLogPath();
EXPECT_TRUE(DiagnosticsLogController::Get()->GenerateSessionLogOnBlockingPool(
save_file_path));
EXPECT_TRUE(base::PathExists(save_file_path));
std::string contents;
EXPECT_TRUE(base::ReadFileToString(save_file_path, &contents));
const std::vector<std::string> log_lines = GetLogLines(contents);
EXPECT_EQ(10u, log_lines.size());
// System state and routine data.
EXPECT_EQ(kSystemLogSectionHeader, log_lines[0]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[1]);
const std::string expected_canceled_routine_msg =
"Inflight Routine Cancelled";
auto system_routine_line = GetLogLineContents(log_lines[2]);
EXPECT_EQ(2u, system_routine_line.size());
EXPECT_EQ(expected_canceled_routine_msg, system_routine_line[1]);
// Network state and routine data.
EXPECT_EQ(kNetworkingLogSectionHeader, log_lines[3]);
EXPECT_EQ(kNetworkingLogNetworkInfoHeader, log_lines[4]);
EXPECT_EQ(kRoutineLogSubsectionHeader, log_lines[5]);
auto network_routine_line = GetLogLineContents(log_lines[6]);
EXPECT_EQ(2u, network_routine_line.size());
EXPECT_EQ(expected_canceled_routine_msg, network_routine_line[1]);
EXPECT_EQ(kNetworkingLogNetworkEventsHeader, log_lines[7]);
EXPECT_EQ(kKeyboardLogSectionHeader, log_lines[8]);
}
TEST_F(DiagnosticsLogControllerTest,
ResetAndInitializeShouldNotLookupProfilePath) {
const base::FilePath expected_path_not_regular_user =
base::FilePath(kTmpDiagnosticsDir);
// Simulate called before delegate configured.
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
InitializeWithFakeDelegate();
// Simulate sign-in user.
ClearLogin();
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateGuestLogin();
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateKioskMode(user_manager::UserType::kKioskChromeApp);
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateKioskMode(user_manager::UserType::kKioskWebApp);
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
}
TEST_F(DiagnosticsLogControllerTest,
ResetAndInitializeShouldLookupProfileUserEmptyPath) {
const base::FilePath expected_path_not_regular_user =
base::FilePath(kTmpDiagnosticsDir);
// Simulate DiagnosticsBrowserDelegate returning empty path.
std::unique_ptr<DiagnosticsBrowserDelegate> delegate_with_empty_file_path =
std::make_unique<FakeDiagnosticsBrowserDelegate>(base::FilePath());
SetBrowserDelegate(std::move(delegate_with_empty_file_path));
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
}
TEST_F(DiagnosticsLogControllerTest,
ResetAndInitializeForShouldLookupProfileUserNonEmptyPath) {
InitializeWithFakeDelegate();
const base::FilePath expected_path_regular_user =
base::FilePath(kDefaultUserDir).Append(kDiangosticsDirName);
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Get()->ResetAndInitializeLogWriters();
EXPECT_EQ(expected_path_regular_user, log_base_path());
}
TEST_F(DiagnosticsLogControllerTest,
LogBaseCorrectlyUpdatedOnActiveUserSessionChanged) {
const base::FilePath expected_path_not_regular_user =
base::FilePath(kTmpDiagnosticsDir);
InitializeWithFakeDelegate();
// Simulate sign-in user.
ClearLogin();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateGuestLogin();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateKioskMode(user_manager::UserType::kKioskChromeApp);
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateKioskMode(user_manager::UserType::kKioskWebApp);
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
SimulateUserLogin({kTestUserEmail});
const base::FilePath expected_path_regular_user =
base::FilePath(kDefaultUserDir).Append(kDiangosticsDirName);
EXPECT_EQ(expected_path_regular_user, log_base_path());
SimulateLockScreen();
EXPECT_EQ(expected_path_regular_user, log_base_path());
SimulateUnlockScreen();
EXPECT_EQ(expected_path_regular_user, log_base_path());
SimulateLogoutActiveUser();
EXPECT_EQ(expected_path_not_regular_user, log_base_path());
}
TEST_F(DiagnosticsLogControllerTest, LogsDeletedOnUserSignin) {
base::ScopedTempDir scoped_dir;
EXPECT_TRUE(scoped_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_dir.GetPath().Append(kFakeUserDir));
const base::FilePath expected_diagnostics_log_path =
expected_path_regular_user.Append(kDiangosticsDirName);
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
// Create directory after initialize to simulate user sign in when a user ran
// diagnostics previously.
EXPECT_TRUE(base::CreateDirectory(expected_diagnostics_log_path));
EXPECT_TRUE(base::PathExists(expected_diagnostics_log_path));
// Sign in and verify the log directory is deleted.
SimulateUserLogin({kTestUserEmail});
task_environment()->RunUntilIdle();
EXPECT_FALSE(base::PathExists(expected_diagnostics_log_path));
}
TEST_F(DiagnosticsLogControllerTest, SetLogWritersUsingLogBasePath) {
base::ScopedTempDir scoped_dir;
EXPECT_TRUE(scoped_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_dir.GetPath().Append(kFakeUserDir));
const base::FilePath expected_diagnostics_log_path =
expected_path_regular_user.Append(kDiangosticsDirName);
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
// After initialize log writers exist.
EXPECT_EQ(expected_diagnostics_log_path, log_base_path());
NetworkingLog& networking_log =
DiagnosticsLogController::Get()->GetNetworkingLog();
RoutineLog& routine_log = DiagnosticsLogController::Get()->GetRoutineLog();
// Simulate events to write files.
const std::vector<std::string> networks{"fake_guid", "other_fake_guid"};
networking_log.UpdateNetworkList(networks, "fake_guid");
networking_log.UpdateNetworkState(mojom::Network::New());
routine_log.LogRoutineCancelled(mojom::RoutineType::kDnsResolution);
routine_log.LogRoutineCancelled(mojom::RoutineType::kCpuStress);
// Wait for Append tasks which create the logs to complete.
task_environment()->RunUntilIdle();
EXPECT_FALSE(
routine_log.GetContentsForCategory(RoutineLog::RoutineCategory::kNetwork)
.empty());
EXPECT_FALSE(
routine_log.GetContentsForCategory(RoutineLog::RoutineCategory::kSystem)
.empty());
EXPECT_TRUE(base::PathExists(
expected_diagnostics_log_path.Append("network_events.log")));
EXPECT_TRUE(base::PathExists(expected_diagnostics_log_path.Append(
"diagnostics_routines_network.log")));
EXPECT_TRUE(base::PathExists(
expected_diagnostics_log_path.Append("diagnostics_routines_system.log")));
}
TEST_F(DiagnosticsLogControllerTest, ClearLogDirectoryOnInitialize) {
base::ScopedTempDir scoped_dir;
EXPECT_TRUE(scoped_dir.CreateUniqueTempDir());
const base::FilePath expected_path_regular_user =
base::FilePath(scoped_dir.GetPath().Append(kFakeUserDir));
const base::FilePath expected_diagnostics_log_path =
expected_path_regular_user.Append(kDiangosticsDirName);
EXPECT_TRUE(base::CreateDirectory(expected_diagnostics_log_path));
EXPECT_TRUE(base::PathExists(expected_diagnostics_log_path));
SimulateUserLogin({kTestUserEmail});
DiagnosticsLogController::Initialize(
std::make_unique<FakeDiagnosticsBrowserDelegate>(
expected_path_regular_user));
// Wait for delete to complete.
task_environment()->RunUntilIdle();
EXPECT_FALSE(base::PathExists(expected_diagnostics_log_path));
// Before routines updated log file does not exist.
DiagnosticsLogController::Get()->GetRoutineLog().LogRoutineCancelled(
mojom::RoutineType::kDnsResolution);
// Wait for append to write logs.
task_environment()->RunUntilIdle();
EXPECT_EQ(expected_diagnostics_log_path, log_base_path());
EXPECT_TRUE(base::PathExists(expected_diagnostics_log_path));
}
} // namespace diagnostics
} // namespace ash
|