1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <google/protobuf/compiler/annotation_test_util.h>
32 
33 #include <cstdint>
34 #include <memory>
35 
36 #include <google/protobuf/testing/file.h>
37 #include <google/protobuf/testing/file.h>
38 #include <google/protobuf/compiler/code_generator.h>
39 #include <google/protobuf/compiler/command_line_interface.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/io/zero_copy_stream.h>
42 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
43 #include <google/protobuf/descriptor.pb.h>
44 #include <google/protobuf/testing/googletest.h>
45 #include <gtest/gtest.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace compiler {
50 namespace annotation_test_util {
51 namespace {
52 
53 // A CodeGenerator that captures the FileDescriptor it's passed as a
54 // FileDescriptorProto.
55 class DescriptorCapturingGenerator : public CodeGenerator {
56  public:
57   // Does not own file; file must outlive the Generator.
DescriptorCapturingGenerator(FileDescriptorProto * file)58   explicit DescriptorCapturingGenerator(FileDescriptorProto* file)
59       : file_(file) {}
60 
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const61   virtual bool Generate(const FileDescriptor* file,
62                         const std::string& parameter, GeneratorContext* context,
63                         std::string* error) const {
64     file->CopyTo(file_);
65     return true;
66   }
67 
68  private:
69   FileDescriptorProto* file_;
70 };
71 }  // namespace
72 
AddFile(const std::string & filename,const std::string & data)73 void AddFile(const std::string& filename, const std::string& data) {
74   GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/" + filename, data,
75                              true));
76 }
77 
RunProtoCompiler(const std::string & filename,const std::string & plugin_specific_args,CommandLineInterface * cli,FileDescriptorProto * file)78 bool RunProtoCompiler(const std::string& filename,
79                       const std::string& plugin_specific_args,
80                       CommandLineInterface* cli, FileDescriptorProto* file) {
81   cli->SetInputsAreProtoPathRelative(true);
82 
83   DescriptorCapturingGenerator capturing_generator(file);
84   cli->RegisterGenerator("--capture_out", &capturing_generator, "");
85 
86   std::string proto_path = "-I" + TestTempDir();
87   std::string capture_out = "--capture_out=" + TestTempDir();
88 
89   const char* argv[] = {"protoc", proto_path.c_str(),
90                         plugin_specific_args.c_str(), capture_out.c_str(),
91                         filename.c_str()};
92 
93   return cli->Run(5, argv) == 0;
94 }
95 
DecodeMetadata(const std::string & path,GeneratedCodeInfo * info)96 bool DecodeMetadata(const std::string& path, GeneratedCodeInfo* info) {
97   std::string data;
98   GOOGLE_CHECK_OK(File::GetContents(path, &data, true));
99   io::ArrayInputStream input(data.data(), data.size());
100   return info->ParseFromZeroCopyStream(&input);
101 }
102 
FindAnnotationsOnPath(const GeneratedCodeInfo & info,const std::string & source_file,const std::vector<int> & path,std::vector<const GeneratedCodeInfo::Annotation * > * annotations)103 void FindAnnotationsOnPath(
104     const GeneratedCodeInfo& info, const std::string& source_file,
105     const std::vector<int>& path,
106     std::vector<const GeneratedCodeInfo::Annotation*>* annotations) {
107   for (int i = 0; i < info.annotation_size(); ++i) {
108     const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i);
109     if (annotation->source_file() != source_file ||
110         annotation->path_size() != path.size()) {
111       continue;
112     }
113     int node = 0;
114     for (; node < path.size(); ++node) {
115       if (annotation->path(node) != path[node]) {
116         break;
117       }
118     }
119     if (node == path.size()) {
120       annotations->push_back(annotation);
121     }
122   }
123 }
124 
FindAnnotationOnPath(const GeneratedCodeInfo & info,const std::string & source_file,const std::vector<int> & path)125 const GeneratedCodeInfo::Annotation* FindAnnotationOnPath(
126     const GeneratedCodeInfo& info, const std::string& source_file,
127     const std::vector<int>& path) {
128   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
129   FindAnnotationsOnPath(info, source_file, path, &annotations);
130   if (annotations.empty()) {
131     return NULL;
132   }
133   return annotations[0];
134 }
135 
AtLeastOneAnnotationMatchesSubstring(const std::string & file_content,const std::vector<const GeneratedCodeInfo::Annotation * > & annotations,const std::string & expected_text)136 bool AtLeastOneAnnotationMatchesSubstring(
137     const std::string& file_content,
138     const std::vector<const GeneratedCodeInfo::Annotation*>& annotations,
139     const std::string& expected_text) {
140   for (std::vector<const GeneratedCodeInfo::Annotation*>::const_iterator
141            i = annotations.begin(),
142            e = annotations.end();
143        i != e; ++i) {
144     const GeneratedCodeInfo::Annotation* annotation = *i;
145     uint32_t begin = annotation->begin();
146     uint32_t end = annotation->end();
147     if (end < begin || end > file_content.size()) {
148       return false;
149     }
150     if (file_content.substr(begin, end - begin) == expected_text) {
151       return true;
152     }
153   }
154   return false;
155 }
156 
AnnotationMatchesSubstring(const std::string & file_content,const GeneratedCodeInfo::Annotation * annotation,const std::string & expected_text)157 bool AnnotationMatchesSubstring(const std::string& file_content,
158                                 const GeneratedCodeInfo::Annotation* annotation,
159                                 const std::string& expected_text) {
160   std::vector<const GeneratedCodeInfo::Annotation*> annotations;
161   annotations.push_back(annotation);
162   return AtLeastOneAnnotationMatchesSubstring(file_content, annotations,
163                                               expected_text);
164 }
165 }  // namespace annotation_test_util
166 }  // namespace compiler
167 }  // namespace protobuf
168 }  // namespace google
169