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