Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/diagnostics/diagnostics_controller_unittest.cc

Issue 16948012: This adds a recovery mode to the diagnostics (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Upload after merge Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/diagnostics/diagnostics_controller.h"
6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "chrome/browser/diagnostics/diagnostics_model.h"
13 #include "chrome/browser/diagnostics/diagnostics_writer.h"
14 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chromeos/chromeos_constants.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace diagnostics {
21
22 // Basic harness to acquire and release the required temporary environment to
23 // run a test in.
24 class DiagnosticsControllerTest : public testing::Test {
25 protected:
26 DiagnosticsControllerTest() : cmdline_(CommandLine::NO_PROGRAM) {}
27
28 virtual ~DiagnosticsControllerTest() {}
29
30 virtual void SetUp() {
31 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
32 base::FilePath test_data;
33 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("user"));
36 base::CopyDirectory(test_data, temp_dir_.path(), true);
37 profile_dir_ = temp_dir_.path().Append(FILE_PATH_LITERAL("user"));
38
39 #if defined(OS_CHROMEOS)
40 // 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 // so that's where the diagnostics and recovery code looks for it.
43
44 // Preserve existing home directory setting, if any.
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
52
53 cmdline_ = CommandLine(CommandLine::NO_PROGRAM);
54 cmdline_.AppendSwitchPath(switches::kUserDataDir, profile_dir_);
55 cmdline_.AppendSwitch(switches::kDiagnostics);
56 cmdline_.AppendSwitch(switches::kDiagnosticsRecovery);
57 writer_.reset();
58 // Use this writer instead, if you want to see the diagnostics output.
59 // writer_.reset(new DiagnosticsWriter(DiagnosticsWriter::MACHINE));
60 }
61
62 virtual void TearDown() {
63 DiagnosticsController::GetInstance()->ClearResults();
64 #if defined(OS_CHROMEOS)
65 if (!old_home_dir_.empty()) {
66 ::setenv("HOME", old_home_dir_.c_str(), 1);
67 } else {
68 ::unsetenv("HOME");
69 }
70 old_home_dir_.clear();
71 #endif
72 }
73
74 void CorruptDataFile(const base::FilePath& path) {
75 // Just write some random characters into the file tInvaludUsero "corrupt"
76 // it.
77 const char bogus_data[] = "wwZ2uNYNuyUVzFbDm3DL";
78 file_util::WriteFile(path, bogus_data, arraysize(bogus_data));
79 }
80
81 scoped_ptr<DiagnosticsModel> model_;
82 CommandLine cmdline_;
83 base::ScopedTempDir temp_dir_;
84 scoped_ptr<DiagnosticsWriter> writer_;
85 base::FilePath profile_dir_;
86
87 #if defined(OS_CHROMEOS)
88 std::string old_home_dir_;
89 #endif
90
91 DISALLOW_COPY_AND_ASSIGN(DiagnosticsControllerTest);
92 };
93
94 TEST_F(DiagnosticsControllerTest, Diagnostics) {
95 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
96 EXPECT_TRUE(DiagnosticsController::GetInstance()->HasResults());
97 const DiagnosticsModel& results =
98 DiagnosticsController::GetInstance()->GetResults();
99 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
100 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
101 for (int i = 0; i < results.GetTestRunCount(); ++i) {
102 const DiagnosticsModel::TestInfo& info(results.GetTest(i));
103 EXPECT_EQ(DiagnosticsModel::TEST_OK, info.GetResult()) << "Test: "
104 << info.GetId();
105 }
106 }
107
108 TEST_F(DiagnosticsControllerTest, RecoverAllOK) {
109 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
110 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
111 EXPECT_TRUE(DiagnosticsController::GetInstance()->HasResults());
112 const DiagnosticsModel& results =
113 DiagnosticsController::GetInstance()->GetResults();
114 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
115 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
116 for (int i = 0; i < results.GetTestRunCount(); ++i) {
117 const DiagnosticsModel::TestInfo& info(results.GetTest(i));
118 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info.GetResult()) << "Test: "
119 << info.GetId();
120 }
121 }
122
123 #if defined(OS_CHROMEOS)
124 TEST_F(DiagnosticsControllerTest, RecoverFromNssCertDbFailure) {
125 base::FilePath db_path = profile_dir_.Append(chromeos::kNssCertDbPath);
126 EXPECT_TRUE(base::PathExists(db_path));
127 CorruptDataFile(db_path);
128 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
129 ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
130 const DiagnosticsModel& results =
131 DiagnosticsController::GetInstance()->GetResults();
132 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
133 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
134
135 const DiagnosticsModel::TestInfo* info = NULL;
136 EXPECT_TRUE(results.GetTestInfo(kSQLiteIntegrityNSSCertTest, &info));
137 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
138 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode());
139
140 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
141 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
142 EXPECT_FALSE(base::PathExists(db_path));
143 }
144
145 TEST_F(DiagnosticsControllerTest, RecoverFromNssKeyDbFailure) {
146 base::FilePath db_path = profile_dir_.Append(chromeos::kNssKeyDbPath);
147 EXPECT_TRUE(base::PathExists(db_path));
148 CorruptDataFile(db_path);
149 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
150 ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
151 const DiagnosticsModel& results =
152 DiagnosticsController::GetInstance()->GetResults();
153 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
154 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
155
156 const DiagnosticsModel::TestInfo* info = NULL;
157 EXPECT_TRUE(results.GetTestInfo(kSQLiteIntegrityNSSKeyTest, &info));
158 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
159 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode());
160
161 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
162 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
163 EXPECT_FALSE(base::PathExists(db_path));
164 }
165 #endif
166
167 } // namespace diagnostics
OLDNEW
« no previous file with comments | « chrome/browser/diagnostics/diagnostics_controller.cc ('k') | chrome/browser/diagnostics/diagnostics_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698