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