~ubuntu-branches/ubuntu/quantal/protobuf/quantal

« back to all changes in this revision

Viewing changes to src/google/protobuf/message_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Iustin Pop
  • Date: 2008-08-03 11:01:44 UTC
  • Revision ID: james.westby@ubuntu.com-20080803110144-uyiw41bf1m2oe17t
Tags: upstream-2.0.0~b
ImportĀ upstreamĀ versionĀ 2.0.0~b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Protocol Buffers - Google's data interchange format
 
2
// Copyright 2008 Google Inc.
 
3
// http://code.google.com/p/protobuf/
 
4
//
 
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
 
8
//
 
9
//      http://www.apache.org/licenses/LICENSE-2.0
 
10
//
 
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.
 
16
 
 
17
// Author: kenton@google.com (Kenton Varda)
 
18
//  Based on original Protocol Buffers design by
 
19
//  Sanjay Ghemawat, Jeff Dean, and others.
 
20
 
 
21
#include <google/protobuf/message.h>
 
22
 
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#ifdef _MSC_VER
 
27
#include <io.h>
 
28
#else
 
29
#include <unistd.h>
 
30
#endif
 
31
#include <sstream>
 
32
 
 
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>
 
40
 
 
41
#include <google/protobuf/testing/googletest.h>
 
42
#include <gtest/gtest.h>
 
43
 
 
44
namespace google {
 
45
namespace protobuf {
 
46
 
 
47
#ifndef O_BINARY
 
48
#ifdef _O_BINARY
 
49
#define O_BINARY _O_BINARY
 
50
#else
 
51
#define O_BINARY 0     // If this isn't defined, the platform doesn't need it.
 
52
#endif
 
53
#endif
 
54
 
 
55
TEST(MessageTest, SerializeHelpers) {
 
56
  // TODO(kenton):  Test more helpers?  They're all two-liners so it seems
 
57
  //   like a waste of time.
 
58
 
 
59
  protobuf_unittest::TestAllTypes message;
 
60
  TestUtil::SetAllFields(&message);
 
61
  stringstream stream;
 
62
 
 
63
  string str1("foo");
 
64
  string str2("bar");
 
65
 
 
66
  message.SerializeToString(&str1);
 
67
  message.AppendToString(&str2);
 
68
  message.SerializeToOstream(&stream);
 
69
 
 
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
 
73
  // stdout.
 
74
  EXPECT_TRUE(str2.substr(3) == str1);
 
75
 
 
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);
 
79
 
 
80
}
 
81
 
 
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);
 
86
 
 
87
  unittest::TestAllTypes message;
 
88
  EXPECT_TRUE(message.ParseFromFileDescriptor(file));
 
89
  TestUtil::ExpectAllFieldsSet(message);
 
90
 
 
91
  EXPECT_GE(close(file), 0);
 
92
}
 
93
 
 
94
TEST(MessageTest, ParseHelpers) {
 
95
  // TODO(kenton):  Test more helpers?  They're all two-liners so it seems
 
96
  //   like a waste of time.
 
97
  string data;
 
98
 
 
99
  {
 
100
    // Set up.
 
101
    protobuf_unittest::TestAllTypes message;
 
102
    TestUtil::SetAllFields(&message);
 
103
    message.SerializeToString(&data);
 
104
  }
 
105
 
 
106
  {
 
107
    // Test ParseFromString.
 
108
    protobuf_unittest::TestAllTypes message;
 
109
    EXPECT_TRUE(message.ParseFromString(data));
 
110
    TestUtil::ExpectAllFieldsSet(message);
 
111
  }
 
112
 
 
113
  {
 
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);
 
120
  }
 
121
}
 
122
 
 
123
TEST(MessageTest, ParseFailsIfNotInitialized) {
 
124
  unittest::TestRequired message;
 
125
  vector<string> errors;
 
126
 
 
127
  {
 
128
    ScopedMemoryLog log;
 
129
    EXPECT_FALSE(message.ParseFromString(""));
 
130
    errors = log.GetMessages(ERROR);
 
131
  }
 
132
 
 
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",
 
136
            errors[0]);
 
137
}
 
138
 
 
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));
 
144
}
 
145
 
 
146
TEST(MessageTest, InitializationErrorString) {
 
147
  unittest::TestRequired message;
 
148
  EXPECT_EQ("a, b, c", message.InitializationErrorString());
 
149
}
 
150
 
 
151
#ifdef GTEST_HAS_DEATH_TEST  // death tests do not work on Windows yet.
 
152
 
 
153
TEST(MessageTest, SerializeFailsIfNotInitialized) {
 
154
  unittest::TestRequired message;
 
155
  string data;
 
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");
 
159
}
 
160
 
 
161
TEST(MessageTest, CheckInitialized) {
 
162
  unittest::TestRequired message;
 
163
  EXPECT_DEATH(message.CheckInitialized(),
 
164
    "Message of type \"protobuf_unittest.TestRequired\" is missing required "
 
165
    "fields: a, b, c");
 
166
}
 
167
 
 
168
#endif  // GTEST_HAS_DEATH_TEST
 
169
 
 
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));
 
175
}
 
176
 
 
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]);
 
185
}
 
186
 
 
187
TEST(MessageTest, ParseFailsOnInvalidMessageEnd) {
 
188
  unittest::TestAllTypes message;
 
189
 
 
190
  // Control case.
 
191
  EXPECT_TRUE(message.ParseFromArray("", 0));
 
192
 
 
193
  // The byte is a valid varint, but not a valid tag (zero).
 
194
  EXPECT_FALSE(message.ParseFromArray("\0", 1));
 
195
 
 
196
  // The byte is a malformed varint.
 
197
  EXPECT_FALSE(message.ParseFromArray("\200", 1));
 
198
 
 
199
  // The byte is an endgroup tag, but we aren't parsing a group.
 
200
  EXPECT_FALSE(message.ParseFromArray("\014", 1));
 
201
}
 
202
 
 
203
TEST(MessageFactoryTest, GeneratedFactoryLookup) {
 
204
  EXPECT_EQ(
 
205
    MessageFactory::generated_factory()->GetPrototype(
 
206
      protobuf_unittest::TestAllTypes::descriptor()),
 
207
    &protobuf_unittest::TestAllTypes::default_instance());
 
208
}
 
209
 
 
210
TEST(MessageFactoryTest, GeneratedFactoryUnknownType) {
 
211
  // Construct a new descriptor.
 
212
  DescriptorPool pool;
 
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);
 
217
 
 
218
  // Trying to construct it should return NULL.
 
219
  EXPECT_TRUE(
 
220
    MessageFactory::generated_factory()->GetPrototype(descriptor) == NULL);
 
221
}
 
222
 
 
223
}  // namespace protobuf
 
224
}  // namespace google