Implement android_webview url intercepting.
[chromium-blink-merge.git] / dbus / message_unittest.cc
blobc61f170ad32da03d9af815d00c4c7611ef754a57
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/eintr_wrapper.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "dbus/object_path.h"
12 #include "dbus/test_proto.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // Test that a byte can be properly written and read. We only have this
16 // test for byte, as repeating this for other basic types is too redundant.
17 TEST(MessageTest, AppendAndPopByte) {
18 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
19 dbus::MessageWriter writer(message.get());
20 writer.AppendByte(123); // The input is 123.
22 dbus::MessageReader reader(message.get());
23 ASSERT_TRUE(reader.HasMoreData()); // Should have data to read.
24 ASSERT_EQ(dbus::Message::BYTE, reader.GetDataType());
26 bool bool_value = false;
27 // Should fail as the type is not bool here.
28 ASSERT_FALSE(reader.PopBool(&bool_value));
30 uint8 byte_value = 0;
31 ASSERT_TRUE(reader.PopByte(&byte_value));
32 EXPECT_EQ(123, byte_value); // Should match with the input.
33 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
35 // Try to get another byte. Should fail.
36 ASSERT_FALSE(reader.PopByte(&byte_value));
39 // Check all basic types can be properly written and read.
40 TEST(MessageTest, AppendAndPopBasicDataTypes) {
41 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
42 dbus::MessageWriter writer(message.get());
44 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
45 writer.AppendByte(0);
46 writer.AppendBool(true);
47 writer.AppendInt16(2);
48 writer.AppendUint16(3);
49 writer.AppendInt32(4);
50 writer.AppendUint32(5);
51 writer.AppendInt64(6);
52 writer.AppendUint64(7);
53 writer.AppendDouble(8.0);
54 writer.AppendString("string");
55 writer.AppendObjectPath(dbus::ObjectPath("/object/path"));
57 uint8 byte_value = 0;
58 bool bool_value = false;
59 int16 int16_value = 0;
60 uint16 uint16_value = 0;
61 int32 int32_value = 0;
62 uint32 uint32_value = 0;
63 int64 int64_value = 0;
64 uint64 uint64_value = 0;
65 double double_value = 0;
66 std::string string_value;
67 dbus::ObjectPath object_path_value;
69 dbus::MessageReader reader(message.get());
70 ASSERT_TRUE(reader.HasMoreData());
71 ASSERT_TRUE(reader.PopByte(&byte_value));
72 ASSERT_TRUE(reader.PopBool(&bool_value));
73 ASSERT_TRUE(reader.PopInt16(&int16_value));
74 ASSERT_TRUE(reader.PopUint16(&uint16_value));
75 ASSERT_TRUE(reader.PopInt32(&int32_value));
76 ASSERT_TRUE(reader.PopUint32(&uint32_value));
77 ASSERT_TRUE(reader.PopInt64(&int64_value));
78 ASSERT_TRUE(reader.PopUint64(&uint64_value));
79 ASSERT_TRUE(reader.PopDouble(&double_value));
80 ASSERT_TRUE(reader.PopString(&string_value));
81 ASSERT_TRUE(reader.PopObjectPath(&object_path_value));
82 ASSERT_FALSE(reader.HasMoreData());
84 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
85 EXPECT_EQ(0, byte_value);
86 EXPECT_EQ(true, bool_value);
87 EXPECT_EQ(2, int16_value);
88 EXPECT_EQ(3U, uint16_value);
89 EXPECT_EQ(4, int32_value);
90 EXPECT_EQ(5U, uint32_value);
91 EXPECT_EQ(6, int64_value);
92 EXPECT_EQ(7U, uint64_value);
93 EXPECT_DOUBLE_EQ(8.0, double_value);
94 EXPECT_EQ("string", string_value);
95 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value);
98 // Check all basic types can be properly written and read.
99 TEST(MessageTest, AppendAndPopFileDescriptor) {
100 if (!dbus::IsDBusTypeUnixFdSupported()) {
101 LOG(WARNING) << "FD passing is not supported";
102 return;
105 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
106 dbus::MessageWriter writer(message.get());
108 // Append stdout.
109 dbus::FileDescriptor temp(1);
110 // Descriptor should not be valid until checked.
111 ASSERT_FALSE(temp.is_valid());
112 // NB: thread IO requirements not relevant for unit tests.
113 temp.CheckValidity();
114 ASSERT_TRUE(temp.is_valid());
115 writer.AppendFileDescriptor(temp);
117 dbus::FileDescriptor fd_value;
119 dbus::MessageReader reader(message.get());
120 ASSERT_TRUE(reader.HasMoreData());
121 ASSERT_TRUE(reader.PopFileDescriptor(&fd_value));
122 ASSERT_FALSE(reader.HasMoreData());
123 // Descriptor is not valid until explicitly checked.
124 ASSERT_FALSE(fd_value.is_valid());
125 fd_value.CheckValidity();
126 ASSERT_TRUE(fd_value.is_valid());
128 // Stdout should be returned but we cannot check the descriptor
129 // value because stdout will be dup'd. Instead check st_rdev
130 // which should be identical.
131 struct stat sb_stdout;
132 int status_stdout = HANDLE_EINTR(fstat(1, &sb_stdout));
133 ASSERT_TRUE(status_stdout >= 0);
134 struct stat sb_fd;
135 int status_fd = HANDLE_EINTR(fstat(fd_value.value(), &sb_fd));
136 ASSERT_TRUE(status_fd >= 0);
137 EXPECT_EQ(sb_stdout.st_rdev, sb_fd.st_rdev);
140 // Check all variant types can be properly written and read.
141 TEST(MessageTest, AppendAndPopVariantDataTypes) {
142 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
143 dbus::MessageWriter writer(message.get());
145 // Append 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path".
146 writer.AppendVariantOfByte(0);
147 writer.AppendVariantOfBool(true);
148 writer.AppendVariantOfInt16(2);
149 writer.AppendVariantOfUint16(3);
150 writer.AppendVariantOfInt32(4);
151 writer.AppendVariantOfUint32(5);
152 writer.AppendVariantOfInt64(6);
153 writer.AppendVariantOfUint64(7);
154 writer.AppendVariantOfDouble(8.0);
155 writer.AppendVariantOfString("string");
156 writer.AppendVariantOfObjectPath(dbus::ObjectPath("/object/path"));
158 uint8 byte_value = 0;
159 bool bool_value = false;
160 int16 int16_value = 0;
161 uint16 uint16_value = 0;
162 int32 int32_value = 0;
163 uint32 uint32_value = 0;
164 int64 int64_value = 0;
165 uint64 uint64_value = 0;
166 double double_value = 0;
167 std::string string_value;
168 dbus::ObjectPath object_path_value;
170 dbus::MessageReader reader(message.get());
171 ASSERT_TRUE(reader.HasMoreData());
172 ASSERT_TRUE(reader.PopVariantOfByte(&byte_value));
173 ASSERT_TRUE(reader.PopVariantOfBool(&bool_value));
174 ASSERT_TRUE(reader.PopVariantOfInt16(&int16_value));
175 ASSERT_TRUE(reader.PopVariantOfUint16(&uint16_value));
176 ASSERT_TRUE(reader.PopVariantOfInt32(&int32_value));
177 ASSERT_TRUE(reader.PopVariantOfUint32(&uint32_value));
178 ASSERT_TRUE(reader.PopVariantOfInt64(&int64_value));
179 ASSERT_TRUE(reader.PopVariantOfUint64(&uint64_value));
180 ASSERT_TRUE(reader.PopVariantOfDouble(&double_value));
181 ASSERT_TRUE(reader.PopVariantOfString(&string_value));
182 ASSERT_TRUE(reader.PopVariantOfObjectPath(&object_path_value));
183 ASSERT_FALSE(reader.HasMoreData());
185 // 0, 1, 2, 3, 4, 5, 6, 7, 8, "string", "/object/path" should be returned.
186 EXPECT_EQ(0, byte_value);
187 EXPECT_EQ(true, bool_value);
188 EXPECT_EQ(2, int16_value);
189 EXPECT_EQ(3U, uint16_value);
190 EXPECT_EQ(4, int32_value);
191 EXPECT_EQ(5U, uint32_value);
192 EXPECT_EQ(6, int64_value);
193 EXPECT_EQ(7U, uint64_value);
194 EXPECT_DOUBLE_EQ(8.0, double_value);
195 EXPECT_EQ("string", string_value);
196 EXPECT_EQ(dbus::ObjectPath("/object/path"), object_path_value);
199 TEST(MessageTest, ArrayOfBytes) {
200 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
201 dbus::MessageWriter writer(message.get());
202 std::vector<uint8> bytes;
203 bytes.push_back(1);
204 bytes.push_back(2);
205 bytes.push_back(3);
206 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
208 dbus::MessageReader reader(message.get());
209 uint8* output_bytes = NULL;
210 size_t length = 0;
211 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
212 ASSERT_FALSE(reader.HasMoreData());
213 ASSERT_EQ(3U, length);
214 EXPECT_EQ(1, output_bytes[0]);
215 EXPECT_EQ(2, output_bytes[1]);
216 EXPECT_EQ(3, output_bytes[2]);
219 TEST(MessageTest, ArrayOfBytes_Empty) {
220 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
221 dbus::MessageWriter writer(message.get());
222 std::vector<uint8> bytes;
223 writer.AppendArrayOfBytes(bytes.data(), bytes.size());
225 dbus::MessageReader reader(message.get());
226 uint8* output_bytes = NULL;
227 size_t length = 0;
228 ASSERT_TRUE(reader.PopArrayOfBytes(&output_bytes, &length));
229 ASSERT_FALSE(reader.HasMoreData());
230 ASSERT_EQ(0U, length);
231 EXPECT_EQ(NULL, output_bytes);
234 TEST(MessageTest, ArrayOfStrings) {
235 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
236 dbus::MessageWriter writer(message.get());
237 std::vector<std::string> strings;
238 strings.push_back("fee");
239 strings.push_back("fie");
240 strings.push_back("foe");
241 strings.push_back("fum");
242 writer.AppendArrayOfStrings(strings);
244 dbus::MessageReader reader(message.get());
245 std::vector<std::string> output_strings;
246 ASSERT_TRUE(reader.PopArrayOfStrings(&output_strings));
247 ASSERT_FALSE(reader.HasMoreData());
248 ASSERT_EQ(4U, output_strings.size());
249 EXPECT_EQ("fee", output_strings[0]);
250 EXPECT_EQ("fie", output_strings[1]);
251 EXPECT_EQ("foe", output_strings[2]);
252 EXPECT_EQ("fum", output_strings[3]);
255 TEST(MessageTest, ArrayOfObjectPaths) {
256 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
257 dbus::MessageWriter writer(message.get());
258 std::vector<dbus::ObjectPath> object_paths;
259 object_paths.push_back(dbus::ObjectPath("/object/path/1"));
260 object_paths.push_back(dbus::ObjectPath("/object/path/2"));
261 object_paths.push_back(dbus::ObjectPath("/object/path/3"));
262 writer.AppendArrayOfObjectPaths(object_paths);
264 dbus::MessageReader reader(message.get());
265 std::vector<dbus::ObjectPath> output_object_paths;
266 ASSERT_TRUE(reader.PopArrayOfObjectPaths(&output_object_paths));
267 ASSERT_FALSE(reader.HasMoreData());
268 ASSERT_EQ(3U, output_object_paths.size());
269 EXPECT_EQ(dbus::ObjectPath("/object/path/1"), output_object_paths[0]);
270 EXPECT_EQ(dbus::ObjectPath("/object/path/2"), output_object_paths[1]);
271 EXPECT_EQ(dbus::ObjectPath("/object/path/3"), output_object_paths[2]);
274 TEST(MessageTest, ProtoBuf) {
275 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
276 dbus::MessageWriter writer(message.get());
277 TestProto send_message;
278 send_message.set_text("testing");
279 send_message.set_number(123);
280 writer.AppendProtoAsArrayOfBytes(send_message);
282 dbus::MessageReader reader(message.get());
283 TestProto receive_message;
284 ASSERT_TRUE(reader.PopArrayOfBytesAsProto(&receive_message));
285 EXPECT_EQ(receive_message.text(), send_message.text());
286 EXPECT_EQ(receive_message.number(), send_message.number());
290 // Test that an array can be properly written and read. We only have this
291 // test for array, as repeating this for other container types is too
292 // redundant.
293 TEST(MessageTest, OpenArrayAndPopArray) {
294 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
295 dbus::MessageWriter writer(message.get());
296 dbus::MessageWriter array_writer(NULL);
297 writer.OpenArray("s", &array_writer); // Open an array of strings.
298 array_writer.AppendString("foo");
299 array_writer.AppendString("bar");
300 array_writer.AppendString("baz");
301 writer.CloseContainer(&array_writer);
303 dbus::MessageReader reader(message.get());
304 ASSERT_EQ(dbus::Message::ARRAY, reader.GetDataType());
305 dbus::MessageReader array_reader(NULL);
306 ASSERT_TRUE(reader.PopArray(&array_reader));
307 ASSERT_FALSE(reader.HasMoreData()); // Should not have more data to read.
309 std::string string_value;
310 ASSERT_TRUE(array_reader.PopString(&string_value));
311 EXPECT_EQ("foo", string_value);
312 ASSERT_TRUE(array_reader.PopString(&string_value));
313 EXPECT_EQ("bar", string_value);
314 ASSERT_TRUE(array_reader.PopString(&string_value));
315 EXPECT_EQ("baz", string_value);
316 // Should not have more data to read.
317 ASSERT_FALSE(array_reader.HasMoreData());
320 // Create a complex message using array, struct, variant, dict entry, and
321 // make sure it can be read properly.
322 TEST(MessageTest, CreateComplexMessageAndReadIt) {
323 scoped_ptr<dbus::Response> message(dbus::Response::CreateEmpty());
324 dbus::MessageWriter writer(message.get());
326 dbus::MessageWriter array_writer(NULL);
327 // Open an array of variants.
328 writer.OpenArray("v", &array_writer);
330 // The first value in the array.
332 dbus::MessageWriter variant_writer(NULL);
333 // Open a variant of a boolean.
334 array_writer.OpenVariant("b", &variant_writer);
335 variant_writer.AppendBool(true);
336 array_writer.CloseContainer(&variant_writer);
339 // The second value in the array.
341 dbus::MessageWriter variant_writer(NULL);
342 // Open a variant of a struct that contains a string and an int32.
343 array_writer.OpenVariant("(si)", &variant_writer);
345 dbus::MessageWriter struct_writer(NULL);
346 variant_writer.OpenStruct(&struct_writer);
347 struct_writer.AppendString("string");
348 struct_writer.AppendInt32(123);
349 variant_writer.CloseContainer(&struct_writer);
351 array_writer.CloseContainer(&variant_writer);
354 // The third value in the array.
356 dbus::MessageWriter variant_writer(NULL);
357 // Open a variant of an array of string-to-int64 dict entries.
358 array_writer.OpenVariant("a{sx}", &variant_writer);
360 // Opens an array of string-to-int64 dict entries.
361 dbus::MessageWriter dict_array_writer(NULL);
362 variant_writer.OpenArray("{sx}", &dict_array_writer);
364 // Opens a string-to-int64 dict entries.
365 dbus::MessageWriter dict_entry_writer(NULL);
366 dict_array_writer.OpenDictEntry(&dict_entry_writer);
367 dict_entry_writer.AppendString("foo");
368 dict_entry_writer.AppendInt64(GG_INT64_C(1234567890123456789));
369 dict_array_writer.CloseContainer(&dict_entry_writer);
371 variant_writer.CloseContainer(&dict_array_writer);
373 array_writer.CloseContainer(&variant_writer);
376 writer.CloseContainer(&array_writer);
378 // What we have created looks like this:
379 EXPECT_EQ("message_type: MESSAGE_METHOD_RETURN\n"
380 "signature: av\n"
381 "\n"
382 "array [\n"
383 " variant bool true\n"
384 " variant struct {\n"
385 " string \"string\"\n"
386 " int32 123\n"
387 " }\n"
388 " variant array [\n"
389 " dict entry {\n"
390 " string \"foo\"\n"
391 " int64 1234567890123456789\n"
392 " }\n"
393 " ]\n"
394 "]\n",
395 message->ToString());
397 dbus::MessageReader reader(message.get());
398 dbus::MessageReader array_reader(NULL);
399 ASSERT_TRUE(reader.PopArray(&array_reader));
401 // The first value in the array.
402 bool bool_value = false;
403 ASSERT_TRUE(array_reader.PopVariantOfBool(&bool_value));
404 EXPECT_EQ(true, bool_value);
406 // The second value in the array.
408 dbus::MessageReader variant_reader(NULL);
409 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
411 dbus::MessageReader struct_reader(NULL);
412 ASSERT_TRUE(variant_reader.PopStruct(&struct_reader));
413 std::string string_value;
414 ASSERT_TRUE(struct_reader.PopString(&string_value));
415 EXPECT_EQ("string", string_value);
416 int32 int32_value = 0;
417 ASSERT_TRUE(struct_reader.PopInt32(&int32_value));
418 EXPECT_EQ(123, int32_value);
419 ASSERT_FALSE(struct_reader.HasMoreData());
421 ASSERT_FALSE(variant_reader.HasMoreData());
424 // The third value in the array.
426 dbus::MessageReader variant_reader(NULL);
427 ASSERT_TRUE(array_reader.PopVariant(&variant_reader));
429 dbus::MessageReader dict_array_reader(NULL);
430 ASSERT_TRUE(variant_reader.PopArray(&dict_array_reader));
432 dbus::MessageReader dict_entry_reader(NULL);
433 ASSERT_TRUE(dict_array_reader.PopDictEntry(&dict_entry_reader));
434 std::string string_value;
435 ASSERT_TRUE(dict_entry_reader.PopString(&string_value));
436 EXPECT_EQ("foo", string_value);
437 int64 int64_value = 0;
438 ASSERT_TRUE(dict_entry_reader.PopInt64(&int64_value));
439 EXPECT_EQ(GG_INT64_C(1234567890123456789), int64_value);
441 ASSERT_FALSE(dict_array_reader.HasMoreData());
443 ASSERT_FALSE(variant_reader.HasMoreData());
445 ASSERT_FALSE(array_reader.HasMoreData());
446 ASSERT_FALSE(reader.HasMoreData());
449 TEST(MessageTest, MethodCall) {
450 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
451 EXPECT_TRUE(method_call.raw_message() != NULL);
452 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType());
453 EXPECT_EQ("MESSAGE_METHOD_CALL", method_call.GetMessageTypeAsString());
454 method_call.SetDestination("com.example.Service");
455 method_call.SetPath(dbus::ObjectPath("/com/example/Object"));
457 dbus::MessageWriter writer(&method_call);
458 writer.AppendString("payload");
460 EXPECT_EQ("message_type: MESSAGE_METHOD_CALL\n"
461 "destination: com.example.Service\n"
462 "path: /com/example/Object\n"
463 "interface: com.example.Interface\n"
464 "member: SomeMethod\n"
465 "signature: s\n"
466 "\n"
467 "string \"payload\"\n",
468 method_call.ToString());
471 TEST(MessageTest, MethodCall_FromRawMessage) {
472 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
473 dbus_message_set_interface(raw_message, "com.example.Interface");
474 dbus_message_set_member(raw_message, "SomeMethod");
476 scoped_ptr<dbus::MethodCall> method_call(
477 dbus::MethodCall::FromRawMessage(raw_message));
478 EXPECT_EQ("com.example.Interface", method_call->GetInterface());
479 EXPECT_EQ("SomeMethod", method_call->GetMember());
482 TEST(MessageTest, Signal) {
483 dbus::Signal signal("com.example.Interface", "SomeSignal");
484 EXPECT_TRUE(signal.raw_message() != NULL);
485 EXPECT_EQ(dbus::Message::MESSAGE_SIGNAL, signal.GetMessageType());
486 EXPECT_EQ("MESSAGE_SIGNAL", signal.GetMessageTypeAsString());
487 signal.SetPath(dbus::ObjectPath("/com/example/Object"));
489 dbus::MessageWriter writer(&signal);
490 writer.AppendString("payload");
492 EXPECT_EQ("message_type: MESSAGE_SIGNAL\n"
493 "path: /com/example/Object\n"
494 "interface: com.example.Interface\n"
495 "member: SomeSignal\n"
496 "signature: s\n"
497 "\n"
498 "string \"payload\"\n",
499 signal.ToString());
502 TEST(MessageTest, Signal_FromRawMessage) {
503 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
504 dbus_message_set_interface(raw_message, "com.example.Interface");
505 dbus_message_set_member(raw_message, "SomeSignal");
507 scoped_ptr<dbus::Signal> signal(
508 dbus::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<dbus::Response> response(dbus::Response::CreateEmpty());
515 EXPECT_TRUE(response->raw_message());
516 EXPECT_EQ(dbus::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 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
523 method_call.SetSerial(kSerial);
525 scoped_ptr<dbus::Response> response(
526 dbus::Response::FromMethodCall(&method_call));
527 EXPECT_EQ(dbus::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 dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
538 method_call.SetSerial(kSerial);
540 scoped_ptr<dbus::ErrorResponse> error_response(
541 dbus::ErrorResponse::FromMethodCall(&method_call,
542 DBUS_ERROR_FAILED,
543 kErrorMessage));
544 EXPECT_EQ(dbus::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 dbus::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<dbus::Response> message(dbus::Response::CreateEmpty());
559 EXPECT_EQ("", message->GetDestination());
560 EXPECT_EQ(dbus::ObjectPath(""), 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(dbus::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(dbus::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<dbus::Response> message(dbus::Response::CreateEmpty());
589 EXPECT_EQ("", message->GetDestination());
590 EXPECT_EQ(dbus::ObjectPath(""), 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(dbus::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(dbus::ObjectPath(""), 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<dbus::Response> message(dbus::Response::CreateEmpty());
621 dbus::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());