tests: fix blocking semantic in DBusProxyTest
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusAddressTranslatorTest.cpp
1 /* Copyright (C) 2013 BMW Group
2  * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3  * Author: Juergen Gehring (juergen.gehring@bmw.de)
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8
9 #include <gtest/gtest.h>
10 #include <fstream>
11 #include <thread>
12 #include <unistd.h>
13
14 #include <CommonAPI/DBus/DBusAddressTranslator.h>
15 #include <CommonAPI/DBus/DBusUtils.h>
16
17 #include <CommonAPI/types.h>
18 #include <CommonAPI/AttributeExtension.h>
19 #include <CommonAPI/Runtime.h>
20
21 #include <CommonAPI/DBus/DBusConnection.h>
22 #include <CommonAPI/DBus/DBusProxy.h>
23 #include <CommonAPI/DBus/DBusRuntime.h>
24
25 #include "commonapi/tests/PredefinedTypeCollection.h"
26 #include "commonapi/tests/DerivedTypeCollection.h"
27 #include "commonapi/tests/TestInterfaceProxy.h"
28 #include "commonapi/tests/TestInterfaceStubDefault.h"
29 #include "commonapi/tests/TestInterfaceDBusStubAdapter.h"
30
31 #include "fakeLegacyService/fake/legacy/service/LegacyInterfaceProxy.h"
32
33
34 static const std::vector<std::string> commonApiAddresses = {
35     "local:no.nothing.service:no.nothing.instance",
36     "local:service:instance",
37     "local:no.interface.service:no.interface.instance",
38     "local:no.connection.service:no.connection.instance",
39     "local:no.object.service:no.object.instance",
40     "local:only.interface.service:only.interface.instance",
41     "local:only.connection.service:only.connection.instance",
42     "local:only.object.service:only.object.instance",
43     "local:fake.legacy.service.LegacyInterface:fake.legacy.service"
44 };
45
46 static const std::string fileString =
47 "[local:no.nothing.service:no.nothing.instance]\n"
48 "\n"
49 "[local:service:instance]\n"
50 "dbus_connection=connection.name\n"
51 "dbus_object=/path/to/object\n"
52 "dbus_interface=service.name\n"
53 "\n"
54 "[local:no.interface.service:no.interface.instance]\n"
55 "dbus_connection=no.interface.connection\n"
56 "dbus_object=/no/interface/path\n"
57 "\n"
58 "[local:no.connection.service:no.connection.instance]\n"
59 "dbus_object=/no/connection/path\n"
60 "dbus_interface=no.connection.interface\n"
61 "\n"
62 "[local:no.object.service:no.object.instance]\n"
63 "dbus_connection=no.object.connection\n"
64 "dbus_interface=no.object.interface\n"
65 "\n"
66 "[local:only.interface.service:only.interface.instance]\n"
67 "dbus_interface=only.interface.interface\n"
68 "\n"
69 "[local:only.connection.service:only.connection.instance]\n"
70 "dbus_connection=only.connection.connection\n"
71 "\n"
72 "[local:only.object.service:only.object.instance]\n"
73 "dbus_object=/only/object/path\n"
74 "\n"
75 "[local:fake.legacy.service.LegacyInterface:fake.legacy.service]\n"
76 "dbus_connection=fake.legacy.service.connection\n"
77 "dbus_object=/some/legacy/path/6259504\n"
78 "dbus_interface=fake.legacy.service.LegacyInterface\n";
79
80 typedef std::vector<CommonAPI::DBus::DBusServiceAddress>::value_type vt;
81 static const std::vector<CommonAPI::DBus::DBusServiceAddress> dbusAddresses = {
82                 vt("no.nothing.instance", "/no/nothing/instance", "no.nothing.service", false),
83                 vt("connection.name", "/path/to/object", "service.name", false),
84                 vt("no.interface.connection", "/no/interface/path", "no.interface.service", false),
85                 vt("no.connection.instance", "/no/connection/path", "no.connection.interface", false),
86                 vt("no.object.connection", "/no/object/instance", "no.object.interface", false),
87                 vt("only.interface.instance", "/only/interface/instance", "only.interface.interface", false),
88                 vt("only.connection.connection", "/only/connection/instance", "only.connection.service", false),
89                 vt("only.object.instance", "/only/object/path", "only.object.service", false),
90                 vt("fake.legacy.service.connection", "/some/legacy/path/6259504", "fake.legacy.service.LegacyInterface", false)
91 };
92
93
94 class Environment: public ::testing::Environment {
95 public:
96     virtual ~Environment() {
97     }
98
99     virtual void SetUp() {
100         configFileName_ = CommonAPI::DBus::getCurrentBinaryFileFQN();
101         configFileName_ += CommonAPI::DBus::DBUS_CONFIG_SUFFIX;
102         std::ofstream configFile(configFileName_);
103         ASSERT_TRUE(configFile.is_open());
104         configFile << fileString;
105         configFile.close();
106     }
107
108     virtual void TearDown() {
109         std::remove(configFileName_.c_str());
110     }
111
112     std::string configFileName_;
113 };
114
115
116 class AddressTranslatorTest: public ::testing::Test {
117 protected:
118     void SetUp() {
119     }
120
121     virtual void TearDown() {
122     }
123 };
124
125
126 TEST_F(AddressTranslatorTest, InstanceCanBeRetrieved) {
127     CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
128 }
129
130
131 TEST_F(AddressTranslatorTest, ParsesDBusAddresses) {
132     CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
133
134     for(unsigned int i = 0; i < commonApiAddresses.size(); i++) {
135         std::string interfaceName, connectionName, objectPath;
136         translator.searchForDBusAddress(commonApiAddresses[i], interfaceName, connectionName, objectPath);
137         ASSERT_EQ(std::get<0>(dbusAddresses[i]), connectionName);
138         ASSERT_EQ(std::get<1>(dbusAddresses[i]), objectPath);
139         ASSERT_EQ(std::get<2>(dbusAddresses[i]), interfaceName);
140     }
141 }
142
143
144 TEST_F(AddressTranslatorTest, ParsesCommonAPIAddresses) {
145     CommonAPI::DBus::DBusAddressTranslator& translator = CommonAPI::DBus::DBusAddressTranslator::getInstance();
146
147     for(unsigned int i = 0; i < commonApiAddresses.size(); i++) {
148         std::string commonApiAddress;
149         translator.searchForCommonAddress(
150                         std::get<2>(dbusAddresses[i]),
151                         std::get<0>(dbusAddresses[i]),
152                         std::get<1>(dbusAddresses[i]),
153                         commonApiAddress);
154         ASSERT_EQ(commonApiAddresses[i], commonApiAddress);
155     }
156 }
157
158
159 TEST_F(AddressTranslatorTest, ServicesUsingPredefinedAddressesCanCommunicate) {
160     std::shared_ptr<CommonAPI::Runtime> runtime;
161     std::shared_ptr<CommonAPI::Factory> proxyFactory;
162     std::shared_ptr<CommonAPI::Factory> stubFactory;
163
164     runtime = CommonAPI::Runtime::load();
165     ASSERT_TRUE((bool)runtime);
166     CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
167     ASSERT_TRUE(dbusRuntime != NULL);
168
169     proxyFactory = runtime->createFactory();
170     ASSERT_TRUE((bool)proxyFactory);
171     stubFactory = runtime->createFactory();
172     ASSERT_TRUE((bool)stubFactory);
173
174     auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>(commonApiAddresses[0]);
175     ASSERT_TRUE((bool)defaultTestProxy);
176
177     auto stub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
178     bool success = stubFactory->registerService(stub, commonApiAddresses[0]);
179     ASSERT_TRUE(success);
180
181     uint32_t v1 = 5;
182     std::string v2 = "Hai :)";
183     CommonAPI::CallStatus stat;
184     defaultTestProxy->testVoidPredefinedTypeMethod(v1, v2, stat);
185
186     ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
187 }
188
189
190 const std::string addressOfFakeLegacyService = commonApiAddresses[8];
191
192 const std::string domainOfFakeLegacyService = "local";
193 const std::string serviceIdOfFakeLegacyService = "fake.legacy.service.LegacyInterface";
194 const std::string participantIdOfFakeLegacyService = "fake.legacy.service";
195
196 TEST_F(AddressTranslatorTest, CreatedProxyHasCorrectCommonApiAddress) {
197     std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
198     ASSERT_TRUE((bool)runtime);
199     CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
200     ASSERT_TRUE(dbusRuntime != NULL);
201
202     std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
203     ASSERT_TRUE((bool)proxyFactory);
204     auto proxyForFakeLegacyService = proxyFactory->buildProxy<fake::legacy::service::LegacyInterfaceProxy>(addressOfFakeLegacyService);
205     ASSERT_TRUE((bool)proxyForFakeLegacyService);
206
207     ASSERT_EQ(addressOfFakeLegacyService, proxyForFakeLegacyService->getAddress());
208     ASSERT_EQ(domainOfFakeLegacyService, proxyForFakeLegacyService->getDomain());
209     ASSERT_EQ(serviceIdOfFakeLegacyService, proxyForFakeLegacyService->getServiceId());
210     ASSERT_EQ(participantIdOfFakeLegacyService, proxyForFakeLegacyService->getInstanceId());
211 }
212
213
214 void fakeLegacyServiceThread() {
215     int resultCode = system("python ./src/test/fakeLegacyService/fakeLegacyService.py");
216     EXPECT_EQ(0, resultCode);
217 }
218
219 TEST_F(AddressTranslatorTest, FakeLegacyServiceCanBeAddressed) {
220     std::thread fakeServiceThread = std::thread(fakeLegacyServiceThread);
221     sleep(1);
222
223     std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
224     ASSERT_TRUE((bool)runtime);
225     CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime));
226     ASSERT_TRUE(dbusRuntime != NULL);
227
228     std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime->createFactory();
229     ASSERT_TRUE((bool)proxyFactory);
230     auto proxyForFakeLegacyService = proxyFactory->buildProxy<fake::legacy::service::LegacyInterfaceProxy>(addressOfFakeLegacyService);
231     ASSERT_TRUE((bool)proxyForFakeLegacyService);
232
233     ASSERT_EQ(addressOfFakeLegacyService, proxyForFakeLegacyService->getAddress());
234
235     CommonAPI::CallStatus status;
236
237     const int32_t input = 42;
238     int32_t output1, output2;
239     proxyForFakeLegacyService->TestMethod(input, status, output1, output2);
240     EXPECT_EQ(CommonAPI::CallStatus::SUCCESS, status);
241     if(CommonAPI::CallStatus::SUCCESS == status) {
242         EXPECT_EQ(input -5, output1);
243         EXPECT_EQ(input +5, output2);
244     }
245
246     std::string greeting;
247     int32_t identifier;
248     proxyForFakeLegacyService->OtherTestMethod(status, greeting, identifier);
249     EXPECT_EQ(CommonAPI::CallStatus::SUCCESS, status);
250     if(CommonAPI::CallStatus::SUCCESS == status) {
251         EXPECT_EQ(std::string("Hello"), greeting);
252         EXPECT_EQ(42, identifier);
253     }
254
255     //end the fake legacy service via dbus
256     int resultCode = system("python ./src/test/fakeLegacyService/sendToFakeLegacyService.py finish");
257     EXPECT_EQ(0, resultCode);
258
259     fakeServiceThread.join();
260 }
261
262
263 int main(int argc, char** argv) {
264     ::testing::InitGoogleTest(&argc, argv);
265     ::testing::AddGlobalTestEnvironment(new Environment());
266     return RUN_ALL_TESTS();
267 }