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

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: Created 7 years, 5 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 "chrome/browser/diagnostics/diagnostics_model.h"
12 #include "chrome/browser/diagnostics/diagnostics_writer.h"
13 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chromeos/chromeos_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace diagnostics {
19
20 namespace {
21 const base::FilePath::CharType kTestUserHomeData[] =
22 FILE_PATH_LITERAL("chrome/test/data/diagnostics/user");
23 }
24
25 // Basic harness to acquire and release the required temporary environment to
26 // run a test in.
27 class DiagnosticsControllerTest : public testing::Test {
28 protected:
29 DiagnosticsControllerTest()
30 : cmdline_(CommandLine::NO_PROGRAM) {
31 }
32
33 virtual ~DiagnosticsControllerTest() { }
34
35 virtual void SetUp() {
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
37 base::CopyDirectory(
38 base::FilePath(kTestUserHomeData), temp_dir_.path(), true);
39 profile_dir_ = temp_dir_.path().Append(FILE_PATH_LITERAL("user"));
40
41 #if defined(OS_CHROMEOS)
42 // Redirect the home dir to the profile directory. We have to do this
43 // because NSS uses the HOME directory to find where to store it's database,
44 // so that's where the diagnostics and recovery code looks for it.
45
46 // Preserve existing home directory setting, if any.
47 const char* old_home_dir = ::getenv("HOME");
48 if (old_home_dir)
49 old_home_dir_ = old_home_dir;
50 else
51 old_home_dir_.clear();
52 ::setenv("HOME", profile_dir_.value().c_str(), 1);
53 #endif
54
55 cmdline_ = CommandLine(CommandLine::NO_PROGRAM);
56 cmdline_.AppendSwitchPath(switches::kUserDataDir, profile_dir_);
57 cmdline_.AppendSwitch(switches::kDiagnostics);
58 cmdline_.AppendSwitch(switches::kDiagnosticsRecovery);
59 writer_.reset();
60 // Use this writer instead, if you want to see the diagnostics output.
61 // writer_.reset(new DiagnosticsWriter(DiagnosticsWriter::MACHINE));
62 }
63
64 virtual void TearDown() {
65 DiagnosticsController::GetInstance()->ClearResults();
66 #if defined(OS_CHROMEOS)
67 if (!old_home_dir_.empty()) {
68 ::setenv("HOME", old_home_dir_.c_str(), 1);
69 } else {
70 ::unsetenv("HOME");
71 }
72 old_home_dir_.clear();
73 #endif
74 }
75
76 void CorruptDataFile(const base::FilePath& path) {
77 // Just write some random characters into the file to "corrupt" it.
78 const std::string bogus_data = "wwZ2uNYNuyUVzFbDm3DL";
79 file_util::WriteFile(path, bogus_data.c_str(), bogus_data.size());
cpu_(ooo_6.6-7.5) 2013/07/25 19:40:56 bogus_data[] = "wwZ2uNYNuyUVzFbDm3DL";
Greg Spencer (Chromium) 2013/07/26 18:51:30 Done.
80 }
81
82 scoped_ptr<DiagnosticsModel> model_;
83 CommandLine cmdline_;
84 base::ScopedTempDir temp_dir_;
85 scoped_ptr<DiagnosticsWriter> writer_;
86 base::FilePath profile_dir_;
87
88 #if defined(OS_CHROMEOS)
89 std::string old_home_dir_;
90 #endif
91
92 DISALLOW_COPY_AND_ASSIGN(DiagnosticsControllerTest);
93 };
94
95 TEST_F(DiagnosticsControllerTest, Diagnostics) {
96 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
97 EXPECT_TRUE(DiagnosticsController::GetInstance()->HasResults());
98 const DiagnosticsModel& results =
99 DiagnosticsController::GetInstance()->GetResults();
100 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
101 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
102 for (int i = 0; i < results.GetTestRunCount(); ++i) {
103 const DiagnosticsModel::TestInfo& info(results.GetTest(i));
104 EXPECT_EQ(DiagnosticsModel::TEST_OK, info.GetResult());
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());
119 }
120 }
121
122 #if defined(OS_CHROMEOS)
123 TEST_F(DiagnosticsControllerTest, RecoverFromNssCertDbFailure) {
124 base::FilePath db_path = profile_dir_.Append(chromeos::kNssCertDbPath);
125 EXPECT_TRUE(base::PathExists(db_path));
126 CorruptDataFile(db_path);
127 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
128 ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
129 const DiagnosticsModel& results =
130 DiagnosticsController::GetInstance()->GetResults();
131 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
132 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
133
134 const DiagnosticsModel::TestInfo* info = NULL;
135 EXPECT_TRUE(results.GetTestInfo(kSQLiteIntegrityNSSCertTest, &info));
136 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
137 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode());
138
139 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
140 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
141 EXPECT_FALSE(base::PathExists(db_path));
142 }
143
144 TEST_F(DiagnosticsControllerTest, RecoverFromNssKeyDbFailure) {
145 base::FilePath db_path = profile_dir_.Append(chromeos::kNssKeyDbPath);
146 EXPECT_TRUE(base::PathExists(db_path));
147 CorruptDataFile(db_path);
148 DiagnosticsController::GetInstance()->Run(cmdline_, writer_.get());
149 ASSERT_TRUE(DiagnosticsController::GetInstance()->HasResults());
150 const DiagnosticsModel& results =
151 DiagnosticsController::GetInstance()->GetResults();
152 EXPECT_EQ(results.GetTestRunCount(), results.GetTestAvailableCount());
153 EXPECT_EQ(DiagnosticsModel::kDiagnosticsTestCount, results.GetTestRunCount());
154
155 const DiagnosticsModel::TestInfo* info = NULL;
156 EXPECT_TRUE(results.GetTestInfo(kSQLiteIntegrityNSSKeyTest, &info));
157 EXPECT_EQ(DiagnosticsModel::TEST_FAIL_CONTINUE, info->GetResult());
158 EXPECT_EQ(DIAG_SQLITE_ERROR_HANDLER_CALLED, info->GetOutcomeCode());
159
160 DiagnosticsController::GetInstance()->RunRecovery(cmdline_, writer_.get());
161 EXPECT_EQ(DiagnosticsModel::RECOVERY_OK, info->GetResult());
162 EXPECT_FALSE(base::PathExists(db_path));
163 }
164 #endif
165
166 } // namespace diagnostics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698