Make policy plugins testable
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 26 Dec 2019 04:47:56 +0000 (13:47 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 27 Dec 2019 04:25:30 +0000 (13:25 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
15 files changed:
packaging/vist.spec
plugins/bluetooth/CMakeLists.txt
plugins/bluetooth/bluetooth-test.cpp [new file with mode: 0644]
plugins/bluetooth/bluetooth.cpp
plugins/bluetooth/bluetooth.hpp [new file with mode: 0644]
plugins/dlog.h [deleted file]
plugins/test-util.hpp [new file with mode: 0644]
plugins/tests.cpp [new file with mode: 0644]
plugins/wifi/CMakeLists.txt
plugins/wifi/wifi-test.cpp [new file with mode: 0644]
plugins/wifi/wifi.cpp
plugins/wifi/wifi.hpp [new file with mode: 0644]
src/osquery/tables/tizen/policy.cpp
src/osquery/tables/tizen/policy_admin.cpp
src/vist/client/virtual-table.hpp

index ee96538..251f0bc 100644 (file)
@@ -120,18 +120,12 @@ Provides internal testcases for ViST implementation.
 %package plugins
 Summary: Virtaul Security Table (policy modules)
 Group: Security/Other
-## Common
-BuildRequires: pkgconfig(buxton2)
-BuildRequires: pkgconfig(dlog)
-BuildRequires: pkgconfig(capi-system-info)
-BuildRequires: pkgconfig(capi-base-common)
-BuildRequires: pkgconfig(klay)
-
 ## Bluetooth
 BuildRequires: pkgconfig(bluetooth-api)
 BuildRequires: pkgconfig(capi-network-bluetooth)
 
 ## Wifi
+BuildRequires: pkgconfig(klay)
 BuildRequires: pkgconfig(capi-network-wifi-manager)
 BuildRequires: pkgconfig(capi-network-connection)
 Requires: klay
@@ -143,3 +137,5 @@ Provides plugins for controlling policies.
 %manifest packaging/%{name}-plugins.manifest
 %{vist_plugin_dir}/bluetooth
 %{vist_plugin_dir}/wifi
+%{_bindir}/vist-plugin-bluetooth-test
+%{_bindir}/vist-plugin-wifi-test
index b18a5bf..9b4e231 100644 (file)
 # limitations under the License.
 #
 SET(TARGET "vist-plugin-bluetooth")
+SET(TARGET_TEST ${TARGET}-test)
 
 SET(PLUGIN_SOURCES "bluetooth.cpp")
 
 SET(DEPENDENCY bluetooth-api
-                          capi-network-bluetooth
-                          klay)
+                          capi-network-bluetooth)
 
 PKG_CHECK_MODULES(PLUGIN_DEPS REQUIRED ${DEPENDENCY})
 
@@ -26,8 +26,26 @@ INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS})
 
 ADD_LIBRARY(${TARGET} SHARED ${PLUGIN_SOURCES})
 SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default")
-TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES})
+TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES}
+                                                               vist-common)
 
 INSTALL(FILES libvist-plugin-bluetooth.so
                RENAME bluetooth
                DESTINATION ${PLUGIN_INSTALL_DIR})
+
+ADD_EXECUTABLE(${TARGET_TEST} ../tests.cpp
+                                                         bluetooth.cpp
+                                                         bluetooth-test.cpp)
+TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES}
+                                                                        vist-common
+                                                                        gtest
+                                                                        pthread)
+INSTALL(TARGETS ${TARGET_TEST}
+               DESTINATION ${CMAKE_INSTALL_BINDIR}
+               PERMISSIONS OWNER_READ
+                                       OWNER_WRITE
+                                       OWNER_EXECUTE
+                                       GROUP_READ
+                                       GROUP_EXECUTE
+                                       WORLD_READ
+                                       WORLD_EXECUTE)
diff --git a/plugins/bluetooth/bluetooth-test.cpp b/plugins/bluetooth/bluetooth-test.cpp
new file mode 100644 (file)
index 0000000..fcf8f3d
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  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 "bluetooth.hpp"
+#include "../test-util.hpp"
+
+#include <vist/exception.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace vist;
+using namespace vist::policy::plugin;
+
+TEST(BluetoothTests, change_policy_state)
+{
+       try {
+               test::change_policy_state<BluetoothState>();
+               test::change_policy_state<DesktopConnectivity>();
+               test::change_policy_state<Pairing>();
+               test::change_policy_state<Tethering>();
+       } catch(const vist::Exception<ErrCode>& e) {
+               EXPECT_TRUE(false) << e.what();
+       }
+}
index 32da9bb..26b3c41 100644 (file)
  *  limitations under the License
  */
 
