Make apix-test stable
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 16 Sep 2019 05:30:31 +0000 (14:30 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 24 Sep 2019 06:35:48 +0000 (15:35 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
api/property.h
api/schema/memory-map.h [deleted file]
api/schema/processes.h
src/apix/manager/manager_impl.cpp
src/apix/manager/tests/manager_tests.cpp
src/apix/notification/tests/notification_tests.cpp
src/apix/property/property.cpp
src/apix/property/tests/property_tests.cpp

index df4d9e1e09351a0454b2feabb2a04a827e1ae166..07d2a7d2746c32e55391d2e35f65affa73f01704 100644 (file)
@@ -44,6 +44,8 @@ public:
        template<typename Struct, typename Member>
        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<Property<T>> datas;
 };
diff --git a/api/schema/memory-map.h b/api/schema/memory-map.h
deleted file mode 100644 (file)
index bf5ee93..0000000
+++ /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 <string>
-
-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
-};
index 5cd05193bd490e4998b0670bcca4ac29e94536e1..d95dd63f39692a5b71509f0de95c382258c4794a 100644 (file)
@@ -26,7 +26,7 @@
 #include <string>
 
 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;
 };
index 55287c240eae91b827510cfa034990a9ccbf010c..f0d1ee69a6ad8f9b923e73ad742c0c3179b1b59a 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "manager_impl.h"
 
+#include <osquery/registry.h>
 #include <osquery/core.h>
 #include <osquery/filesystem/filesystem.h>
 #include <osquery/flags.h>
@@ -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)
index 7f4a3d9c5ad13634a00abdb41845325b23e4e076..30cac9a93dff68eed1cc383db54837c8d501f906 100644 (file)
@@ -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;
 }
index f26bfa3554fa22d16cc3c10aa9efa09b93ee13f3..721bbf18595b1963e190acba8fce204f9a28ae9a 100644 (file)
@@ -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++;
        };
 
index e06dce297f83f7caeeb1061fe065484757ea2d1e..03a6deae5ff7f64ec5cffb01588aceecdd9e30ba 100644 (file)
@@ -26,7 +26,6 @@
 #include <schema/processes.h>
 #include <schema/users.h>
 #include <schema/groups.h>
-#include <schema/memory-map.h>
 
 #include <osquery/logger.h>
 
@@ -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<T>::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<Member>(value);
+       } catch (...) {
+               LOG(ERROR) << "Failed to casting [key]: " << key;
+               return Member();
        }
 }
 
@@ -174,11 +165,4 @@ template unsigned long long int Property<Groups>::operator[](unsigned long long
 template std::string Property<Groups>::at(std::string Groups::*) const;
 template std::string Property<Groups>::operator[](std::string Groups::*) const;
 
-template class Property<MemoryMap>;
-template class Properties<MemoryMap>;
-template int Property<MemoryMap>::at(int MemoryMap::*) const;
-template int Property<MemoryMap>::operator[](int MemoryMap::*) const;
-template std::string Property<MemoryMap>::at(std::string MemoryMap::*) const;
-template std::string Property<MemoryMap>::operator[](std::string MemoryMap::*) const;
-
 } // namespace osquery
index 803852408d6328a5765a5b8be92519b38e3ffc66..2d8d4df7dec57cf42187c57c88028b2bc9758210 100644 (file)
@@ -24,7 +24,6 @@
 #include <schema/processes.h>
 #include <schema/users.h>
 #include <schema/groups.h>
-#include <schema/memory-map.h>
 
 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<Time>().at(&Time::hour);
-       VLOG(1) << "\t minutes: " << Property<Time>().at(&Time::minutes);
-       VLOG(1) << "\t seconds: " <<  Property<Time>().at(&Time::seconds);
+       LOG(INFO) << "[Test] time table:";
+       LOG(INFO) << "\t hour: " << Property<Time>().at(&Time::hour);
+       LOG(INFO) << "\t minutes: " << Property<Time>().at(&Time::minutes);
+       LOG(INFO) << "\t seconds: " <<  Property<Time>().at(&Time::seconds);
 
        EXPECT_NE(result.hour, -1);
        EXPECT_NE(result.minutes, -1);
