blob: 5adb782d0f9b45c928b7889ee2cb2d000dc854bd [file] [log] [blame]
Yong Hongcc481892019-05-16 20:26:05 +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
Yong Hong52724f02019-05-24 21:13:06 +08006#include <memory>
Yong Hongcc481892019-05-16 20:26:05 +08007#include <string>
8
Yong Hong52724f02019-05-24 21:13:06 +08009#include <base/files/file_path.h>
10#include <base/files/file_util.h>
Yong Hongcc481892019-05-16 20:26:05 +080011#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 Hong52724f02019-05-24 21:13:06 +080016#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 Hongcc481892019-05-16 20:26:05 +080019#include "hardware_verifier/verifier_impl.h"
20
21using google::protobuf::util::MessageDifferencer;
22
23namespace hardware_verifier {
24
25namespace {
26
Yong Hong52724f02019-05-24 21:13:06 +080027constexpr auto kPrototxtExtension = ".prototxt";
28
29class FakeVbSystemPropertyGetter : public VbSystemPropertyGetter {
30 public:
31 int GetCrosDebug() const override { return 1; }
32};
Yong Hongcc481892019-05-16 20:26:05 +080033
34} // namespace
35
36class TestVerifierImpl : public testing::Test {
37 protected:
Yong Hong52724f02019-05-24 21:13:06 +080038 void SetUp() override {
39 pr_getter_.reset(new ProbeResultGetterImpl());
40 vs_getter_.reset(new HwVerificationSpecGetterImpl(
41 std::unique_ptr<VbSystemPropertyGetter>(
42 new FakeVbSystemPropertyGetter())));
Yong Hong52724f02019-05-24 21:13:06 +080043
Yong Hong85a4dff2019-05-25 04:22:47 +080044 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 Hongcc481892019-05-16 20:26:05 +080057 }
Yong Hong52724f02019-05-24 21:13:06 +080058
Yong Hong52724f02019-05-24 21:13:06 +080059 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 Hong85a4dff2019-05-25 04:22:47 +080085 EXPECT_TRUE(hw_verification_report_differencer_.Compare(
86 actual_hw_verification_report.value(), expect_hw_verification_report));
Yong Hong52724f02019-05-24 21:13:06 +080087 }
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 Hong85a4dff2019-05-25 04:22:47 +0800107 MessageDifferencer hw_verification_report_differencer_;
Yong Hongcc481892019-05-16 20:26:05 +0800108};
109
Yong Hong52724f02019-05-24 21:13:06 +0800110TEST_F(TestVerifierImpl, TestVerifySuccWithSample1) {
111 TestVerifySuccWithSampleData("probe_result_1", "hw_verification_spec_1",
112 "expect_hw_verification_report_1");
Yong Hongcc481892019-05-16 20:26:05 +0800113}
114
Yong Hong52724f02019-05-24 21:13:06 +0800115TEST_F(TestVerifierImpl, TestVerifySuccWithSample2) {
116 TestVerifySuccWithSampleData("probe_result_2", "hw_verification_spec_1",
117 "expect_hw_verification_report_2");
Yong Hongcc481892019-05-16 20:26:05 +0800118}
119
Yong Hong52724f02019-05-24 21:13:06 +0800120TEST_F(TestVerifierImpl, TestVerifySuccWithSample3) {
121 TestVerifySuccWithSampleData("probe_result_3", "hw_verification_spec_1",
122 "expect_hw_verification_report_3");
Yong Hongcc481892019-05-16 20:26:05 +0800123}
124
Yong Hong85a4dff2019-05-25 04:22:47 +0800125TEST_F(TestVerifierImpl, TestVerifySuccWithSample4) {
126 TestVerifySuccWithSampleData("probe_result_4", "hw_verification_spec_1",
127 "expect_hw_verification_report_4");
128}
129
Yong Hong52724f02019-05-24 21:13:06 +0800130TEST_F(TestVerifierImpl, TestVerifyFailWithSample1) {
131 TestVerifyFailWithSampleData("probe_result_bad_1", "hw_verification_spec_1");
Yong Hongcc481892019-05-16 20:26:05 +0800132}
133
Yong Hong52724f02019-05-24 21:13:06 +0800134TEST_F(TestVerifierImpl, TestVerifyFailWithSample2) {
135 TestVerifyFailWithSampleData("probe_result_bad_2", "hw_verification_spec_1");
136}
Yong Hongcc481892019-05-16 20:26:05 +0800137
Yong Hong52724f02019-05-24 21:13:06 +0800138TEST_F(TestVerifierImpl, TestVerifyFailWithSample3) {
139 TestVerifyFailWithSampleData("probe_result_1", "hw_verification_spec_bad_1");
Yong Hongcc481892019-05-16 20:26:05 +0800140}
141
Yong Hong85a4dff2019-05-25 04:22:47 +0800142TEST_F(TestVerifierImpl, TestVerifyFailWithSample4) {
143 TestVerifyFailWithSampleData("probe_result_1", "hw_verification_spec_bad_2");
144}
145
Yong Hongcc481892019-05-16 20:26:05 +0800146} // namespace hardware_verifier