2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
20 * @brief Implementation file for test cases for DBus internal tests.
24 #include <dpl/test/test_runner.h>
25 #include <dpl/event/event_listener.h>
26 #include <dpl/dbus/exception.h>
27 #include <dpl/dbus/connection.h>
28 #include <dpl/dbus/interface.h>
29 #include <dpl/dbus/server.h>
30 #include "dbus_test.h"
33 const std::string dbusServiceName = "org.freedesktop.DBus";
34 const std::string dbusObjectPath = "/";
35 const std::string dbusInterfaceName = "org.freedesktop.DBus";
36 const std::string dbusMethodGetId = "GetId";
38 const std::string serviceName = "org.tizen.DBusTestService";
39 const std::string objectPath = "/org/tizen/DBusTestService";
40 const std::string interfaceName = "org.tizen.DBusTestService";
41 const std::string methodNameEcho = "echo";
42 const std::string methodNameQuit = "quit";
43 const std::string nodeInfo =
44 "<?xml version='1.0'?>"
46 " <interface name='" + interfaceName + "'>"
49 " <arg type='s' name='challenge' direction='in'/>"
50 " <arg type='s' name='response' direction='out'/>"
53 + methodNameQuit + "'>"
58 const std::string challenge = "Hello world!";
60 const int DEFAULT_TIMEOUT = 2; // in seconds
63 RUNNER_TEST_GROUP_INIT(DPL)
66 Name: AcquireSessionBus
67 Description: tests acquiring session bus
68 Expected: no exceptions
70 RUNNER_TEST(AcquireSessionBus)
73 DPL::DBus::Connection::sessionBus();
74 } catch (const DPL::DBus::Exception& ex) {
75 RUNNER_ASSERT_MSG(false, ex.DumpToString());
80 Name: AcquireSystemBus
81 Description: tests acquiring system bus
82 Expected: no exceptions
84 RUNNER_TEST(AcquireSystemBus)
87 DPL::DBus::Connection::systemBus();
88 } catch (const DPL::DBus::Exception& ex) {
89 RUNNER_ASSERT_MSG(false, ex.DumpToString());
94 Name: ServerCreateFail
95 Description: tests creating server with wrong address
96 Expected: exception should occur
98 RUNNER_TEST(ServerCreateFail)
100 bool exceptionCaught = false;
102 DPL::DBus::ServerPtr server = DPL::DBus::Server::create("wrong address");
103 } catch (const DPL::Exception& ex) {
104 exceptionCaught = true;
106 RUNNER_ASSERT(exceptionCaught);
110 Name: ServerCreateAndConnection
111 Description: tests creating server and connecting to it
112 Expected: no exceptions
114 RUNNER_TEST(ServerCreateAndConnection)
117 DPL::DBus::ServerPtr server = DPL::DBus::Server::create("unix:abstract=/tmp/testAddresss");
119 DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=/tmp/testAddresss");
121 } catch (const DPL::DBus::Exception& ex) {
122 RUNNER_ASSERT_MSG(false, ex.DumpToString());
128 Description: tests creating connection when server is not running
129 Expected: exception should occur
131 RUNNER_TEST(ConnectionFail)
133 bool exceptionCaught = false;
135 DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=/tmp/testAddresss");
137 } catch (const DPL::DBus::Exception& ex) {
138 exceptionCaught = true;
140 RUNNER_ASSERT(exceptionCaught);
144 Name: ConnectionFail2
145 Description: tests creating connection when server is running on different address
146 Expected: exception should occur
148 RUNNER_TEST(ConnectionFail2)
150 bool exceptionCaught = false;
152 DPL::DBus::ServerPtr server = DPL::DBus::Server::create("unix:abstract=/tmp/testAddresssToFail");
154 DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=wrongAddress");
156 } catch (const DPL::DBus::Exception& ex) {
157 exceptionCaught = true;
159 RUNNER_ASSERT(exceptionCaught);
166 Description: creates dbus interface from xml string
167 Expected: interface should be created correctly
169 RUNNER_TEST(ParseNodeInfo)
172 auto ifaces = DPL::DBus::Interface::fromXMLString(nodeInfo);
173 RUNNER_ASSERT(!ifaces.empty());
175 auto iface = ifaces.at(0);
176 RUNNER_ASSERT(NULL != iface->getVTable());
177 RUNNER_ASSERT(NULL != iface->getInfo());
178 } catch (const DPL::DBus::Exception& ex) {
179 RUNNER_ASSERT_MSG(false, ex.DumpToString());
184 Name: InvokeRemoteMethod
185 Description: performs procedure call via dbus
186 Expected: call should return not empty id
188 RUNNER_TEST(InvokeRemoteMethod)
191 auto connection = DPL::DBus::Connection::systemBus();
192 auto freedesktop = connection->createObjectProxy(dbusServiceName,
194 auto getId = freedesktop->createMethodProxy<std::string>
195 (dbusInterfaceName, dbusMethodGetId);
196 RUNNER_ASSERT(!getId().empty());
197 } catch (const DPL::DBus::Exception& ex) {
198 RUNNER_ASSERT_MSG(false, ex.DumpToString());
202 class RegisterServiceListener :
203 public DPL::Event::EventListener<DPL::DBus::ConnectionEvents::
204 ServiceNameAcquiredEvent>
207 void OnEventReceived(
208 const DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent& event)
210 DBusTest& test = DBusTestManager::getInstance().getCurrentTest();
212 auto name = event.GetArg0();
213 if (serviceName == name) {
216 test.fail("Acquired service name: " + name);
223 Name: RegisterService
224 Description: tests event listener for AcquiredEvent in context of dbus
225 Expected: event should be received
227 DBUS_TEST(RegisterService)
230 RegisterServiceListener listener;
232 auto connection = DPL::DBus::Connection::sessionBus();
233 connection->DPL::Event::EventSupport<DPL::DBus::ConnectionEvents::
234 ServiceNameAcquiredEvent>::
235 AddListener(&listener);
236 connection->registerService(serviceName);
238 DBusTestManager::getInstance().getCurrentTest().run(DEFAULT_TIMEOUT);
240 connection->unregisterService(serviceName);
242 } catch (const DPL::DBus::Exception& ex) {
243 RUNNER_ASSERT_MSG(false, ex.DumpToString());
249 * - object registration (done on the wrt-dbus-test-service side)
250 * - service registration (done on the wrt-dbus-test-service side)
251 * - dispatching method calls (done on the wrt-dbus-test-service side)
252 * - launching dbus service on demand
253 * - invoking remote method(s)
255 DBUS_TEST(InvokeTestService)
258 auto connection = DPL::DBus::Connection::sessionBus();
259 auto testService = connection->createObjectProxy(serviceName,
261 auto echo = testService->createMethodProxy<std::string, std::string>
262 (interfaceName, methodNameEcho);
263 auto response = echo(challenge);
265 testService->createMethodProxy<void>(interfaceName, methodNameQuit) ();
267 RUNNER_ASSERT_MSG(response == challenge,
268 "[challenge = " << challenge <<
269 ", response = " << response << "]");
270 } catch (const DPL::DBus::Exception& ex) {
271 RUNNER_ASSERT_MSG(false, ex.DumpToString());