This source file includes following definitions.
- SetUp
- TearDown
- SetUpBrokenBus
- CallMethod
- CallMethodWithErrorCallback
- WaitForResponses
- OnResponse
- WaitForErrors
- OnError
- OnTestSignal
- OnRootTestSignal
- OnTest2Signal
- OnConnected
- OnDisconnected
- WaitForTestSignal
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SetUp
- OnAdditionalTestSignal
- OnAdditionalConnected
- TEST_F
#include <algorithm>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "dbus/test_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace dbus {
namespace {
const int kHugePayloadSize = 64 << 20;
}
class EndToEndAsyncTest : public testing::Test {
public:
EndToEndAsyncTest() : on_disconnected_call_count_(0) {}
virtual void SetUp() {
base::ThreadRestrictions::SetIOAllowed(false);
dbus_thread_.reset(new base::Thread("D-Bus Thread"));
base::Thread::Options thread_options;
thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
TestService::Options options;
options.dbus_task_runner = dbus_thread_->message_loop_proxy();
test_service_.reset(new TestService(options));
ASSERT_TRUE(test_service_->StartService());
ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
ASSERT_TRUE(test_service_->HasDBusThread());
Bus::Options bus_options;
bus_options.bus_type = Bus::SESSION;
bus_options.connection_type = Bus::PRIVATE;
bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
bus_options.disconnected_callback =
base::Bind(&EndToEndAsyncTest::OnDisconnected, base::Unretained(this));
bus_ = new Bus(bus_options);
object_proxy_ = bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
ASSERT_TRUE(bus_->HasDBusThread());
object_proxy_->ConnectToSignal(
"org.chromium.TestInterface",
"Test",
base::Bind(&EndToEndAsyncTest::OnTestSignal,
base::Unretained(this)),
base::Bind(&EndToEndAsyncTest::OnConnected,
base::Unretained(this)));
message_loop_.Run();
object_proxy_->ConnectToSignal(
"org.chromium.TestInterface",
"Test2",
base::Bind(&EndToEndAsyncTest::OnTest2Signal,
base::Unretained(this)),
base::Bind(&EndToEndAsyncTest::OnConnected,
base::Unretained(this)));
message_loop_.Run();
root_object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
ObjectPath("/"));
ASSERT_TRUE(bus_->HasDBusThread());
root_object_proxy_->ConnectToSignal(
"org.chromium.TestInterface",
"Test",
base::Bind(&EndToEndAsyncTest::OnRootTestSignal,
base::Unretained(this)),
base::Bind(&EndToEndAsyncTest::OnConnected,
base::Unretained(this)));
message_loop_.Run();
}
virtual void TearDown() {
bus_->ShutdownOnDBusThreadAndBlock();
test_service_->ShutdownAndBlock();
base::ThreadRestrictions::SetIOAllowed(true);
test_service_->Stop();
}
protected:
void SetUpBrokenBus() {
bus_->ShutdownOnDBusThreadAndBlock();
const char kInvalidAddress[] = "";
Bus::Options bus_options;
bus_options.bus_type = Bus::CUSTOM_ADDRESS;
bus_options.address = kInvalidAddress;
bus_options.connection_type = Bus::PRIVATE;
bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
bus_ = new Bus(bus_options);
ASSERT_TRUE(bus_->HasDBusThread());
object_proxy_ = bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
}
void CallMethod(MethodCall* method_call,
int timeout_ms) {
object_proxy_->CallMethod(method_call,
timeout_ms,
base::Bind(&EndToEndAsyncTest::OnResponse,
base::Unretained(this)));
}
void CallMethodWithErrorCallback(MethodCall* method_call,
int timeout_ms) {
object_proxy_->CallMethodWithErrorCallback(
method_call,
timeout_ms,
base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this)));
}
void WaitForResponses(size_t num_responses) {
while (response_strings_.size() < num_responses) {
message_loop_.Run();
}
}
void OnResponse(Response* response) {
if (response) {
MessageReader reader(response);
std::string response_string;
ASSERT_TRUE(reader.PopString(&response_string));
response_strings_.push_back(response_string);
} else {
response_strings_.push_back(std::string());
}
message_loop_.Quit();
};
void WaitForErrors(size_t num_errors) {
while (error_names_.size() < num_errors) {
message_loop_.Run();
}
}
void OnError(ErrorResponse* error) {
if (error) {
ASSERT_NE("", error->GetErrorName());
error_names_.push_back(error->GetErrorName());
} else {
error_names_.push_back(std::string());
}
message_loop_.Quit();
}
void OnTestSignal(Signal* signal) {
MessageReader reader(signal);
ASSERT_TRUE(reader.PopString(&test_signal_string_));
message_loop_.Quit();
}
void OnRootTestSignal(Signal* signal) {
MessageReader reader(signal);
ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
message_loop_.Quit();
}
void OnTest2Signal(Signal* signal) {
MessageReader reader(signal);
message_loop_.Quit();
}
void OnConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
ASSERT_TRUE(success);
message_loop_.Quit();
}
void OnDisconnected() {
message_loop_.Quit();
++on_disconnected_call_count_;
}
void WaitForTestSignal() {
message_loop_.Run();
}
base::MessageLoop message_loop_;
std::vector<std::string> response_strings_;
std::vector<std::string> error_names_;
scoped_ptr<base::Thread> dbus_thread_;
scoped_refptr<Bus> bus_;
ObjectProxy* object_proxy_;
ObjectProxy* root_object_proxy_;
scoped_ptr<TestService> test_service_;
std::string test_signal_string_;
std::string root_test_signal_string_;
int on_disconnected_call_count_;
};
TEST_F(EndToEndAsyncTest, Echo) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
EXPECT_EQ(kHello, response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForResponses(1);
EXPECT_EQ(kHello, response_strings_[0]);
EXPECT_TRUE(error_names_.empty());
}
TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
const char* kMessages[] = { "foo", "bar", "baz" };
for (size_t i = 0; i < arraysize(kMessages); ++i) {
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kMessages[i]);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
}
WaitForResponses(3);
std::sort(response_strings_.begin(), response_strings_.end());
EXPECT_EQ("bar", response_strings_[0]);
EXPECT_EQ("baz", response_strings_[1]);
EXPECT_EQ("foo", response_strings_[2]);
}
TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
const std::string kHugePayload(kHugePayloadSize, 'o');
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHugePayload);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
EXPECT_EQ(kHugePayload, response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, BrokenBus) {
const char* kHello = "hello";
SetUpBrokenBus();
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
ASSERT_EQ("", response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
const char* kHello = "hello";
SetUpBrokenBus();
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ("", error_names_[0]);
}
TEST_F(EndToEndAsyncTest, Timeout) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = 0;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
ASSERT_EQ("", response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = 0;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
}
TEST_F(EndToEndAsyncTest, AsyncEcho) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
EXPECT_EQ(kHello, response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, NonexistentMethod) {
MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
ASSERT_EQ("", response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
}
TEST_F(EndToEndAsyncTest, BrokenMethod) {
MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethod(&method_call, timeout_ms);
WaitForResponses(1);
ASSERT_EQ("", response_strings_[0]);
}
TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
}
TEST_F(EndToEndAsyncTest, InvalidObjectPath) {
const ObjectPath invalid_object_path("/org/chromium/TestObject/");
object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
invalid_object_path);
MethodCall method_call("org.chromium.TestInterface", "Echo");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ("", error_names_[0]);
}
TEST_F(EndToEndAsyncTest, InvalidServiceName) {
const std::string invalid_service_name = ":1/2";
object_proxy_ = bus_->GetObjectProxy(
invalid_service_name, ObjectPath("org.chromium.TestObject"));
MethodCall method_call("org.chromium.TestInterface", "Echo");
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
CallMethodWithErrorCallback(&method_call, timeout_ms);
WaitForErrors(1);
ASSERT_TRUE(response_strings_.empty());
ASSERT_EQ("", error_names_[0]);
}
TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
const char* kHello = "hello";
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT;
object_proxy_->CallMethod(&method_call,
timeout_ms,
ObjectProxy::EmptyResponseCallback());
message_loop_.PostDelayedTask(FROM_HERE,
base::MessageLoop::QuitClosure(),
TestTimeouts::tiny_timeout());
message_loop_.Run();
}
TEST_F(EndToEndAsyncTest, TestSignal) {
const char kMessage[] = "hello, world";
test_service_->SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
}
TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
const char kMessage[] = "hello, world";
test_service_->SendTestSignalFromRoot(kMessage);
WaitForTestSignal();
ASSERT_TRUE(test_signal_string_.empty());
ASSERT_EQ(kMessage, root_test_signal_string_);
}
TEST_F(EndToEndAsyncTest, TestHugeSignal) {
const std::string kHugeMessage(kHugePayloadSize, 'o');
test_service_->SendTestSignal(kHugeMessage);
WaitForTestSignal();
ASSERT_EQ(kHugeMessage, test_signal_string_);
}
TEST_F(EndToEndAsyncTest, DisconnectedSignal) {
bus_->GetDBusTaskRunner()->PostTask(FROM_HERE,
base::Bind(&Bus::ClosePrivateConnection,
base::Unretained(bus_.get())));
message_loop_.Run();
EXPECT_EQ(1, on_disconnected_call_count_);
}
class SignalMultipleHandlerTest : public EndToEndAsyncTest {
public:
SignalMultipleHandlerTest() {
}
virtual void SetUp() {
EndToEndAsyncTest::SetUp();
object_proxy_->ConnectToSignal(
"org.chromium.TestInterface",
"Test",
base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
base::Unretained(this)),
base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected,
base::Unretained(this)));
message_loop_.Run();
}
protected:
void OnAdditionalTestSignal(Signal* signal) {
MessageReader reader(signal);
ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
message_loop_.Quit();
}
void OnAdditionalConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
ASSERT_TRUE(success);
message_loop_.Quit();
}
std::string additional_test_signal_string_;
};
TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
const char kMessage[] = "hello, world";
test_service_->SendTestSignal(kMessage);
WaitForTestSignal();
ASSERT_EQ(kMessage, test_signal_string_);
ASSERT_EQ(kMessage, additional_test_signal_string_);
}
}