+#include "bluetooth.hpp"
+
 #include <bluetooth.h>
 #include <bluetooth-api.h>
 #include <bluetooth_internal.h>
 
-#include <vist/sdk/policy-model.hpp>
-#include <vist/sdk/policy-provider.hpp>
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
 
 #include <memory>
 
-#include "../dlog.h"
-
-#define BT_FAILED(ret)                                       \
-       (((int)(ret) == BLUETOOTH_DPM_RESULT_ACCESS_DENIED) ||   \
-        ((int)(ret) == BLUETOOTH_DPM_RESULT_FAIL))
-
-#define POLICY_IS_ALLOWED(enable)                            \
-       ((int)(enable) ? BLUETOOTH_DPM_ALLOWED :                 \
-                                        BLUETOOTH_DPM_RESTRICTED)
-
-#define STATE_CHANGE_IS_ALLOWED(enable)                      \
-       ((int)(enable) ? BLUETOOTH_DPM_BT_ALLOWED :              \
-                                        BLUETOOTH_DPM_BT_RESTRICTED)
-
-using namespace vist::policy;
-
-class ModeChange : public PolicyModel {
-public:
-       ModeChange() : PolicyModel("bluetooth", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue& value) override
-       {
-               int ret = ::bluetooth_dpm_set_allow_mode(STATE_CHANGE_IS_ALLOWED(value));
-               if (BT_FAILED(ret))
-                       throw std::runtime_error("Failed to set bluetooth.");
-       }
-};
-
-class DesktopConnectivity : public PolicyModel {
-public:
-       DesktopConnectivity() :
-               PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue& value) override
-       {
-               int ret = ::bluetooth_dpm_set_desktop_connectivity_state(POLICY_IS_ALLOWED(value));
-               if (BT_FAILED(ret))
-                       throw std::runtime_error("Failed to set bt_desktop_connectivity.");
-       }
-};
-
-class Pairing: public PolicyModel {
-public:
-       Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue& value) override
-       {
-               int ret = ::bluetooth_dpm_set_pairing_state(POLICY_IS_ALLOWED(value));
-               if (BT_FAILED(ret))
-                       throw std::runtime_error("Failed to set bt_pairing.");
-       }
-};
-
-class Tethering: public PolicyModel {
-public:
-       Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue&) {}
-};
-
-class Bluetooth : public PolicyProvider {
-public:
-       Bluetooth(const std::string& name) : PolicyProvider(name)
-       {
-               if (::bt_initialize() != BT_ERROR_NONE)
-                       ERROR(PLUGINS,"Bluetooth framework was not initilaized");
-       }
-
-       ~Bluetooth()
-       {
-               ::bt_deinitialize();
-       }
-};
+namespace vist {
+namespace policy {
+namespace plugin {
+
+namespace {
+
+bool failed(auto value)
+{
+       return value == BLUETOOTH_DPM_RESULT_ACCESS_DENIED || value == BLUETOOTH_DPM_RESULT_FAIL;
+}
+
+auto allowed(int value)
+{
+       return value ? BLUETOOTH_DPM_ALLOWED : BLUETOOTH_DPM_RESTRICTED;
+}
+
+auto bt_allowed(int value)
+{
+       return value ? BLUETOOTH_DPM_BT_ALLOWED : BLUETOOTH_DPM_BT_RESTRICTED;
+}
+
+} // anonymous namespace
+
+void BluetoothState::onChanged(const PolicyValue& value)
+{
+       auto enable = bt_allowed(value);
+       auto ret = ::bluetooth_dpm_set_allow_mode(enable);
+       if (failed(ret))
+               THROW(ErrCode::RuntimeError) << "Failed to change bluetooth state: " << ret;
+
+       INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable;
+}
+
+void DesktopConnectivity::onChanged(const PolicyValue& value)
+{
+       auto enable = allowed(value);
+       auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable);
+       if (failed(ret))
+               THROW(ErrCode::RuntimeError) << "Failed to change bt_desktop_connectivity: " << ret;
+
+       INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable;
+}
+
+void Pairing::onChanged(const PolicyValue& value)
+{
+       auto enable = allowed(value);
+       auto ret = ::bluetooth_dpm_set_pairing_state(enable);
+       if (failed(ret))
+               THROW(ErrCode::RuntimeError) << "Failed to change bluetooth pairing: " << ret;
+
+       INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable;
+}
+
+void Tethering::onChanged(const PolicyValue& value)
+{
+       auto enable = value;
+       INFO(VIST_PLUGIN) << "Bluetooth tethering state is changed to " << enable;
+}
+
+Bluetooth::Bluetooth(const std::string& name) : PolicyProvider(name)
+{
+       if (::bt_initialize() != BT_ERROR_NONE)
+               THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider.";
+}
+
+Bluetooth::~Bluetooth()
+{
+       ::bt_deinitialize();
+}
 
 // TODO(Sangwan): Add privilege to provider
 #define PRIVILEGE "http://tizen.org/privilege/dpm.bluetooth"
 
 extern "C" PolicyProvider* PolicyFactory()
 {
-       INFO(PLUGINS, "Bluetooth plugin loaded.");
+       INFO(VIST_PLUGIN) << "Bluetooth plugin loaded.";
        Bluetooth* provider = new Bluetooth("bluetooth");
-       provider->add(std::make_shared<ModeChange>());
+       provider->add(std::make_shared<BluetoothState>());
        provider->add(std::make_shared<DesktopConnectivity>());
        provider->add(std::make_shared<Pairing>());
        provider->add(std::make_shared<Tethering>());
 
        return provider;
 }
+
+} // namespace plugin
+} // namespace policy
+} // namespace vist
diff --git a/plugins/bluetooth/bluetooth.hpp b/plugins/bluetooth/bluetooth.hpp
new file mode 100644 (file)
index 0000000..b32bf66
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *  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 <vist/sdk/policy-model.hpp>
+#include <vist/sdk/policy-provider.hpp>
+#include <vist/sdk/policy-value.hpp>
+
+namespace vist {
+namespace policy {
+namespace plugin {
+
+class BluetoothState : public PolicyModel {
+public:
+       BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+class DesktopConnectivity : public PolicyModel {
+public:
+       DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+class Pairing: public PolicyModel {
+public:
+       Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+class Tethering: public PolicyModel {
+public:
+       Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {}
+       void onChanged(const PolicyValue&);
+};
+
+class Bluetooth final : public PolicyProvider {
+public:
+       Bluetooth(const std::string& name);
+       ~Bluetooth();
+};
+
+} // namespace plugin
+} // namespace policy
+} // namespace vist
diff --git a/plugins/dlog.h b/plugins/dlog.h
deleted file mode 100644 (file)
index 7d85170..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- *  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 DLogied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-
-#pragma once
-
-#include <klay/audit/logger.h> 
-#include <klay/audit/dlog-sink.h>
-
-#define PLUGINS osquery::DLog::getSink()
-
-namespace osquery {
-
-class DLog final {
-public:
-       DLog(const DLog&) = delete;
-       DLog& operator=(const DLog&) = delete;
-
-       DLog(DLog&&) noexcept = default;
-       DLog& operator=(DLog&&) noexcept = default;
-
-       static inline DLog& instance()
-       {
-               static DLog dlog;
-               return dlog;
-       }
-
-       static inline audit::LogSink* getSink()
-       {
-               return DLog::instance().logSink.get();
-       }
-
-private:
-       DLog()
-       {
-               auto dlog = new audit::DlogLogSink("VIST_PLUGIN");
-               this->logSink.reset(dynamic_cast<audit::LogSink*>(dlog));
-       }
-       ~DLog() noexcept = default;
-
-       std::unique_ptr<audit::LogSink> logSink;
-};
-
-} // namespace osquery
diff --git a/plugins/test-util.hpp b/plugins/test-util.hpp
new file mode 100644 (file)
index 0000000..06a278e
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  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 <vist/sdk/policy-model.hpp>
+#include <vist/sdk/policy-value.hpp>
+
+#include <memory>
+
+namespace vist {
+namespace test {
+
+using namespace vist::policy;
+
+template <typename T>
+void change_policy_state()
+{
+       std::shared_ptr<PolicyModel> policy = std::make_shared<T>();
+       /// enable policy
+       policy->onChanged(PolicyValue(1));
+       /// disable policy
+       policy->onChanged(PolicyValue(0));
+}
+
+} // namespace test
+} // namespace vist
diff --git a/plugins/tests.cpp b/plugins/tests.cpp
new file mode 100644 (file)
index 0000000..0fcfc89
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ *  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 <gtest/gtest.h>
+
+int main(int argc, char* argv[]) {
+       testing::InitGoogleTest(&argc, argv);
+       return RUN_ALL_TESTS();
+}
index d236772..4a70348 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 #
 SET(TARGET "vist-plugin-wifi")
+SET(TARGET_TEST ${TARGET}-test)
 
 SET(PLUGIN_SOURCES "wifi.cpp")
 
@@ -26,8 +27,25 @@ INCLUDE_DIRECTORIES(SYSTEM ${PLUGIN_DEPS_INCLUDE_DIRS})
 
 ADD_LIBRARY(${TARGET} SHARED ${PLUGIN_SOURCES})
 SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_FLAGS "-fvisibility=default")
-TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES})
-
+TARGET_LINK_LIBRARIES(${TARGET} ${PLUGIN_DEPS_LIBRARIES}
+                                                               vist-common)
 INSTALL(FILES libvist-plugin-wifi.so
-        RENAME wifi
-        DESTINATION ${PLUGIN_INSTALL_DIR})
+               RENAME wifi
+               DESTINATION ${PLUGIN_INSTALL_DIR})
+
+ADD_EXECUTABLE(${TARGET_TEST} ../tests.cpp
+                                                         wifi.cpp
+                                                         wifi-test.cpp)
+TARGET_LINK_LIBRARIES(${TARGET_TEST} ${PLUGIN_DEPS_LIBRARIES}
+                                                                        vist-common
+                                                                        gtest
+                                                                        pthread)
+INSTALL(TARGETS ${TARGET_TEST}
+               DESTINATION ${CMAKE_INSTALL_BINDIR}
+               PERMISSIONS OWNER_READ
+                                       OWNER_WRITE
+                                       OWNER_EXECUTE
+                                       GROUP_READ
+                                       GROUP_EXECUTE
+                                       WORLD_READ
+                                       WORLD_EXECUTE)
diff --git a/plugins/wifi/wifi-test.cpp b/plugins/wifi/wifi-test.cpp
new file mode 100644 (file)
index 0000000..ca048fd
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  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 "wifi.hpp"
+#include "../test-util.hpp"
+
+#include <vist/exception.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace vist;
+using namespace vist::policy::plugin;
+
+TEST(WifiTests, change_policy_state)
+{
+       try {
+               test::change_policy_state<WifiState>();
+               test::change_policy_state<ProfileChange>();
+               test::change_policy_state<Hotspot>();
+               test::change_policy_state<SsidRestriction>();
+       } catch(const vist::Exception<ErrCode>& e) {
+               EXPECT_TRUE(false) << e.what();
+       }
+}
index 5dff58f..cb169d4 100644 (file)
  *  limitations under the License
  */
 
