blob: 3058fe034e692cfd36ab74eab173e6df2d2ae06e [file] [log] [blame]
Yong Hongcb45e082019-01-30 18:55:16 +08001/* 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 Chen5af45462020-03-20 18:06:11 +080012#include <metrics/metrics_library_mock.h>
Yong Hongcb45e082019-01-30 18:55:16 +080013#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 Chen5af45462020-03-20 18:06:11 +080018#include "hardware_verifier/observer.h"
Yong Hongcb45e082019-01-30 18:55:16 +080019#include "hardware_verifier/probe_result_getter_fake.h"
Stimim Chen464418f2020-04-20 10:37:08 +080020#include "hardware_verifier/test_utils.h"
Yong Hongcb45e082019-01-30 18:55:16 +080021#include "hardware_verifier/verifier_fake.h"
22
Stimim Chen464418f2020-04-20 10:37:08 +080023using ::testing::_;
24using ::testing::AtLeast;
25
Yong Hongcb45e082019-01-30 18:55:16 +080026namespace hardware_verifier {
27
28class CLITest : public testing::Test {
29 protected:
Stimim Chen464418f2020-04-20 10:37:08 +080030 void SetUp() override {
Yong Hongcb45e082019-01-30 18:55:16 +080031 pr_getter_ = new FakeProbeResultGetter();
32 vp_getter_ = new FakeHwVerificationSpecGetter();
33 verifier_ = new FakeVerifier();
34 output_stream_.reset(new std::ostringstream());
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080035
Stimim Chen464418f2020-04-20 10:37:08 +080036 auto metrics = std::make_unique<MetricsLibraryMock>();
37 metrics_ = metrics.get();
38 Observer::GetInstance()->SetMetricsLibrary(std::move(metrics));
Yong Hongcb45e082019-01-30 18:55:16 +080039
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 Chen464418f2020-04-20 10:37:08 +080054 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 Hongcb45e082019-01-30 18:55:16 +080061 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 Chen464418f2020-04-20 10:37:08 +080066 MetricsLibraryMock* metrics_;
Yong Hongcb45e082019-01-30 18:55:16 +080067};
68
69TEST_F(CLITest, TestBasicFlow) {
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080070 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080071 CLIVerificationResult::kPass);
72}
73
74TEST_F(CLITest, TestHandleWaysToGetProbeResults) {
75 pr_getter_->set_runtime_probe_fail();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080076 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080077 CLIVerificationResult::kProbeFail);
78
79 pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}});
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080080 EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080081 CLIVerificationResult::kPass);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080082 EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080083 CLIVerificationResult::kInvalidProbeResultFile);
84}
85
86TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) {
87 vp_getter_->SetDefaultInvalid();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080088 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080089 CLIVerificationResult::kInvalidHwVerificationSpecFile);
90
91 vp_getter_->set_files({{"path", HwVerificationSpec()}});
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080092 EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080093 CLIVerificationResult::kPass);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080094 EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080095 CLIVerificationResult::kInvalidHwVerificationSpecFile);
96}
97
98TEST_F(CLITest, TestVerifyFail) {
99 verifier_->SetVerifyFail();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +0800100 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +0800101 CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment);
102
103 HwVerificationReport vr;
104 vr.set_is_compliant(false);
105 verifier_->SetVerifySuccess(vr);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +0800106 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +0800107 CLIVerificationResult::kFail);
108}
109
110TEST_F(CLITest, TestOutput) {
111 HwVerificationReport vr;
112 vr.set_is_compliant(true);
113
114 verifier_->SetVerifySuccess(vr);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +0800115 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +0800116 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 Chen918b3ba2020-03-20 18:26:37 +0800123 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText),
Yong Hongcb45e082019-01-30 18:55:16 +0800124 CLIVerificationResult::kPass);
125 EXPECT_FALSE(output_stream_->str().empty());
126}
127
Stimim Chen464418f2020-04-20 10:37:08 +0800128TEST_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
148TEST_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 Hongcb45e082019-01-30 18:55:16 +0800168} // namespace hardware_verifier