tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / dbus / test_cases.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    TestCases.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for DBus internal tests.
21  */
22
23 #include <string>
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"
31
32 namespace {
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";
37
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'?>"
45     "<node>"
46     "  <interface name='" + interfaceName + "'>"
47                                             "    <method name='" +
48     methodNameEcho + "'>"
49                      "      <arg type='s' name='challenge' direction='in'/>"
50                      "      <arg type='s' name='response' direction='out'/>"
51                      "    </method>"
52                      "    <method name='"
53     + methodNameQuit + "'>"
54                        "    </method>"
55                        "  </interface>"
56                        "</node>";
57
58 const std::string challenge = "Hello world!";
59
60 const int DEFAULT_TIMEOUT = 2; // in seconds
61 }
62
63 RUNNER_TEST_GROUP_INIT(DPL)
64
65 /*
66 Name: AcquireSessionBus
67 Description: tests acquiring session bus
68 Expected: no exceptions
69 */
70 RUNNER_TEST(AcquireSessionBus)
71 {
72     try {
73         DPL::DBus::Connection::sessionBus();
74     } catch (const DPL::DBus::Exception& ex) {
75         RUNNER_ASSERT_MSG(false, ex.DumpToString());
76     }
77 }
78
79 /*
80 Name: AcquireSystemBus
81 Description: tests acquiring system bus
82 Expected: no exceptions
83 */
84 RUNNER_TEST(AcquireSystemBus)
85 {
86     try {
87         DPL::DBus::Connection::systemBus();
88     } catch (const DPL::DBus::Exception& ex) {
89         RUNNER_ASSERT_MSG(false, ex.DumpToString());
90     }
91 }
92
93 /*
94 Name: ServerCreateFail
95 Description: tests creating server with wrong address
96 Expected: exception should occur
97 */
98 RUNNER_TEST(ServerCreateFail)
99 {
100     bool exceptionCaught = false;
101     try {
102         DPL::DBus::ServerPtr server = DPL::DBus::Server::create("wrong address");
103     } catch (const DPL::Exception& ex) {
104         exceptionCaught = true;
105     }
106     RUNNER_ASSERT(exceptionCaught);
107 }
108
109 /*
110 Name: ServerCreateAndConnection
111 Description: tests creating server and connecting to it
112 Expected: no exceptions
113 */
114 RUNNER_TEST(ServerCreateAndConnection)
115 {
116     try {
117         DPL::DBus::ServerPtr server = DPL::DBus::Server::create("unix:abstract=/tmp/testAddresss");
118         server->start();
119         DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=/tmp/testAddresss");
120         RUNNER_ASSERT(con);
121     } catch (const DPL::DBus::Exception& ex) {
122         RUNNER_ASSERT_MSG(false, ex.DumpToString());
123     }
124 }
125
126 /*
127 Name: ConnectionFail
128 Description: tests creating connection when server is not running
129 Expected: exception should occur
130 */
131 RUNNER_TEST(ConnectionFail)
132 {
133     bool exceptionCaught = false;
134     try {
135         DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=/tmp/testAddresss");
136         RUNNER_ASSERT(con);
137     } catch (const DPL::DBus::Exception& ex) {
138         exceptionCaught = true;
139     }
140     RUNNER_ASSERT(exceptionCaught);
141 }
142
143 /*
144 Name: ConnectionFail2
145 Description: tests creating connection when server is running on different address
146 Expected: exception should occur
147 */
148 RUNNER_TEST(ConnectionFail2)
149 {
150     bool exceptionCaught = false;
151     try {
152         DPL::DBus::ServerPtr server = DPL::DBus::Server::create("unix:abstract=/tmp/testAddresssToFail");
153         server->start();
154         DPL::DBus::ConnectionPtr con = DPL::DBus::Connection::connectTo("unix:abstract=wrongAddress");
155         RUNNER_ASSERT(con);
156     } catch (const DPL::DBus::Exception& ex) {
157         exceptionCaught = true;
158     }
159     RUNNER_ASSERT(exceptionCaught);
160 }
161
162
163
164 /*
165 Name: ParseNodeInfo
166 Description: creates dbus interface from xml string
167 Expected: interface should be created correctly
168 */
169 RUNNER_TEST(ParseNodeInfo)
170 {
171     try {
172         auto ifaces = DPL::DBus::Interface::fromXMLString(nodeInfo);
173         RUNNER_ASSERT(!ifaces.empty());
174
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());
180     }
181 }
182
183 /*
184 Name: InvokeRemoteMethod
185 Description: performs procedure call via dbus
186 Expected: call should return not empty id
187 */
188 RUNNER_TEST(InvokeRemoteMethod)
189 {
190     try {
191         auto connection = DPL::DBus::Connection::systemBus();
192         auto freedesktop = connection->createObjectProxy(dbusServiceName,
193                                                          dbusObjectPath);
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());
199     }
200 }
201
202 class RegisterServiceListener :
203     public DPL::Event::EventListener<DPL::DBus::ConnectionEvents::
204                                          ServiceNameAcquiredEvent>
205 {
206   public:
207     void OnEventReceived(
208         const DPL::DBus::ConnectionEvents::ServiceNameAcquiredEvent& event)
209     {
210         DBusTest& test = DBusTestManager::getInstance().getCurrentTest();
211
212         auto name = event.GetArg0();
213         if (serviceName == name) {
214             test.success();
215         } else {
216             test.fail("Acquired service name: " + name);
217         }
218         test.quit();
219     }
220 };
221
222 /*
223 Name: RegisterService
224 Description: tests event listener for AcquiredEvent in context of dbus
225 Expected: event should be received
226 */
227 DBUS_TEST(RegisterService)
228 {
229     try {
230         RegisterServiceListener listener;
231
232         auto connection = DPL::DBus::Connection::sessionBus();
233         connection->DPL::Event::EventSupport<DPL::DBus::ConnectionEvents::
234                                                  ServiceNameAcquiredEvent>::
235             AddListener(&listener);
236         connection->registerService(serviceName);
237
238         DBusTestManager::getInstance().getCurrentTest().run(DEFAULT_TIMEOUT);
239
240         connection->unregisterService(serviceName);
241
242     } catch (const DPL::DBus::Exception& ex) {
243         RUNNER_ASSERT_MSG(false, ex.DumpToString());
244     }
245 }
246
247 /**
248  * This test checks:
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)
254  */
255 DBUS_TEST(InvokeTestService)
256 {
257     try {
258         auto connection = DPL::DBus::Connection::sessionBus();
259         auto testService = connection->createObjectProxy(serviceName,
260                                                          objectPath);
261         auto echo = testService->createMethodProxy<std::string, std::string>
262                 (interfaceName, methodNameEcho);
263         auto response = echo(challenge);
264
265         testService->createMethodProxy<void>(interfaceName, methodNameQuit) ();
266
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());
272     }
273 }