blob: 43ccc7df312be501c06d2bbe24650b93a7bf1738 [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"
20#include "hardware_verifier/verifier_fake.h"
21
22namespace hardware_verifier {
23
24class CLITest : public testing::Test {
25 protected:
26 void SetUp() {
27 pr_getter_ = new FakeProbeResultGetter();
28 vp_getter_ = new FakeHwVerificationSpecGetter();
29 verifier_ = new FakeVerifier();
30 output_stream_.reset(new std::ostringstream());
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080031
32 Observer::GetInstance()->SetMetricsLibrary(
33 std::make_unique<MetricsLibraryMock>());
Yong Hongcb45e082019-01-30 18:55:16 +080034
35 cli_ = std::make_unique<CLI>();
36 cli_->pr_getter_.reset(pr_getter_);
37 cli_->vp_getter_.reset(vp_getter_);
38 cli_->verifier_.reset(verifier_);
39 cli_->output_stream_ = output_stream_.get();
40
41 // set everything works by default.
42 pr_getter_->set_runtime_probe_output(runtime_probe::ProbeResult());
43 vp_getter_->set_default(HwVerificationSpec());
44 HwVerificationReport positive_report;
45 positive_report.set_is_compliant(true);
46 verifier_->SetVerifySuccess(positive_report);
47 }
48
49 std::unique_ptr<CLI> cli_;
50 FakeProbeResultGetter* pr_getter_;
51 FakeHwVerificationSpecGetter* vp_getter_;
52 FakeVerifier* verifier_;
53 std::unique_ptr<std::ostringstream> output_stream_;
54};
55
56TEST_F(CLITest, TestBasicFlow) {
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080057 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080058 CLIVerificationResult::kPass);
59}
60
61TEST_F(CLITest, TestHandleWaysToGetProbeResults) {
62 pr_getter_->set_runtime_probe_fail();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080063 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080064 CLIVerificationResult::kProbeFail);
65
66 pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}});
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080067 EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080068 CLIVerificationResult::kPass);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080069 EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080070 CLIVerificationResult::kInvalidProbeResultFile);
71}
72
73TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) {
74 vp_getter_->SetDefaultInvalid();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080075 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080076 CLIVerificationResult::kInvalidHwVerificationSpecFile);
77
78 vp_getter_->set_files({{"path", HwVerificationSpec()}});
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080079 EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080080 CLIVerificationResult::kPass);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080081 EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080082 CLIVerificationResult::kInvalidHwVerificationSpecFile);
83}
84
85TEST_F(CLITest, TestVerifyFail) {
86 verifier_->SetVerifyFail();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080087 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080088 CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment);
89
90 HwVerificationReport vr;
91 vr.set_is_compliant(false);
92 verifier_->SetVerifySuccess(vr);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +080093 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +080094 CLIVerificationResult::kFail);
95}
96
97TEST_F(CLITest, TestOutput) {
98 HwVerificationReport vr;
99 vr.set_is_compliant(true);
100
101 verifier_->SetVerifySuccess(vr);
Wei-Han Chen918b3ba2020-03-20 18:26:37 +0800102 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin),
Yong Hongcb45e082019-01-30 18:55:16 +0800103 CLIVerificationResult::kPass);
104 HwVerificationReport result;
105 EXPECT_TRUE(result.ParseFromString(output_stream_->str()));
106 EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(result, vr));
107
108 // For human readable format, only check if there's something printed.
109 *output_stream_ = std::ostringstream();
Wei-Han Chen918b3ba2020-03-20 18:26:37 +0800110 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText),
Yong Hongcb45e082019-01-30 18:55:16 +0800111 CLIVerificationResult::kPass);
112 EXPECT_FALSE(output_stream_->str().empty());
113}
114
115} // namespace hardware_verifier