blob: 6e192a0ba6cfed8de5bb3299e4dae69d6c4b12a2 [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) {
Kevin Linef321a52020-09-01 18:31:52 +080070 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080071 CLIVerificationResult::kPass);
72}
73
74TEST_F(CLITest, TestHandleWaysToGetProbeResults) {
75 pr_getter_->set_runtime_probe_fail();
Kevin Linef321a52020-09-01 18:31:52 +080076 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080077 CLIVerificationResult::kProbeFail);
78
79 pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}});
Kevin Linef321a52020-09-01 18:31:52 +080080 EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080081 CLIVerificationResult::kPass);
Kevin Linef321a52020-09-01 18:31:52 +080082 EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080083 CLIVerificationResult::kInvalidProbeResultFile);
84}
85
86TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) {
87 vp_getter_->SetDefaultInvalid();
Kevin Linef321a52020-09-01 18:31:52 +080088 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080089 CLIVerificationResult::kInvalidHwVerificationSpecFile);
90
91 vp_getter_->set_files({{"path", HwVerificationSpec()}});
Kevin Linef321a52020-09-01 18:31:52 +080092 EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080093 CLIVerificationResult::kPass);
Kevin Linef321a52020-09-01 18:31:52 +080094 EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +080095 CLIVerificationResult::kInvalidHwVerificationSpecFile);
96}
97
98TEST_F(CLITest, TestVerifyFail) {
99 verifier_->SetVerifyFail();
Kevin Linef321a52020-09-01 18:31:52 +0800100 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, false),
Yong Hongcb45e082019-01-30 18:55:16 +0800101 CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment);
102
103 HwVerificationReport vr;
104 vr.set_is_compliant(false);
105 verifier_->SetVerifySuccess(vr);
Kevin Linef321a52020-09-01 18:31:52 +0800106 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, false),
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);
Kevin Linef321a52020-09-01 18:31:52 +0800115 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, true),
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();
Kevin Linef321a52020-09-01 18:31:52 +0800123 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, false),
124 CLIVerificationResult::kPass);
125 EXPECT_FALSE(output_stream_->str().empty());
126
127 *output_stream_ = std::ostringstream();
128 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, true),
Yong Hongcb45e082019-01-30 18:55:16 +0800129 CLIVerificationResult::kPass);
130 EXPECT_FALSE(output_stream_->str().empty());
131}
132
Stimim Chen464418f2020-04-20 10:37:08 +0800133TEST_F(CLITest, TestVerifyReportSample1) {
134 const auto& path = GetTestDataPath()
135 .Append("verifier_impl_sample_data")
136 .Append("expect_hw_verification_report_1.prototxt");
137
138 const auto& vr = LoadHwVerificationReport(path);
139 verifier_->SetVerifySuccess(vr);
140
141 // This is for recording running time.
142 EXPECT_CALL(*metrics_, SendToUMA(_, _, _, _, _)).Times(AtLeast(1));
143 EXPECT_CALL(
144 *metrics_,
145 SendBoolToUMA("ChromeOS.HardwareVerifier.Report.IsCompliant", true));
146 // This is for recording qualification status of each components.
147 EXPECT_CALL(*metrics_, SendEnumToUMA(_, _, _)).Times(AtLeast(3));
148
Kevin Linef321a52020-09-01 18:31:52 +0800149 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, false),
Stimim Chen464418f2020-04-20 10:37:08 +0800150 CLIVerificationResult::kPass);
151}
152
153TEST_F(CLITest, TestVerifyReportSample2) {
154 const auto& path = GetTestDataPath()
155 .Append("verifier_impl_sample_data")
156 .Append("expect_hw_verification_report_2.prototxt");
157
158 const auto& vr = LoadHwVerificationReport(path);
159 verifier_->SetVerifySuccess(vr);
160
161 // This is for recording running time.
162 EXPECT_CALL(*metrics_, SendToUMA(_, _, _, _, _)).Times(AtLeast(1));
163 EXPECT_CALL(
164 *metrics_,
165 SendBoolToUMA("ChromeOS.HardwareVerifier.Report.IsCompliant", false));
166 // This is for recording qualification status of each components.
167 EXPECT_CALL(*metrics_, SendEnumToUMA(_, _, _)).Times(AtLeast(2));
168
Kevin Linef321a52020-09-01 18:31:52 +0800169 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, false),
Stimim Chen464418f2020-04-20 10:37:08 +0800170 CLIVerificationResult::kFail);
171}
172
Yong Hongcb45e082019-01-30 18:55:16 +0800173} // namespace hardware_verifier