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> |
| 12 | #include <runtime_probe/proto_bindings/runtime_probe.pb.h> |
| 13 | |
| 14 | #include "hardware_verifier/cli.h" |
| 15 | #include "hardware_verifier/hardware_verifier.pb.h" |
| 16 | #include "hardware_verifier/hw_verification_spec_getter_fake.h" |
| 17 | #include "hardware_verifier/probe_result_getter_fake.h" |
| 18 | #include "hardware_verifier/verifier_fake.h" |
| 19 | |
| 20 | namespace hardware_verifier { |
| 21 | |
| 22 | class CLITest : public testing::Test { |
| 23 | protected: |
| 24 | void SetUp() { |
| 25 | pr_getter_ = new FakeProbeResultGetter(); |
| 26 | vp_getter_ = new FakeHwVerificationSpecGetter(); |
| 27 | verifier_ = new FakeVerifier(); |
| 28 | output_stream_.reset(new std::ostringstream()); |
| 29 | |
| 30 | cli_ = std::make_unique<CLI>(); |
| 31 | cli_->pr_getter_.reset(pr_getter_); |
| 32 | cli_->vp_getter_.reset(vp_getter_); |
| 33 | cli_->verifier_.reset(verifier_); |
| 34 | cli_->output_stream_ = output_stream_.get(); |
| 35 | |
| 36 | // set everything works by default. |
| 37 | pr_getter_->set_runtime_probe_output(runtime_probe::ProbeResult()); |
| 38 | vp_getter_->set_default(HwVerificationSpec()); |
| 39 | HwVerificationReport positive_report; |
| 40 | positive_report.set_is_compliant(true); |
| 41 | verifier_->SetVerifySuccess(positive_report); |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<CLI> cli_; |
| 45 | FakeProbeResultGetter* pr_getter_; |
| 46 | FakeHwVerificationSpecGetter* vp_getter_; |
| 47 | FakeVerifier* verifier_; |
| 48 | std::unique_ptr<std::ostringstream> output_stream_; |
| 49 | }; |
| 50 | |
| 51 | TEST_F(CLITest, TestBasicFlow) { |
| 52 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 53 | CLIVerificationResult::kPass); |
| 54 | } |
| 55 | |
| 56 | TEST_F(CLITest, TestHandleWaysToGetProbeResults) { |
| 57 | pr_getter_->set_runtime_probe_fail(); |
| 58 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 59 | CLIVerificationResult::kProbeFail); |
| 60 | |
| 61 | pr_getter_->set_file_probe_results({{"path", runtime_probe::ProbeResult()}}); |
| 62 | EXPECT_EQ(cli_->Run("path", "", CLIOutputFormat::kProtoBin), |
| 63 | CLIVerificationResult::kPass); |
| 64 | EXPECT_EQ(cli_->Run("path2", "", CLIOutputFormat::kProtoBin), |
| 65 | CLIVerificationResult::kInvalidProbeResultFile); |
| 66 | } |
| 67 | |
| 68 | TEST_F(CLITest, TestHandleWaysToGetHwVerificationSpec) { |
| 69 | vp_getter_->SetDefaultInvalid(); |
| 70 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 71 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 72 | |
| 73 | vp_getter_->set_files({{"path", HwVerificationSpec()}}); |
| 74 | EXPECT_EQ(cli_->Run("", "path", CLIOutputFormat::kProtoBin), |
| 75 | CLIVerificationResult::kPass); |
| 76 | EXPECT_EQ(cli_->Run("", "path2", CLIOutputFormat::kProtoBin), |
| 77 | CLIVerificationResult::kInvalidHwVerificationSpecFile); |
| 78 | } |
| 79 | |
| 80 | TEST_F(CLITest, TestVerifyFail) { |
| 81 | verifier_->SetVerifyFail(); |
| 82 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 83 | CLIVerificationResult::kProbeResultHwVerificationSpecMisalignment); |
| 84 | |
| 85 | HwVerificationReport vr; |
| 86 | vr.set_is_compliant(false); |
| 87 | verifier_->SetVerifySuccess(vr); |
| 88 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 89 | CLIVerificationResult::kFail); |
| 90 | } |
| 91 | |
| 92 | TEST_F(CLITest, TestOutput) { |
| 93 | HwVerificationReport vr; |
| 94 | vr.set_is_compliant(true); |
| 95 | |
| 96 | verifier_->SetVerifySuccess(vr); |
| 97 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoBin), |
| 98 | CLIVerificationResult::kPass); |
| 99 | HwVerificationReport result; |
| 100 | EXPECT_TRUE(result.ParseFromString(output_stream_->str())); |
| 101 | EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(result, vr)); |
| 102 | |
| 103 | // For human readable format, only check if there's something printed. |
| 104 | *output_stream_ = std::ostringstream(); |
| 105 | EXPECT_EQ(cli_->Run("", "", CLIOutputFormat::kProtoText), |
| 106 | CLIVerificationResult::kPass); |
| 107 | EXPECT_FALSE(output_stream_->str().empty()); |
| 108 | } |
| 109 | |
| 110 | } // namespace hardware_verifier |