OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/message.h" | 5 #include "dbus/message.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 // Test that a byte can be properly written and read. We only have this | 12 // Test that a byte can be properly written and read. We only have this |
12 // test for byte, as repeating this for other basic types is too redundant. | 13 // test for byte, as repeating this for other basic types is too redundant. |
13 TEST(MessageTest, AppendAndPopByte) { | 14 TEST(MessageTest, AppendAndPopByte) { |
14 dbus::Message message; | 15 dbus::Message message; |
15 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); | 16 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
16 dbus::MessageWriter writer(&message); | 17 dbus::MessageWriter writer(&message); |
17 writer.AppendByte(123); // The input is 123. | 18 writer.AppendByte(123); // The input is 123. |
18 | 19 |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 TEST(MessageTest, Message) { | 338 TEST(MessageTest, Message) { |
338 dbus::Message message; | 339 dbus::Message message; |
339 EXPECT_TRUE(message.raw_message() == NULL); | 340 EXPECT_TRUE(message.raw_message() == NULL); |
340 EXPECT_EQ(dbus::Message::MESSAGE_INVALID, message.GetMessageType()); | 341 EXPECT_EQ(dbus::Message::MESSAGE_INVALID, message.GetMessageType()); |
341 } | 342 } |
342 | 343 |
343 TEST(MessageTest, MethodCall) { | 344 TEST(MessageTest, MethodCall) { |
344 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); | 345 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); |
345 EXPECT_TRUE(method_call.raw_message() != NULL); | 346 EXPECT_TRUE(method_call.raw_message() != NULL); |
346 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType()); | 347 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_CALL, method_call.GetMessageType()); |
347 method_call.SetServiceName("com.example.Service"); | 348 method_call.SetDestination("com.example.Service"); |
348 method_call.SetObjectPath("/com/example/Object"); | 349 method_call.SetPath("/com/example/Object"); |
349 | 350 |
350 dbus::MessageWriter writer(&method_call); | 351 dbus::MessageWriter writer(&method_call); |
351 writer.AppendString("payload"); | 352 writer.AppendString("payload"); |
352 | 353 |
353 EXPECT_EQ("destination: com.example.Service\n" | 354 EXPECT_EQ("destination: com.example.Service\n" |
354 "path: /com/example/Object\n" | 355 "path: /com/example/Object\n" |
355 "interface: com.example.Interface\n" | 356 "interface: com.example.Interface\n" |
356 "member: SomeMethod\n" | 357 "member: SomeMethod\n" |
357 "signature: s\n" | 358 "signature: s\n" |
358 "\n" | 359 "\n" |
359 "string \"payload\"\n", | 360 "string \"payload\"\n", |
360 method_call.ToString()); | 361 method_call.ToString()); |
361 } | 362 } |
362 | 363 |
| 364 TEST(MessageTest, MethodCall_FromRawMessage) { |
| 365 DBusMessage* raw_message = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL); |
| 366 dbus_message_set_interface(raw_message, "com.example.Interface"); |
| 367 dbus_message_set_member(raw_message, "SomeMethod"); |
| 368 |
| 369 scoped_ptr<dbus::MethodCall> method_call( |
| 370 dbus::MethodCall::FromRawMessage(raw_message)); |
| 371 EXPECT_EQ("com.example.Interface", method_call->GetInterface()); |
| 372 EXPECT_EQ("SomeMethod", method_call->GetMember()); |
| 373 } |
| 374 |
363 TEST(MessageTest, Response) { | 375 TEST(MessageTest, Response) { |
364 dbus::Response response; | 376 dbus::Response response; |
365 EXPECT_TRUE(response.raw_message() == NULL); | 377 EXPECT_TRUE(response.raw_message() == NULL); |
366 response.reset_raw_message( | 378 response.reset_raw_message( |
367 dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN)); | 379 dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN)); |
368 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response.GetMessageType()); | 380 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response.GetMessageType()); |
369 } | 381 } |
370 | 382 |
| 383 TEST(MergeTest, Response_FromMethodCall) { |
| 384 const uint32 kSerial = 123; |
| 385 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); |
| 386 method_call.SetSerial(kSerial); |
| 387 |
| 388 scoped_ptr<dbus::Response> response( |
| 389 dbus::Response::FromMethodCall(&method_call)); |
| 390 EXPECT_EQ(dbus::Message::MESSAGE_METHOD_RETURN, response->GetMessageType()); |
| 391 // The serial should be copied to the reply serial. |
| 392 EXPECT_EQ(kSerial, response->GetReplySerial()); |
| 393 } |
| 394 |
| 395 TEST(MergeTest, ErrorResponse) { |
| 396 dbus::ErrorResponse error_response; |
| 397 EXPECT_TRUE(error_response.raw_message() == NULL); |
| 398 error_response.reset_raw_message( |
| 399 dbus_message_new(DBUS_MESSAGE_TYPE_ERROR)); |
| 400 EXPECT_EQ(dbus::Message::MESSAGE_ERROR, error_response.GetMessageType()); |
| 401 } |
| 402 |
| 403 TEST(MergeTest, ErrorResponse_FromMethodCall) { |
| 404 const uint32 kSerial = 123; |
| 405 const char kErrorMessage[] = "error message"; |
| 406 |
| 407 dbus::MethodCall method_call("com.example.Interface", "SomeMethod"); |
| 408 method_call.SetSerial(kSerial); |
| 409 |
| 410 scoped_ptr<dbus::ErrorResponse> error_response( |
| 411 dbus::ErrorResponse::FromMethodCall(&method_call, |
| 412 DBUS_ERROR_FAILED, |
| 413 kErrorMessage)); |
| 414 EXPECT_EQ(dbus::Message::MESSAGE_ERROR, error_response->GetMessageType()); |
| 415 // The serial should be copied to the reply serial. |
| 416 EXPECT_EQ(kSerial, error_response->GetReplySerial()); |
| 417 |
| 418 // Error message should be added to the payload. |
| 419 dbus::MessageReader reader(error_response.get()); |
| 420 std::string error_message; |
| 421 ASSERT_TRUE(reader.PopString(&error_message)); |
| 422 EXPECT_EQ(kErrorMessage, error_message); |
| 423 } |
| 424 |
371 TEST(MessageTest, ToString_EmptyMessage) { | 425 TEST(MessageTest, ToString_EmptyMessage) { |
372 dbus::Message message; | 426 dbus::Message message; |
373 EXPECT_EQ("", message.ToString()); | 427 EXPECT_EQ("", message.ToString()); |
374 } | 428 } |
| 429 |
| 430 TEST(MessageTest, GetAndSetHeaders) { |
| 431 dbus::Message message; |
| 432 message.reset_raw_message(dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL)); |
| 433 |
| 434 EXPECT_EQ("", message.GetDestination()); |
| 435 EXPECT_EQ("", message.GetPath()); |
| 436 EXPECT_EQ("", message.GetInterface()); |
| 437 EXPECT_EQ("", message.GetMember()); |
| 438 EXPECT_EQ("", message.GetErrorName()); |
| 439 EXPECT_EQ("", message.GetSender()); |
| 440 EXPECT_EQ(0U, message.GetSerial()); |
| 441 EXPECT_EQ(0U, message.GetReplySerial()); |
| 442 |
| 443 message.SetDestination("org.chromium.destination"); |
| 444 message.SetPath("/org/chromium/path"); |
| 445 message.SetInterface("org.chromium.interface"); |
| 446 message.SetMember("member"); |
| 447 message.SetErrorName("org.chromium.error"); |
| 448 message.SetSender(":1.2"); |
| 449 message.SetSerial(123); |
| 450 message.SetReplySerial(456); |
| 451 |
| 452 EXPECT_EQ("org.chromium.destination", message.GetDestination()); |
| 453 EXPECT_EQ("/org/chromium/path", message.GetPath()); |
| 454 EXPECT_EQ("org.chromium.interface", message.GetInterface()); |
| 455 EXPECT_EQ("member", message.GetMember()); |
| 456 EXPECT_EQ("org.chromium.error", message.GetErrorName()); |
| 457 EXPECT_EQ(":1.2", message.GetSender()); |
| 458 EXPECT_EQ(123U, message.GetSerial()); |
| 459 EXPECT_EQ(456U, message.GetReplySerial()); |
| 460 } |
OLD | NEW |