Adjust to coding style rules
[platform/core/security/vasum.git] / tests / unit_tests / socket_test_service / socket-test.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Lukasz Kostyra <l.kostyra@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author  Lukasz Kostyra (l.kostyra@samsung.com)
22  * @brief   Mini-service for IPC Socket mechanism tests
23  */
24
25 #include "config.hpp"
26 #include "socket-test.hpp"
27 #include "logger/logger.hpp"
28 #include "logger/backend-journal.hpp"
29 #include "ipc/internals/socket.hpp"
30 #include "ipc/exception.hpp"
31
32 #include <cstring>
33 #include <memory>
34
35 using namespace vasum::socket_test;
36 using namespace ipc;
37 using namespace logger;
38
39 // NOTE this is a single-usage program, only meant to test vasum::ipc::Socket module.
40 //      It's purpose is to be activated when needed by systemd socket activation mechanism.
41 int main()
42 {
43     Logger::setLogLevel(LogLevel::TRACE);
44     Logger::setLogBackend(new SystemdJournalBackend());
45
46     try {
47         Socket listeningSocket(Socket::createSocket(SOCKET_PATH));
48         if (listeningSocket.getFD() < 0) {
49             LOGE("Failed to connect to socket!");
50             return 1;
51         }
52
53         std::shared_ptr<Socket> clientSocket = listeningSocket.accept();
54         LOGI("Connected! Emitting message to client.");
55         clientSocket->write(TEST_MESSAGE.c_str(), TEST_MESSAGE.size());
56         LOGI("Message sent through socket! Exiting.");
57     } catch (const IPCException& e) {
58         LOGE("IPC exception caught! " << e.what());
59         return 1;
60     }
61
62     return 0;
63 }