Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 1 | /* Copyright 2019 The Chromium OS 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 | |
| 6 | #include <memory> |
| 7 | #include <sstream> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include <google/protobuf/util/message_differencer.h> |
| 11 | #include <gtest/gtest.h> |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 12 | #include <metrics/metrics_library_mock.h> |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 13 | #include <runtime_probe/proto_bindings/runtime_probe.pb.h> |
| 14 | |
| 15 | #include "hardware_verifier/cli.h" |
| 16 | #include "hardware_verifier/hardware_verifier.pb.h" |
| 17 | #include "hardware_verifier/hw_verification_spec_getter_fake.h" |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame] | 18 | #include "hardware_verifier/observer.h" |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 19 | #include "hardware_verifier/probe_result_getter_fake.h" |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 20 | #include "hardware_verifier/test_utils.h" |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 21 | #include "hardware_verifier/verifier_fake.h" |
| 22 | |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 23 | using ::testing::_; |
| 24 | using ::testing::AtLeast; |
| 25 | |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 26 | namespace hardware_verifier { |
| 27 | |
| 28 | class CLITest : public testing::Test { |
| 29 | protected: |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 30 | void SetUp() override { |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 31 | pr_getter_ = new FakeProbeResultGetter(); |
| 32 | vp_getter_ = new FakeHwVerificationSpecGetter(); |
| 33 | verifier_ = new FakeVerifier(); |
| 34 | output_stream_.reset(new std::ostringstream()); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 35 | |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 36 | auto metrics = std::make_unique<MetricsLibraryMock>(); |
| 37 | metrics_ = metrics.get(); |
| 38 | Observer::GetInstance()->SetMetricsLibrary(std::move(metrics)); |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 39 | |
| 40 | cli_ = std::make_unique<CLI>(); |
| 41 | cli_->pr_getter_.reset(pr_getter_); |
| 42 | cli_->vp_getter_.reset(vp_getter_); |
| 43 | cli_->verifier_.reset(verifier_); |
| 44 | cli_->output_stream_ = output_stream_.get(); |
| 45 | |
| 46 | // set everything works by default. |
| 47 | pr_getter_->set_runtime_probe_output(runtime_probe::ProbeResult()); |
| 48 | vp_getter_->set_default(HwVerificationSpec()); |
| 49 | HwVerificationReport positive_report; |
| 50 | positive_report.set_is_compliant(true); |
| 51 | verifier_->SetVerifySuccess(positive_report); |
| 52 | } |
| 53 | |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 54 | void TearDown() override { |
| 55 | // We have to clear the MetricsLibraryMock manually, because |
| 56 | // Observer::GetInstance() object is a singleton, which won't be destroyed |
| 57 | // across the tests. |
| 58 | Observer::GetInstance()->SetMetricsLibrary(nullptr); |
| 59 | } |
| 60 | |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 61 | std::unique_ptr<CLI> cli_; |
| 62 | FakeProbeResultGetter* pr_getter_; |
| 63 | FakeHwVerificationSpecGetter* vp_getter_; |
| 64 | FakeVerifier* verifier_; |
| 65 | std::unique_ptr<std::ostringstream> output_stream_; |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 66 | MetricsLibraryMock* metrics_; |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | TEST_F(CLITest, TestBasicFlow) { |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 70 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 71 | CLIVerificationResult::kPass); |
| 72 | } |
| 73 | |
| 74 | TEST_F(CLITest, TestHandleWaysToGetProbeResults) { |
| 75 | pr_getter_->set_runtime_probe_fail(); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 76 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 77 | CLIVerificationResult::kProbeFail); |
| 78 | |
| 79 | pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}}); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 80 | EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 81 | CLIVerificationResult::kPass); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 82 | EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 83 | CLIVerificationResult::kInvalidProbeResultFile); |
| 84 | } |
| 85 | |
| 86 | TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) { |
| 87 | vp_getter_->SetDefaultInvalid(); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 88 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 89 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 90 | |
| 91 | vp_getter_->set_files({{"path", HwVerificationSpec()}}); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 92 | EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 93 | CLIVerificationResult::kPass); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 94 | EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 95 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 96 | } |
| 97 | |
| 98 | TEST_F(CLITest, TestVerifyFail) { |
| 99 | verifier_->SetVerifyFail(); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 100 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 101 | CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment); |
| 102 | |
| 103 | HwVerificationReport vr; |
| 104 | vr.set_is_compliant(false); |
| 105 | verifier_->SetVerifySuccess(vr); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 106 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 107 | CLIVerificationResult::kFail); |
| 108 | } |
| 109 | |
| 110 | TEST_F(CLITest, TestOutput) { |
| 111 | HwVerificationReport vr; |
| 112 | vr.set_is_compliant(true); |
| 113 | |
| 114 | verifier_->SetVerifySuccess(vr); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 115 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 116 | CLIVerificationResult::kPass); |
| 117 | HwVerificationReport result; |
| 118 | EXPECT_TRUE(result.ParseFromString(output_stream_->str())); |
| 119 | EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(result, vr)); |
| 120 | |
| 121 | // For human readable format, only check if there's something printed. |
| 122 | *output_stream_ = std::ostringstream(); |
Wei-Han Chen | 918b3ba | 2020-03-20 18:26:37 +0800 | [diff] [blame] | 123 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 124 | CLIVerificationResult::kPass); |
| 125 | EXPECT_FALSE(output_stream_->str().empty()); |
| 126 | } |
| 127 | |
Stimim Chen | 464418f | 2020-04-20 10:37:08 +0800 | [diff] [blame^] | 128 | TEST_F(CLITest, TestVerifyReportSample1) { |
| 129 | const auto& path = GetTestDataPath() |
| 130 | .Append("verifier_impl_sample_data") |
| 131 | .Append("expect_hw_verification_report_1.prototxt"); |
| 132 | |
| 133 | const auto& vr = LoadHwVerificationReport(path); |
| 134 | verifier_->SetVerifySuccess(vr); |
| 135 | |
| 136 | // This is for recording running time. |
| 137 | EXPECT_CALL(*metrics_, SendToUMA(_, _, _, _, _)).Times(AtLeast(1)); |
| 138 | EXPECT_CALL( |
| 139 | *metrics_, |
| 140 | SendBoolToUMA("ChromeOS.HardwareVerifier.Report.IsCompliant", true)); |
| 141 | // This is for recording qualification status of each components. |
| 142 | EXPECT_CALL(*metrics_, SendEnumToUMA(_, _, _)).Times(AtLeast(3)); |
| 143 | |
| 144 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText), |
| 145 | CLIVerificationResult::kPass); |
| 146 | } |
| 147 | |
| 148 | TEST_F(CLITest, TestVerifyReportSample2) { |
| 149 | const auto& path = GetTestDataPath() |
| 150 | .Append("verifier_impl_sample_data") |
| 151 | .Append("expect_hw_verification_report_2.prototxt"); |
| 152 | |
| 153 | const auto& vr = LoadHwVerificationReport(path); |
| 154 | verifier_->SetVerifySuccess(vr); |
| 155 | |
| 156 | // This is for recording running time. |
| 157 | EXPECT_CALL(*metrics_, SendToUMA(_, _, _, _, _)).Times(AtLeast(1)); |
| 158 | EXPECT_CALL( |
| 159 | *metrics_, |
| 160 | SendBoolToUMA("ChromeOS.HardwareVerifier.Report.IsCompliant", false)); |
| 161 | // This is for recording qualification status of each components. |
| 162 | EXPECT_CALL(*metrics_, SendEnumToUMA(_, _, _)).Times(AtLeast(2)); |
| 163 | |
| 164 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText), |
| 165 | CLIVerificationResult::kFail); |
| 166 | } |
| 167 | |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 168 | } // namespace hardware_verifier |