Yong Hong | cc48189 | 2019-05-16 20:26:05 +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 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 6 | #include <memory> |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 9 | #include <base/files/file_path.h> |
| 10 | #include <base/files/file_util.h> |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 11 | #include <google/protobuf/util/message_differencer.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | #include <runtime_probe/proto_bindings/runtime_probe.pb.h> |
| 14 | |
| 15 | #include "hardware_verifier/hardware_verifier.pb.h" |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 16 | #include "hardware_verifier/hw_verification_spec_getter_impl.h" |
| 17 | #include "hardware_verifier/probe_result_getter_impl.h" |
| 18 | #include "hardware_verifier/test_utils.h" |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 19 | #include "hardware_verifier/verifier_impl.h" |
| 20 | |
| 21 | using google::protobuf::util::MessageDifferencer; |
| 22 | |
| 23 | namespace hardware_verifier { |
| 24 | |
| 25 | namespace { |
| 26 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 27 | constexpr auto kPrototxtExtension = ".prototxt"; |
| 28 | |
| 29 | class FakeVbSystemPropertyGetter : public VbSystemPropertyGetter { |
| 30 | public: |
| 31 | int GetCrosDebug() const override { return 1; } |
| 32 | }; |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | class TestVerifierImpl : public testing::Test { |
| 37 | protected: |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 38 | void SetUp() override { |
| 39 | pr_getter_.reset(new ProbeResultGetterImpl()); |
| 40 | vs_getter_.reset(new HwVerificationSpecGetterImpl( |
| 41 | std::unique_ptr<VbSystemPropertyGetter>( |
| 42 | new FakeVbSystemPropertyGetter()))); |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 43 | |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 44 | hw_verification_report_differencer_.TreatAsSet( |
| 45 | HwVerificationReport::descriptor()->FindFieldByName( |
| 46 | "found_component_infos")); |
| 47 | const auto* category_enum_desc = |
| 48 | runtime_probe::ProbeRequest_SupportCategory_descriptor(); |
| 49 | for (int i = 0; i < category_enum_desc->value_count(); ++i) { |
| 50 | const auto* field_desc = |
| 51 | HwVerificationReport_GenericDeviceInfo::descriptor()->FindFieldByName( |
| 52 | category_enum_desc->value(i)->name()); |
| 53 | if (field_desc) { |
| 54 | hw_verification_report_differencer_.TreatAsSet(field_desc); |
| 55 | } |
| 56 | } |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 57 | } |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 58 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 59 | HwVerificationSpec LoadHwVerificationSpec(const base::FilePath& file_path) { |
| 60 | const auto& result = vs_getter_->GetFromFile(file_path); |
| 61 | EXPECT_TRUE(result); |
| 62 | return result.value(); |
| 63 | } |
| 64 | |
| 65 | runtime_probe::ProbeResult LoadProbeResult(const base::FilePath& file_path) { |
| 66 | const auto& result = pr_getter_->GetFromFile(file_path); |
| 67 | EXPECT_TRUE(result); |
| 68 | return result.value(); |
| 69 | } |
| 70 | |
| 71 | void TestVerifySuccWithSampleData(const std::string& probe_result_sample_name, |
| 72 | const std::string& spec_sample_name, |
| 73 | const std::string& report_sample_name) { |
| 74 | const auto& probe_result = LoadProbeResult(GetSampleDataPath().Append( |
| 75 | probe_result_sample_name + kPrototxtExtension)); |
| 76 | const auto& hw_verification_spec = LoadHwVerificationSpec( |
| 77 | GetSampleDataPath().Append(spec_sample_name + kPrototxtExtension)); |
| 78 | const auto& expect_hw_verification_report = LoadHwVerificationReport( |
| 79 | GetSampleDataPath().Append(report_sample_name + kPrototxtExtension)); |
| 80 | |
| 81 | VerifierImpl verifier; |
| 82 | const auto& actual_hw_verification_report = |
| 83 | verifier.Verify(probe_result, hw_verification_spec); |
| 84 | EXPECT_TRUE(actual_hw_verification_report); |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 85 | EXPECT_TRUE(hw_verification_report_differencer_.Compare( |
| 86 | actual_hw_verification_report.value(), expect_hw_verification_report)); |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void TestVerifyFailWithSampleData(const std::string& probe_result_sample_name, |
| 90 | const std::string& spec_sample_name) { |
| 91 | const auto& probe_result = LoadProbeResult(GetSampleDataPath().Append( |
| 92 | probe_result_sample_name + kPrototxtExtension)); |
| 93 | const auto& hw_verification_spec = LoadHwVerificationSpec( |
| 94 | GetSampleDataPath().Append(spec_sample_name + kPrototxtExtension)); |
| 95 | |
| 96 | VerifierImpl verifier; |
| 97 | EXPECT_FALSE(verifier.Verify(probe_result, hw_verification_spec)); |
| 98 | } |
| 99 | |
| 100 | base::FilePath GetSampleDataPath() { |
| 101 | return GetTestDataPath().Append("verifier_impl_sample_data"); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | std::unique_ptr<ProbeResultGetter> pr_getter_; |
| 106 | std::unique_ptr<HwVerificationSpecGetter> vs_getter_; |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 107 | MessageDifferencer hw_verification_report_differencer_; |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 110 | TEST_F(TestVerifierImpl, TestVerifySuccWithSample1) { |
| 111 | TestVerifySuccWithSampleData("probe_result_1", "hw_verification_spec_1", |
| 112 | "expect_hw_verification_report_1"); |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 115 | TEST_F(TestVerifierImpl, TestVerifySuccWithSample2) { |
| 116 | TestVerifySuccWithSampleData("probe_result_2", "hw_verification_spec_1", |
| 117 | "expect_hw_verification_report_2"); |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 120 | TEST_F(TestVerifierImpl, TestVerifySuccWithSample3) { |
| 121 | TestVerifySuccWithSampleData("probe_result_3", "hw_verification_spec_1", |
| 122 | "expect_hw_verification_report_3"); |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 125 | TEST_F(TestVerifierImpl, TestVerifySuccWithSample4) { |
| 126 | TestVerifySuccWithSampleData("probe_result_4", "hw_verification_spec_1", |
| 127 | "expect_hw_verification_report_4"); |
| 128 | } |
| 129 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 130 | TEST_F(TestVerifierImpl, TestVerifyFailWithSample1) { |
| 131 | TestVerifyFailWithSampleData("probe_result_bad_1", "hw_verification_spec_1"); |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 134 | TEST_F(TestVerifierImpl, TestVerifyFailWithSample2) { |
| 135 | TestVerifyFailWithSampleData("probe_result_bad_2", "hw_verification_spec_1"); |
| 136 | } |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 137 | |
Yong Hong | 52724f0 | 2019-05-24 21:13:06 +0800 | [diff] [blame] | 138 | TEST_F(TestVerifierImpl, TestVerifyFailWithSample3) { |
| 139 | TestVerifyFailWithSampleData("probe_result_1", "hw_verification_spec_bad_1"); |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Yong Hong | 85a4dff | 2019-05-25 04:22:47 +0800 | [diff] [blame] | 142 | TEST_F(TestVerifierImpl, TestVerifyFailWithSample4) { |
| 143 | TestVerifyFailWithSampleData("probe_result_1", "hw_verification_spec_bad_2"); |
| 144 | } |
| 145 | |
Yong Hong | cc48189 | 2019-05-16 20:26:05 +0800 | [diff] [blame] | 146 | } // namespace hardware_verifier |