From: Sangwan Kwon Date: Tue, 15 Oct 2019 08:13:40 +0000 (+0900) Subject: Add server-client feature X-Git-Tag: accepted/tizen/unified/20200810.122954~182 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=16f527f06b9861b60b41f3d05a3fea2cb9391755;p=platform%2Fcore%2Fsecurity%2Fvist.git Add server-client feature Signed-off-by: Sangwan Kwon --- diff --git a/src/apix/CMakeLists.txt b/src/apix/CMakeLists.txt index b5c12fb..ddde13c 100644 --- a/src/apix/CMakeLists.txt +++ b/src/apix/CMakeLists.txt @@ -18,9 +18,10 @@ SET(${TARGET_APIX_LIB}_SRCS "") SET(${TARGET_APIX_LIB}_DEPS "") SET(${TARGET_APIX_LIB}_TESTS "") -ADD_SUBDIRECTORY(property) +ADD_SUBDIRECTORY(ipc) ADD_SUBDIRECTORY(manager) ADD_SUBDIRECTORY(notification) +ADD_SUBDIRECTORY(property) ADD_LIBRARY(${TARGET_APIX_LIB} STATIC ${${TARGET_APIX_LIB}_SRCS}) TARGET_LINK_LIBRARIES(${TARGET_APIX_LIB} ${${TARGET_APIX_LIB}_DEPS} diff --git a/src/apix/ipc/CMakeLists.txt b/src/apix/ipc/CMakeLists.txt new file mode 100644 index 0000000..7fb5a77 --- /dev/null +++ b/src/apix/ipc/CMakeLists.txt @@ -0,0 +1,16 @@ +# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License + +FILE(GLOB SERVER_TESTS "tests/*.cpp") +ADD_APIX_TEST(${SERVER_TESTS}) diff --git a/src/apix/ipc/client.h b/src/apix/ipc/client.h new file mode 100644 index 0000000..ddf7a5c --- /dev/null +++ b/src/apix/ipc/client.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#pragma once + +#include +#include + +#include + +namespace ipc { + +class Client final { +public: + Client(const Client&) = delete; + Client& operator=(const Client&) = delete; + + Client(Client&&) = delete; + Client& operator=(Client&&) = delete; + + static std::unique_ptr& Instance(const std::string& sock) + { + static Client client(sock); + return client.instance; + } + +private: + explicit Client(const std::string& sock) : + instance(std::make_unique(sock)) + { + instance->connect(); + } + ~Client() = default; + + std::unique_ptr instance; +}; + +} // namespace ipc diff --git a/src/apix/ipc/server.h b/src/apix/ipc/server.h new file mode 100644 index 0000000..5bbcc49 --- /dev/null +++ b/src/apix/ipc/server.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#pragma once + +#include +#include + +#include + +namespace ipc { + +class Server final { +public: + Server(const Server&) = delete; + Server& operator=(const Server&) = delete; + + Server(Server&&) = delete; + Server& operator=(Server&&) = delete; + + static std::unique_ptr& Instance(const std::string& sock) + { + static Server server(sock); + return server.instance; + } + +private: + explicit Server(const std::string& sock) : + instance(std::make_unique(sock)) {} + ~Server() = default; + + std::unique_ptr instance; +}; + +} // namespace ipc diff --git a/src/apix/ipc/tests/ipc_tests.cpp b/src/apix/ipc/tests/ipc_tests.cpp new file mode 100644 index 0000000..40b7451 --- /dev/null +++ b/src/apix/ipc/tests/ipc_tests.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +#include + +#include "../server.h" +#include "../client.h" + +#include +#include + +#include +#include + +using namespace ipc; + +namespace { + std::string g_socket = "/tmp/vist-test"; + + void init() + { + } + +} // anonymous namespace + +class TestServer { +public: + void init() + { + auto& server = ipc::Server::Instance(g_socket); + server->expose(this, "", (std::string)(TestServer::testMethod)()); + server->start(); + } + + std::string testMethod() + { + return "test"; + } +}; + +class IpcTests : public testing::Test { +public: + void SetUp() override + { + ::unlink(g_socket.c_str()); + + this->pid = fork(); + if (pid < 0) { + return; + } else if (pid == 0) { + TestServer server; + server.init(); + + } + + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + void TearDown() override + { + if (::kill(pid, SIGTERM) == -1) + return; + + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + +private: + pid_t pid = -1; +}; + +TEST_F(IpcTests, method_call) { + auto& client = ipc::Client::Instance(g_socket); + + std::string ret = client->methodCall("TestServer::testMethod"); + EXPECT_EQ(ret, "test"); +} diff --git a/src/apix/manager/tests/policy_tests.cpp b/src/apix/manager/tests/policy_tests.cpp index e3a5a57..18ab5f6 100644 --- a/src/apix/manager/tests/policy_tests.cpp +++ b/src/apix/manager/tests/policy_tests.cpp @@ -64,7 +64,6 @@ TEST_F(PolicydTests, update) { query = "SELECT * FROM policy WHERE name = 'bluetooth'"; rows = OsqueryManager::execute(query); - /// Initial policy value EXPECT_EQ(rows[0]["value"], std::to_string(3)); manager.disenroll("admin", 0);