From: Sangwan Kwon Date: Mon, 10 Aug 2020 07:34:44 +0000 (+0900) Subject: Apply schema API to bluetooth-policy table X-Git-Tag: submit/tizen/20200819.001851~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0115cd7c02bec50d346248811140376f54e807ed;p=platform%2Fcore%2Fsecurity%2Fvist.git Apply schema API to bluetooth-policy table [example] - bluetooth.select(Bluetooth::State); - bluetooth.update(Bluetooth::State = 1, Bluetooth::Pairing = 0); Change-Id: Ia27ef58e44e311dea3652f8bd63f22cd77db5b92 Signed-off-by: Sangwan Kwon --- diff --git a/src/vist/schema/bluetooth.hpp b/src/vist/schema/bluetooth.hpp deleted file mode 100644 index 18ce416..0000000 --- a/src/vist/schema/bluetooth.hpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2020-present 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 - -namespace vist { -namespace schema { - -struct Bluetooth { - int state; - int desktopConnectivity; - int pairing; - int tethering; - - DECLARE_COLUMN(State, "state", &Bluetooth::state); - DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::desktopConnectivity); - DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing); - DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering); -}; - -DECLARE_TABLE(BluetoothTable, "bluetooth", Bluetooth::State, - Bluetooth::DesktopConnectivity, - Bluetooth::Pairing, - Bluetooth::Tethering); - -} // namesapce schema -} // namesapce vist diff --git a/tables/policy/bluetooth/policy.cpp b/tables/policy/bluetooth/policy.cpp index 49ba0b4..b90a05e 100644 --- a/tables/policy/bluetooth/policy.cpp +++ b/tables/policy/bluetooth/policy.cpp @@ -45,7 +45,13 @@ auto bt_allowed(int value) } // anonymous namespace -void BluetoothState::onChanged(const PolicyValue& value) + +BluetoothProvider::State::State() : + PolicyModel(GetPolicyName(schema::Bluetooth::State), PolicyValue(1)) +{ +} + +void BluetoothProvider::State::onChanged(const PolicyValue& value) { auto enable = bt_allowed(value); auto ret = ::bluetooth_dpm_set_allow_mode(enable); @@ -55,7 +61,12 @@ void BluetoothState::onChanged(const PolicyValue& value) INFO(VIST_PLUGIN) << "Bluetooth state is changed to " << enable; } -void DesktopConnectivity::onChanged(const PolicyValue& value) +BluetoothProvider::DesktopConnectivity::DesktopConnectivity() : + PolicyModel(GetPolicyName(schema::Bluetooth::DesktopConnectivity), PolicyValue(1)) +{ +} + +void BluetoothProvider::DesktopConnectivity::onChanged(const PolicyValue& value) { auto enable = allowed(value); auto ret = ::bluetooth_dpm_set_desktop_connectivity_state(enable); @@ -65,7 +76,12 @@ void DesktopConnectivity::onChanged(const PolicyValue& value) INFO(VIST_PLUGIN) << "Bluetooth desktop connectivity state is changed to " << enable; } -void Pairing::onChanged(const PolicyValue& value) +BluetoothProvider::Pairing::Pairing() : + PolicyModel(GetPolicyName(schema::Bluetooth::Pairing), PolicyValue(1)) +{ +} + +void BluetoothProvider::Pairing::onChanged(const PolicyValue& value) { auto enable = allowed(value); auto ret = ::bluetooth_dpm_set_pairing_state(enable); @@ -75,19 +91,24 @@ void Pairing::onChanged(const PolicyValue& value) INFO(VIST_PLUGIN) << "Bluetooth pairing state is changed to " << enable; } -void Tethering::onChanged(const PolicyValue& value) +BluetoothProvider::Tethering::Tethering() : + PolicyModel(GetPolicyName(schema::Bluetooth::Tethering), PolicyValue(1)) +{ +} + +void BluetoothProvider::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) +BluetoothProvider::BluetoothProvider(const std::string& name) : PolicyProvider(name) { if (::bt_initialize() != BT_ERROR_NONE) THROW(ErrCode::RuntimeError) << "Failed to init bluetooth provider."; } -Bluetooth::~Bluetooth() +BluetoothProvider::~BluetoothProvider() { ::bt_deinitialize(); } diff --git a/tables/policy/bluetooth/policy.hpp b/tables/policy/bluetooth/policy.hpp index 3c927df..a83f47c 100644 --- a/tables/policy/bluetooth/policy.hpp +++ b/tables/policy/bluetooth/policy.hpp @@ -20,38 +20,42 @@ #include #include +#include "schema.hpp" + namespace vist { namespace policy { -class BluetoothState : public PolicyModel { +class BluetoothProvider final : public PolicyProvider { public: - BluetoothState() : PolicyModel("bluetooth", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; + BluetoothProvider(const std::string& name); + ~BluetoothProvider(); -class DesktopConnectivity : public PolicyModel { -public: - DesktopConnectivity() : PolicyModel("bluetooth-desktop-connectivity", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; + struct State : public PolicyModel { + State(); + void onChanged(const PolicyValue& value) override; + }; -class Pairing: public PolicyModel { -public: - Pairing() : PolicyModel("bluetooth-pairing", PolicyValue(1)) {} - void onChanged(const PolicyValue& value) override; -}; + struct DesktopConnectivity : public PolicyModel { + DesktopConnectivity(); + void onChanged(const PolicyValue& value) override; + }; -class Tethering: public PolicyModel { -public: - Tethering() : PolicyModel("bluetooth-tethering", PolicyValue(1)) {} - void onChanged(const PolicyValue&); -}; + struct Pairing: public PolicyModel { + Pairing(); + void onChanged(const PolicyValue& value) override; + }; + + struct Tethering: public PolicyModel { + Tethering(); + void onChanged(const PolicyValue&) override; + }; -class Bluetooth final : public PolicyProvider { -public: - Bluetooth(const std::string& name); - ~Bluetooth(); }; +template +std::string GetPolicyName(const Column& column) { + return schema::bluetooth.name + "-" + column.name; +} + } // namespace policy } // namespace vist diff --git a/tables/policy/bluetooth/schema.hpp b/tables/policy/bluetooth/schema.hpp new file mode 100644 index 0000000..d422c20 --- /dev/null +++ b/tables/policy/bluetooth/schema.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020-present 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 + +namespace vist { +namespace schema { + +struct Bluetooth { + int state; + int desktopConnectivity; + int pairing; + int tethering; + + DECLARE_COLUMN(State, "state", &Bluetooth::state); + DECLARE_COLUMN(DesktopConnectivity, "desktopConnectivity", &Bluetooth::desktopConnectivity); + DECLARE_COLUMN(Pairing, "pairing", &Bluetooth::pairing); + DECLARE_COLUMN(Tethering, "tethering", &Bluetooth::tethering); +}; + +DECLARE_TABLE(bluetooth, "bluetooth", Bluetooth::State, + Bluetooth::DesktopConnectivity, + Bluetooth::Pairing, + Bluetooth::Tethering); + +} // namesapce schema +} // namesapce vist diff --git a/tables/policy/bluetooth/table.cpp b/tables/policy/bluetooth/table.cpp index 6bedb72..060b3ee 100644 --- a/tables/policy/bluetooth/table.cpp +++ b/tables/policy/bluetooth/table.cpp @@ -34,29 +34,29 @@ namespace table { namespace { -std::map ALIAS = { - { "state", "bluetooth" }, - { "desktopConnectivity", "bluetooth-desktop-connectivity" }, - { "pairing", "bluetooth-pairing" }, - { "tethering", "bluetooth-tethering"} -}; - void setPolicy(const std::string& name, int value) { vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value)); } +std::string getPolicy(const std::string& name) +{ + int value = vist::policy::API::Get(name); + return std::to_string(value); +} + } // anonymous namespace void BluetoothTable::init() { Builder::table("bluetooth"); - auto provider = std::make_shared("bluetooth"); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); - provider->add(std::make_shared()); + using namespace vist::policy; + auto provider = std::make_shared("bluetooth"); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); + provider->add(std::make_shared()); policy::API::Admin::AddProvider(provider); @@ -65,11 +65,13 @@ void BluetoothTable::init() TableColumns BluetoothTable::columns() const { + // Define Virtual table's schema (column) + using namespace schema; return { - Builder::column("state"), - Builder::column("desktopConnectivity"), - Builder::column("pairing"), - Builder::column("tethering") + Builder::column(Bluetooth::State.name), + Builder::column(Bluetooth::DesktopConnectivity.name), + Builder::column(Bluetooth::Pairing.name), + Builder::column(Bluetooth::Tethering.name) }; } @@ -79,11 +81,16 @@ QueryData BluetoothTable::select(QueryContext&) INFO(VIST) << "Select query about bluetooth table."; + using namespace schema; + using namespace policy; + // Policy name format: bluetooth-xxx + // Virtual table column name formant: xxx Row row; - for (const auto& [schemaName, policyName] : ALIAS) { - int value = vist::policy::API::Get(policyName); - row[schemaName] = std::to_string(value); - } + row[Bluetooth::State.name] = getPolicy(GetPolicyName(Bluetooth::State)); + row[Bluetooth::DesktopConnectivity.name] = + getPolicy(GetPolicyName(Bluetooth::DesktopConnectivity)); + row[Bluetooth::Pairing.name] = getPolicy(GetPolicyName(Bluetooth::Pairing)); + row[Bluetooth::Tethering.name] = getPolicy(GetPolicyName(Bluetooth::Tethering)); QueryData results; results.emplace_back(std::move(row)); @@ -99,11 +106,12 @@ QueryData BluetoothTable::update(QueryContext&, const PluginRequest& request) INFO(VIST) << "Update query about bluetooth table."; - /// TODO(Sangwan): Sync vtab schema with policy definition - setPolicy("bluetooth", Parser::column(request, 0)); - setPolicy("bluetooth-desktop-connectivity", Parser::column(request, 1)); - setPolicy("bluetooth-pairing", Parser::column(request, 2)); - setPolicy("bluetooth-tethering", Parser::column(request, 3)); + using namespace schema; + using namespace policy; + setPolicy(GetPolicyName(Bluetooth::State), Parser::column(request, 0)); + setPolicy(GetPolicyName(Bluetooth::DesktopConnectivity), Parser::column(request, 1)); + setPolicy(GetPolicyName(Bluetooth::Pairing), Parser::column(request, 2)); + setPolicy(GetPolicyName(Bluetooth::Tethering), Parser::column(request, 3)); return success(); diff --git a/tables/policy/bluetooth/tests/bluetooth.cpp b/tables/policy/bluetooth/tests/bluetooth.cpp index d682805..920f2f6 100644 --- a/tables/policy/bluetooth/tests/bluetooth.cpp +++ b/tables/policy/bluetooth/tests/bluetooth.cpp @@ -15,6 +15,7 @@ */ #include "../policy.hpp" +#include "../schema.hpp" #include #include @@ -45,10 +46,10 @@ void change_policy_state() TEST(BluetoothTests, change_policy_state) { try { - change_policy_state(); - change_policy_state(); - change_policy_state(); - change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); + change_policy_state(); } catch(const vist::Exception& e) { EXPECT_TRUE(false) << e.what(); } @@ -81,3 +82,27 @@ TEST(BluetoothTests, set_policies) Query::Execute("DELETE FROM policy_admin WHERE name = 'vist-bluetooth-policy-test'"); } + +TEST(BluetoothTests, schema) +{ + using namespace vist::schema; + std::string select1 = bluetooth.selectAll(); + EXPECT_EQ(select1, "SELECT * FROM bluetooth"); + + std::string select2 = bluetooth.select(Bluetooth::State); + EXPECT_EQ(select2, "SELECT state FROM bluetooth"); + + std::string update1 = bluetooth.update(Bluetooth::State = 1); + EXPECT_EQ(update1, "UPDATE bluetooth SET state = 1"); + + std::string update2 = bluetooth.update(Bluetooth::State = 1, Bluetooth::Pairing = 0); + EXPECT_EQ(update2, "UPDATE bluetooth SET state = 1, pairing = 0"); +} + +TEST(BluetoothTests, type_safe_query) +{ + std::string query = schema::bluetooth.selectAll(); + auto rows = Query::Execute(query); + + EXPECT_TRUE(rows.size() == 1); +}