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" |
| 20 | #include "hardware_verifier/verifier_fake.h" |
| 21 | |
| 22 | namespace hardware_verifier { |
| 23 | |
| 24 | class 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 Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 31 | observer = new Observer(); |
| 32 | observer->SetMetricsLibrary(std::make_unique<MetricsLibraryMock>()); |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 33 | |
| 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 Chen | 9a4f759 | 2020-01-21 16:23:27 +0800 | [diff] [blame] | 53 | |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 54 | Observer* observer; |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | TEST_F(CLITest, TestBasicFlow) { |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 58 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 59 | CLIVerificationResult::kPass); |
| 60 | } |
| 61 | |
| 62 | TEST_F(CLITest, TestHandleWaysToGetProbeResults) { |
| 63 | pr_getter_->set_runtime_probe_fail(); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 64 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 65 | CLIVerificationResult::kProbeFail); |
| 66 | |
| 67 | pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}}); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 68 | EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 69 | CLIVerificationResult::kPass); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 70 | EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 71 | CLIVerificationResult::kInvalidProbeResultFile); |
| 72 | } |
| 73 | |
| 74 | TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) { |
| 75 | vp_getter_->SetDefaultInvalid(); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 76 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 77 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 78 | |
| 79 | vp_getter_->set_files({{"path", HwVerificationSpec()}}); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 80 | EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 81 | CLIVerificationResult::kPass); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 82 | EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 83 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 84 | } |
| 85 | |
| 86 | TEST_F(CLITest, TestVerifyFail) { |
| 87 | verifier_->SetVerifyFail(); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 88 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 89 | CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment); |
| 90 | |
| 91 | HwVerificationReport vr; |
| 92 | vr.set_is_compliant(false); |
| 93 | verifier_->SetVerifySuccess(vr); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 94 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 95 | CLIVerificationResult::kFail); |
| 96 | } |
| 97 | |
| 98 | TEST_F(CLITest, TestOutput) { |
| 99 | HwVerificationReport vr; |
| 100 | vr.set_is_compliant(true); |
| 101 | |
| 102 | verifier_->SetVerifySuccess(vr); |
Wei-Han Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 103 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 104 | 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 Chen | 5af4546 | 2020-03-20 18:06:11 +0800 | [diff] [blame^] | 111 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kText, observer), |
Yong Hong | cb45e08 | 2019-01-30 18:55:16 +0800 | [diff] [blame] | 112 | CLIVerificationResult::kPass); |
| 113 | EXPECT_FALSE(output_stream_->str().empty()); |
| 114 | } |
| 115 | |
| 116 | } // namespace hardware_verifier |