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"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "dbus/object_path.h"
14 #include "dbus/test_proto.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // Test that a byte can be properly written and read. We only have this
20 // test for byte, as repeating this for other basic types is too redundant.
21 TEST(MessageTest
, AppendAndPopByte
) {
22 scoped_ptr
<Response
> message(Response::CreateEmpty());
23 MessageWriter
writer(message
.get());
24 writer
.AppendByte(123); // The input is 123.
26 MessageReader
reader(message
.get());
27 ASSERT_TRUE(reader
.HasMoreData()); // Should have data to read.
28 ASSERT_EQ(Message::BYTE
, reader
.GetDataType());
29 ASSERT_EQ("y", reader
.GetDataSignature());
31 bool bool_value
= false;
32 // Should fail as the type is not bool here.
33 ASSERT_FALSE(reader
.PopBool(&bool_value
));
36 ASSERT_TRUE(reader
.PopByte(&byte_value
));
37 EXPECT_EQ(123, byte_value
); // Should match with the input.
38 ASSERT_FALSE(reader
.HasMoreData()); // Should not have more data to read.
40 // Try to get another byte. Should fail.
41 ASSERT_FALSE(reader
.PopByte(&byte_value
));
44 // Check all basic types can be properly written and read.
45 TEST(MessageTest
, AppendAndPopBasicDataTypes
) {
46 scoped_ptr
<Response
> message(Response::CreateEmpty());
47 MessageWriter
writer(message
.get());
49 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
51 writer
.AppendBool(true);
52 writer
.AppendInt16(2);
53 writer
.AppendUint16(3);
54 writer
.AppendInt32(4);
55 writer
.AppendUint32(5);
56 writer
.AppendInt64(6);
57 writer
.AppendUint64(7);
58 writer
.AppendDouble(8.0);
59 writer
.AppendString("string");
60 writer
.AppendObjectPath(ObjectPath("/object/path"));
63 bool bool_value
= false;
64 int16 int16_value
= 0;
65 uint16 uint16_value
= 0;
66 int32 int32_value
= 0;
67 uint32 uint32_value
= 0;
68 int64 int64_value
= 0;
69 uint64 uint64_value
= 0;
70 double double_value
= 0;
71 std::string string_value
;
72 ObjectPath object_path_value
;
74 MessageReader
reader(message
.get());
75 ASSERT_TRUE(reader
.HasMoreData());
76 ASSERT_EQ("y", reader
.GetDataSignature());
77 ASSERT_TRUE(reader
.PopByte(&byte_value
));
78 ASSERT_EQ("b", reader
.GetDataSignature());
79 ASSERT_TRUE(reader
.PopBool(&bool_value
));
80 ASSERT_EQ("n", reader
.GetDataSignature());
81 ASSERT_TRUE(reader
.PopInt16(&int16_value
));
82 ASSERT_EQ("q", reader
.GetDataSignature());
83 ASSERT_TRUE(reader
.PopUint16(&uint16_value
));
84 ASSERT_EQ("i", reader
.GetDataSignature());
85 ASSERT_TRUE(reader
.PopInt32(&int32_value
));
86 ASSERT_EQ("u", reader
.GetDataSignature());
87 ASSERT_TRUE(reader
.PopUint32(&uint32_value
));
88 ASSERT_EQ("x", reader
.GetDataSignature());
89 ASSERT_TRUE(reader
.PopInt64(&int64_value
));
90 ASSERT_EQ("t", reader
.GetDataSignature());
91 ASSERT_TRUE(reader
.PopUint64(&uint64_value
));
92 ASSERT_EQ("d", reader
.GetDataSignature());
93 ASSERT_TRUE(reader
.PopDouble(&double_value
));
94 ASSERT_EQ("s", reader
.GetDataSignature());
95 ASSERT_TRUE(reader
.PopString(&string_value
));
96 ASSERT_EQ("o", reader
.GetDataSignature());
97 ASSERT_TRUE(reader
.PopObjectPath(&object_path_value
));
98 ASSERT_EQ("", reader
.GetDataSignature());
99 ASSERT_FALSE(reader
.HasMoreData());
101 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
102 EXPECT_EQ(0, byte_value
);
103 EXPECT_EQ(true, bool_value
);
104 EXPECT_EQ(2, int16_value
);
105 EXPECT_EQ(3U, uint16_value
);
106 EXPECT_EQ(4, int32_value
);
107 EXPECT_EQ(5U, uint32_value
);
108 EXPECT_EQ(6, int64_value
);
109 EXPECT_EQ(7U, uint64_value
);
110 EXPECT_DOUBLE_EQ(8.0, double_value
);
111 EXPECT_EQ("string", string_value
);
112 EXPECT_EQ(ObjectPath("/object/path"), object_path_value
);
115 // Check all basic types can be properly written and read.
116 TEST(MessageTest
, AppendAndPopFileDescriptor
) {
117 if (!IsDBusTypeUnixFdSupported()) {
118 LOG(WARNING
) << "FD passing is not supported";
122 scoped_ptr
<Response
> message(Response::CreateEmpty());
123 MessageWriter
writer(message
.get());
126 FileDescriptor
temp(1);
127 // Descriptor should not be valid until checked.
128 ASSERT_FALSE(temp
.is_valid());
129 // NB: thread IO requirements not relevant for unit tests.
130 temp
.CheckValidity();
131 ASSERT_TRUE(temp
.is_valid());
132 writer
.AppendFileDescriptor(temp
);
134 FileDescriptor fd_value
;
136 MessageReader
reader(message
.get());
137 ASSERT_TRUE(reader
.HasMoreData());
138 ASSERT_EQ(Message::UNIX_FD
, reader
.GetDataType());
139 ASSERT_EQ("h", reader
.GetDataSignature());
140 ASSERT_TRUE(reader
.PopFileDescriptor(&fd_value
));
141 ASSERT_FALSE(reader
.HasMoreData());
142 // Descriptor is not valid until explicitly checked.
143 ASSERT_FALSE(fd_value
.is_valid());
144 fd_value
.CheckValidity();
145 ASSERT_TRUE(fd_value
.is_valid());
147 // Stdout should be returned but we cannot check the descriptor
148 // value because stdout will be dup'd. Instead check st_rdev
149 // which should be identical.
150 struct stat sb_stdout
;
151 int status_stdout
= HANDLE_EINTR(fstat(1, &sb_stdout
));
152 ASSERT_GE(status_stdout
, 0);
154 int status_fd
= HANDLE_EINTR(fstat(fd_value
.value(), &sb_fd
));
155 ASSERT_GE(status_fd
, 0);
156 EXPECT_EQ(sb_stdout
.st_rdev
, sb_fd
.st_rdev
);
159 // Check all variant types can be properly written and read.
160 TEST(MessageTest
, AppendAndPopVariantDataTypes
) {
161 scoped_ptr
<Response
> message(Response::CreateEmpty());
162 MessageWriter
writer(message
.get());
164 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
165 writer
.AppendVariantOfByte(0);
166 writer
.AppendVariantOfBool(true);
167 writer
.AppendVariantOfInt16(2);
168 writer
.AppendVariantOfUint16(3);
169 writer
.AppendVariantOfInt32(4);
170 writer
.AppendVariantOfUint32(5);
171 writer
.AppendVariantOfInt64(6);
172 writer
.AppendVariantOfUint64(7);
173 writer
.AppendVariantOfDouble(8.0);
174 writer
.AppendVariantOfString("string");
175 writer
.AppendVariantOfObjectPath(ObjectPath("/object/path"));
177 uint8 byte_value
= 0;
178 bool bool_value
= false;
179 int16 int16_value
= 0;
180 uint16 uint16_value
= 0;
181 int32 int32_value
= 0;
182 uint32 uint32_value
= 0;
183 int64 int64_value
= 0;
184 uint64 uint64_value
= 0;
185 double double_value
= 0;
186 std::string string_value
;
187 ObjectPath object_path_value
;
189 MessageReader
reader(message
.get());
190 ASSERT_TRUE(reader
.HasMoreData());
191 ASSERT_EQ("v", reader
.GetDataSignature());
192 ASSERT_TRUE(reader
.PopVariantOfByte(&byte_value
));
193 ASSERT_EQ("v", reader
.GetDataSignature());
194 ASSERT_TRUE(reader
.PopVariantOfBool(&bool_value
));
195 ASSERT_EQ("v", reader
.GetDataSignature());
196 ASSERT_TRUE(reader
.PopVariantOfInt16(&int16_value
));
197 ASSERT_EQ("v", reader
.GetDataSignature());
198 ASSERT_TRUE(reader
.PopVariantOfUint16(&uint16_value
));
199 ASSERT_EQ("v", reader
.GetDataSignature());
200 ASSERT_TRUE(reader
.PopVariantOfInt32(&int32_value
));
201 ASSERT_EQ("v", reader
.GetDataSignature());
202 ASSERT_TRUE(reader
.PopVariantOfUint32(&uint32_value
));
203 ASSERT_EQ("v", reader
.GetDataSignature());
204 ASSERT_TRUE(reader
.PopVariantOfInt64(&int64_value
));
205 ASSERT_EQ("v", reader
.GetDataSignature());
206 ASSERT_TRUE(reader
.PopVariantOfUint64(&uint64_value
));
207 ASSERT_EQ("v", reader
.GetDataSignature());
208 ASSERT_TRUE(reader
.PopVariantOfDouble(&double_value
));
209 ASSERT_EQ("v", reader
.GetDataSignature());
210 ASSERT_TRUE(reader
.PopVariantOfString(&string_value
));
211 ASSERT_EQ("v", reader
.GetDataSignature());
212 ASSERT_TRUE(reader
.PopVariantOfObjectPath(&object_path_value
));
213 ASSERT_EQ("", reader
.GetDataSignature());
214 ASSERT_FALSE(reader
.HasMoreData());
216 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
217 EXPECT_EQ(0, byte_value
);
218 EXPECT_EQ(true, bool_value
);
219 EXPECT_EQ(2, int16_value
);
220 EXPECT_EQ(3U, uint16_value
);
221 EXPECT_EQ(4, int32_value
);
222 EXPECT_EQ(5U, uint32_value
);
223 EXPECT_EQ(6, int64_value
);
224 EXPECT_EQ(7U, uint64_value
);
225 EXPECT_DOUBLE_EQ(8.0, double_value
);
226 EXPECT_EQ("string", string_value
);
227 EXPECT_EQ(ObjectPath("/object/path"), object_path_value
);
230 TEST(MessageTest
, ArrayOfBytes
) {
231 scoped_ptr
<Response
> message(Response::CreateEmpty());
232 MessageWriter
writer(message
.get());
233 std::vector
<uint8
> bytes
;
237 writer
.AppendArrayOfBytes(bytes
.data(), bytes
.size());
239 MessageReader
reader(message
.get());
240 const uint8
* output_bytes
= NULL
;
242 ASSERT_EQ("ay", reader
.GetDataSignature());
243 ASSERT_TRUE(reader
.PopArrayOfBytes(&output_bytes
, &length
));
244 ASSERT_FALSE(reader
.HasMoreData());
245 ASSERT_EQ(3U, length
);
246 EXPECT_EQ(1, output_bytes
[0]);
247 EXPECT_EQ(2, output_bytes
[1]);
248 EXPECT_EQ(3, output_bytes
[2]);
251 TEST(MessageTest
, ArrayOfBytes_Empty
) {
252 scoped_ptr
<Response
> message(Response::CreateEmpty());
253 MessageWriter
writer(message
.get());
254 std::vector
<uint8
> bytes
;
255 writer
.AppendArrayOfBytes(bytes
.data(), bytes
.size());
257 MessageReader
reader(message
.get());
258 const uint8
* output_bytes
= NULL
;
260 ASSERT_EQ("ay", reader
.GetDataSignature());
261 ASSERT_TRUE(reader
.PopArrayOfBytes(&output_bytes
, &length
));
262 ASSERT_FALSE(reader
.HasMoreData());
263 ASSERT_EQ(0U, length
);
264 EXPECT_EQ(NULL
, output_bytes
);
267 TEST(MessageTest
, ArrayOfStrings
) {
268 scoped_ptr
<Response
> message(Response::CreateEmpty());
269 MessageWriter
writer(message
.get());
270 std::vector
<std::string
> strings
;
271 strings
.push_back("fee");
272 strings
.push_back("fie");
273 strings
.push_back("foe");
274 strings
.push_back("fum");
275 writer
.AppendArrayOfStrings(strings
);
277 MessageReader
reader(message
.get());
278 std::vector
<std::string
> output_strings
;
279 ASSERT_EQ("as", reader
.GetDataSignature());
280 ASSERT_TRUE(reader
.PopArrayOfStrings(&output_strings
));
281 ASSERT_FALSE(reader
.HasMoreData());
282 ASSERT_EQ(4U, output_strings
.size());
283 EXPECT_EQ("fee", output_strings
[0]);
284 EXPECT_EQ("fie", output_strings
[1]);
285 EXPECT_EQ("foe", output_strings
[2]);
286 EXPECT_EQ("fum", output_strings
[3]);
289 TEST(MessageTest
, ArrayOfObjectPaths
) {
290 scoped_ptr
<Response
> message(Response::CreateEmpty());
291 MessageWriter
writer(message
.get());
292 std::vector
<ObjectPath
> object_paths
;
293 object_paths
.push_back(ObjectPath("/object/path/1"));
294 object_paths
.push_back(ObjectPath("/object/path/2"));
295 object_paths
.push_back(ObjectPath("/object/path/3"));
296 writer
.AppendArrayOfObjectPaths(object_paths
);
298 MessageReader
reader(message
.get());
299 std::vector
<ObjectPath
> output_object_paths
;
300 ASSERT_EQ("ao", reader
.GetDataSignature());
301 ASSERT_TRUE(reader
.PopArrayOfObjectPaths(&output_object_paths
));
302 ASSERT_FALSE(reader
.HasMoreData());
303 ASSERT_EQ(3U, output_object_paths
.size());
304 EXPECT_EQ(ObjectPath("/object/path/1"), output_object_paths
[0]);
305 EXPECT_EQ(ObjectPath("/object/path/2"), output_object_paths
[1]);
306 EXPECT_EQ(ObjectPath("/object/path/3"), output_object_paths
[2]);
309 TEST(MessageTest
, ProtoBuf
) {
310 scoped_ptr
<Response
> message(Response::CreateEmpty());
311 MessageWriter
writer(message
.get());
312 TestProto send_message
;
313 send_message
.set_text("testing");
314 send_message
.set_number(123);
315 writer
.AppendProtoAsArrayOfBytes(send_message
);
317 MessageReader
reader(message
.get());
318 TestProto receive_message
;
319 ASSERT_EQ("ay", reader
.GetDataSignature());
320 ASSERT_TRUE(reader
.PopArrayOfBytesAsProto(&receive_message
));
321 EXPECT_EQ(receive_message
.text(), send_message
.text());
322 EXPECT_EQ(receive_message
.number(), send_message
.number());
326 // Test that an array can be properly written and read. We only have this
327 // test for array, as repeating this for other container types is too
329 TEST(MessageTest
, OpenArrayAndPopArray
) {
330 scoped_ptr
<Response
> message(Response::CreateEmpty());
331 MessageWriter
writer(message
.get());
332 MessageWriter
array_writer(NULL
);
333 writer
.OpenArray("s", &array_writer
); // Open an array of strings.
334 array_writer
.AppendString("foo");
335 array_writer
.AppendString("bar");
336 array_writer
.AppendString("baz");
337 writer
.CloseContainer(&array_writer
);
339 MessageReader
reader(message
.get());
340 ASSERT_EQ(Message::ARRAY
, reader
.GetDataType());
341 ASSERT_EQ("as", reader
.GetDataSignature());
342 MessageReader
array_reader(NULL
);
343 ASSERT_TRUE(reader
.PopArray(&array_reader
));
344 ASSERT_FALSE(reader
.HasMoreData()); // Should not have more data to read.
346 std::string string_value
;
347 ASSERT_TRUE(array_reader
.PopString(&string_value
));
348 EXPECT_EQ("foo", string_value
);
349 ASSERT_TRUE(array_reader
.PopString(&string_value
));
350 EXPECT_EQ("bar", string_value
);
351 ASSERT_TRUE(array_reader
.PopString(&string_value
));
352 EXPECT_EQ("baz", string_value
);
353 // Should not have more data to read.
354 ASSERT_FALSE(array_reader
.HasMoreData());
357 // Create a complex message using array, struct, variant, dict entry, and
358 // make sure it can be read properly.
359 TEST(MessageTest
, CreateComplexMessageAndReadIt
) {
360 scoped_ptr
<Response
> message(Response::CreateEmpty());
361 MessageWriter
writer(message
.get());
363 MessageWriter
array_writer(NULL
);
364 // Open an array of variants.
365 writer
.OpenArray("v", &array_writer
);
367 // The first value in the array.
369 MessageWriter
variant_writer(NULL
);
370 // Open a variant of a boolean.
371 array_writer
.OpenVariant("b", &variant_writer
);
372 variant_writer
.AppendBool(true);
373 array_writer
.CloseContainer(&variant_writer
);
376 // The second value in the array.
378 MessageWriter
variant_writer(NULL
);
379 // Open a variant of a struct that contains a string and an int32.
380 array_writer
.OpenVariant("(si)", &variant_writer
);
382 MessageWriter
struct_writer(NULL
);
383 variant_writer
.OpenStruct(&struct_writer
);
384 struct_writer
.AppendString("string");
385 struct_writer
.AppendInt32(123);
386 variant_writer
.CloseContainer(&struct_writer
);
388 array_writer
.CloseContainer(&variant_writer
);
391 // The third value in the array.
393 MessageWriter
variant_writer(NULL
);
394 // Open a variant of an array of string-to-int64 dict entries.
395 array_writer
.OpenVariant("a{sx}", &variant_writer
);
397 // Opens an array of string-to-int64 dict entries.
398 MessageWriter
dict_array_writer(NULL
);
399 variant_writer
.OpenArray("{sx}", &dict_array_writer
);
401 // Opens a string-to-int64 dict entries.
402 MessageWriter
dict_entry_writer(NULL
);
403 dict_array_writer
.OpenDictEntry(&dict_entry_writer
);
404 dict_entry_writer
.AppendString("foo");
405 dict_entry_writer
.AppendInt64(INT64_C(1234567890123456789));
406 dict_array_writer
.CloseContainer(&dict_entry_writer
);
408 variant_writer
.CloseContainer(&dict_array_writer
);
410 array_writer
.CloseContainer(&variant_writer
);
413 writer
.CloseContainer(&array_writer
);
415 // What we have created looks like this:
416 EXPECT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
420 " variant bool true\n"
421 " variant struct {\n"
422 " string \"string\"\n"
428 " int64 1234567890123456789\n"
432 message
->ToString());
434 MessageReader
reader(message
.get());
435 ASSERT_EQ("av", reader
.GetDataSignature());
436 MessageReader
array_reader(NULL
);
437 ASSERT_TRUE(reader
.PopArray(&array_reader
));
439 // The first value in the array.
440 bool bool_value
= false;
441 ASSERT_EQ("v", array_reader
.GetDataSignature());
442 ASSERT_TRUE(array_reader
.PopVariantOfBool(&bool_value
));
443 EXPECT_EQ(true, bool_value
);
445 // The second value in the array.
447 MessageReader
variant_reader(NULL
);
448 ASSERT_TRUE(array_reader
.PopVariant(&variant_reader
));
450 MessageReader
struct_reader(NULL
);
451 ASSERT_EQ("(si)", variant_reader
.GetDataSignature());
452 ASSERT_TRUE(variant_reader
.PopStruct(&struct_reader
));
453 std::string string_value
;
454 ASSERT_TRUE(struct_reader
.PopString(&string_value
));
455 EXPECT_EQ("string", string_value
);
456 int32 int32_value
= 0;
457 ASSERT_TRUE(struct_reader
.PopInt32(&int32_value
));
458 EXPECT_EQ(123, int32_value
);
459 ASSERT_FALSE(struct_reader
.HasMoreData());
461 ASSERT_FALSE(variant_reader
.HasMoreData());
464 // The third value in the array.
466 MessageReader
variant_reader(NULL
);
467 ASSERT_TRUE(array_reader
.PopVariant(&variant_reader
));
469 MessageReader
dict_array_reader(NULL
);
470 ASSERT_EQ("a{sx}", variant_reader
.GetDataSignature());
471 ASSERT_TRUE(variant_reader
.PopArray(&dict_array_reader
));
473 MessageReader
dict_entry_reader(NULL
);
474 ASSERT_TRUE(dict_array_reader
.PopDictEntry(&dict_entry_reader
));
475 std::string string_value
;
476 ASSERT_TRUE(dict_entry_reader
.PopString(&string_value
));
477 EXPECT_EQ("foo", string_value
);
478 int64 int64_value
= 0;
479 ASSERT_TRUE(dict_entry_reader
.PopInt64(&int64_value
));
480 EXPECT_EQ(INT64_C(1234567890123456789), int64_value
);
482 ASSERT_FALSE(dict_array_reader
.HasMoreData());
484 ASSERT_FALSE(variant_reader
.HasMoreData());
486 ASSERT_FALSE(array_reader
.HasMoreData());
487 ASSERT_FALSE(reader
.HasMoreData());
490 TEST(MessageTest
, MethodCall
) {
491 MethodCall
method_call("com.example.Interface", "SomeMethod");
492 EXPECT_TRUE(method_call
.raw_message() != NULL
);
493 EXPECT_EQ(Message::MESSAGE_METHOD_CALL
, method_call
.GetMessageType());
494 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call
.GetMessageTypeAsString());
495 method_call
.SetDestination("com.example.Service");
496 method_call
.SetPath(ObjectPath("/com/example/Object"));
498 MessageWriter
writer(&method_call
);
499 writer
.AppendString("payload");
501 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
502 "destination: com.example.Service\n"
503 "path: /com/example/Object\n"
504 "interface: com.example.Interface\n"
505 "member: SomeMethod\n"
508 "string \"payload\"\n",
509 method_call
.ToString());
512 TEST(MessageTest
, MethodCall_FromRawMessage
) {
513 DBusMessage
* raw_message
= dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL
);
514 dbus_message_set_interface(raw_message
, "com.example.Interface");
515 dbus_message_set_member(raw_message
, "SomeMethod");
517 scoped_ptr
<MethodCall
> method_call(MethodCall::FromRawMessage(raw_message
));
518 EXPECT_EQ("com.example.Interface", method_call
->GetInterface());
519 EXPECT_EQ("SomeMethod", method_call
->GetMember());
522 TEST(MessageTest
, Signal
) {
523 Signal
signal("com.example.Interface", "SomeSignal");
524 EXPECT_TRUE(signal
.raw_message() != NULL
);
525 EXPECT_EQ(Message::MESSAGE_SIGNAL
, signal
.GetMessageType());
526 EXPECT_EQ("MESSAGE_SIGNAL", signal
.GetMessageTypeAsString());
527 signal
.SetPath(ObjectPath("/com/example/Object"));
529 MessageWriter
writer(&signal
);
530 writer
.AppendString("payload");
532 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
533 "path: /com/example/Object\n"
534 "interface: com.example.Interface\n"
535 "member: SomeSignal\n"
538 "string \"payload\"\n",
542 TEST(MessageTest
, Signal_FromRawMessage
) {
543 DBusMessage
* raw_message
= dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL
);
544 dbus_message_set_interface(raw_message
, "com.example.Interface");
545 dbus_message_set_member(raw_message
, "SomeSignal");
547 scoped_ptr
<Signal
> signal(Signal::FromRawMessage(raw_message
));
548 EXPECT_EQ("com.example.Interface", signal
->GetInterface());
549 EXPECT_EQ("SomeSignal", signal
->GetMember());
552 TEST(MessageTest
, Response
) {
553 scoped_ptr
<Response
> response(Response::CreateEmpty());
554 EXPECT_TRUE(response
->raw_message());
555 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN
, response
->GetMessageType());
556 EXPECT_EQ("MESSAGE_METHOD_RETURN", response
->GetMessageTypeAsString());
559 TEST(MessageTest
, Response_FromMethodCall
) {
560 const uint32 kSerial
= 123;
561 MethodCall
method_call("com.example.Interface", "SomeMethod");
562 method_call
.SetSerial(kSerial
);
564 scoped_ptr
<Response
> response(
565 Response::FromMethodCall(&method_call
));
566 EXPECT_EQ(Message::MESSAGE_METHOD_RETURN
, response
->GetMessageType());
567 EXPECT_EQ("MESSAGE_METHOD_RETURN", response
->GetMessageTypeAsString());
568 // The serial should be copied to the reply serial.
569 EXPECT_EQ(kSerial
, response
->GetReplySerial());
572 TEST(MessageTest
, ErrorResponse_FromMethodCall
) {
573 const uint32 kSerial
= 123;
574 const char kErrorMessage
[] = "error message";
576 MethodCall
method_call("com.example.Interface", "SomeMethod");
577 method_call
.SetSerial(kSerial
);
579 scoped_ptr
<ErrorResponse
> error_response(
580 ErrorResponse::FromMethodCall(&method_call
,
583 EXPECT_EQ(Message::MESSAGE_ERROR
, error_response
->GetMessageType());
584 EXPECT_EQ("MESSAGE_ERROR", error_response
->GetMessageTypeAsString());
585 // The serial should be copied to the reply serial.
586 EXPECT_EQ(kSerial
, error_response
->GetReplySerial());
588 // Error message should be added to the payload.
589 MessageReader
reader(error_response
.get());
590 std::string error_message
;
591 ASSERT_TRUE(reader
.PopString(&error_message
));
592 EXPECT_EQ(kErrorMessage
, error_message
);
595 TEST(MessageTest
, GetAndSetHeaders
) {
596 scoped_ptr
<Response
> message(Response::CreateEmpty());
598 EXPECT_EQ("", message
->GetDestination());
599 EXPECT_EQ(ObjectPath(std::string()), message
->GetPath());
600 EXPECT_EQ("", message
->GetInterface());
601 EXPECT_EQ("", message
->GetMember());
602 EXPECT_EQ("", message
->GetErrorName());
603 EXPECT_EQ("", message
->GetSender());
604 EXPECT_EQ(0U, message
->GetSerial());
605 EXPECT_EQ(0U, message
->GetReplySerial());
607 EXPECT_TRUE(message
->SetDestination("org.chromium.destination"));
608 EXPECT_TRUE(message
->SetPath(ObjectPath("/org/chromium/path")));
609 EXPECT_TRUE(message
->SetInterface("org.chromium.interface"));
610 EXPECT_TRUE(message
->SetMember("member"));
611 EXPECT_TRUE(message
->SetErrorName("org.chromium.error"));
612 EXPECT_TRUE(message
->SetSender(":1.2"));
613 message
->SetSerial(123);
614 message
->SetReplySerial(456);
616 EXPECT_EQ("org.chromium.destination", message
->GetDestination());
617 EXPECT_EQ(ObjectPath("/org/chromium/path"), message
->GetPath());
618 EXPECT_EQ("org.chromium.interface", message
->GetInterface());
619 EXPECT_EQ("member", message
->GetMember());
620 EXPECT_EQ("org.chromium.error", message
->GetErrorName());
621 EXPECT_EQ(":1.2", message
->GetSender());
622 EXPECT_EQ(123U, message
->GetSerial());
623 EXPECT_EQ(456U, message
->GetReplySerial());
626 TEST(MessageTest
, SetInvalidHeaders
) {
627 scoped_ptr
<Response
> message(Response::CreateEmpty());
628 EXPECT_EQ("", message
->GetDestination());
629 EXPECT_EQ(ObjectPath(std::string()), message
->GetPath());
630 EXPECT_EQ("", message
->GetInterface());
631 EXPECT_EQ("", message
->GetMember());
632 EXPECT_EQ("", message
->GetErrorName());
633 EXPECT_EQ("", message
->GetSender());
635 // Empty element between periods.
636 EXPECT_FALSE(message
->SetDestination("org..chromium"));
637 // Trailing '/' is only allowed for the root path.
638 EXPECT_FALSE(message
->SetPath(ObjectPath("/org/chromium/")));
639 // Interface name cannot contain '/'.
640 EXPECT_FALSE(message
->SetInterface("org/chromium/interface"));
641 // Member name cannot begin with a digit.
642 EXPECT_FALSE(message
->SetMember("1member"));
643 // Error name cannot begin with a period.
644 EXPECT_FALSE(message
->SetErrorName(".org.chromium.error"));
645 // Disallowed characters.
646 EXPECT_FALSE(message
->SetSender("?!#*"));
648 EXPECT_EQ("", message
->GetDestination());
649 EXPECT_EQ(ObjectPath(std::string()), message
->GetPath());
650 EXPECT_EQ("", message
->GetInterface());
651 EXPECT_EQ("", message
->GetMember());
652 EXPECT_EQ("", message
->GetErrorName());
653 EXPECT_EQ("", message
->GetSender());
656 TEST(MessageTest
, ToString_LongString
) {
657 const std::string
kLongString(1000, 'o');
659 scoped_ptr
<Response
> message(Response::CreateEmpty());
660 MessageWriter
writer(message
.get());
661 writer
.AppendString(kLongString
);
663 ASSERT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
665 "string \"oooooooooooooooooooooooooooooooooooooooooooooooo"
666 "oooooooooooooooooooooooooooooooooooooooooooooooooooo... "
667 "(1000 bytes in total)\"\n",
668 message
->ToString());