From 521d496449144e9850cdd44173fba932032b1ef4 Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Mon, 16 Sep 2019 14:30:31 +0900 Subject: [PATCH] Make apix-test stable Signed-off-by: Sangwan Kwon --- api/property.h | 4 + api/schema/memory-map.h | 33 ----- api/schema/processes.h | 13 +- src/apix/manager/manager_impl.cpp | 15 ++- src/apix/manager/tests/manager_tests.cpp | 16 +-- .../notification/tests/notification_tests.cpp | 12 +- src/apix/property/property.cpp | 30 +---- src/apix/property/tests/property_tests.cpp | 124 +++++++----------- 8 files changed, 83 insertions(+), 164 deletions(-) delete mode 100644 api/schema/memory-map.h diff --git a/api/property.h b/api/property.h index df4d9e1..07d2a7d 100644 --- a/api/property.h +++ b/api/property.h @@ -44,6 +44,8 @@ public: template Member operator[](Member Struct::*) const; + inline std::size_t size() const { return data.size(); } + private: KeyValuePair data; }; @@ -63,6 +65,8 @@ public: inline Iter end() { return datas.end(); } inline CIter end() const { return datas.end(); } + inline std::size_t size() const { return datas.size(); } + private: std::vector> datas; }; diff --git a/api/schema/memory-map.h b/api/schema/memory-map.h deleted file mode 100644 index bf5ee93..0000000 --- a/api/schema/memory-map.h +++ /dev/null @@ -1,33 +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 implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - - -/** - * @file memory-map.h - * @brief The scheme of memory map (sync with osquery/tables/spec/linux/memory_map.table) - */ - - -#pragma once - -#include - -struct MemoryMap { - int region; /// Region index - std::string type; /// Textual description - std::string start; /// Start address of memory region - std::string end; /// End address of memory region -}; diff --git a/api/schema/processes.h b/api/schema/processes.h index 5cd0519..d95dd63 100644 --- a/api/schema/processes.h +++ b/api/schema/processes.h @@ -26,7 +26,7 @@ #include struct Processes { - int pid; + long long int pid; std::string name; std::string path; std::string cmdline; @@ -34,12 +34,7 @@ struct Processes { long long int gid; long long int euid; long long int egid; - std::string on_disk; -// std::string wired_size; // It doen't supported - std::string resident_size; - std::string phys_footprint; - std::string user_time; - std::string system_time; - std::string start_time; - int parent; + int on_disk; + long long int resident_size; + long long int parent; }; diff --git a/src/apix/manager/manager_impl.cpp b/src/apix/manager/manager_impl.cpp index 55287c2..f0d1ee6 100644 --- a/src/apix/manager/manager_impl.cpp +++ b/src/apix/manager/manager_impl.cpp @@ -21,6 +21,7 @@ #include "manager_impl.h" +#include #include #include #include @@ -46,6 +47,8 @@ ManagerImpl::ManagerImpl() boost::filesystem::create_directories(logDir); LOG(INFO) << "Initalize osquery manager. "; + + registryAndPluginInit(); } ManagerImpl::~ManagerImpl() noexcept @@ -62,13 +65,13 @@ ManagerImpl& ManagerImpl::instance() Rows ManagerImpl::execute(const std::string& query) { LOG(INFO) << "Execute query: " << query; + osquery::SQL sql(query, true); + if (!sql.ok()) { + LOG(ERROR) << "Executing query failed: " << sql.getMessageString(); + return Rows(); + } - osquery::QueryData results; - auto status = osquery::query(query, results); - if (!status.ok()) - LOG(ERROR) << "Executing query failed: " << status.getCode(); - - return results; + return std::move(sql.rows()); } void ManagerImpl::subscribe(const std::string& table, const Callback& callback) diff --git a/src/apix/manager/tests/manager_tests.cpp b/src/apix/manager/tests/manager_tests.cpp index 7f4a3d9..30cac9a 100644 --- a/src/apix/manager/tests/manager_tests.cpp +++ b/src/apix/manager/tests/manager_tests.cpp @@ -30,18 +30,18 @@ TEST_F(ManagerTests, test_manager_execute) { auto rows = OsqueryManager::execute(query); EXPECT_EQ(rows.size(), 1); - VLOG(1) << "[Test] time table rows:"; - VLOG(1) << "\t hour: " << rows[0]["hour"]; - VLOG(1) << "\t minutes: " << rows[0]["minutes"]; - VLOG(1) << "\t seconds: " << rows[0]["seconds"]; + LOG(INFO) << "[Test] time table rows:"; + LOG(INFO) << "\t hour: " << rows[0]["hour"]; + LOG(INFO) << "\t minutes: " << rows[0]["minutes"]; + LOG(INFO) << "\t seconds: " << rows[0]["seconds"]; } TEST_F(ManagerTests, test_manager_subscribe) { int called = 0; auto callback = [&](const Row& row) { - VLOG(1) << "NotifyCallback called:"; + LOG(INFO) << "NotifyCallback called:"; for (const auto& r : row) - VLOG(1) << "\t" << r.first << " : " << r.second; + LOG(INFO) << "\t" << r.first << " : " << r.second; called++; }; @@ -63,7 +63,7 @@ TEST_F(ManagerTests, test_manager_columns) { auto columns = OsqueryManager::columns("time"); EXPECT_TRUE(columns.size() > 0); - VLOG(1) << "[Test] Enabled columns of time table:"; + LOG(INFO) << "[Test] Enabled columns of time table:"; for (const auto& c : columns) - VLOG(1) << "\t" << c; + LOG(INFO) << "\t" << c; } diff --git a/src/apix/notification/tests/notification_tests.cpp b/src/apix/notification/tests/notification_tests.cpp index f26bfa3..721bbf1 100644 --- a/src/apix/notification/tests/notification_tests.cpp +++ b/src/apix/notification/tests/notification_tests.cpp @@ -27,9 +27,9 @@ TEST_F(NotificationTests, test_add_positive) { auto& notifier = Notification::instance(); auto callback = [](const Row& row) { - VLOG(1) << "NotifyCallback called:"; + LOG(INFO) << "NotifyCallback called:"; for (const auto& r : row) - VLOG(1) << "\t" << r.first << " : " << r.second; + LOG(INFO) << "\t" << r.first << " : " << r.second; }; auto s = notifier.add("test", std::move(callback)); @@ -40,9 +40,9 @@ TEST_F(NotificationTests, test_add_negative) { auto& notifier = Notification::instance(); auto callback = [](const Row& row) { - VLOG(1) << "NotifyCallback called:"; + LOG(INFO) << "NotifyCallback called:"; for (const auto& r : row) - VLOG(1) << "\t" << r.first << " : " << r.second; + LOG(INFO) << "\t" << r.first << " : " << r.second; }; auto s = notifier.add("", std::move(callback)); @@ -54,9 +54,9 @@ TEST_F(NotificationTests, test_emit_positive) { int called = 0; auto callback = [&](const Row& row) { - VLOG(1) << "NotifyCallback called:"; + LOG(INFO) << "NotifyCallback called:"; for (const auto& r : row) - VLOG(1) << "\t" << r.first << " : " << r.second; + LOG(INFO) << "\t" << r.first << " : " << r.second; called++; }; diff --git a/src/apix/property/property.cpp b/src/apix/property/property.cpp index e06dce2..03a6dea 100644 --- a/src/apix/property/property.cpp +++ b/src/apix/property/property.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include @@ -52,12 +51,6 @@ auto processes = make_table("processes", make_column("euid", &Processes::euid), make_column("egid", &Processes::egid), make_column("on_disk", &Processes::on_disk), -// make_column("wired_size", &Processes::wired_size), - make_column("resident_size", &Processes::resident_size), - make_column("phys_footprint", &Processes::phys_footprint), - make_column("user_time", &Processes::user_time), - make_column("system_time", &Processes::system_time), - make_column("start_time", &Processes::start_time), make_column("parent", &Processes::parent)); auto users = make_table("users", @@ -75,13 +68,7 @@ auto groups = make_table("groups", make_column("gid_signed", &Groups::gid_signed), make_column("groupname", &Groups::groupname)); -auto memoryMap = make_table("memory_map", - make_column("region", &MemoryMap::region), - make_column("type", &MemoryMap::type), - make_column("start", &MemoryMap::start), - make_column("end", &MemoryMap::end)); - -auto db = make_database("db", time, processes, users, groups, memoryMap); +auto db = make_database("db", time, processes, users, groups); } // anonymous namespace @@ -120,9 +107,13 @@ Member Property::at(Member Struct::* field) const if (value.empty()) { LOG(ERROR) << "The value of key[" << key << "] is not exist."; return Member(); - } else { - /// TODO(Sangwan): Catch boost::bad_lexical_cast + } + + try { return boost::lexical_cast(value); + } catch (...) { + LOG(ERROR) << "Failed to casting [key]: " << key; + return Member(); } } @@ -174,11 +165,4 @@ template unsigned long long int Property::operator[](unsigned long long template std::string Property::at(std::string Groups::*) const; template std::string Property::operator[](std::string Groups::*) const; -template class Property; -template class Properties; -template int Property::at(int MemoryMap::*) const; -template int Property::operator[](int MemoryMap::*) const; -template std::string Property::at(std::string MemoryMap::*) const; -template std::string Property::operator[](std::string MemoryMap::*) const; - } // namespace osquery diff --git a/src/apix/property/tests/property_tests.cpp b/src/apix/property/tests/property_tests.cpp index 8038524..2d8d4df 100644 --- a/src/apix/property/tests/property_tests.cpp +++ b/src/apix/property/tests/property_tests.cpp @@ -24,7 +24,6 @@ #include #include #include -#include using namespace osquery; @@ -39,16 +38,16 @@ TEST_F(PropertyTests, property) { result.seconds = time.at(&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; + LOG(INFO) << "[Test] time table:"; + LOG(INFO) << "\t hour: " << result.hour; + LOG(INFO) << "\t minutes: " << result.minutes; + LOG(INFO) << "\t seconds: " << result.seconds; /// Each query execution - VLOG(1) << "[Test] time table:"; - VLOG(1) << "\t hour: " << Property