Add Property API for type-safe getter
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 2 Jul 2019 23:50:21 +0000 (08:50 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 9 Jul 2019 23:56:30 +0000 (08:56 +0900)
-- Time table schema --
struct time {
  int hour;
  int minutes;
  int seconds;
};

-- API usage --
Property<Time> time;
Time result = {
  time[&Time::hour];
  time[&Time::minutes];
  time[&Time::seconds];
};

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
CMakeLists.txt
api/property.h [new file with mode: 0644]
api/schema/time.h [new file with mode: 0644]
osquery/CMakeLists.txt
osquery/property/CMakeLists.txt [new file with mode: 0644]
osquery/property/property.cpp [new file with mode: 0644]
osquery/property/property_tests.cpp [new file with mode: 0644]
tsqb/CMakeLists.txt
tsqb/tsqb.hxx

index ac19d8e8ff2572e8e43a00ca60aae05dcf27df69..7cceea9720b1ca33e79bb997bdb03be0ee0f93bc 100644 (file)
@@ -45,6 +45,7 @@ INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}")
 INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/api")
 INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include")
 INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/sqlite3")
+INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/tsqb")
 INCLUDE_DIRECTORIES("/usr/local/include")
 
 ENABLE_TESTING()
diff --git a/api/property.h b/api/property.h
new file mode 100644 (file)
index 0000000..8faa7c2
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ *  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 property.h
+ * @brief Provides type-safe getter method
+ */
+
+
+#pragma once
+
+#include <string>
+#include <map>
+#include <stdexcept>
+
+namespace osquery {
+
+/// TBD: Consider error handling.
+template <typename T>
+class Property {
+public:
+       explicit Property();
+
+       template<typename Struct, typename Member>
+       Member get(Member Struct::*);
+
+private:
+       std::map<std::string, std::string> data;
+};
+
+} // namespace osquery
diff --git a/api/schema/time.h b/api/schema/time.h
new file mode 100644 (file)
index 0000000..18a8034
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ *  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 time.h
+ * @brief The scheme of time (sync with osquery/tables/spec/time.table)
+ */
+
+
+#pragma once
+
+struct Time {
+       int hour;
+       int minutes;
+       int seconds;
+};
index 13924a6cc9d7711101335c47682493113eff34c9..e07b096bcd51e6163aaa45fcff09dd7a04102e6a 100644 (file)
@@ -98,6 +98,7 @@ ADD_SUBDIRECTORY(tables)
 # tizen feature
 ADD_SUBDIRECTORY(manager)
 ADD_SUBDIRECTORY(notification)
+ADD_SUBDIRECTORY(property)
 
 ## Table generation #############################################################
 FILE(GLOB TABLE_FILES "tables/specs/*.table")
diff --git a/osquery/property/CMakeLists.txt b/osquery/property/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4e8fe1e
--- /dev/null
@@ -0,0 +1,17 @@
+#  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
+
+ADD_OSQUERY_LIBRARY(TRUE osquery_property property.cpp)
+
+ADD_OSQUERY_TEST(TRUE osquery_property_tests property_tests.cpp)
diff --git a/osquery/property/property.cpp b/osquery/property/property.cpp
new file mode 100644 (file)
index 0000000..2c4e09b
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  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 property.cpp
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @brief Implementation of Property
+ */
+
+#include <osquery_manager.h>
+#include <property.h>
+#include <schema/time.h>
+
+#include <tsqb.hxx>
+
+#include <boost/lexical_cast.hpp>
+
+namespace {
+
+using namespace tsqb;
+auto time = make_table("time",
+                                          make_column("hour", &Time::hour),
+                                          make_column("minutes", &Time::minutes),
+                                          make_column("seconds", &Time::seconds));
+
+auto db = make_database("db", time);
+
+} // anonymous namespace
+
+namespace osquery {
+
+template <typename T>
+Property<T>::Property()
+{
+       auto results = OsqueryManager::execute(db.selectAll<T>());
+       if (results.size() > 0)
+               this->data = std::move(results[0]);
+}
+
+template <typename T>
+template<typename Struct, typename Member>
+Member Property<T>::get(Member Struct::* field)
+{
+       if (this->data.size() == 0)
+               throw std::runtime_error("Data is not exist.");
+
+       std::string key = db.getColumnName(field);
+       if (key.empty())
+               throw std::runtime_error("Key is not exist.");
+
+       /// Convert "table.column" to "column"
+       std::size_t pos = key.find(".");
+       if (pos != std::string::npos && pos != key.size() - 1)
+               key = key.substr(pos + 1);
+
+       std::string value = this->data[key];
+       if (value.empty())
+               throw std::runtime_error("Value is not exist.");
+
+       /// TODO(Sangwan): Catch boost::bad_lexical_cast
+       return boost::lexical_cast<Member>(value);
+}
+
+/// Explicit instantiation
+template class Property<Time>;
+template int Property<Time>::get(int Time::*);
+
+} // namespace osquery
diff --git a/osquery/property/property_tests.cpp b/osquery/property/property_tests.cpp
new file mode 100644 (file)
index 0000000..26de243
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  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>
+
+#include <osquery/logger.h>
+
+#include <property.h>
+#include <schema/time.h>
+
+using namespace osquery;
+
+class PropertyTests : public testing::Test {};
+
+TEST_F(PropertyTests, get) {
+       Time result = { -1, -1, -1 };
+
+       Property<Time> time;
+       result.hour = time.get(&Time::hour);
+       result.minutes = time.get(&Time::minutes);
+       result.seconds = time.get(&Time::seconds);
+
+       /// Once query execution
+       VLOG(1) << "[Test] time table:";
+       VLOG(1) << "\t hour: " << result.hour;
+       VLOG(1) << "\t minutes: " << result.minutes;
+       VLOG(1) << "\t seconds: " << result.seconds;
+
+       /// Each query execution
+       VLOG(1) << "[Test] time table:";
+       VLOG(1) << "\t hour: " << Property<Time>().get(&Time::hour);
+       VLOG(1) << "\t minutes: " << Property<Time>().get(&Time::minutes);
+       VLOG(1) << "\t seconds: " <<  Property<Time>().get(&Time::seconds);
+
+       EXPECT_NE(result.hour, -1);
+       EXPECT_NE(result.minutes, -1);
+       EXPECT_NE(result.seconds, -1);
+}
+
+int main(int argc, char* argv[]) {
+       testing::InitGoogleTest(&argc, argv);
+       return RUN_ALL_TESTS();
+}
index 00e8ef459518e84e4af7cbd1b2c2dc020ee37900..4c77699b5ae16714105eac3a624fcca1ea49915e 100644 (file)
@@ -12,7 +12,4 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License
 
-
-INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include")
-
 ADD_OSQUERY_TEST(FALSE tsqb_tests tsqb-tests.cpp)
index 30ee01087ed55b9cfb900855bcc50e8e4db8e4e6..c3791f071a84e6cb3777a5d4ce6b385c4cab9918 100644 (file)
@@ -21,9 +21,9 @@
 
 #pragma once
 
-#include "database.hxx"
-#include "table.hxx"
-#include "column.hxx"
-#include "expression.hxx"
-#include "condition.hxx"
-#include "util.hxx"
+#include "include/database.hxx"
+#include "include/table.hxx"
+#include "include/column.hxx"
+#include "include/expression.hxx"
+#include "include/condition.hxx"
+#include "include/util.hxx"