Fix client API to support generic type
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 25 Nov 2019 09:02:53 +0000 (18:02 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Thu, 28 Nov 2019 04:36:35 +0000 (13:36 +0900)
- example -
VirtualTable<Policy<int>> table;
for(const auto& row : table) {
  Policy<int> policy = { row[&Policy<int>::name], row[&Policy<int>::value] };
  INFO(VIST_CLIENT) << "Policy table:";
  INFO(VIST_CLIENT) << "\t name: " << policy.name;
  INFO(VIST_CLIENT) << "\t value: " << policy.value;
}

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
specs/tizen/policy.table
src/osquery/tables/tizen/policy.cpp
src/vist/client/schema/policy.hpp
src/vist/client/tests/virtual-table.cpp
src/vist/client/virtual-table.cpp
src/vist/service/tests/core.cpp

index b51be812f20862d367e243fef69596da9becd9d3..32eaa4fd6fffca213afaf323df791df3f078b01a 100644 (file)
@@ -8,5 +8,5 @@ implementation("tizen/policy@genPolicy")
 implementation_update("tizen/policy@updatePolicy")
 examples([
   "select * from policy where name = 'bluetooth'",
-  "update policy set value = 1 where name = 'bluetooth'",
+  "update policy set value = 'I/1' where name = 'bluetooth'",
 ])
index e29a5ce3cfdd7943777ad9bf1380c871066e7209..083a409d3a4efeed81b7e83a186422233d2c95f0 100644 (file)
@@ -35,22 +35,20 @@ QueryData genPolicy(QueryContext& context) try {
                auto names = context.constraints["name"].getAll(EQUALS);
                for (const auto& name : names) {
                        auto ret = vist::policy::API::Get(name);
-                       int value = ret;
 
                        Row r;
                        r["name"] = TEXT(name);
-                       r["value"] = TEXT(value);
+                       r["value"] = TEXT(ret.dump());
 
                        results.emplace_back(std::move(r));
                }
        } else { /// select *;
                auto policies = vist::policy::API::GetAll();
                for (auto& policy : policies) {
-                       int value = policy.second;
-
                        Row r;
+                       INFO(VIST) << "***";
                        r["name"] = TEXT(policy.first);
-                       r["value"] = TEXT(value);
+                       r["value"] = TEXT(policy.second.dump());
 
                        results.emplace_back(std::move(r));
                }
@@ -78,9 +76,9 @@ QueryData updatePolicy(QueryContext& context, const PluginRequest& request) try
                throw std::runtime_error("Wrong request format.");
 
        std::string name = document[0].GetString();
-       int value = std::stoi(document[1].GetString());
+       std::string dumpedValue = document[1].GetString();
 
-       vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(value));
+       vist::policy::API::Admin::Set(name, vist::policy::PolicyValue(dumpedValue, true));
 
        Row r;
        r["status"] = "success";
index 2d33fc4952f505242351d4cda7d91373dcdd8138..92eb521da5fc3b01a0a997a67ec4dfb6203e6adb 100644 (file)
 namespace vist {
 namespace schema {
 
+template <typename T>
 struct Policy {
        std::string     name;
-       std::string value;
+       T value;
 };
 
 } // namesapce schema
index 959df7516fd955ebc88a9b2c67c99b29a7848578..5930c5a4a4b66890a816b2f71e7b7d7b40375a8c 100644 (file)
@@ -106,11 +106,11 @@ TEST(VirtualTableTests, processes_table) {
 }
 
 TEST(VirtualTableTests, policy_table) {
-       VirtualTable<Policy> table;
+       VirtualTable<Policy<int>> table;
        EXPECT_TRUE(table.size() > 0);
 
        for(const auto& row : table) {
-               Policy policy = { row[&Policy::name], row[&Policy::value] };
+               Policy<int> policy = { row[&Policy<int>::name], row[&Policy<int>::value] };
 
                INFO(VIST_CLIENT) << "[Test] Policy table:";
                INFO(VIST_CLIENT) << "\t name: " << policy.name;
index fe84e2d44d30cd272e18ea76c68fe5d8e7fca89d..8dd6e0cfa18c767d6668e5519765cf179ff1664b 100644 (file)
@@ -22,6 +22,7 @@
 #include <vist/client/schema/time.hpp>
 
 #include <vist/exception.hpp>
+#include <vist/stringfy.hpp>
 #include <vist/logger.hpp>
 #include <vist/query-builder.hpp>
 
@@ -50,8 +51,8 @@ auto processes = make_table("processes",
                                                        make_column("parent", &Processes::parent));
 
 auto policy = make_table("policy",
-                                                make_column("name", &Policy::name),
-                                                make_column("value", &Policy::value));
+                                                make_column("name", &Policy<int>::name),
+                                                make_column("value", &Policy<int>::value));
 
 auto metaDB = make_database("db", time, processes, policy);
 
@@ -94,6 +95,9 @@ Member VirtualRow<T>::at(Member Struct::* field) const
                return Member();
        }
 
+       if (std::is_same<T, Policy<int>>::value && key == "value")
+               return static_cast<Member>(Stringfy::Restore(value));
+
        try {
                return boost::lexical_cast<Member>(value);
        } catch (...) {
@@ -103,7 +107,7 @@ Member VirtualRow<T>::at(Member Struct::* field) const
 }
 
 template <typename T>
-template<typename Struct, typename Member>
+template <typename Struct, typename Member>
 Member VirtualRow<T>::operator[](Member Struct::*field) const
 {
        return this->at(field);
@@ -132,9 +136,13 @@ template long long int VirtualRow<Processes>::operator[](long long int Processes
 template std::string VirtualRow<Processes>::at(std::string Processes::*) const;
 template std::string VirtualRow<Processes>::operator[](std::string Processes::*) const;
 
-template class VirtualTable<Policy>;
-template class VirtualRow<Policy>;
-template std::string VirtualRow<Policy>::at(std::string Policy::*) const;
-template std::string VirtualRow<Policy>::operator[](std::string Policy::*) const;
+template class VirtualTable<Policy<int>>;
+template class VirtualRow<Policy<int>>;
+/// name column
+template std::string VirtualRow<Policy<int>>::at(std::string Policy<int>::*) const;
+template std::string VirtualRow<Policy<int>>::operator[](std::string Policy<int>::*) const;
+/// value<T> column
+template int VirtualRow<Policy<int>>::at(int Policy<int>::*) const;
+template int VirtualRow<Policy<int>>::operator[](int Policy<int>::*) const;
 
 } // namespace vist
index 8ba411b29b553ed16edced610bd87a302ead4719..dae3bf71ac978ae7695cd5febed50d0caaf8adf3 100644 (file)
@@ -47,15 +47,15 @@ TEST_F(CoreTests, query_update)
        std::string statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
        auto rows = Vist::Query(statement);
        /// Initial policy value
-       EXPECT_EQ(rows[0]["value"], std::to_string(1));
+       EXPECT_EQ(rows[0]["value"], "I/1");
 
-       statement = "UPDATE policy SET value = '3' WHERE name = 'bluetooth'";
+       statement = "UPDATE policy SET value = 'I/3' WHERE name = 'bluetooth'";
        rows = Vist::Query(statement);
        EXPECT_EQ(rows.size(), 0);
 
        statement = "SELECT * FROM policy WHERE name = 'bluetooth'";
        rows = Vist::Query(statement);
-       EXPECT_EQ(rows[0]["value"], std::to_string(3));
+       EXPECT_EQ(rows[0]["value"], "I/3");
 
        policy::API::Admin::Disenroll("admin");
 }