From 38421ad24031f0418be43009a7740d6b268eb56a Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 26 Nov 2019 15:23:01 +0900 Subject: [PATCH] Add sample plugin Signed-off-by: Sangwan Kwon --- packaging/vist.spec | 1 + plugins/CMakeLists.txt | 2 + plugins/sample/CMakeLists.txt | 25 +++++++++++ plugins/sample/sample.cpp | 85 ++++++++++++++++++++++++++++++++++++++ src/vist/policy/policy-storage.cpp | 2 - src/vist/policy/tests/core.cpp | 33 +++++++++++---- 6 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 plugins/sample/CMakeLists.txt create mode 100644 plugins/sample/sample.cpp diff --git a/packaging/vist.spec b/packaging/vist.spec index e902ba2..66e834b 100644 --- a/packaging/vist.spec +++ b/packaging/vist.spec @@ -113,6 +113,7 @@ Provides internal testcases for ViST implementation. %files test %{_bindir}/osquery-test %{_bindir}/vist-test +%{vist_plugin_dir}/sample ## ViST Plugins - ########################################################### %package plugins diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 4055891..c0bde83 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -17,6 +17,8 @@ INCLUDE(FindPkgConfig) SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack") +ADD_SUBDIRECTORY(sample) + ADD_SUBDIRECTORY(bluetooth) ADD_SUBDIRECTORY(wifi) #ADD_SUBDIRECTORY(usb) diff --git a/plugins/sample/CMakeLists.txt b/plugins/sample/CMakeLists.txt new file mode 100644 index 0000000..e203969 --- /dev/null +++ b/plugins/sample/CMakeLists.txt @@ -0,0 +1,25 @@ +# 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. +# +SET(TARGET "vist-plugin-sample") + +INCLUDE_DIRECTORIES(SYSTEM) + +ADD_LIBRARY(${TARGET} SHARED sample.cpp) +SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default") +TARGET_LINK_LIBRARIES(${TARGET} vist-common) + +INSTALL(FILES libvist-plugin-sample.so + RENAME sample + DESTINATION ${PLUGIN_INSTALL_DIR}) diff --git a/plugins/sample/sample.cpp b/plugins/sample/sample.cpp new file mode 100644 index 0000000..5327fb1 --- /dev/null +++ b/plugins/sample/sample.cpp @@ -0,0 +1,85 @@ +/* + * 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 +#include + +#include + +#include +#include + +using namespace vist; +using namespace vist::policy; + +class SampleIntPolicy : public PolicyModel { +public: + SampleIntPolicy() : PolicyModel("sample-int-policy", PolicyValue(7)) + { + } + + void onChanged(const PolicyValue& value) override + { + INFO(VIST_PLUGIN) << "sample-int-policy is changed to " + << static_cast(value); + } +}; + +class SampleStrPolicy : public PolicyModel { +public: + SampleStrPolicy() : PolicyModel("sample-str-policy", PolicyValue("TEST")) + { + } + + void onChanged(const PolicyValue& value) override + { + INFO(VIST_PLUGIN) << "sample-str-policy is changed to " + << static_cast(value); + } + + int compare(const PolicyValue& lhs, const PolicyValue& rhs) const override + { + std::string lvalue = lhs; + std::string rvalue = rhs; + + INFO(VIST_PLUGIN) << "Custom compare method is called with [" << lvalue + << "], [" << rvalue << "]"; + + return lvalue.compare(rvalue); + } +}; + +class Sample : public PolicyProvider { +public: + Sample(const std::string& name) : PolicyProvider(name) + { + } + + ~Sample() + { + } +}; + +extern "C" PolicyProvider* PolicyFactory() +{ + INFO(VIST_PLUGIN) << "Sample plugin loaded."; + Sample* provider = new Sample("sample"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + + return provider; +} diff --git a/src/vist/policy/policy-storage.cpp b/src/vist/policy/policy-storage.cpp index f7d777f..b51e202 100644 --- a/src/vist/policy/policy-storage.cpp +++ b/src/vist/policy/policy-storage.cpp @@ -225,8 +225,6 @@ void PolicyStorage::update(const std::string& admin, this->syncPolicyActivated(); } -/// TODO(sangwan.kwon) Re-design strictest logic -/// PolicyValue PolicyStorage::strictest(const PolicyValue& policy) PolicyValue PolicyStorage::strictest(const std::shared_ptr& policy) { if (this->definitions.find(policy->getName()) == this->definitions.end()) diff --git a/src/vist/policy/tests/core.cpp b/src/vist/policy/tests/core.cpp index 8c1ca83..3166887 100644 --- a/src/vist/policy/tests/core.cpp +++ b/src/vist/policy/tests/core.cpp @@ -28,25 +28,44 @@ TEST(PolicyCoreTests, policy_loader) { EXPECT_TRUE(manager.policies.size() > 0); } -TEST(PolicyCoreTests, policy_set_get) { +TEST(PolicyCoreTests, policy_set_get_int) { auto& manager = PolicyManager::Instance(); manager.enroll("testAdmin"); - manager.set("bluetooth", PolicyValue(5), "testAdmin"); + manager.set("sample-int-policy", PolicyValue(5), "testAdmin"); - auto policy = manager.get("bluetooth"); + auto policy = manager.get("sample-int-policy"); EXPECT_EQ(static_cast(policy), 5); manager.enroll("testAdmin1"); - manager.set("bluetooth", PolicyValue(10), "testAdmin1"); + manager.set("sample-int-policy", PolicyValue(10), "testAdmin1"); /// Manager should return the strongest policy. - policy = manager.get("bluetooth"); + policy = manager.get("sample-int-policy"); EXPECT_EQ(static_cast(policy), 10); manager.disenroll("testAdmin"); manager.disenroll("testAdmin1"); } +TEST(PolicyCoreTests, policy_set_get_str) { + auto& manager = PolicyManager::Instance(); + manager.enroll("testAdmin"); + manager.set("sample-str-policy", PolicyValue("AAA"), "testAdmin"); + + auto policy = manager.get("sample-str-policy"); + EXPECT_EQ(static_cast(policy), "AAA"); + + manager.enroll("testAdmin1"); + manager.set("sample-str-policy", PolicyValue("BBB"), "testAdmin1"); + + /// Manager should return the strongest policy. + policy = manager.get("sample-str-policy"); + EXPECT_EQ(static_cast(policy), "AAA"); + + manager.disenroll("testAdmin"); + manager.disenroll("testAdmin1"); +} + TEST(PolicyCoreTests, policy_get_all) { auto& manager = PolicyManager::Instance(); auto policies = manager.getAll(); @@ -55,8 +74,8 @@ TEST(PolicyCoreTests, policy_get_all) { TEST(PolicyCoreTests, policy_get_policy) { auto& manager = PolicyManager::Instance(); - const auto& policy = manager.getPolicy("bluetooth"); - EXPECT_EQ(policy->getName(), "bluetooth"); + const auto& policy = manager.getPolicy("sample-int-policy"); + EXPECT_EQ(policy->getName(), "sample-int-policy"); bool raised = false; try { -- 2.7.4