• 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/testing/file.h>
48 #include <google/protobuf/testing/file.h>
49 #include <google/protobuf/compiler/cpp/generator.h>
50 #include <google/protobuf/compiler/importer.h>
51 #include <google/protobuf/test_util2.h>
52 #include <google/protobuf/io/zero_copy_stream_impl.h>
53 #include <google/protobuf/descriptor.h>
54 #include <google/protobuf/stubs/strutil.h>
55 #include <google/protobuf/testing/googletest.h>
56 #include <gtest/gtest.h>
57 #include <google/protobuf/stubs/substitute.h>
58 #include <google/protobuf/compiler/cpp/helpers.h>
59 #include <google/protobuf/stubs/map_util.h>
60 #include <google/protobuf/stubs/stl_util.h>
61 
62 namespace google {
63 namespace protobuf {
64 namespace compiler {
65 namespace cpp {
66 
67 namespace {
68 
69 class MockErrorCollector : public MultiFileErrorCollector {
70  public:
MockErrorCollector()71   MockErrorCollector() {}
~MockErrorCollector()72   ~MockErrorCollector() override {}
73 
74   std::string text_;
75 
76   // implements ErrorCollector ---------------------------------------
AddError(const std::string & filename,int line,int column,const std::string & message)77   void AddError(const std::string& filename, int line, int column,
78                 const std::string& message) override {
79     strings::SubstituteAndAppend(&text_, "$0:$1:$2: $3\n", filename, line, column,
80                               message);
81   }
82 };
83 
84 class MockGeneratorContext : public GeneratorContext {
85  public:
ExpectFileMatches(const std::string & virtual_filename,const std::string & physical_filename)86   void ExpectFileMatches(const std::string& virtual_filename,
87                          const std::string& physical_filename) {
88     auto it = files_.find(virtual_filename);
89     ASSERT_TRUE(it != files_.end())
90         << "Generator failed to generate file: " << virtual_filename;
91 
92     std::string expected_contents = *it->second;
93     std::string actual_contents;
94     GOOGLE_CHECK_OK(
95         File::GetContents(TestUtil::TestSourceDir() + "/" + physical_filename,
96                           &actual_contents, true))
97         << physical_filename;
98     CleanStringLineEndings(&actual_contents, false);
99 
100 #ifdef WRITE_FILES  // Define to debug mismatched files.
101     GOOGLE_CHECK_OK(File::SetContents("/tmp/expected.cc", expected_contents,
102                                true));
103     GOOGLE_CHECK_OK(
104         File::SetContents("/tmp/actual.cc", actual_contents, true));
105 #endif
106 
107     ASSERT_EQ(expected_contents, actual_contents)
108         << physical_filename
109         << " needs to be regenerated.  Please run "
110            "generate_descriptor_proto.sh. "
111            "Then add this file to your CL.";
112   }
113 
114   // implements GeneratorContext --------------------------------------
115 
Open(const std::string & filename)116   io::ZeroCopyOutputStream* Open(const std::string& filename) override {
117     auto& map_slot = files_[filename];
118     map_slot.reset(new std::string);
119     return new io::StringOutputStream(map_slot.get());
120   }
121 
122  private:
123   std::map<std::string, std::unique_ptr<std::string>> files_;
124 };
125 
126 const char kDescriptorParameter[] = "dllexport_decl=PROTOBUF_EXPORT";
127 const char kPluginParameter[] = "dllexport_decl=PROTOC_EXPORT";
128 
129 
130 const char* test_protos[][2] = {
131     {"google/protobuf/descriptor", kDescriptorParameter},
132     {"google/protobuf/compiler/plugin", kPluginParameter},
133 };
134 
TEST(BootstrapTest,GeneratedFilesMatch)135 TEST(BootstrapTest, GeneratedFilesMatch) {
136   // We need a mapping from the actual file to virtual and actual path
137   // of the data to compare to.
138   std::map<std::string, std::string> vpath_map;
139   std::map<std::string, std::string> rpath_map;
140   rpath_map["third_party/protobuf/test_messages_proto2"] =
141       "net/proto2/z_generated_example/test_messages_proto2";
142   rpath_map["third_party/protobuf/test_messages_proto3"] =
143       "net/proto2/z_generated_example/test_messages_proto3";
144   rpath_map["net/proto2/internal/proto2_weak"] =
145       "net/proto2/z_generated_example/proto2_weak";
146 
147   DiskSourceTree source_tree;
148   source_tree.MapPath("", TestUtil::TestSourceDir());
149 
150   for (auto file_parameter : test_protos) {
151     MockErrorCollector error_collector;
152     Importer importer(&source_tree, &error_collector);
153     const FileDescriptor* file =
154         importer.Import(file_parameter[0] + std::string(".proto"));
155     ASSERT_TRUE(file != nullptr)
156         << "Can't import file " << file_parameter[0] + std::string(".proto")
157         << "\n";
158     EXPECT_EQ("", error_collector.text_);
159     CppGenerator generator;
160     MockGeneratorContext context;
161 #ifdef GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE
162     generator.set_opensource_runtime(true);
163     generator.set_runtime_include_base(GOOGLE_PROTOBUF_RUNTIME_INCLUDE_BASE);
164 #endif
165     std::string error;
166     ASSERT_TRUE(generator.Generate(file, file_parameter[1], &context, &error));
167 
168     std::string vpath =
169         FindWithDefault(vpath_map, file_parameter[0], file_parameter[0]);
170     std::string rpath =
171         FindWithDefault(rpath_map, file_parameter[0], file_parameter[0]);
172     context.ExpectFileMatches(vpath + ".pb.cc", rpath + ".pb.cc");
173     context.ExpectFileMatches(vpath + ".pb.h", rpath + ".pb.h");
174   }
175 }
176 
177 // test Generate in cpp_generator.cc
TEST(BootstrapTest,OptionNotExist)178 TEST(BootstrapTest, OptionNotExist) {
179   cpp::CppGenerator generator;
180   DescriptorPool pool;
181   GeneratorContext* generator_context = nullptr;
182   std::string parameter = "aaa";
183   std::string error;
184   ASSERT_FALSE(generator.Generate(
185       pool.FindFileByName("google/protobuf/descriptor.proto"), parameter,
186       generator_context, &error));
187   EXPECT_EQ(error, "Unknown generator option: " + parameter);
188 }
189 
190 }  // namespace
191 
192 }  // namespace cpp
193 }  // namespace compiler
194 }  // namespace protobuf
195 }  // namespace google
196