LevelDBDatabase::Compact compacts db (on disk) w/o requiring reopen.
[chromium-blink-merge.git] / dbus / message_unittest.cc
blob0c944d9d6171fd7a04fc8a7e18b22b9b3e9eaa26
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "dbus/message.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/posix/eintr_wrapper.h"
11 #include "dbus/object_path.h"
12 #include "dbus/test_proto.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace dbus {
17 // Test that a byte can be properly written and read. We only have this
18 // test for byte, as repeating this for other basic types is too redundant.
19 TEST(MessageTest, AppendAndPopByte) {
20 scoped_ptr<Response> message(Response::CreateEmpty());
21 MessageWriter writer(message.get());
22 writer.AppendByte(123); // The input is 123.
24 MessageReader reader(message.get());
25 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read.
26 ASSERT_EQ(Message::BYTE, reader.GetDataType());
28 bool bool_value = false;
29 // Should fail as the type is not bool here.
30 ASSERT_FALSE(reader.PopBool(&bool_value));
32 uint8 byte_value = 0;
33 ASSERT_TRUE(reader.PopByte(&byte_value));
34 EXPECT_EQ(123, byte_value); // Should match with the input.
35 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
37 // Try to get another byte. Should fail.
38 ASSERT_FALSE(reader.PopByte(&byte_value));
41 // Check all basic types can be properly written and read.
42 TEST(MessageTest, AppendAndPopBasicDataTypes) {
43 scoped_ptr<Response> message(Response::CreateEmpty());
44 MessageWriter writer(message.get());
46 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
47 writer.AppendByte(0);
48 writer.AppendBool(true);
49 writer.AppendInt16(2);
50 writer.AppendUint16(3);
51 writer.AppendInt32(4);
52 writer.AppendUint32(5);
53 writer.AppendInt64(6);
54 writer.AppendUint64(7);
55 writer.AppendDouble(8.0);
56 writer.AppendString("string");
57 writer.AppendObjectPath(ObjectPath("/object/path"));
59 uint8 byte_value = 0;
60 bool bool_value = false;
61 int16 int16_value = 0;
62 uint16 uint16_value = 0;
63 int32 int32_value = 0;
64 uint32 uint32_value = 0;
65 int64 int64_value = 0;
66 uint64 uint64_value = 0;
67 double double_value = 0;
68 std::string string_value;
69 ObjectPath object_path_value;
71 MessageReader reader(message.get());
72 ASSERT_TRUE(reader.HasMoreData());
73 ASSERT_TRUE(reader.PopByte(&byte_value));
74 ASSERT_TRUE(reader.PopBool(&bool_value));
75 ASSERT_TRUE(reader.PopInt16(&int16_value));
76 ASSERT_TRUE(reader.PopUint16(&uint16_value));
77 ASSERT_TRUE(reader.PopInt32(&int32_value));
78 ASSERT_TRUE(reader.PopUint32(&uint32_value));
79 ASSERT_TRUE(reader.PopInt64(&int64_value));
80 ASSERT_TRUE(reader.PopUint64(&uint64_value));
81 ASSERT_TRUE(reader.PopDouble(&double_value));
82 ASSERT_TRUE(reader.PopString(&string_value));
83 ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
84 ASSERT_FALSE(reader.HasMoreData());
86 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
87 EXPECT_EQ(0, byte_value);
88 EXPECT_EQ(true, bool_value);
89 EXPECT_EQ(2, int16_value);
90 EXPECT_EQ(3U, uint16_value);
91 EXPECT_EQ(4, int32_value);
92 EXPECT_EQ(5U, uint32_value);
93 EXPECT_EQ(6, int64_value);
94 EXPECT_EQ(7U, uint64_value);
95 EXPECT_DOUBLE_EQ(8.0, double_value);
96 EXPECT_EQ("string", string_value);
97 EXPECT_EQ(ObjectPath("/object/path"), object_path_value);
100 // Check all basic types can be properly written and read.
101 TEST(MessageTest, AppendAndPopFileDescriptor) {
102 if (!IsDBusTypeUnixFdSupported()) {
103 LOG(WARNING) << "FD passing is not supported";
104 return;
107 scoped_ptr<Response> message(Response::CreateEmpty());
108 MessageWriter writer(message.get());
110 // Append stdout.
111 FileDescriptor temp(1);
112 // Descriptor should not be valid until checked.
113 ASSERT_FALSE(temp.is_valid());
114 // NB: thread IO requirements not relevant for unit tests.
115 temp.CheckValidity();
116 ASSERT_TRUE(temp.is_valid());
117 writer.AppendFileDescriptor(temp);
119 FileDescriptor fd_value;
121 MessageReader reader(message.get());
122 ASSERT_TRUE(reader.HasMoreData());
123 ASSERT_TRUE(reader.PopFileDescriptor(&fd_value));
124 ASSERT_FALSE(reader.HasMoreData());
125 // Descriptor is not valid until explicitly checked.
126 ASSERT_FALSE(fd_value.is_valid());
127 fd_value.CheckValidity();
128 ASSERT_TRUE(fd_value.is_valid());
130 // Stdout should be returned but we cannot check the descriptor
131 // value because stdout will be dup'd. Instead check st_rdev
132 // which should be identical.
133 struct stat sb_stdout;
134 int status_stdout = HANDLE_EINTR(fstat(1, &sb_stdout));
135 ASSERT_GE(status_stdout, 0);
136 struct stat sb_fd;
137 int status_fd = HANDLE_EINTR(fstat(fd_value.value(), &sb_fd));
138 ASSERT_GE(status_fd, 0);
139 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev);
142 // Check all variant types can be properly written and read.
143 TEST(MessageTest, AppendAndPopVariantDataTypes) {
144 scoped_ptr<Response> message(Response::CreateEmpty());
145 MessageWriter writer(message.get());
147 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
148 writer.AppendVariantOfByte(0);
149 writer.AppendVariantOfBool(true);
150 writer.AppendVariantOfInt16(2);
151 writer.AppendVariantOfUint16(3);
152 writer.AppendVariantOfInt32(4);
153 writer.AppendVariantOfUint32(5);
154 writer.AppendVariantOfInt64(6);
155 writer.AppendVariantOfUint64(7);
156 writer.AppendVariantOfDouble(8.0);
157 writer.AppendVariantOfString("string");
158 writer.AppendVariantOfObjectPath(ObjectPath("/object/path"));
160 uint8 byte_value = 0;
161 bool bool_value = false;
162 int16 int16_value = 0;
163 uint16 uint16_value = 0;
164 int32 int32_value = 0;
165 uint32 uint32_value = 0;
166 int64 int64_value = 0;
167 uint64 uint64_value = 0;
168 double double_value = 0;
169 std::string string_value;
170 ObjectPath object_path_value;
172 MessageReader reader(message.get());
173 ASSERT_TRUE(reader.HasMoreData());
174 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value));
175 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value));
176 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value));
177 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value));
178 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value));
179 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value));
180 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value));
181 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value));
182 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value));
183 ASSERT_TRUE(reader.PopVariantOfString(&string_value));
184 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value));
185 ASSERT_FALSE(reader.HasMoreData());
187 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
188 EXPECT_EQ(0, byte_value);
189 EXPECT_EQ(true, bool_value);
190 EXPECT_EQ(2, int16_value);
191 EXPECT_EQ(3U, uint16_value);
192 EXPECT_EQ(4, int32_value);
193 EXPECT_EQ(5U, uint32_value);
194 EXPECT_EQ(6, int64_value);
195 EXPECT_EQ(7U, uint64_value);
196 EXPECT_DOUBLE_EQ(8.0, double_value);
197 EXPECT_EQ("string", string_value);
198 EXPECT_EQ(ObjectPath("/object/path"), object_path_value);
201 TEST(MessageTest, ArrayOfBytes) {
202 scoped_ptr<Response> message(Response::CreateEmpty());
203 MessageWriter writer(message.get());
204 std::vector<uint8> bytes;
205 bytes.push_back(1);
206 bytes.push_back(2);
207 bytes.push_back(3);
208 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
210 MessageReader reader(message.get());
211 const uint8* output_bytes = NULL;
212 size_t length = 0;
213 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
214 ASSERT_FALSE(reader.HasMoreData());
215 ASSERT_EQ(3U, length);
216 EXPECT_EQ(1, output_bytes[0]);
217 EXPECT_EQ(2, output_bytes[1]);
218 EXPECT_EQ(3, output_bytes[2]);
221 TEST(MessageTest, ArrayOfBytes_Empty) {
222 scoped_ptr<Response> message(Response::CreateEmpty());
223 MessageWriter writer(message.get());
224 std::vector<uint8> bytes;
225 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
227 MessageReader reader(message.get());
228 const uint8* output_bytes = NULL;
229 size_t length = 0;
230 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
231 ASSERT_FALSE(reader.HasMoreData());
232 ASSERT_EQ(0U, length);
233 EXPECT_EQ(NULL, output_bytes);
236 TEST(MessageTest, ArrayOfStrings) {
237 scoped_ptr<Response> message(Response::CreateEmpty());
238 MessageWriter writer(message.get());
239 std::vector<std::string> strings;
240 strings.push_back("fee");
241 strings.push_back("fie");
242 strings.push_back("foe");
243 strings.push_back("fum");
244 writer.AppendArrayOfStrings(strings);
246 MessageReader reader(message.get());
247 std::vector<std::string> output_strings;
248 ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings));
249 ASSERT_FALSE(reader.HasMoreData());
250 ASSERT_EQ(4U, output_strings.size());
251 EXPECT_EQ("fee", output_strings[0]);
252 EXPECT_EQ("fie", output_strings[1]);
253 EXPECT_EQ("foe", output_strings[2]);
254 EXPECT_EQ("fum", output_strings[3]);
257 TEST(MessageTest, ArrayOfObjectPaths) {
258 scoped_ptr<Response> message(Response::CreateEmpty());
259 MessageWriter writer(message.get());
260 std::vector<ObjectPath> object_paths;
261 object_paths.push_back(ObjectPath("/object/path/1"));
262 object_paths.push_back(ObjectPath("/object/path/2"));
263 object_paths.push_back(ObjectPath("/object/path/3"));
264 writer.AppendArrayOfObjectPaths(object_paths);
266 MessageReader reader(message.get());
267 std::vector<ObjectPath> output_object_paths;
268 ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths));
269 ASSERT_FALSE(reader.HasMoreData());
270 ASSERT_EQ(3U, output_object_paths.size());
271 EXPECT_EQ(ObjectPath("/object/path/1"), output_object_paths[0]);
272 EXPECT_EQ(ObjectPath("/object/path/2"), output_object_paths[1]);
273 EXPECT_EQ(ObjectPath("/object/path/3"), output_object_paths[2]);
276 TEST(MessageTest, ProtoBuf) {
277 scoped_ptr<Response> message(Response::CreateEmpty());
278 MessageWriter writer(message.get());
279 TestProto send_message;
280 send_message.set_text("testing");
281 send_message.set_number(123);
282 writer.AppendProtoAsArrayOfBytes(send_message);
284 MessageReader reader(message.get());
285 TestProto receive_message;
286 ASSERT_TRUE(reader.PopArrayOfBytesAsProto(&receive_message));
287 EXPECT_EQ(receive_message.text(), send_message.text());
288 EXPECT_EQ(receive_message.number(), send_message.number());
292 // Test that an array can be properly written and read. We only have this
293 // test for array, as repeating this for other container types is too
294 // redundant.
295 TEST(MessageTest, OpenArrayAndPopArray) {
296 scoped_ptr<Response> message(Response::CreateEmpty());
297 MessageWriter writer(message.get());
298 MessageWriter array_writer(NULL);
299 writer.OpenArray("s", &array_writer); // Open an array of strings.
300 array_writer.AppendString("foo");
301 array_writer.AppendString("bar");
302 array_writer.AppendString("baz");
303 writer.CloseContainer(&array_writer);
305 MessageReader reader(message.get());
306 ASSERT_EQ(Message::ARRAY, reader.GetDataType());
307 MessageReader array_reader(NULL);
308 ASSERT_TRUE(reader.PopArray(&array_reader));
309 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
311 std::string string_value;
312 ASSERT_TRUE(array_reader.PopString(&string_value));
313 EXPECT_EQ("foo", string_value);
314 ASSERT_TRUE(array_reader.PopString(&string_value));
315 EXPECT_EQ("bar", string_value);
316 ASSERT_TRUE(array_reader.PopString(&string_value));
317 EXPECT_EQ("baz", string_value);
318 // Should not have more data to read.
319 ASSERT_FALSE(array_reader.HasMoreData());
322 // Create a complex message using array, struct, variant, dict entry, and
323 // make sure it can be read properly.
324 TEST(MessageTest, CreateComplexMessageAndReadIt) {
325 scoped_ptr<Response> message(Response::CreateEmpty());
326 MessageWriter writer(message.get());
328 MessageWriter array_writer(NULL);
329 // Open an array of variants.
330 writer.OpenArray("v", &array_writer);
332 // The first value in the array.
334 MessageWriter variant_writer(NULL);
335 // Open a variant of a boolean.
336 array_writer.OpenVariant("b", &variant_writer);
337 variant_writer.AppendBool(true);
338 array_writer.CloseContainer(&variant_writer);
341 // The second value in the array.
343 MessageWriter variant_writer(NULL);
344 // Open a variant of a struct that contains a string and an int32.
345 array_writer.OpenVariant("(si)", &variant_writer);
347 MessageWriter struct_writer(NULL);
348 variant_writer.OpenStruct(&struct_writer);
349 struct_writer.AppendString("string");
350 struct_writer.AppendInt32(123);
351 variant_writer.CloseContainer(&struct_writer);
353 array_writer.CloseContainer(&variant_writer);
356 // The third value in the array.
358 MessageWriter variant_writer(NULL);
359 // Open a variant of an array of string-to-int64 dict entries.
360 array_writer.OpenVariant("a{sx}", &variant_writer);
362 // Opens an array of string-to-int64 dict entries.
363 MessageWriter dict_array_writer(NULL);
364 variant_writer.OpenArray("{sx}", &dict_array_writer);
366 // Opens a string-to-int64 dict entries.
367 MessageWriter dict_entry_writer(NULL);
368 dict_array_writer.OpenDictEntry(&dict_entry_writer);
369 dict_entry_writer.AppendString("foo");
370 dict_entry_writer.AppendInt64(GG_INT64_C(1234567890123456789));
371 dict_array_writer.CloseContainer(&dict_entry_writer);
373 variant_writer.CloseContainer(&dict_array_writer);
375 array_writer.CloseContainer(&variant_writer);
378 writer.CloseContainer(&array_writer);
380 // What we have created looks like this:
381 EXPECT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
382 "signature: av\n"
383 "\n"
384 "array [\n"
385 " variant bool true\n"
386 " variant struct {\n"
387 " string \"string\"\n"
388 " int32 123\n"
389 " }\n"
390 " variant array [\n"
391 " dict entry {\n"
392 " string \"foo\"\n"
393 " int64 1234567890123456789\n"
394 " }\n"
395 " ]\n"
396 "]\n",
397 message->ToString());
399 MessageReader reader(message.get());
400 MessageReader array_reader(NULL);
401 ASSERT_TRUE(reader.PopArray(&array_reader));
403 // The first value in the array.
404 bool bool_value = false;
405 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
406 EXPECT_EQ(true, bool_value);
408 // The second value in the array.
410 MessageReader variant_reader(NULL);
411 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
413 MessageReader struct_reader(NULL);
414 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
415 std::string string_value;
416 ASSERT_TRUE(struct_reader.PopString(&string_value));
417 EXPECT_EQ("string", string_value);
418 int32 int32_value = 0;
419 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
420 EXPECT_EQ(123, int32_value);
421 ASSERT_FALSE(struct_reader.HasMoreData());
423 ASSERT_FALSE(variant_reader.HasMoreData());
426 // The third value in the array.
428 MessageReader variant_reader(NULL);
429 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
431 MessageReader dict_array_reader(NULL);
432 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
434 MessageReader dict_entry_reader(NULL);
435 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
436 std::string string_value;
437 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
438 EXPECT_EQ("foo", string_value);
439 int64 int64_value = 0;
440 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
441 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value);
443 ASSERT_FALSE(dict_array_reader.HasMoreData());
445 ASSERT_FALSE(variant_reader.HasMoreData());
447 ASSERT_FALSE(array_reader.HasMoreData());
448 ASSERT_FALSE(reader.HasMoreData());
451 TEST(MessageTest, MethodCall) {
452 MethodCall method_call("com.example.Interface", "SomeMethod");
453 EXPECT_TRUE(method_call.raw_message() != NULL);
454 EXPECT_EQ(Message::MESSAGE_METHOD_CALL, method_call.GetMessageType());
455 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call.GetMessageTypeAsString());
456 method_call.SetDestination("com.example.Service");
457 method_call.SetPath(ObjectPath("/com/example/Object"));
459 MessageWriter writer(&method_call);
460 writer.AppendString("payload");
462 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
463 "destination: com.example.Service\n"
464 "path: /com/example/Object\n"
465 "interface: com.example.Interface\n"
466 "member: SomeMethod\n"
467 "signature: s\n"
468 "\n"
469 "string \"payload\"\n",
470 method_call.ToString());
473 TEST(MessageTest, MethodCall_FromRawMessage) {
474 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
475 dbus_message_set_interface(raw_message, "com.example.Interface");
476 dbus_message_set_member(raw_message, "SomeMethod");
478 scoped_ptr<MethodCall> method_call(MethodCall::FromRawMessage(raw_message));
479 EXPECT_EQ("com.example.Interface", method_call->GetInterface());
480 EXPECT_EQ("SomeMethod", method_call->GetMember());
483 TEST(MessageTest, Signal) {
484 Signal signal("com.example.Interface", "SomeSignal");
485 EXPECT_TRUE(signal.raw_message() != NULL);
486 EXPECT_EQ(Message::MESSAGE_SIGNAL, signal.GetMessageType());
487 EXPECT_EQ("MESSAGE_SIGNAL", signal.GetMessageTypeAsString());
488 signal.SetPath(ObjectPath("/com/example/Object"));
490 MessageWriter writer(&signal);
491 writer.AppendString("payload");
493 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
494 "path: /com/example/Object\n"
495 "interface: com.example.Interface\n"
496 "member: SomeSignal\n"
497 "signature: s\n"
498 "\n"
499 "string \"payload\"\n",
500 signal.ToString());
503 TEST(MessageTest, Signal_FromRawMessage) {
504 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
505 dbus_message_set_interface(raw_message, "com.example.Interface");
506 dbus_message_set_member(raw_message, "SomeSignal");
508 scoped_ptr<Signal> signal(Signal::FromRawMessage(raw_message));
509 EXPECT_EQ("com.example.Interface", signal->GetInterface());
510 EXPECT_EQ("SomeSignal", signal->GetMember());
513 TEST(MessageTest, Response) {
514 scoped_ptr<Response> response(Response::CreateEmpty());
515 EXPECT_TRUE(response->raw_message());
516 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
517 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
520 TEST(MessageTest, Response_FromMethodCall) {
521 const uint32 kSerial = 123;
522 MethodCall method_call("com.example.Interface", "SomeMethod");
523 method_call.SetSerial(kSerial);
525 scoped_ptr<Response> response(
526 Response::FromMethodCall(&method_call));
527 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN, response->GetMessageType());
528 EXPECT_EQ("MESSAGE_METHOD_RETURN", response->GetMessageTypeAsString());
529 // The serial should be copied to the reply serial.
530 EXPECT_EQ(kSerial, response->GetReplySerial());
533 TEST(MessageTest, ErrorResponse_FromMethodCall) {
534 const uint32 kSerial = 123;
535 const char kErrorMessage[] = "error message";
537 MethodCall method_call("com.example.Interface", "SomeMethod");
538 method_call.SetSerial(kSerial);
540 scoped_ptr<ErrorResponse> error_response(
541 ErrorResponse::FromMethodCall(&method_call,
542 DBUS_ERROR_FAILED,
543 kErrorMessage));
544 EXPECT_EQ(Message::MESSAGE_ERROR, error_response->GetMessageType());
545 EXPECT_EQ("MESSAGE_ERROR", error_response->GetMessageTypeAsString());
546 // The serial should be copied to the reply serial.
547 EXPECT_EQ(kSerial, error_response->GetReplySerial());
549 // Error message should be added to the payload.
550 MessageReader reader(error_response.get());
551 std::string error_message;
552 ASSERT_TRUE(reader.PopString(&error_message));
553 EXPECT_EQ(kErrorMessage, error_message);
556 TEST(MessageTest, GetAndSetHeaders) {
557 scoped_ptr<Response> message(Response::CreateEmpty());
559 EXPECT_EQ("", message->GetDestination());
560 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
561 EXPECT_EQ("", message->GetInterface());
562 EXPECT_EQ("", message->GetMember());
563 EXPECT_EQ("", message->GetErrorName());
564 EXPECT_EQ("", message->GetSender());
565 EXPECT_EQ(0U, message->GetSerial());
566 EXPECT_EQ(0U, message->GetReplySerial());
568 EXPECT_TRUE(message->SetDestination("org.chromium.destination"));
569 EXPECT_TRUE(message->SetPath(ObjectPath("/org/chromium/path")));
570 EXPECT_TRUE(message->SetInterface("org.chromium.interface"));
571 EXPECT_TRUE(message->SetMember("member"));
572 EXPECT_TRUE(message->SetErrorName("org.chromium.error"));
573 EXPECT_TRUE(message->SetSender(":1.2"));
574 message->SetSerial(123);
575 message->SetReplySerial(456);
577 EXPECT_EQ("org.chromium.destination", message->GetDestination());
578 EXPECT_EQ(ObjectPath("/org/chromium/path"), message->GetPath());
579 EXPECT_EQ("org.chromium.interface", message->GetInterface());
580 EXPECT_EQ("member", message->GetMember());
581 EXPECT_EQ("org.chromium.error", message->GetErrorName());
582 EXPECT_EQ(":1.2", message->GetSender());
583 EXPECT_EQ(123U, message->GetSerial());
584 EXPECT_EQ(456U, message->GetReplySerial());
587 TEST(MessageTest, SetInvalidHeaders) {
588 scoped_ptr<Response> message(Response::CreateEmpty());
589 EXPECT_EQ("", message->GetDestination());
590 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
591 EXPECT_EQ("", message->GetInterface());
592 EXPECT_EQ("", message->GetMember());
593 EXPECT_EQ("", message->GetErrorName());
594 EXPECT_EQ("", message->GetSender());
596 // Empty element between periods.
597 EXPECT_FALSE(message->SetDestination("org..chromium"));
598 // Trailing '/' is only allowed for the root path.
599 EXPECT_FALSE(message->SetPath(ObjectPath("/org/chromium/")));
600 // Interface name cannot contain '/'.
601 EXPECT_FALSE(message->SetInterface("org/chromium/interface"));
602 // Member name cannot begin with a digit.
603 EXPECT_FALSE(message->SetMember("1member"));
604 // Error name cannot begin with a period.
605 EXPECT_FALSE(message->SetErrorName(".org.chromium.error"));
606 // Disallowed characters.
607 EXPECT_FALSE(message->SetSender("?!#*"));
609 EXPECT_EQ("", message->GetDestination());
610 EXPECT_EQ(ObjectPath(std::string()), message->GetPath());
611 EXPECT_EQ("", message->GetInterface());
612 EXPECT_EQ("", message->GetMember());
613 EXPECT_EQ("", message->GetErrorName());
614 EXPECT_EQ("", message->GetSender());
617 TEST(MessageTest, ToString_LongString) {
618 const std::string kLongString(1000, 'o');
620 scoped_ptr<Response> message(Response::CreateEmpty());
621 MessageWriter writer(message.get());
622 writer.AppendString(kLongString);
624 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
625 "signature: s\n\n"
626 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo"
627 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... "
628 "(1000 bytes in total)\"\n",
629 message->ToString());
632 } // namespace dbus