blob: e4408fb9b72e13522c7ae4776a941cf70986ab7f [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 Chen5af45462020-03-20 18:06:11 +080031 observer = new Observer();
32 observer->SetMetricsLibrary(std::make_unique<MetricsLibraryMock>());
Yong Hongcb45e082019-01-30 18:55:16 +080033
34 cli_ = std::make_unique<CLI>();
35 cli_->pr_getter_.reset(pr_getter_);
36 cli_->vp_getter_.reset(vp_getter_);
37 cli_->verifier_.reset(verifier_);
38 cli_->output_stream_ = output_stream_.get();
39
40 // set everything works by default.
41 pr_getter_->set_runtime_probe_output(runtime_probe::ProbeResult());
42 vp_getter_->set_default(HwVerificationSpec());
43 HwVerificationReport positive_report;
44 positive_report.set_is_compliant(true);
45 verifier_->SetVerifySuccess(positive_report);
46 }
47
48 std::unique_ptr<CLI> cli_;
49 FakeProbeResultGetter* pr_getter_;
50 FakeHwVerificationSpecGetter* vp_getter_;
51 FakeVerifier* verifier_;
52 std::unique_ptr<std::ostringstream> output_stream_;
Wei-Han Chen9a4f7592020-01-21 16:23:27 +080053
Wei-Han Chen5af45462020-03-20 18:06:11 +080054 Observer* observer;
Yong Hongcb45e082019-01-30 18:55:16 +080055};
56
57TEST_F(CLITest, TestBasicFlow) {
Wei-Han Chen5af45462020-03-20 18:06:11 +080058 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080059 CLIVerificationResult::kPass);
60}
61
62TEST_F(CLITest, TestHandleWaysToGetProbeResults) {
63 pr_getter_->set_runtime_probe_fail();
Wei-Han Chen5af45462020-03-20 18:06:11 +080064 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080065 CLIVerificationResult::kProbeFail);
66
67 pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}});
Wei-Han Chen5af45462020-03-20 18:06:11 +080068 EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080069 CLIVerificationResult::kPass);
Wei-Han Chen5af45462020-03-20 18:06:11 +080070 EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080071 CLIVerificationResult::kInvalidProbeResultFile);
72}
73
74TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) {
75 vp_getter_->SetDefaultInvalid();
Wei-Han Chen5af45462020-03-20 18:06:11 +080076 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080077 CLIVerificationResult::kInvalidHwVerificationSpecFile);
78
79 vp_getter_->set_files({{"path", HwVerificationSpec()}});
Wei-Han Chen5af45462020-03-20 18:06:11 +080080 EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080081 CLIVerificationResult::kPass);
Wei-Han Chen5af45462020-03-20 18:06:11 +080082 EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080083 CLIVerificationResult::kInvalidHwVerificationSpecFile);
84}
85
86TEST_F(CLITest, TestVerifyFail) {
87 verifier_->SetVerifyFail();
Wei-Han Chen5af45462020-03-20 18:06:11 +080088 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080089 CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment);
90
91 HwVerificationReport vr;
92 vr.set_is_compliant(false);
93 verifier_->SetVerifySuccess(vr);
Wei-Han Chen5af45462020-03-20 18:06:11 +080094 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +080095 CLIVerificationResult::kFail);
96}
97
98TEST_F(CLITest, TestOutput) {
99 HwVerificationReport vr;
100 vr.set_is_compliant(true);
101
102 verifier_->SetVerifySuccess(vr);
Wei-Han Chen5af45462020-03-20 18:06:11 +0800103 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer),
Yong Hongcb45e082019-01-30 18:55:16 +0800104 CLIVerificationResult::kPass);
105 HwVerificationReport result;
106 EXPECT_TRUE(result.ParseFromString(output_stream_->str()));
107 EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(result, vr));
108
109 // For human readable format, only check if there's something printed.
110 *output_stream_ = std::ostringstream();
Wei-Han Chen5af45462020-03-20 18:06:11 +0800111 EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, observer),
Yong Hongcb45e082019-01-30 18:55:16 +0800112 CLIVerificationResult::kPass);
113 EXPECT_FALSE(output_stream_->str().empty());
114}
115
116} // namespace hardware_verifier