/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/message_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 281 lines · 181 code · 49 blank · 51 comment · 4 complexity · c411ae4bd43922b7956cb5728dcce398 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/message.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #ifdef _MSC_VER
  38. #include <io.h>
  39. #else
  40. #include <unistd.h>
  41. #endif
  42. #include <sstream>
  43. #include <fstream>
  44. #include <google/protobuf/stubs/common.h>
  45. #include <google/protobuf/io/zero_copy_stream_impl.h>
  46. #include <google/protobuf/io/coded_stream.h>
  47. #include <google/protobuf/descriptor.h>
  48. #include <google/protobuf/descriptor.pb.h>
  49. #include <google/protobuf/unittest.pb.h>
  50. #include <google/protobuf/test_util.h>
  51. #include <google/protobuf/testing/googletest.h>
  52. #include <gtest/gtest.h>
  53. namespace google {
  54. namespace protobuf {
  55. #ifndef O_BINARY
  56. #ifdef _O_BINARY
  57. #define O_BINARY _O_BINARY
  58. #else
  59. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  60. #endif
  61. #endif
  62. TEST(MessageTest, SerializeHelpers) {
  63. // TODO(kenton): Test more helpers? They're all two-liners so it seems
  64. // like a waste of time.
  65. protobuf_unittest::TestAllTypes message;
  66. TestUtil::SetAllFields(&message);
  67. stringstream stream;
  68. string str1("foo");
  69. string str2("bar");
  70. EXPECT_TRUE(message.SerializeToString(&str1));
  71. EXPECT_TRUE(message.AppendToString(&str2));
  72. EXPECT_TRUE(message.SerializeToOstream(&stream));
  73. EXPECT_EQ(str1.size() + 3, str2.size());
  74. EXPECT_EQ("bar", str2.substr(0, 3));
  75. // Don't use EXPECT_EQ because we don't want to dump raw binary data to
  76. // stdout.
  77. EXPECT_TRUE(str2.substr(3) == str1);
  78. // GCC gives some sort of error if we try to just do stream.str() == str1.
  79. string temp = stream.str();
  80. EXPECT_TRUE(temp == str1);
  81. EXPECT_TRUE(message.SerializeAsString() == str1);
  82. }
  83. TEST(MessageTest, SerializeToBrokenOstream) {
  84. ofstream out;
  85. protobuf_unittest::TestAllTypes message;
  86. message.set_optional_int32(123);
  87. EXPECT_FALSE(message.SerializeToOstream(&out));
  88. }
  89. TEST(MessageTest, ParseFromFileDescriptor) {
  90. string filename = TestSourceDir() +
  91. "/google/protobuf/testdata/golden_message";
  92. int file = open(filename.c_str(), O_RDONLY | O_BINARY);
  93. unittest::TestAllTypes message;
  94. EXPECT_TRUE(message.ParseFromFileDescriptor(file));
  95. TestUtil::ExpectAllFieldsSet(message);
  96. EXPECT_GE(close(file), 0);
  97. }
  98. TEST(MessageTest, ParsePackedFromFileDescriptor) {
  99. string filename =
  100. TestSourceDir() +
  101. "/google/protobuf/testdata/golden_packed_fields_message";
  102. int file = open(filename.c_str(), O_RDONLY | O_BINARY);
  103. unittest::TestPackedTypes message;
  104. EXPECT_TRUE(message.ParseFromFileDescriptor(file));
  105. TestUtil::ExpectPackedFieldsSet(message);
  106. EXPECT_GE(close(file), 0);
  107. }
  108. TEST(MessageTest, ParseHelpers) {
  109. // TODO(kenton): Test more helpers? They're all two-liners so it seems
  110. // like a waste of time.
  111. string data;
  112. {
  113. // Set up.
  114. protobuf_unittest::TestAllTypes message;
  115. TestUtil::SetAllFields(&message);
  116. message.SerializeToString(&data);
  117. }
  118. {
  119. // Test ParseFromString.
  120. protobuf_unittest::TestAllTypes message;
  121. EXPECT_TRUE(message.ParseFromString(data));
  122. TestUtil::ExpectAllFieldsSet(message);
  123. }
  124. {
  125. // Test ParseFromIstream.
  126. protobuf_unittest::TestAllTypes message;
  127. stringstream stream(data);
  128. EXPECT_TRUE(message.ParseFromIstream(&stream));
  129. EXPECT_TRUE(stream.eof());
  130. TestUtil::ExpectAllFieldsSet(message);
  131. }
  132. {
  133. // Test ParseFromBoundedZeroCopyStream.
  134. string data_with_junk(data);
  135. data_with_junk.append("some junk on the end");
  136. io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
  137. protobuf_unittest::TestAllTypes message;
  138. EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
  139. TestUtil::ExpectAllFieldsSet(message);
  140. }
  141. {
  142. // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
  143. // EOF is reached before the expected number of bytes.
  144. io::ArrayInputStream stream(data.data(), data.size());
  145. protobuf_unittest::TestAllTypes message;
  146. EXPECT_FALSE(
  147. message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
  148. }
  149. }
  150. TEST(MessageTest, ParseFailsIfNotInitialized) {
  151. unittest::TestRequired message;
  152. vector<string> errors;
  153. {
  154. ScopedMemoryLog log;
  155. EXPECT_FALSE(message.ParseFromString(""));
  156. errors = log.GetMessages(ERROR);
  157. }
  158. ASSERT_EQ(1, errors.size());
  159. EXPECT_EQ("Can't parse message of type \"protobuf_unittest.TestRequired\" "
  160. "because it is missing required fields: a, b, c",
  161. errors[0]);
  162. }
  163. TEST(MessageTest, BypassInitializationCheckOnParse) {
  164. unittest::TestRequired message;
  165. io::ArrayInputStream raw_input(NULL, 0);
  166. io::CodedInputStream input(&raw_input);
  167. EXPECT_TRUE(message.MergePartialFromCodedStream(&input));
  168. }
  169. TEST(MessageTest, InitializationErrorString) {
  170. unittest::TestRequired message;
  171. EXPECT_EQ("a, b, c", message.InitializationErrorString());
  172. }
  173. #ifdef GTEST_HAS_DEATH_TEST // death tests do not work on Windows yet.
  174. TEST(MessageTest, SerializeFailsIfNotInitialized) {
  175. unittest::TestRequired message;
  176. string data;
  177. EXPECT_DEBUG_DEATH(EXPECT_TRUE(message.SerializeToString(&data)),
  178. "Can't serialize message of type \"protobuf_unittest.TestRequired\" because "
  179. "it is missing required fields: a, b, c");
  180. }
  181. TEST(MessageTest, CheckInitialized) {
  182. unittest::TestRequired message;
  183. EXPECT_DEATH(message.CheckInitialized(),
  184. "Message of type \"protobuf_unittest.TestRequired\" is missing required "
  185. "fields: a, b, c");
  186. }
  187. #endif // GTEST_HAS_DEATH_TEST
  188. TEST(MessageTest, BypassInitializationCheckOnSerialize) {
  189. unittest::TestRequired message;
  190. io::ArrayOutputStream raw_output(NULL, 0);
  191. io::CodedOutputStream output(&raw_output);
  192. EXPECT_TRUE(message.SerializePartialToCodedStream(&output));
  193. }
  194. TEST(MessageTest, FindInitializationErrors) {
  195. unittest::TestRequired message;
  196. vector<string> errors;
  197. message.FindInitializationErrors(&errors);
  198. ASSERT_EQ(3, errors.size());
  199. EXPECT_EQ("a", errors[0]);
  200. EXPECT_EQ("b", errors[1]);
  201. EXPECT_EQ("c", errors[2]);
  202. }
  203. TEST(MessageTest, ParseFailsOnInvalidMessageEnd) {
  204. unittest::TestAllTypes message;
  205. // Control case.
  206. EXPECT_TRUE(message.ParseFromArray("", 0));
  207. // The byte is a valid varint, but not a valid tag (zero).
  208. EXPECT_FALSE(message.ParseFromArray("\0", 1));
  209. // The byte is a malformed varint.
  210. EXPECT_FALSE(message.ParseFromArray("\200", 1));
  211. // The byte is an endgroup tag, but we aren't parsing a group.
  212. EXPECT_FALSE(message.ParseFromArray("\014", 1));
  213. }
  214. TEST(MessageFactoryTest, GeneratedFactoryLookup) {
  215. EXPECT_EQ(
  216. MessageFactory::generated_factory()->GetPrototype(
  217. protobuf_unittest::TestAllTypes::descriptor()),
  218. &protobuf_unittest::TestAllTypes::default_instance());
  219. }
  220. TEST(MessageFactoryTest, GeneratedFactoryUnknownType) {
  221. // Construct a new descriptor.
  222. DescriptorPool pool;
  223. FileDescriptorProto file;
  224. file.set_name("foo.proto");
  225. file.add_message_type()->set_name("Foo");
  226. const Descriptor* descriptor = pool.BuildFile(file)->message_type(0);
  227. // Trying to construct it should return NULL.
  228. EXPECT_TRUE(
  229. MessageFactory::generated_factory()->GetPrototype(descriptor) == NULL);
  230. }
  231. } // namespace protobuf
  232. } // namespace google