Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | // This file declares helper functions for use in tests that expect a valid |
| 6 | // installation, possibly of a specific type. Validation violations result in |
| 7 | // test failures. |
| 8 | |
| 9 | #include "chrome/installer/util/installation_validation_helper.h" |
| 10 | |
| 11 | #include "base/logging.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "chrome/installer/util/installation_state.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace installer { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // A helper class that installs a log message handler to add a test failure for |
| 21 | // each ERROR message. Only one instance of this class may be live at a time. |
| 22 | class FailureLogHelper { |
| 23 | public: |
| 24 | FailureLogHelper(); |
| 25 | ~FailureLogHelper(); |
| 26 | |
| 27 | private: |
| 28 | static bool AddFailureForLogMessage(int severity, |
| 29 | const char* file, |
| 30 | int line, |
| 31 | size_t message_start, |
| 32 | const std::string& str); |
| 33 | |
| 34 | static const logging::LogSeverity kViolationSeverity_; |
| 35 | static logging::LogMessageHandlerFunction old_message_handler_; |
| 36 | static int old_min_log_level_; |
| 37 | }; |
| 38 | |
| 39 | // InstallationValidator logs all violations at ERROR level. |
| 40 | // static |
| 41 | const logging::LogSeverity |
| 42 | FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR; |
| 43 | |
| 44 | // static |
| 45 | logging::LogMessageHandlerFunction |
| 46 | FailureLogHelper::old_message_handler_ = NULL; |
| 47 | |
| 48 | // static |
| 49 | int FailureLogHelper::old_min_log_level_ = |
| 50 | FailureLogHelper::kViolationSeverity_; |
| 51 | |
| 52 | FailureLogHelper::FailureLogHelper() { |
| 53 | LOG_ASSERT(old_message_handler_ == NULL); |
| 54 | |
| 55 | // The validator logs at ERROR level. Ensure that it generates messages so we |
| 56 | // can transform them into test failures. |
| 57 | old_min_log_level_ = logging::GetMinLogLevel(); |
| 58 | if (old_min_log_level_ > kViolationSeverity_) |
| 59 | logging::SetMinLogLevel(kViolationSeverity_); |
| 60 | |
| 61 | old_message_handler_ = logging::GetLogMessageHandler(); |
| 62 | logging::SetLogMessageHandler(&AddFailureForLogMessage); |
| 63 | } |
| 64 | |
| 65 | FailureLogHelper::~FailureLogHelper() { |
| 66 | logging::SetLogMessageHandler(old_message_handler_); |
| 67 | old_message_handler_ = NULL; |
| 68 | |
| 69 | if (old_min_log_level_ > kViolationSeverity_) |
| 70 | logging::SetMinLogLevel(old_min_log_level_); |
| 71 | } |
| 72 | |
| 73 | // A logging::LogMessageHandlerFunction that adds a non-fatal test failure |
| 74 | // (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at |
| 75 | // the severity of validation violations. All other messages are sent through |
| 76 | // the default logging pipeline. |
| 77 | // static |
| 78 | bool FailureLogHelper::AddFailureForLogMessage(int severity, |
| 79 | const char* file, |
| 80 | int line, |
| 81 | size_t message_start, |
| 82 | const std::string& str) { |
| 83 | if (severity == kViolationSeverity_ && !str.empty()) { |
| 84 | // Remove the trailing newline, if present. |
| 85 | size_t message_length = str.size() - message_start; |
| 86 | if (*str.rbegin() == '\n') |
| 87 | --message_length; |
| 88 | ADD_FAILURE_AT(file, line) |
| 89 | << base::StringPiece(str.c_str() + message_start, message_length); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | if (old_message_handler_ != NULL) |
| 94 | return (old_message_handler_)(severity, file, line, message_start, str); |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
| 101 | InstallationValidator::InstallationType ExpectValidInstallation( |
| 102 | bool system_level) { |
| 103 | FailureLogHelper log_helper; |
| 104 | InstallationValidator::InstallationType found_type = |
| 105 | InstallationValidator::NO_PRODUCTS; |
| 106 | |
| 107 | EXPECT_TRUE(InstallationValidator::ValidateInstallationType(system_level, |
| 108 | &found_type)); |
| 109 | return found_type; |
| 110 | } |
| 111 | |
| 112 | InstallationValidator::InstallationType ExpectValidInstallationForState( |
| 113 | const InstallationState& machine_state, |
| 114 | bool system_level) { |
| 115 | FailureLogHelper log_helper; |
| 116 | InstallationValidator::InstallationType found_type = |
| 117 | InstallationValidator::NO_PRODUCTS; |
| 118 | |
| 119 | EXPECT_TRUE(InstallationValidator::ValidateInstallationTypeForState( |
| 120 | machine_state, system_level, &found_type)); |
| 121 | return found_type; |
| 122 | } |
| 123 | |
| 124 | void ExpectInstallationOfType(bool system_level, |
| 125 | InstallationValidator::InstallationType type) { |
| 126 | EXPECT_EQ(type, ExpectValidInstallation(system_level)); |
| 127 | } |
| 128 | |
| 129 | void ExpectInstallationOfTypeForState( |
| 130 | const InstallationState& machine_state, |
| 131 | bool system_level, |
| 132 | InstallationValidator::InstallationType type) { |
| 133 | EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level)); |
| 134 | } |
| 135 | |
| 136 | } // namespace installer |