xref: /aosp_15_r20/external/cronet/base/test/test_proto_loader.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #ifndef BASE_TEST_TEST_PROTO_LOADER_H_
6 #define BASE_TEST_TEST_PROTO_LOADER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <string_view>
11 #include "base/files/file_path.h"
12 
13 namespace base {
14 
15 #if defined(COMPONENT_BUILD)
16 #if defined(WIN32)
17 
18 #if defined(PROTO_TEST_IMPLEMENTATION)
19 #define PROTO_TEST_EXPORT __declspec(dllexport)
20 #else
21 #define PROTO_TEST_EXPORT __declspec(dllimport)
22 #endif  // defined(PROTO_TEST_IMPLEMENTATION)
23 
24 #else  // defined(WIN32)
25 #if defined(PROTO_TEST_IMPLEMENTATION)
26 #define PROTO_TEST_EXPORT __attribute__((visibility("default")))
27 #else
28 #define PROTO_TEST_EXPORT
29 #endif
30 #endif
31 #else  // defined(COMPONENT_BUILD)
32 #define PROTO_TEST_EXPORT
33 #endif
34 
35 // This class works around the fact that chrome only includes the lite runtime
36 // of protobufs. Lite protobufs inherit from |MessageLite| and cannot be used to
37 // parse from text format. Parsing from text
38 // format is useful in tests. We cannot include the full version of a protobuf
39 // in test code because it would clash with the lite version.
40 //
41 // This class uses the protobuf descriptors (generated at compile time) to
42 // generate a |Message| that can be used to parse from text. This message can
43 // then be serialized to binary which can be parsed by the |MessageLite|.
44 class PROTO_TEST_EXPORT TestProtoSetLoader {
45  public:
46   explicit TestProtoSetLoader(const base::FilePath& descriptor_path);
47   explicit TestProtoSetLoader(std::string_view descriptor_binary_proto);
48   ~TestProtoSetLoader();
49   TestProtoSetLoader(const TestProtoSetLoader&) = delete;
50   TestProtoSetLoader& operator=(const TestProtoSetLoader&) = delete;
51 
52   // Parse a text proto into a binary proto. `type_name` is the full message
53   // type name including the package. This CHECK fails if the type is not
54   // found, or if the text cannot be parsed.
55   std::string ParseFromText(std::string_view type_name,
56                             const std::string& proto_text) const;
57 
58   // Returns the text proto format of `message`. This CHECK fails on error.
59   std::string PrintToText(std::string_view type_name,
60                           const std::string& message) const;
61 
62  private:
63   // Hide dependencies of protobuf_full from the header, so that
64   // proto_test_support doesn't need to add protobuf_full as a public dep.
65   class State;
66 
67   std::unique_ptr<State> state_;
68 };
69 
70 // Same as TestProtoSetLoader, but for a single message type.
71 class PROTO_TEST_EXPORT TestProtoLoader {
72  public:
73   TestProtoLoader(const base::FilePath& descriptor_path,
74                   std::string_view type_name);
75   TestProtoLoader(std::string_view descriptor_binary_proto,
76                   std::string_view type_name);
77   ~TestProtoLoader();
78   TestProtoLoader(const TestProtoLoader&) = delete;
79   TestProtoLoader& operator=(const TestProtoLoader&) = delete;
80 
81   void ParseFromText(const std::string& proto_text, std::string& message) const;
82   void PrintToText(const std::string& message, std::string& proto_text) const;
83 
84  private:
85   TestProtoSetLoader set_loader_;
86   std::string type_name_;
87 };
88 
89 }  // namespace base
90 
91 #endif  // BASE_TEST_TEST_PROTO_LOADER_H_
92