@@ -64,10 +63,10 @@ TEST_F(PropertyTests, propertyArrayOp) {
        result.seconds = time[&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;
 
        EXPECT_NE(result.hour, -1);
        EXPECT_NE(result.minutes, -1);
@@ -75,28 +74,12 @@ TEST_F(PropertyTests, propertyArrayOp) {
 }
 
 TEST_F(PropertyTests, propertiesProcesses) {
-       Processes result = {
-               -1, /// pid
-               "", /// name
-               "", /// path
-               "", /// cmdline
-               -1, /// uid
-               -1, /// gid
-               -1, /// euid
-               -1, /// egid
-               "", /// on_disk
-//             "", /// wired_size
-               "", /// resident_size
-               "", /// phys_footprint
-               "", /// user_time
-               "", /// system_time
-               "", /// start_time
-               -1  /// parent
-               };
-
+       Processes result;
        Properties<Processes> processes;
+       EXPECT_TRUE(processes.size() > 0);
 
        for(auto& p : processes) {
+               EXPECT_TRUE(p.size() > 0);
                result.pid = p.at(&Processes::pid);
                result.name = p.at(&Processes::name);
                result.path = p.at(&Processes::path);
@@ -106,66 +89,49 @@ TEST_F(PropertyTests, propertiesProcesses) {
                result.euid = p.at(&Processes::euid);
                result.egid = p.at(&Processes::egid);
                result.on_disk = p.at(&Processes::on_disk);
-//             result.wired_size = p.at(&Processes::wired_size);
-               result.resident_size = p.at(&Processes::resident_size);
-               result.phys_footprint = p.at(&Processes::phys_footprint);
-               result.user_time = p.at(&Processes::user_time);
-               result.system_time = p.at(&Processes::system_time);
-               result.start_time = p.at(&Processes::start_time);
                result.parent = p.at(&Processes::parent);
 
-               VLOG(1) << "[Test] Processes table:";
-               VLOG(1) << "\t pid: " << result.pid;
-               VLOG(1) << "\t name: " << result.name;
-               VLOG(1) << "\t path: " << result.path;
-               VLOG(1) << "\t cmdline: " << result.cmdline;
-               VLOG(1) << "\t uid: " << result.uid;
-               VLOG(1) << "\t gid: " << result.gid;
-               VLOG(1) << "\t euid: " << result.euid;
-               VLOG(1) << "\t egid: " << result.egid;
-               VLOG(1) << "\t on_disk: " << result.on_disk;
-//             VLOG(1) << "\t wired_size: " << result.wired_size;
-               VLOG(1) << "\t resident_size: " << result.resident_size;
-               VLOG(1) << "\t phys_footprint: " << result.phys_footprint;
-               VLOG(1) << "\t user_time: " << result.user_time;
-               VLOG(1) << "\t system_time: " << result.system_time;
-               VLOG(1) << "\t start_time: " << result.start_time;
-               VLOG(1) << "\t parent: " << result.parent;
+               LOG(INFO) << "[Test] Processes table:";
+               LOG(INFO) << "\t pid: " << result.pid;
+               LOG(INFO) << "\t name: " << result.name;
+               LOG(INFO) << "\t path: " << result.path;
+               LOG(INFO) << "\t cmdline: " << result.cmdline;
+               LOG(INFO) << "\t uid: " << result.uid;
+               LOG(INFO) << "\t gid: " << result.gid;
+               LOG(INFO) << "\t euid: " << result.euid;
+               LOG(INFO) << "\t egid: " << result.egid;
+               LOG(INFO) << "\t on_disk: " << result.on_disk;
+               LOG(INFO) << "\t parent: " << result.parent;
        }
 }
 
 TEST_F(PropertyTests, propertiesUsers) {
        Properties<Users> users;
+       EXPECT_TRUE(users.size() > 0);
+
        for(const auto& user : users) {
-               VLOG(1) << "[Test] User table:";
-               VLOG(1) << "\t uid: " << user[&Users::uid];
-               VLOG(1) << "\t gid: " << user[&Users::gid];
-               VLOG(1) << "\t uid_signed: " << user[&Users::uid_signed];
-               VLOG(1) << "\t gid_signed: " << user[&Users::gid_signed];
-               VLOG(1) << "\t username: " << user[&Users::username];
-               VLOG(1) << "\t description: " << user[&Users::description];
-               VLOG(1) << "\t directory: " << user[&Users::directory];
-               VLOG(1) << "\t shell: " << user[&Users::shell];
+               EXPECT_TRUE(user.size() > 0);
+               LOG(INFO) << "[Test] User table:";
+               LOG(INFO) << "\t uid: " << user[&Users::uid];
+               LOG(INFO) << "\t gid: " << user[&Users::gid];
+               LOG(INFO) << "\t uid_signed: " << user[&Users::uid_signed];
+               LOG(INFO) << "\t gid_signed: " << user[&Users::gid_signed];
+               LOG(INFO) << "\t username: " << user[&Users::username];
+               LOG(INFO) << "\t description: " << user[&Users::description];
+               LOG(INFO) << "\t directory: " << user[&Users::directory];
+               LOG(INFO) << "\t shell: " << user[&Users::shell];
        }
 }
 
 TEST_F(PropertyTests, propertiesGroups) {
        Properties<Groups> groups;
-       for(const auto& group : groups) {
-               VLOG(1) << "[Test] Group table:";
-               VLOG(1) << "\t gid: " << group[&Groups::gid];
-               VLOG(1) << "\t gid_signed: " << group[&Groups::gid_signed];
-               VLOG(1) << "\t groupname: " << group[&Groups::groupname];
-       }
-}
+       EXPECT_TRUE(groups.size() > 0);
 
-TEST_F(PropertyTests, propertiesMemoryMap) {
-       Properties<MemoryMap> memoryMap;
-       for(const auto& mm : memoryMap) {
-               VLOG(1) << "[Test] memory_map table:";
-               VLOG(1) << "\t region: " << mm[&MemoryMap::region];
-               VLOG(1) << "\t type: " << mm[&MemoryMap::type];
-               VLOG(1) << "\t start: " << mm[&MemoryMap::start];
-               VLOG(1) << "\t end: " << mm[&MemoryMap::end];
+       for(const auto& group : groups) {
+               EXPECT_TRUE(group.size() > 0);
+               LOG(INFO) << "[Test] Group table:";
+               LOG(INFO) << "\t gid: " << group[&Groups::gid];
+               LOG(INFO) << "\t gid_signed: " << group[&Groups::gid_signed];
+               LOG(INFO) << "\t groupname: " << group[&Groups::groupname];
        }
 }