1*6777b538SAndroid Build Coastguard Worker // Copyright 2023 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "base/test/test_proto_loader.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include "base/check.h"
8*6777b538SAndroid Build Coastguard Worker #include "base/files/file_util.h"
9*6777b538SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/path_service.h"
12*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_split.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
14*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/descriptor.h"
15*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/descriptor.pb.h"
16*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/descriptor_database.h"
17*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/dynamic_message.h"
18*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/message.h"
19*6777b538SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/text_format.h"
20*6777b538SAndroid Build Coastguard Worker
21*6777b538SAndroid Build Coastguard Worker namespace base {
22*6777b538SAndroid Build Coastguard Worker
23*6777b538SAndroid Build Coastguard Worker class TestProtoSetLoader::State {
24*6777b538SAndroid Build Coastguard Worker public:
State(std::string_view descriptor_binary_proto)25*6777b538SAndroid Build Coastguard Worker explicit State(std::string_view descriptor_binary_proto) {
26*6777b538SAndroid Build Coastguard Worker CHECK(descriptor_set_.ParseFromArray(descriptor_binary_proto.data(),
27*6777b538SAndroid Build Coastguard Worker descriptor_binary_proto.size()))
28*6777b538SAndroid Build Coastguard Worker << "Couldn't parse descriptor";
29*6777b538SAndroid Build Coastguard Worker for (auto& file : descriptor_set_.file()) {
30*6777b538SAndroid Build Coastguard Worker descriptor_database_.Add(file);
31*6777b538SAndroid Build Coastguard Worker }
32*6777b538SAndroid Build Coastguard Worker descriptor_pool_ = std::make_unique<google::protobuf::DescriptorPool>(
33*6777b538SAndroid Build Coastguard Worker &descriptor_database_);
34*6777b538SAndroid Build Coastguard Worker }
35*6777b538SAndroid Build Coastguard Worker
NewMessage(std::string_view full_type_name)36*6777b538SAndroid Build Coastguard Worker std::unique_ptr<google::protobuf::Message> NewMessage(
37*6777b538SAndroid Build Coastguard Worker std::string_view full_type_name) {
38*6777b538SAndroid Build Coastguard Worker const google::protobuf::Descriptor* message_type =
39*6777b538SAndroid Build Coastguard Worker descriptor_pool_->FindMessageTypeByName(std::string(full_type_name));
40*6777b538SAndroid Build Coastguard Worker CHECK(message_type) << "Couldn't find proto message type "
41*6777b538SAndroid Build Coastguard Worker << full_type_name;
42*6777b538SAndroid Build Coastguard Worker return base::WrapUnique(
43*6777b538SAndroid Build Coastguard Worker dynamic_message_factory_.GetPrototype(message_type)->New());
44*6777b538SAndroid Build Coastguard Worker }
45*6777b538SAndroid Build Coastguard Worker
46*6777b538SAndroid Build Coastguard Worker private:
47*6777b538SAndroid Build Coastguard Worker google::protobuf::FileDescriptorSet descriptor_set_;
48*6777b538SAndroid Build Coastguard Worker google::protobuf::SimpleDescriptorDatabase descriptor_database_;
49*6777b538SAndroid Build Coastguard Worker std::unique_ptr<google::protobuf::DescriptorPool> descriptor_pool_;
50*6777b538SAndroid Build Coastguard Worker google::protobuf::DynamicMessageFactory dynamic_message_factory_;
51*6777b538SAndroid Build Coastguard Worker };
52*6777b538SAndroid Build Coastguard Worker
TestProtoSetLoader(std::string_view descriptor_binary_proto)53*6777b538SAndroid Build Coastguard Worker TestProtoSetLoader::TestProtoSetLoader(std::string_view descriptor_binary_proto)
54*6777b538SAndroid Build Coastguard Worker : state_(std::make_unique<State>(descriptor_binary_proto)) {}
55*6777b538SAndroid Build Coastguard Worker
TestProtoSetLoader(const base::FilePath & descriptor_path)56*6777b538SAndroid Build Coastguard Worker TestProtoSetLoader::TestProtoSetLoader(const base::FilePath& descriptor_path) {
57*6777b538SAndroid Build Coastguard Worker std::string file_contents;
58*6777b538SAndroid Build Coastguard Worker CHECK(base::ReadFileToString(descriptor_path, &file_contents))
59*6777b538SAndroid Build Coastguard Worker << "Couldn't load contents of " << descriptor_path;
60*6777b538SAndroid Build Coastguard Worker state_ = std::make_unique<State>(file_contents);
61*6777b538SAndroid Build Coastguard Worker }
62*6777b538SAndroid Build Coastguard Worker
63*6777b538SAndroid Build Coastguard Worker TestProtoSetLoader::~TestProtoSetLoader() = default;
64*6777b538SAndroid Build Coastguard Worker
ParseFromText(const std::string_view type_name,const std::string & proto_text) const65*6777b538SAndroid Build Coastguard Worker std::string TestProtoSetLoader::ParseFromText(
66*6777b538SAndroid Build Coastguard Worker const std::string_view type_name,
67*6777b538SAndroid Build Coastguard Worker const std::string& proto_text) const {
68*6777b538SAndroid Build Coastguard Worker // Create a message of the given type, parse, and return.
69*6777b538SAndroid Build Coastguard Worker std::unique_ptr<google::protobuf::Message> message =
70*6777b538SAndroid Build Coastguard Worker state_->NewMessage(type_name);
71*6777b538SAndroid Build Coastguard Worker CHECK(
72*6777b538SAndroid Build Coastguard Worker google::protobuf::TextFormat::ParseFromString(proto_text, message.get()));
73*6777b538SAndroid Build Coastguard Worker return message->SerializeAsString();
74*6777b538SAndroid Build Coastguard Worker }
75*6777b538SAndroid Build Coastguard Worker
PrintToText(const std::string_view type_name,const std::string & serialized_message) const76*6777b538SAndroid Build Coastguard Worker std::string TestProtoSetLoader::PrintToText(
77*6777b538SAndroid Build Coastguard Worker const std::string_view type_name,
78*6777b538SAndroid Build Coastguard Worker const std::string& serialized_message) const {
79*6777b538SAndroid Build Coastguard Worker // Create a message of the given type, read the serialized message, and
80*6777b538SAndroid Build Coastguard Worker // print to text format.
81*6777b538SAndroid Build Coastguard Worker std::unique_ptr<google::protobuf::Message> message =
82*6777b538SAndroid Build Coastguard Worker state_->NewMessage(type_name);
83*6777b538SAndroid Build Coastguard Worker CHECK(message->ParseFromString(serialized_message));
84*6777b538SAndroid Build Coastguard Worker std::string proto_text;
85*6777b538SAndroid Build Coastguard Worker CHECK(google::protobuf::TextFormat::PrintToString(*message, &proto_text));
86*6777b538SAndroid Build Coastguard Worker return proto_text;
87*6777b538SAndroid Build Coastguard Worker }
88*6777b538SAndroid Build Coastguard Worker
TestProtoLoader(std::string_view descriptor_binary_proto,std::string_view type_name)89*6777b538SAndroid Build Coastguard Worker TestProtoLoader::TestProtoLoader(std::string_view descriptor_binary_proto,
90*6777b538SAndroid Build Coastguard Worker std::string_view type_name)
91*6777b538SAndroid Build Coastguard Worker : set_loader_(descriptor_binary_proto), type_name_(type_name) {}
92*6777b538SAndroid Build Coastguard Worker
TestProtoLoader(const base::FilePath & descriptor_path,std::string_view type_name)93*6777b538SAndroid Build Coastguard Worker TestProtoLoader::TestProtoLoader(const base::FilePath& descriptor_path,
94*6777b538SAndroid Build Coastguard Worker std::string_view type_name)
95*6777b538SAndroid Build Coastguard Worker : set_loader_(descriptor_path), type_name_(type_name) {}
96*6777b538SAndroid Build Coastguard Worker
97*6777b538SAndroid Build Coastguard Worker TestProtoLoader::~TestProtoLoader() = default;
98*6777b538SAndroid Build Coastguard Worker
ParseFromText(const std::string & proto_text,std::string & serialized_message) const99*6777b538SAndroid Build Coastguard Worker void TestProtoLoader::ParseFromText(const std::string& proto_text,
100*6777b538SAndroid Build Coastguard Worker std::string& serialized_message) const {
101*6777b538SAndroid Build Coastguard Worker serialized_message = set_loader_.ParseFromText(type_name_, proto_text);
102*6777b538SAndroid Build Coastguard Worker }
103*6777b538SAndroid Build Coastguard Worker
PrintToText(const std::string & serialized_message,std::string & proto_text) const104*6777b538SAndroid Build Coastguard Worker void TestProtoLoader::PrintToText(const std::string& serialized_message,
105*6777b538SAndroid Build Coastguard Worker std::string& proto_text) const {
106*6777b538SAndroid Build Coastguard Worker proto_text = set_loader_.PrintToText(type_name_, serialized_message);
107*6777b538SAndroid Build Coastguard Worker }
108*6777b538SAndroid Build Coastguard Worker
109*6777b538SAndroid Build Coastguard Worker } // namespace base
110