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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file declares helper functions for use in tests that expect a valid
// installation, possibly of a specific type. Validation violations result in
// test failures.
#include "chrome/installer/util/installation_validation_helper.h"
#include "base/logging.h"
#include "base/strings/string_piece.h"
#include "chrome/installer/util/installation_state.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace installer {
namespace {
// A helper class that installs a log message handler to add a test failure for
// each ERROR message. Only one instance of this class may be live at a time.
class FailureLogHelper {
public:
FailureLogHelper();
~FailureLogHelper();
private:
static bool AddFailureForLogMessage(int severity,
const char* file,
int line,
size_t message_start,
const std::string& str);
static const logging::LogSeverity kViolationSeverity_;
static logging::LogMessageHandlerFunction old_message_handler_;
static int old_min_log_level_;
};
// InstallationValidator logs all violations at ERROR level.
// static
const logging::LogSeverity
FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR;
// static
logging::LogMessageHandlerFunction
FailureLogHelper::old_message_handler_ = NULL;
// static
int FailureLogHelper::old_min_log_level_ =
FailureLogHelper::kViolationSeverity_;
FailureLogHelper::FailureLogHelper() {
LOG_ASSERT(old_message_handler_ == NULL);
// The validator logs at ERROR level. Ensure that it generates messages so we
// can transform them into test failures.
old_min_log_level_ = logging::GetMinLogLevel();
if (old_min_log_level_ > kViolationSeverity_)
logging::SetMinLogLevel(kViolationSeverity_);
old_message_handler_ = logging::GetLogMessageHandler();
logging::SetLogMessageHandler(&AddFailureForLogMessage);
}
FailureLogHelper::~FailureLogHelper() {
logging::SetLogMessageHandler(old_message_handler_);
old_message_handler_ = NULL;
if (old_min_log_level_ > kViolationSeverity_)
logging::SetMinLogLevel(old_min_log_level_);
}
// A logging::LogMessageHandlerFunction that adds a non-fatal test failure
// (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at
// the severity of validation violations. All other messages are sent through
// the default logging pipeline.
// static
bool FailureLogHelper::AddFailureForLogMessage(int severity,
const char* file,
int line,
size_t message_start,
const std::string& str) {
if (severity == kViolationSeverity_ && !str.empty()) {
// Remove the trailing newline, if present.
size_t message_length = str.size() - message_start;
if (*str.rbegin() == '\n')
--message_length;
ADD_FAILURE_AT(file, line)
<< base::StringPiece(str.c_str() + message_start, message_length);
return true;
}
if (old_message_handler_ != NULL)
return (old_message_handler_)(severity, file, line, message_start, str);
return false;
}
} // namespace
InstallationValidator::InstallationType ExpectValidInstallation(
bool system_level) {
FailureLogHelper log_helper;
InstallationValidator::InstallationType found_type =
InstallationValidator::NO_PRODUCTS;
EXPECT_TRUE(InstallationValidator::ValidateInstallationType(system_level,
&found_type));
return found_type;
}
InstallationValidator::InstallationType ExpectValidInstallationForState(
const InstallationState& machine_state,
bool system_level) {
FailureLogHelper log_helper;
InstallationValidator::InstallationType found_type =
InstallationValidator::NO_PRODUCTS;
EXPECT_TRUE(InstallationValidator::ValidateInstallationTypeForState(
machine_state, system_level, &found_type));
return found_type;
}
void ExpectInstallationOfType(bool system_level,
InstallationValidator::InstallationType type) {
EXPECT_EQ(type, ExpectValidInstallation(system_level));
}
void ExpectInstallationOfTypeForState(
const InstallationState& machine_state,
bool system_level,
InstallationValidator::InstallationType type) {
EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
}
} // namespace installer
|