1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h"
12 // Test that a byte can be properly written and read. We only have this
13 // test for byte, as repeating this for other basic types is too redundant.
14 TEST(MessageTest
, AppendAndPopByte
) {
15 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
16 dbus::MessageWriter
writer(message
.get());
17 writer
.AppendByte(123); // The input is 123.
19 dbus::MessageReader
reader(message
.get());
20 ASSERT_TRUE(reader
.HasMoreData()); // Should have data to read.
21 ASSERT_EQ(dbus::Message::BYTE
, reader
.GetDataType());
23 bool bool_value
= false;
24 // Should fail as the type is not bool here.
25 ASSERT_FALSE(reader
.PopBool(&bool_value
));
28 ASSERT_TRUE(reader
.PopByte(&byte_value
));
29 EXPECT_EQ(123, byte_value
); // Should match with the input.
30 ASSERT_FALSE(reader
.HasMoreData()); // Should not have more data to read.
32 // Try to get another byte. Should fail.
33 ASSERT_FALSE(reader
.PopByte(&byte_value
));
36 // Check all basic types can be properly written and read.
37 TEST(MessageTest
, AppendAndPopBasicDataTypes
) {
38 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
39 dbus::MessageWriter
writer(message
.get());
41 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
43 writer
.AppendBool(true);
44 writer
.AppendInt16(2);
45 writer
.AppendUint16(3);
46 writer
.AppendInt32(4);
47 writer
.AppendUint32(5);
48 writer
.AppendInt64(6);
49 writer
.AppendUint64(7);
50 writer
.AppendDouble(8.0);
51 writer
.AppendString("string");
52 writer
.AppendObjectPath("/object/path");
55 bool bool_value
= false;
56 int16 int16_value
= 0;
57 uint16 uint16_value
= 0;
58 int32 int32_value
= 0;
59 uint32 uint32_value
= 0;
60 int64 int64_value
= 0;
61 uint64 uint64_value
= 0;
62 double double_value
= 0;
63 std::string string_value
;
64 std::string object_path_value
;
66 dbus::MessageReader
reader(message
.get());
67 ASSERT_TRUE(reader
.HasMoreData());
68 ASSERT_TRUE(reader
.PopByte(&byte_value
));
69 ASSERT_TRUE(reader
.PopBool(&bool_value
));
70 ASSERT_TRUE(reader
.PopInt16(&int16_value
));
71 ASSERT_TRUE(reader
.PopUint16(&uint16_value
));
72 ASSERT_TRUE(reader
.PopInt32(&int32_value
));
73 ASSERT_TRUE(reader
.PopUint32(&uint32_value
));
74 ASSERT_TRUE(reader
.PopInt64(&int64_value
));
75 ASSERT_TRUE(reader
.PopUint64(&uint64_value
));
76 ASSERT_TRUE(reader
.PopDouble(&double_value
));
77 ASSERT_TRUE(reader
.PopString(&string_value
));
78 ASSERT_TRUE(reader
.PopObjectPath(&object_path_value
));
79 ASSERT_FALSE(reader
.HasMoreData());
81 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
82 EXPECT_EQ(0, byte_value
);
83 EXPECT_EQ(true, bool_value
);
84 EXPECT_EQ(2, int16_value
);
85 EXPECT_EQ(3U, uint16_value
);
86 EXPECT_EQ(4, int32_value
);
87 EXPECT_EQ(5U, uint32_value
);
88 EXPECT_EQ(6, int64_value
);
89 EXPECT_EQ(7U, uint64_value
);
90 EXPECT_DOUBLE_EQ(8.0, double_value
);
91 EXPECT_EQ("string", string_value
);
92 EXPECT_EQ("/object/path", object_path_value
);
95 // Check all variant types can be properly written and read.
96 TEST(MessageTest
, AppendAndPopVariantDataTypes
) {
97 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
98 dbus::MessageWriter
writer(message
.get());
100 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
101 writer
.AppendVariantOfByte(0);
102 writer
.AppendVariantOfBool(true);
103 writer
.AppendVariantOfInt16(2);
104 writer
.AppendVariantOfUint16(3);
105 writer
.AppendVariantOfInt32(4);
106 writer
.AppendVariantOfUint32(5);
107 writer
.AppendVariantOfInt64(6);
108 writer
.AppendVariantOfUint64(7);
109 writer
.AppendVariantOfDouble(8.0);
110 writer
.AppendVariantOfString("string");
111 writer
.AppendVariantOfObjectPath("/object/path");
113 uint8 byte_value
= 0;
114 bool bool_value
= false;
115 int16 int16_value
= 0;
116 uint16 uint16_value
= 0;
117 int32 int32_value
= 0;
118 uint32 uint32_value
= 0;
119 int64 int64_value
= 0;
120 uint64 uint64_value
= 0;
121 double double_value
= 0;
122 std::string string_value
;
123 std::string object_path_value
;
125 dbus::MessageReader
reader(message
.get());
126 ASSERT_TRUE(reader
.HasMoreData());
127 ASSERT_TRUE(reader
.PopVariantOfByte(&byte_value
));
128 ASSERT_TRUE(reader
.PopVariantOfBool(&bool_value
));
129 ASSERT_TRUE(reader
.PopVariantOfInt16(&int16_value
));
130 ASSERT_TRUE(reader
.PopVariantOfUint16(&uint16_value
));
131 ASSERT_TRUE(reader
.PopVariantOfInt32(&int32_value
));
132 ASSERT_TRUE(reader
.PopVariantOfUint32(&uint32_value
));
133 ASSERT_TRUE(reader
.PopVariantOfInt64(&int64_value
));
134 ASSERT_TRUE(reader
.PopVariantOfUint64(&uint64_value
));
135 ASSERT_TRUE(reader
.PopVariantOfDouble(&double_value
));
136 ASSERT_TRUE(reader
.PopVariantOfString(&string_value
));
137 ASSERT_TRUE(reader
.PopVariantOfObjectPath(&object_path_value
));
138 ASSERT_FALSE(reader
.HasMoreData());
140 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
141 EXPECT_EQ(0, byte_value
);
142 EXPECT_EQ(true, bool_value
);
143 EXPECT_EQ(2, int16_value
);
144 EXPECT_EQ(3U, uint16_value
);
145 EXPECT_EQ(4, int32_value
);
146 EXPECT_EQ(5U, uint32_value
);
147 EXPECT_EQ(6, int64_value
);
148 EXPECT_EQ(7U, uint64_value
);
149 EXPECT_DOUBLE_EQ(8.0, double_value
);
150 EXPECT_EQ("string", string_value
);
151 EXPECT_EQ("/object/path", object_path_value
);
154 TEST(MessageTest
, ArrayOfBytes
) {
155 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
156 dbus::MessageWriter
writer(message
.get());
157 std::vector
<uint8
> bytes
;
161 writer
.AppendArrayOfBytes(bytes
.data(), bytes
.size());
163 dbus::MessageReader
reader(message
.get());
164 uint8
* output_bytes
= NULL
;
166 ASSERT_TRUE(reader
.PopArrayOfBytes(&output_bytes
, &length
));
167 ASSERT_FALSE(reader
.HasMoreData());
168 ASSERT_EQ(3U, length
);
169 EXPECT_EQ(1, output_bytes
[0]);
170 EXPECT_EQ(2, output_bytes
[1]);
171 EXPECT_EQ(3, output_bytes
[2]);
174 TEST(MessageTest
, ArrayOfBytes_Empty
) {
175 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
176 dbus::MessageWriter
writer(message
.get());
177 std::vector
<uint8
> bytes
;
178 writer
.AppendArrayOfBytes(bytes
.data(), bytes
.size());
180 dbus::MessageReader
reader(message
.get());
181 uint8
* output_bytes
= NULL
;
183 ASSERT_TRUE(reader
.PopArrayOfBytes(&output_bytes
, &length
));
184 ASSERT_FALSE(reader
.HasMoreData());
185 ASSERT_EQ(0U, length
);
186 EXPECT_EQ(NULL
, output_bytes
);
189 TEST(MessageTest
, ArrayOfStrings
) {
190 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
191 dbus::MessageWriter
writer(message
.get());
192 std::vector
<std::string
> strings
;
193 strings
.push_back("fee");
194 strings
.push_back("fie");
195 strings
.push_back("foe");
196 strings
.push_back("fum");
197 writer
.AppendArrayOfStrings(strings
);
199 dbus::MessageReader
reader(message
.get());
200 std::vector
<std::string
> output_strings
;
201 ASSERT_TRUE(reader
.PopArrayOfStrings(&output_strings
));
202 ASSERT_FALSE(reader
.HasMoreData());
203 ASSERT_EQ(4U, output_strings
.size());
204 EXPECT_EQ("fee", output_strings
[0]);
205 EXPECT_EQ("fie", output_strings
[1]);
206 EXPECT_EQ("foe", output_strings
[2]);
207 EXPECT_EQ("fum", output_strings
[3]);
210 TEST(MessageTest
, ArrayOfObjectPaths
) {
211 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
212 dbus::MessageWriter
writer(message
.get());
213 std::vector
<std::string
> object_paths
;
214 object_paths
.push_back("/object/path/1");
215 object_paths
.push_back("/object/path/2");
216 object_paths
.push_back("/object/path/3");
217 writer
.AppendArrayOfObjectPaths(object_paths
);
219 dbus::MessageReader
reader(message
.get());
220 std::vector
<std::string
> output_object_paths
;
221 ASSERT_TRUE(reader
.PopArrayOfObjectPaths(&output_object_paths
));
222 ASSERT_FALSE(reader
.HasMoreData());
223 ASSERT_EQ(3U, output_object_paths
.size());
224 EXPECT_EQ("/object/path/1", output_object_paths
[0]);
225 EXPECT_EQ("/object/path/2", output_object_paths
[1]);
226 EXPECT_EQ("/object/path/3", output_object_paths
[2]);
229 // Test that an array can be properly written and read. We only have this
230 // test for array, as repeating this for other container types is too
232 TEST(MessageTest
, OpenArrayAndPopArray
) {
233 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
234 dbus::MessageWriter
writer(message
.get());
235 dbus::MessageWriter
array_writer(message
.get());
236 writer
.OpenArray("s", &array_writer
); // Open an array of strings.
237 array_writer
.AppendString("foo");
238 array_writer
.AppendString("bar");
239 array_writer
.AppendString("baz");
240 writer
.CloseContainer(&array_writer
);
242 dbus::MessageReader
reader(message
.get());
243 ASSERT_EQ(dbus::Message::ARRAY
, reader
.GetDataType());
244 dbus::MessageReader
array_reader(message
.get());
245 ASSERT_TRUE(reader
.PopArray(&array_reader
));
246 ASSERT_FALSE(reader
.HasMoreData()); // Should not have more data to read.
248 std::string string_value
;
249 ASSERT_TRUE(array_reader
.PopString(&string_value
));
250 EXPECT_EQ("foo", string_value
);
251 ASSERT_TRUE(array_reader
.PopString(&string_value
));
252 EXPECT_EQ("bar", string_value
);
253 ASSERT_TRUE(array_reader
.PopString(&string_value
));
254 EXPECT_EQ("baz", string_value
);
255 // Should not have more data to read.
256 ASSERT_FALSE(array_reader
.HasMoreData());
259 // Create a complex message using array, struct, variant, dict entry, and
260 // make sure it can be read properly.
261 TEST(MessageTest
, CreateComplexMessageAndReadIt
) {
262 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
263 dbus::MessageWriter
writer(message
.get());
265 dbus::MessageWriter
array_writer(message
.get());
266 // Open an array of variants.
267 writer
.OpenArray("v", &array_writer
);
269 // The first value in the array.
271 dbus::MessageWriter
variant_writer(message
.get());
272 // Open a variant of a boolean.
273 array_writer
.OpenVariant("b", &variant_writer
);
274 variant_writer
.AppendBool(true);
275 array_writer
.CloseContainer(&variant_writer
);
278 // The second value in the array.
280 dbus::MessageWriter
variant_writer(message
.get());
281 // Open a variant of a struct that contains a string and an int32.
282 array_writer
.OpenVariant("(si)", &variant_writer
);
284 dbus::MessageWriter
struct_writer(message
.get());
285 variant_writer
.OpenStruct(&struct_writer
);
286 struct_writer
.AppendString("string");
287 struct_writer
.AppendInt32(123);
288 variant_writer
.CloseContainer(&struct_writer
);
290 array_writer
.CloseContainer(&variant_writer
);
293 // The third value in the array.
295 dbus::MessageWriter
variant_writer(message
.get());
296 // Open a variant of an array of string-to-int64 dict entries.
297 array_writer
.OpenVariant("a{sx}", &variant_writer
);
299 // Opens an array of string-to-int64 dict entries.
300 dbus::MessageWriter
dict_array_writer(message
.get());
301 variant_writer
.OpenArray("{sx}", &dict_array_writer
);
303 // Opens a string-to-int64 dict entries.
304 dbus::MessageWriter
dict_entry_writer(message
.get());
305 dict_array_writer
.OpenDictEntry(&dict_entry_writer
);
306 dict_entry_writer
.AppendString("foo");
307 dict_entry_writer
.AppendInt64(GG_INT64_C(1234567890123456789));
308 dict_array_writer
.CloseContainer(&dict_entry_writer
);
310 variant_writer
.CloseContainer(&dict_array_writer
);
312 array_writer
.CloseContainer(&variant_writer
);
315 writer
.CloseContainer(&array_writer
);
317 // What we have created looks like this:
318 EXPECT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
322 " variant bool true\n"
323 " variant struct {\n"
324 " string \"string\"\n"
330 " int64 1234567890123456789\n"
334 message
->ToString());
336 dbus::MessageReader
reader(message
.get());
337 dbus::MessageReader
array_reader(message
.get());
338 ASSERT_TRUE(reader
.PopArray(&array_reader
));
340 // The first value in the array.
341 bool bool_value
= false;
342 ASSERT_TRUE(array_reader
.PopVariantOfBool(&bool_value
));
343 EXPECT_EQ(true, bool_value
);
345 // The second value in the array.
347 dbus::MessageReader
variant_reader(message
.get());
348 ASSERT_TRUE(array_reader
.PopVariant(&variant_reader
));
350 dbus::MessageReader
struct_reader(message
.get());
351 ASSERT_TRUE(variant_reader
.PopStruct(&struct_reader
));
352 std::string string_value
;
353 ASSERT_TRUE(struct_reader
.PopString(&string_value
));
354 EXPECT_EQ("string", string_value
);
355 int32 int32_value
= 0;
356 ASSERT_TRUE(struct_reader
.PopInt32(&int32_value
));
357 EXPECT_EQ(123, int32_value
);
358 ASSERT_FALSE(struct_reader
.HasMoreData());
360 ASSERT_FALSE(variant_reader
.HasMoreData());
363 // The third value in the array.
365 dbus::MessageReader
variant_reader(message
.get());
366 ASSERT_TRUE(array_reader
.PopVariant(&variant_reader
));
368 dbus::MessageReader
dict_array_reader(message
.get());
369 ASSERT_TRUE(variant_reader
.PopArray(&dict_array_reader
));
371 dbus::MessageReader
dict_entry_reader(message
.get());
372 ASSERT_TRUE(dict_array_reader
.PopDictEntry(&dict_entry_reader
));
373 std::string string_value
;
374 ASSERT_TRUE(dict_entry_reader
.PopString(&string_value
));
375 EXPECT_EQ("foo", string_value
);
376 int64 int64_value
= 0;
377 ASSERT_TRUE(dict_entry_reader
.PopInt64(&int64_value
));
378 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value
);
380 ASSERT_FALSE(dict_array_reader
.HasMoreData());
382 ASSERT_FALSE(variant_reader
.HasMoreData());
384 ASSERT_FALSE(array_reader
.HasMoreData());
385 ASSERT_FALSE(reader
.HasMoreData());
388 TEST(MessageTest
, MethodCall
) {
389 dbus::MethodCall
method_call("com.example.Interface", "SomeMethod");
390 EXPECT_TRUE(method_call
.raw_message() != NULL
);
391 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL
, method_call
.GetMessageType());
392 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call
.GetMessageTypeAsString());
393 method_call
.SetDestination("com.example.Service");
394 method_call
.SetPath("/com/example/Object");
396 dbus::MessageWriter
writer(&method_call
);
397 writer
.AppendString("payload");
399 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
400 "destination: com.example.Service\n"
401 "path: /com/example/Object\n"
402 "interface: com.example.Interface\n"
403 "member: SomeMethod\n"
406 "string \"payload\"\n",
407 method_call
.ToString());
410 TEST(MessageTest
, MethodCall_FromRawMessage
) {
411 DBusMessage
* raw_message
= dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL
);
412 dbus_message_set_interface(raw_message
, "com.example.Interface");
413 dbus_message_set_member(raw_message
, "SomeMethod");
415 scoped_ptr
<dbus::MethodCall
> method_call(
416 dbus::MethodCall::FromRawMessage(raw_message
));
417 EXPECT_EQ("com.example.Interface", method_call
->GetInterface());
418 EXPECT_EQ("SomeMethod", method_call
->GetMember());
421 TEST(MessageTest
, Signal
) {
422 dbus::Signal
signal("com.example.Interface", "SomeSignal");
423 EXPECT_TRUE(signal
.raw_message() != NULL
);
424 EXPECT_EQ(dbus::Message::MESSAGE_SIGNAL
, signal
.GetMessageType());
425 EXPECT_EQ("MESSAGE_SIGNAL", signal
.GetMessageTypeAsString());
426 signal
.SetPath("/com/example/Object");
428 dbus::MessageWriter
writer(&signal
);
429 writer
.AppendString("payload");
431 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
432 "path: /com/example/Object\n"
433 "interface: com.example.Interface\n"
434 "member: SomeSignal\n"
437 "string \"payload\"\n",
441 TEST(MessageTest
, Signal_FromRawMessage
) {
442 DBusMessage
* raw_message
= dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL
);
443 dbus_message_set_interface(raw_message
, "com.example.Interface");
444 dbus_message_set_member(raw_message
, "SomeSignal");
446 scoped_ptr
<dbus::Signal
> signal(
447 dbus::Signal::FromRawMessage(raw_message
));
448 EXPECT_EQ("com.example.Interface", signal
->GetInterface());
449 EXPECT_EQ("SomeSignal", signal
->GetMember());
452 TEST(MessageTest
, Response
) {
453 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
454 EXPECT_TRUE(response
->raw_message());
455 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN
, response
->GetMessageType());
456 EXPECT_EQ("MESSAGE_METHOD_RETURN", response
->GetMessageTypeAsString());
459 TEST(MessageTest
, Response_FromMethodCall
) {
460 const uint32 kSerial
= 123;
461 dbus::MethodCall
method_call("com.example.Interface", "SomeMethod");
462 method_call
.SetSerial(kSerial
);
464 scoped_ptr
<dbus::Response
> response(
465 dbus::Response::FromMethodCall(&method_call
));
466 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN
, response
->GetMessageType());
467 EXPECT_EQ("MESSAGE_METHOD_RETURN", response
->GetMessageTypeAsString());
468 // The serial should be copied to the reply serial.
469 EXPECT_EQ(kSerial
, response
->GetReplySerial());
472 TEST(MessageTest
, ErrorResponse_FromMethodCall
) {
473 const uint32 kSerial
= 123;
474 const char kErrorMessage
[] = "error message";
476 dbus::MethodCall
method_call("com.example.Interface", "SomeMethod");
477 method_call
.SetSerial(kSerial
);
479 scoped_ptr
<dbus::ErrorResponse
> error_response(
480 dbus::ErrorResponse::FromMethodCall(&method_call
,
483 EXPECT_EQ(dbus::Message::MESSAGE_ERROR
, error_response
->GetMessageType());
484 EXPECT_EQ("MESSAGE_ERROR", error_response
->GetMessageTypeAsString());
485 // The serial should be copied to the reply serial.
486 EXPECT_EQ(kSerial
, error_response
->GetReplySerial());
488 // Error message should be added to the payload.
489 dbus::MessageReader
reader(error_response
.get());
490 std::string error_message
;
491 ASSERT_TRUE(reader
.PopString(&error_message
));
492 EXPECT_EQ(kErrorMessage
, error_message
);
495 TEST(MessageTest
, GetAndSetHeaders
) {
496 scoped_ptr
<dbus::Response
> message(dbus::Response::CreateEmpty());
498 EXPECT_EQ("", message
->GetDestination());
499 EXPECT_EQ("", message
->GetPath());
500 EXPECT_EQ("", message
->GetInterface());
501 EXPECT_EQ("", message
->GetMember());
502 EXPECT_EQ("", message
->GetErrorName());
503 EXPECT_EQ("", message
->GetSender());
504 EXPECT_EQ(0U, message
->GetSerial());
505 EXPECT_EQ(0U, message
->GetReplySerial());
507 message
->SetDestination("org.chromium.destination");
508 message
->SetPath("/org/chromium/path");
509 message
->SetInterface("org.chromium.interface");
510 message
->SetMember("member");
511 message
->SetErrorName("org.chromium.error");
512 message
->SetSender(":1.2");
513 message
->SetSerial(123);
514 message
->SetReplySerial(456);
516 EXPECT_EQ("org.chromium.destination", message
->GetDestination());
517 EXPECT_EQ("/org/chromium/path", message
->GetPath());
518 EXPECT_EQ("org.chromium.interface", message
->GetInterface());
519 EXPECT_EQ("member", message
->GetMember());
520 EXPECT_EQ("org.chromium.error", message
->GetErrorName());
521 EXPECT_EQ(":1.2", message
->GetSender());
522 EXPECT_EQ(123U, message
->GetSerial());
523 EXPECT_EQ(456U, message
->GetReplySerial());