OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/diagnostics/diagnostics_controller.h" | 5 #include "chrome/browser/diagnostics/diagnostics_controller.h" |
6 | 6 |
| 7 #include "base/base_paths.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "chrome/browser/diagnostics/diagnostics_model.h" | 13 #include "chrome/browser/diagnostics/diagnostics_model.h" |
13 #include "chrome/browser/diagnostics/diagnostics_writer.h" | 14 #include "chrome/browser/diagnostics/diagnostics_writer.h" |
14 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 15 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
(...skipping 16 matching lines...) Expand all Loading... |
33 PathService::Get(chrome::DIR_TEST_DATA, &test_data); | 34 PathService::Get(chrome::DIR_TEST_DATA, &test_data); |
34 test_data = test_data.Append(FILE_PATH_LITERAL("diagnostics")); | 35 test_data = test_data.Append(FILE_PATH_LITERAL("diagnostics")); |
35 test_data = test_data.Append(FILE_PATH_LITERAL("user")); | 36 test_data = test_data.Append(FILE_PATH_LITERAL("user")); |
36 base::CopyDirectory(test_data, temp_dir_.path(), true); | 37 base::CopyDirectory(test_data, temp_dir_.path(), true); |
37 profile_dir_ = temp_dir_.path().Append(FILE_PATH_LITERAL("user")); | 38 profile_dir_ = temp_dir_.path().Append(FILE_PATH_LITERAL("user")); |
38 | 39 |
39 #if defined(OS_CHROMEOS) | 40 #if defined(OS_CHROMEOS) |
40 // Redirect the home dir to the profile directory. We have to do this | 41 // Redirect the home dir to the profile directory. We have to do this |
41 // because NSS uses the HOME directory to find where to store it's database, | 42 // because NSS uses the HOME directory to find where to store it's database, |
42 // so that's where the diagnostics and recovery code looks for it. | 43 // so that's where the diagnostics and recovery code looks for it. |
43 | 44 PathService::Get(base::DIR_HOME, &old_home_dir_); |
44 // Preserve existing home directory setting, if any. | 45 PathService::Override(base::DIR_HOME, profile_dir_); |
45 const char* old_home_dir = ::getenv("HOME"); | |
46 if (old_home_dir) | |
47 old_home_dir_ = old_home_dir; | |
48 else | |
49 old_home_dir_.clear(); | |
50 ::setenv("HOME", profile_dir_.value().c_str(), 1); | |
51 #endif | 46 #endif |
52 | 47 |
53 cmdline_ = CommandLine(CommandLine::NO_PROGRAM); | 48 cmdline_ = CommandLine(CommandLine::NO_PROGRAM); |
54 cmdline_.AppendSwitchPath(switches::kUserDataDir, profile_dir_); | 49 cmdline_.AppendSwitchPath(switches::kUserDataDir, profile_dir_); |
55 cmdline_.AppendSwitch(switches::kDiagnostics); | 50 cmdline_.AppendSwitch(switches::kDiagnostics); |
56 cmdline_.AppendSwitch(switches::kDiagnosticsRecovery); | 51 cmdline_.AppendSwitch(switches::kDiagnosticsRecovery); |
57 writer_.reset(); | 52 writer_.reset(); |
58 // Use this writer instead, if you want to see the diagnostics output. | 53 // Use this writer instead, if you want to see the diagnostics output. |
59 // writer_.reset(new DiagnosticsWriter(DiagnosticsWriter::MACHINE)); | 54 // writer_.reset(new DiagnosticsWriter(DiagnosticsWriter::MACHINE)); |
60 } | 55 } |
61 | 56 |
62 virtual void TearDown() { | 57 virtual void TearDown() { |
63 DiagnosticsController::GetInstance()->ClearResults(); | 58 DiagnosticsController::GetInstance()->ClearResults(); |
64 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
65 if (!old_home_dir_.empty()) { | 60 PathService::Override(base::DIR_HOME, old_home_dir_); |
66 ::setenv("HOME", old_home_dir_.c_str(), 1); | |
67 } else { | |
68 ::unsetenv("HOME"); | |
69 } | |
70 old_home_dir_.clear(); | 61 old_home_dir_.clear(); |
71 #endif | 62 #endif |
72 } | 63 } |
73 | 64 |
74 void CorruptDataFile(const base::FilePath& path) { | 65 void CorruptDataFile(const base::FilePath& path) { |
75 // Just write some random characters into the file tInvaludUsero "corrupt" | 66 // Just write some random characters into the file tInvaludUsero "corrupt" |
76 // it. | 67 // it. |
77 const char bogus_data[] = "wwZ2uNYNuyUVzFbDm3DL"; | 68 const char bogus_data[] = "wwZ2uNYNuyUVzFbDm3DL"; |
78 base::WriteFile(path, bogus_data, arraysize(bogus_data)); | 69 base::WriteFile(path, bogus_data, arraysize(bogus_data)); |
79 } | 70 } |
80 | 71 |
81 scoped_ptr<DiagnosticsModel> model_; | 72 scoped_ptr<DiagnosticsModel> model_; |
82 CommandLine cmdline_; | 73 CommandLine cmdline_; |
83 base::ScopedTempDir temp_dir_; | 74 base::ScopedTempDir temp_dir_; |
84 scoped_ptr<DiagnosticsWriter> writer_; | 75 scoped_ptr<DiagnosticsWriter> writer_; |
85 base::FilePath profile_dir_; | 76 base::FilePath profile_dir_; |
86 | 77 |
87 #if defined(OS_CHROMEOS) | 78 #if defined(OS_CHROMEOS) |
88 std::string old_home_dir_; | 79 base::FilePath old_home_dir_; |
89 #endif | 80 #endif |
90 | 81 |
91 DISALLOW_COPY_AND_ASSIGN(DiagnosticsControllerTest); | 82 DISALLOW_COPY_AND_ASSIGN(DiagnosticsControllerTest); |
92 }; | 83 }; |
93 | 84 |
94 TEST_F(DiagnosticsControllerTest, Diagnostics) { | 85 TEST_F(DiagnosticsControllerTest, Diagnostics) { |
95 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get()); | 86 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get()); |
96 EXPECT_TRUE(DiagnosticsController::GetInstance()->HasResults()); | 87 EXPECT_TRUE(DiagnosticsController::GetInstance()->HasResults()); |
97 const DiagnosticsModel& results = | 88 const DiagnosticsModel& results = |
98 DiagnosticsController::GetInstance()->GetResults(); | 89 DiagnosticsController::GetInstance()->GetResults(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult()); | 151 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult()); |
161 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode()); | 152 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode()); |
162 | 153 |
163 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get()); | 154 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get()); |
164 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult()); | 155 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult()); |
165 EXPECT_FALSE(base::PathExists(db_path)); | 156 EXPECT_FALSE(base::PathExists(db_path)); |
166 } | 157 } |
167 #endif | 158 #endif |
168 | 159 |
169 } // namespace diagnostics | 160 } // namespace diagnostics |
OLD | NEW |