• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "google/protobuf/compiler/annotation_test_util.h"
9 
10 #include <cstdint>
11 #include <memory>
12 
13 #include "google/protobuf/testing/file.h"
14 #include "google/protobuf/testing/file.h"
15 #include "google/protobuf/compiler/code_generator.h"
16 #include "google/protobuf/compiler/command_line_interface.h"
17 #include "google/protobuf/descriptor.pb.h"
18 #include "google/protobuf/testing/googletest.h"
19 #include <gtest/gtest.h>
20 #include "absl/log/absl_check.h"
21 #include "absl/strings/string_view.h"
22 #include "google/protobuf/io/printer.h"
23 #include "google/protobuf/io/zero_copy_stream.h"
24 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
25 
26 namespace google {
27 namespace protobuf {
28 namespace compiler {
29 namespace annotation_test_util {
30 namespace {
31 
32 // A CodeGenerator that captures the FileDescriptor it's passed as a
33 // FileDescriptorProto.
34 class DescriptorCapturingGenerator : public CodeGenerator {
35  public:
36   // Does not own file; file must outlive the Generator.
DescriptorCapturingGenerator(FileDescriptorProto * file)37   explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
38       : file_(file) {}
39 
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const40   bool Generate(const FileDescriptor* file, const std::string& parameter,
41                 GeneratorContext* context, std::string* error) const override {
42     file->CopyTo(file_);
43     return true;
44   }
45 
46  private:
47   FileDescriptorProto* file_;
48 };
49 }  // namespace
50 
AddFile(absl::string_view filename,absl::string_view data)51 void AddFile(absl::string_view filename, absl::string_view data) {
52   ABSL_CHECK_OK(File::SetContents(
53       absl::StrCat(TestTempDir(), "/", filename), data, true));
54 }
55 
RunProtoCompiler(const std::string & filename,const std::string & plugin_specific_args,CommandLineInterface * cli,FileDescriptorProto * file)56 bool RunProtoCompiler(const std::string& filename,
57                       const std::string& plugin_specific_args,
58                       CommandLineInterface* cli, FileDescriptorProto* file) {
59   cli->SetInputsAreProtoPathRelative(true);
60 
61   DescriptorCapturingGenerator capturing_generator(file);
62   cli->RegisterGenerator("--capture_out", &capturing_generator, "");
63 
64   std::string proto_path = absl::StrCat("-I", TestTempDir());
65   std::string capture_out = absl::StrCat("--capture_out=", TestTempDir());
66 
67   const char* argv[] = {"protoc", proto_path.c_str(),
68                         plugin_specific_args.c_str(), capture_out.c_str(),
69                         filename.c_str()};
70 
71   return cli->Run(5, argv) == 0;
72 }
73 
DecodeMetadata(const std::string & path,GeneratedCodeInfo * info)74 bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info) {
75   std::string data;
76   ABSL_CHECK_OK(File::GetContents(path, &data, true));
77   io::ArrayInputStream input(data.data(), data.size());
78   return info->ParseFromZeroCopyStream(&input);
79 }
80 
FindAnnotationsOnPath(const GeneratedCodeInfo & info,const std::string & source_file,const std::vector<int> & path,std::vector<const GeneratedCodeInfo::Annotation * > * annotations)81 void FindAnnotationsOnPath(
82     const GeneratedCodeInfo& info, const std::string& source_file,
83     const std::vector<int>& path,
84     std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
85   for (int i = 0; i < info.annotation_size(); ++i) {
86     const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
87     if (annotation->source_file() != source_file ||
88         annotation->path_size() != path.size()) {
89       continue;
90     }
91     int node = 0;
92     for (; node < path.size(); ++node) {
93       if (annotation->path(node) != path[node]) {
94         break;
95       }
96     }
97     if (node == path.size()) {
98       annotations->push_back(annotation);
99     }
100   }
101 }
102 
FindAnnotationOnPath(const GeneratedCodeInfo & info,const std::string & source_file,const std::vector<int> & path)103 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
104     const GeneratedCodeInfo& info, const std::string& source_file,
105     const std::vector<int>& path) {
106   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
107   FindAnnotationsOnPath(info, source_file, path, &annotations);
108   if (annotations.empty()) {
109     return nullptr;
110   }
111   return annotations[0];
112 }
113 
AtLeastOneAnnotationMatchesSubstring(const std::string & file_content,const std::vector<const GeneratedCodeInfo::Annotation * > & annotations,const std::string & expected_text,absl::optional<GeneratedCodeInfo::Annotation::Semantic> semantic)114 bool AtLeastOneAnnotationMatchesSubstring(
115     const std::string& file_content,
116     const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
117     const std::string& expected_text,
118     absl::optional<GeneratedCodeInfo::Annotation::Semantic> semantic) {
119   for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
120            i = annotations.begin(),
121            e = annotations.end();
122        i != e; ++i) {
123     const GeneratedCodeInfo::Annotation* annotation = *i;
124     uint32_t begin = annotation->begin();
125     uint32_t end = annotation->end();
126     if (end < begin || end > file_content.size()) {
127       return false;
128     }
129     if (file_content.substr(begin, end - begin) == expected_text) {
130       return !semantic || annotation->semantic() == *semantic;
131     }
132   }
133   return false;
134 }
135 
AnnotationMatchesSubstring(const std::string & file_content,const GeneratedCodeInfo::Annotation * annotation,const std::string & expected_text)136 bool AnnotationMatchesSubstring(const std::string& file_content,
137                                 const GeneratedCodeInfo::Annotation* annotation,
138                                 const std::string& expected_text) {
139   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
140   annotations.push_back(annotation);
141   return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
142                                               expected_text);
143 }
144 
GetAnnotationSubstring(absl::string_view file_content,const GeneratedCodeInfo::Annotation & annotation)145 absl::optional<absl::string_view> GetAnnotationSubstring(
146     absl::string_view file_content,
147     const GeneratedCodeInfo::Annotation& annotation) {
148   auto begin = annotation.begin();
149   auto end = annotation.end();
150   if (begin < 0 || end < begin || end > file_content.size()) {
151     return absl::nullopt;
152   }
153   return file_content.substr(begin, end - begin);
154 }
155 }  // namespace annotation_test_util
156 }  // namespace compiler
157 }  // namespace protobuf
158 }  // namespace google
159