-#include <arpa/inet.h>
-#include <wifi-manager.h>
+#include "wifi.hpp"
 
-#include <vist/sdk/policy-model.hpp>
-#include <vist/sdk/policy-provider.hpp>
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
 
 #include <klay/dbus/connection.h>
 
 #include <memory>
 
-#include "../dlog.h"
-
 #define NETCONFIG_INTERFACE            \
        "net.netconfig",                        \
        "/net/netconfig/network",       \
        "net.netconfig.network"
 
-using namespace vist::policy;
-
-class ModeChange : public PolicyModel {
-public:
-       ModeChange() : PolicyModel("wifi", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue& value) override
-       {
-               int enable = value;
-               klay::dbus::Connection &systemDBus = klay::dbus::Connection::getSystem();
-               systemDBus.methodcall(NETCONFIG_INTERFACE,
-                                                         "DevicePolicySetWifi",
-                                                         -1,
-                                                         "",
-                                                         "(i)",
-                                                         enable);
-       }
-};
-
-class ProfileChange : public PolicyModel {
-public:
-       ProfileChange() : PolicyModel("wifi-profile-change", PolicyValue(1)) {}
-
-       void onChanged(const PolicyValue& value) override
-       {
-               int enable = value;
-               dbus::Connection &systemDBus = dbus::Connection::getSystem();
-               systemDBus.methodcall(NETCONFIG_INTERFACE,
-                                                         "DevicePolicySetWifiProfile",
-                                                         -1,
-                                                         "",
-                                                         "(i)",
-                                                         enable);
-       }
-};
+namespace vist {
+namespace policy {
+namespace plugin {
 
-class Hotspot : public PolicyModel {
-public:
-       Hotspot() : PolicyModel("wifi-hotspot", PolicyValue(1)) {}
+void WifiState::onChanged(const PolicyValue& value)
+{
+       int enable = value;
+       INFO(VIST_PLUGIN) << "Wifi state is changed to " << enable;
+       klay::dbus::Connection &systemDBus = klay::dbus::Connection::getSystem();
+       systemDBus.methodcall(NETCONFIG_INTERFACE,
+                                                 "DevicePolicySetWifi",
+                                                 -1,
+                                                 "",
+                                                 "(i)",
+                                                 enable);
+}
 
-       void onChanged(const PolicyValue&) override
-       {
-               /// N/A
-       }
-};
+void ProfileChange::onChanged(const PolicyValue& value)
+{
+       int enable = value;
+       INFO(VIST_PLUGIN) << "Wifi profile change state is changed to " << enable;
+       dbus::Connection &systemDBus = dbus::Connection::getSystem();
+       systemDBus.methodcall(NETCONFIG_INTERFACE,
+                                                 "DevicePolicySetWifiProfile",
+                                                 -1,
+                                                 "",
+                                                 "(i)",
+                                                 enable);
+}
 
-class SsidRestriction : public PolicyModel {
-public:
-       SsidRestriction() : PolicyModel("wifi-ssid-restriction", PolicyValue(0)) {}
+void Hotspot::onChanged(const PolicyValue& value)
+{
+       int enable = value;
+       INFO(VIST_PLUGIN) << "Wifi hotspot change state is changed to " << enable;
+}
 
-       void onChanged(const PolicyValue&) override
-       {
-               /// N/A
-       }
-};
-
-class Wifi : public PolicyProvider {
-public:
-       Wifi(const std::string& name) : PolicyProvider(name)
-       {
-               int ret = ::wifi_manager_initialize(&handle);
-               if (ret != WIFI_MANAGER_ERROR_NONE) {
-                       if (ret == WIFI_MANAGER_ERROR_NOT_SUPPORTED)
-                               return;
-
-                       throw std::runtime_error("WiFi Manager initialization failed.");
-               }
-       }
+void SsidRestriction::onChanged(const PolicyValue& value)
+{
+       int enable = value;
+       INFO(VIST_PLUGIN) << "Wifi ssid restriction change state is changed to " << enable;
+}
 
-       ~Wifi()
-       {
-               if (handle == nullptr)
-                       return;
+Wifi::Wifi(const std::string& name) : PolicyProvider(name)
+{
+       int ret = ::wifi_manager_initialize(&handle);
+       if (ret != WIFI_MANAGER_ERROR_NONE) {
+               if (ret == WIFI_MANAGER_ERROR_NOT_SUPPORTED)
+                       ERROR("Wifi manager is not supported.");
 
-               ::wifi_manager_deinitialize(handle);
+               THROW(ErrCode::RuntimeError) << "Failed to init WiFi Manager.";
        }
+}
 
-private:
-       ::wifi_manager_h handle = nullptr;
-};
+Wifi::~Wifi()
+{
+       ::wifi_manager_deinitialize(handle);
+}
 
 // TODO(Sangwan): Add privilege to provider
 #define PRIVILEGE "http://tizen.org/privilege/dpm.wifi"
 
 extern "C" PolicyProvider* PolicyFactory()
 {
-       INFO(PLUGINS, "Wifi plugin loaded.");
+       INFO(VIST_PLUGIN) << "Wifi plugin loaded.";
        Wifi* provider = new Wifi("wifi");
-       provider->add(std::make_shared<ModeChange>());
+       provider->add(std::make_shared<WifiState>());
        provider->add(std::make_shared<ProfileChange>());
        provider->add(std::make_shared<Hotspot>());
        provider->add(std::make_shared<SsidRestriction>());
 
        return provider;
 }
+
+} // namespace plugin
+} // namespace policy
+} // namespace vist
diff --git a/plugins/wifi/wifi.hpp b/plugins/wifi/wifi.hpp
new file mode 100644 (file)
index 0000000..b076cbb
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  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 <vist/sdk/policy-model.hpp>
+#include <vist/sdk/policy-provider.hpp>
+#include <vist/sdk/policy-value.hpp>
+
+#include <wifi-manager.h>
+
+namespace vist {
+namespace policy {
+namespace plugin {
+
+struct WifiState : public PolicyModel {
+       WifiState() : PolicyModel("wifi", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+struct ProfileChange : public PolicyModel {
+       ProfileChange() : PolicyModel("wifi-profile-change", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+struct Hotspot : public PolicyModel {
+       Hotspot() : PolicyModel("wifi-hotspot", PolicyValue(1)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+struct SsidRestriction : public PolicyModel {
+       SsidRestriction() : PolicyModel("wifi-ssid-restriction", PolicyValue(0)) {}
+       void onChanged(const PolicyValue& value) override;
+};
+
+class Wifi final : public PolicyProvider {
+public:
+       Wifi(const std::string& name);
+       ~Wifi();
+
+private:
+       ::wifi_manager_h handle = nullptr;
+};
+
+} // namespace plugin
+} // namespace policy
+} // namespace vist
index 7ba5544..3d8041a 100644 (file)
@@ -22,6 +22,7 @@
 #include <osquery/tables.h>
 
 #include <vist/policy/api.hpp>
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 
 namespace osquery {
@@ -41,6 +42,8 @@ Row convert(const std::string& name, const vist::policy::PolicyValue& value)
 
 namespace tables {
 
+using namespace vist;
+
 QueryData genPolicy(QueryContext& context) try {
        INFO(VIST) << "Select query about policy table.";
 
@@ -63,8 +66,12 @@ QueryData genPolicy(QueryContext& context) try {
        }
 
        return results;
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to select query on policy.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
@@ -91,8 +98,12 @@ QueryData updatePolicy(QueryContext& context, const PluginRequest& request) try
        Row r;
        r["status"] = "success";
        return { r };
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to update query on policy.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
index 66aafe0..58a67f3 100644 (file)
 #include <osquery/tables.h>
 
 #include <vist/policy/api.hpp>
+#include <vist/exception.hpp>
 #include <vist/logger.hpp>
 
+namespace osquery {
+
 namespace {
 
 std::string getValue(std::string&& alias, const std::string& key)
@@ -52,9 +55,10 @@ std::string parseAdmin(const std::string& request, bool insert = true)
 
 } // anonymous namespace
 
-namespace osquery {
 namespace tables {
 
+using namespace vist;
+
 QueryData genPolicyAdmin(QueryContext& context) try {
        INFO(VIST) << "Select query about policy-admin table.";
 
@@ -88,8 +92,12 @@ QueryData genPolicyAdmin(QueryContext& context) try {
        }
 
        return results;
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to select query on policy-admin.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
@@ -106,8 +114,12 @@ QueryData insertPolicyAdmin(QueryContext& context, const PluginRequest& request)
        Row r;
        r["status"] = "success";
        return { r };
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to insert query on policy-admin.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
@@ -124,8 +136,12 @@ QueryData deletePolicyAdmin(QueryContext& context, const PluginRequest& request)
        Row r;
        r["status"] = "success";
        return { r };
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to delete query on policy-admin.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
@@ -152,11 +168,16 @@ QueryData updatePolicyAdmin(QueryContext& context, const PluginRequest& request)
        Row r;
        r["status"] = "success";
        return { r };
+} catch (const vist::Exception<ErrCode>& e) {
+       ERROR(VIST) << "Failed to query: " << e.what();
+       Row r;
+       return { r };
 } catch (...) {
-       ERROR(VIST) << "Failed to insert query on policy-admin.";
+       ERROR(VIST) << "Failed to query with unknown exception.";
        Row r;
        return { r };
 }
 
+
 } // namespace tables
 } // namespace osquery
index a64077a..6490941 100644 (file)
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <vist/exception.hpp>
+
 #include <map>
 #include <string>
 #include <vector>
@@ -59,11 +61,13 @@ public:
        inline std::size_t size() const { return rows.size(); }
 
        template<typename Struct, typename Member>
-       VirtualRow<T>& filter(Member Struct::*field, const std::string& value)
+       VirtualRow<T>& filter(Member Struct::*field, const std::string& name)
        {
                for (auto& row : this->rows)
-                       if (row[field] == value)
+                       if (row[field] == name)
                                return row;
+
+               THROW(ErrCode::RuntimeError) << "Not exist field: " << name;
        }
 
 private: