• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // This test insures that net/proto2/proto/descriptor.pb.{h,cc} match exactly
36 // what would be generated by the protocol compiler.  These files are not
37 // generated automatically at build time because they are compiled into the
38 // protocol compiler itself.  So, if they were auto-generated, you'd have a
39 // chicken-and-egg problem.
40 //
41 // If this test fails, run the script
42 // "generate_descriptor_proto.sh" and add
43 // descriptor.pb.{h,cc} to your changelist.
44 
45 #include <map>
46 
47 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
48 #include <google/protobuf/compiler/cpp/cpp_generator.h>
49 #include <google/protobuf/compiler/importer.h>
50 #include <google/protobuf/test_util2.h>
51 #include <google/protobuf/io/zero_copy_stream_impl.h>
52 #include <google/protobuf/descriptor.h>
53 #include <google/protobuf/stubs/strutil.h>
54 #include <google/protobuf/stubs/substitute.h>
55 #include <google/protobuf/stubs/map_util.h>
56 #include <google/protobuf/stubs/stl_util.h>
57 
58 #include <google/protobuf/testing/file.h>
59 #include <google/protobuf/testing/file.h>
60 #include <google/protobuf/testing/googletest.h>
61 #include <gtest/gtest.h>
62 
63 namespace google {
64 namespace protobuf {
65 namespace compiler {
66 namespace cpp {
67 
68 namespace {
69 
70 class MockErrorCollector : public MultiFileErrorCollector {
71  public:
MockErrorCollector()72   MockErrorCollector() {}
~MockErrorCollector()73   ~MockErrorCollector() {}
74 
75   std::string text_;
76 
77   // implements ErrorCollector ---------------------------------------
AddError(const std::string & filename,int line,int column,const std::string & message)78   void AddError(const std::string& filename, int line, int column,
79                 const std::string& message) {
80     strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line,
81                                  column, message);
82   }
83 };
84 
85 class MockGeneratorContext : public GeneratorContext {
86  public:
MockGeneratorContext()87   MockGeneratorContext() {}
~MockGeneratorContext()88   ~MockGeneratorContext() { STLDeleteValues(&files_); }
89 
ExpectFileMatches(const std::string & virtual_filename,const std::string & physical_filename)90   void ExpectFileMatches(const std::string& virtual_filename,
91                          const std::string& physical_filename) {
92     std::string* expected_contents =
93         FindPtrOrNull(files_, virtual_filename);
94     ASSERT_TRUE(expected_contents != NULL)
95         << "Generator failed to generate file: " << virtual_filename;
96 
97     std::string actual_contents;
98     GOOGLE_CHECK_OK(
99         File::GetContents(TestUtil::TestSourceDir() + "/" + physical_filename,
100                           &actual_contents, true))
101         << physical_filename;
102     CleanStringLineEndings(&actual_contents, false);
103 
104 #ifdef WRITE_FILES  // Define to debug mismatched files.
105     GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", *expected_contents,
106                                true));
107     GOOGLE_CHECK_OK(
108         File::SetContents("/tmp/actual.cc", actual_contents, true));
109 #endif
110 
111     ASSERT_EQ(*expected_contents, actual_contents)
112         << physical_filename
113         << " needs to be regenerated.  Please run "
114            "generate_descriptor_proto.sh. "
115            "Then add this file to your CL.";
116   }
117 
118   // implements GeneratorContext --------------------------------------
119 
Open(const std::string & filename)120   virtual io::ZeroCopyOutputStream* Open(const std::string& filename) {
121     std::string** map_slot = &files_[filename];
122     delete *map_slot;
123     *map_slot = new std::string;
124 
125     return new io::StringOutputStream(*map_slot);
126   }
127 
128  private:
129   std::map<std::string, std::string*> files_;
130 };
131 
132 const char kDescriptorParameter[] = "dllexport_decl=PROTOBUF_EXPORT";
133 const char kPluginParameter[] = "dllexport_decl=PROTOC_EXPORT";
134 
135 
136 const char* test_protos[][2] = {
137     {"google/protobuf/descriptor", kDescriptorParameter},
138     {"google/protobuf/compiler/plugin", kPluginParameter},
139 };
140 
TEST(BootstrapTest,GeneratedFilesMatch)141 TEST(BootstrapTest, GeneratedFilesMatch) {
142   // We need a mapping from the actual file to virtual and actual path
143   // of the data to compare to.
144   std::map<std::string, std::string> vpath_map;
145   std::map<std::string, std::string> rpath_map;
146   rpath_map
147       ["third_party/protobuf_legacy_opensource/src/google/protobuf/"
148        "test_messages_proto2"] =
149           "net/proto2/z_generated_example/test_messages_proto2";
150   rpath_map
151       ["third_party/protobuf_legacy_opensource/src/google/protobuf/"
152        "test_messages_proto3"] =
153           "net/proto2/z_generated_example/test_messages_proto3";
154   rpath_map["net/proto2/internal/proto2_weak"] =
155       "net/proto2/z_generated_example/proto2_weak";
156 
157   DiskSourceTree source_tree;
158   source_tree.MapPath("", TestUtil::TestSourceDir());
159 
160   for (auto file_parameter : test_protos) {
161     MockErrorCollector error_collector;
162     Importer importer(&source_tree, &error_collector);
163     const FileDescriptor* file =
164         importer.Import(file_parameter[0] + std::string(".proto"));
165     ASSERT_TRUE(file != nullptr)
166         << "Can't import file " << file_parameter[0] + string(".proto") << "\n";
167     EXPECT_EQ("", error_collector.text_);
168     CppGenerator generator;
169     MockGeneratorContext context;
170 #ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE
171     generator.set_opensource_runtime(true);
172     generator.set_runtime_include_base(GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE);
173 #endif
174     std::string error;
175     ASSERT_TRUE(generator.Generate(file, file_parameter[1], &context, &error));
176 
177     std::string vpath =
178         FindWithDefault(vpath_map, file_parameter[0], file_parameter[0]);
179     std::string rpath =
180         FindWithDefault(rpath_map, file_parameter[0], file_parameter[0]);
181     context.ExpectFileMatches(vpath + ".pb.cc", rpath + ".pb.cc");
182     context.ExpectFileMatches(vpath + ".pb.h", rpath + ".pb.h");
183   }
184 }
185 
186 }  // namespace
187 
188 }  // namespace cpp
189 }  // namespace compiler
190 }  // namespace protobuf
191 }  // namespace google
192