1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
17
// Author: kenton@google.com (Kenton Varda)
18
// Based on original Protocol Buffers design by
19
// Sanjay Ghemawat, Jeff Dean, and others.
21
#include <google/protobuf/message.h>
23
#include <sys/types.h>
33
#include <google/protobuf/stubs/common.h>
34
#include <google/protobuf/io/zero_copy_stream_impl.h>
35
#include <google/protobuf/io/coded_stream.h>
36
#include <google/protobuf/descriptor.h>
37
#include <google/protobuf/descriptor.pb.h>
38
#include <google/protobuf/unittest.pb.h>
39
#include <google/protobuf/test_util.h>
41
#include <google/protobuf/testing/googletest.h>
42
#include <gtest/gtest.h>
49
#define O_BINARY _O_BINARY
51
#define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
55
TEST(MessageTest, SerializeHelpers) {
56
// TODO(kenton): Test more helpers? They're all two-liners so it seems
57
// like a waste of time.
59
protobuf_unittest::TestAllTypes message;
60
TestUtil::SetAllFields(&message);
66
message.SerializeToString(&str1);
67
message.AppendToString(&str2);
68
message.SerializeToOstream(&stream);
70
EXPECT_EQ(str1.size() + 3, str2.size());
71
EXPECT_EQ("bar", str2.substr(0, 3));
72
// Don't use EXPECT_EQ because we don't want to dump raw binary data to
74
EXPECT_TRUE(str2.substr(3) == str1);
76
// GCC gives some sort of error if we try to just do stream.str() == str1.
77
string temp = stream.str();
78
EXPECT_TRUE(temp == str1);
82
TEST(MessageTest, ParseFromFileDescriptor) {
83
string filename = TestSourceDir() +
84
"/google/protobuf/testdata/golden_message";
85
int file = open(filename.c_str(), O_RDONLY | O_BINARY);
87
unittest::TestAllTypes message;
88
EXPECT_TRUE(message.ParseFromFileDescriptor(file));
89
TestUtil::ExpectAllFieldsSet(message);
91
EXPECT_GE(close(file), 0);
94
TEST(MessageTest, ParseHelpers) {
95
// TODO(kenton): Test more helpers? They're all two-liners so it seems
96
// like a waste of time.
101
protobuf_unittest::TestAllTypes message;
102
TestUtil::SetAllFields(&message);
103
message.SerializeToString(&data);
107
// Test ParseFromString.
108
protobuf_unittest::TestAllTypes message;
109
EXPECT_TRUE(message.ParseFromString(data));
110
TestUtil::ExpectAllFieldsSet(message);
114
// Test ParseFromIstream.
115
protobuf_unittest::TestAllTypes message;
116
stringstream stream(data);
117
EXPECT_TRUE(message.ParseFromIstream(&stream));
118
EXPECT_TRUE(stream.eof());
119
TestUtil::ExpectAllFieldsSet(message);
123
TEST(MessageTest, ParseFailsIfNotInitialized) {
124
unittest::TestRequired message;
125
vector<string> errors;
129
EXPECT_FALSE(message.ParseFromString(""));
130
errors = log.GetMessages(ERROR);
133
ASSERT_EQ(1, errors.size());
134
EXPECT_EQ("Can't parse message of type \"protobuf_unittest.TestRequired\" "
135
"because it is missing required fields: a, b, c",
139
TEST(MessageTest, BypassInitializationCheckOnParse) {
140
unittest::TestRequired message;
141
io::ArrayInputStream raw_input(NULL, 0);
142
io::CodedInputStream input(&raw_input);
143
EXPECT_TRUE(message.MergePartialFromCodedStream(&input));
146
TEST(MessageTest, InitializationErrorString) {
147
unittest::TestRequired message;
148
EXPECT_EQ("a, b, c", message.InitializationErrorString());
151
#ifdef GTEST_HAS_DEATH_TEST // death tests do not work on Windows yet.
153
TEST(MessageTest, SerializeFailsIfNotInitialized) {
154
unittest::TestRequired message;
156
EXPECT_DEBUG_DEATH(EXPECT_TRUE(message.SerializeToString(&data)),
157
"Can't serialize message of type \"protobuf_unittest.TestRequired\" because "
158
"it is missing required fields: a, b, c");
161
TEST(MessageTest, CheckInitialized) {
162
unittest::TestRequired message;
163
EXPECT_DEATH(message.CheckInitialized(),
164
"Message of type \"protobuf_unittest.TestRequired\" is missing required "
168
#endif // GTEST_HAS_DEATH_TEST
170
TEST(MessageTest, BypassInitializationCheckOnSerialize) {
171
unittest::TestRequired message;
172
io::ArrayOutputStream raw_output(NULL, 0);
173
io::CodedOutputStream output(&raw_output);
174
EXPECT_TRUE(message.SerializePartialToCodedStream(&output));
177
TEST(MessageTest, FindInitializationErrors) {
178
unittest::TestRequired message;
179
vector<string> errors;
180
message.FindInitializationErrors(&errors);
181
ASSERT_EQ(3, errors.size());
182
EXPECT_EQ("a", errors[0]);
183
EXPECT_EQ("b", errors[1]);
184
EXPECT_EQ("c", errors[2]);
187
TEST(MessageTest, ParseFailsOnInvalidMessageEnd) {
188
unittest::TestAllTypes message;
191
EXPECT_TRUE(message.ParseFromArray("", 0));
193
// The byte is a valid varint, but not a valid tag (zero).
194
EXPECT_FALSE(message.ParseFromArray("\0", 1));
196
// The byte is a malformed varint.
197
EXPECT_FALSE(message.ParseFromArray("\200", 1));
199
// The byte is an endgroup tag, but we aren't parsing a group.
200
EXPECT_FALSE(message.ParseFromArray("\014", 1));
203
TEST(MessageFactoryTest, GeneratedFactoryLookup) {
205
MessageFactory::generated_factory()->GetPrototype(
206
protobuf_unittest::TestAllTypes::descriptor()),
207
&protobuf_unittest::TestAllTypes::default_instance());
210
TEST(MessageFactoryTest, GeneratedFactoryUnknownType) {
211
// Construct a new descriptor.
213
FileDescriptorProto file;
214
file.set_name("foo.proto");
215
file.add_message_type()->set_name("Foo");
216
const Descriptor* descriptor = pool.BuildFile(file)->message_type(0);
218
// Trying to construct it should return NULL.
220
MessageFactory::generated_factory()->GetPrototype(descriptor) == NULL);
223
} // namespace protobuf
224
} // namespace google