osquery-1/osquery/database/tests/query_tests.cpp

121 lines
4.0 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
2015-04-27 09:12:58 +00:00
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-07-31 00:35:19 +00:00
#include <algorithm>
#include <ctime>
#include <deque>
#include <boost/filesystem/operations.hpp>
2014-07-31 00:35:19 +00:00
#include <gtest/gtest.h>
#include "osquery/core/test_util.h"
2015-05-24 01:52:42 +00:00
#include "osquery/database/query.h"
2014-07-31 00:35:19 +00:00
2014-09-30 02:06:33 +00:00
const std::string kTestingQueryDBPath = "/tmp/rocksdb-osquery-querytests";
2014-08-15 07:25:30 +00:00
namespace osquery {
2014-07-31 00:35:19 +00:00
class QueryTests : public testing::Test {
2014-09-30 02:06:33 +00:00
public:
2015-04-27 21:57:04 +00:00
void SetUp() { db_ = DBHandle::getInstanceAtPath(kTestingQueryDBPath); }
2015-04-27 09:12:58 +00:00
void TearDown() { boost::filesystem::remove_all(kTestingQueryDBPath); }
public:
2015-04-27 21:57:04 +00:00
std::shared_ptr<DBHandle> db_;
};
2014-07-31 00:35:19 +00:00
TEST_F(QueryTests, test_private_members) {
auto query = getOsqueryScheduledQuery();
auto cf = Query("foobar", query);
2014-07-31 00:35:19 +00:00
EXPECT_EQ(cf.query_, query);
}
TEST_F(QueryTests, test_add_and_get_current_results) {
2015-04-27 21:57:04 +00:00
// Test adding a "current" set of results to a scheduled query instance.
2014-07-31 00:35:19 +00:00
auto query = getOsqueryScheduledQuery();
auto cf = Query("foobar", query);
2015-04-27 21:57:04 +00:00
auto status = cf.addNewResults(getTestDBExpectedResults(), db_);
EXPECT_TRUE(status.ok());
EXPECT_EQ(status.toString(), "OK");
// Simulate results from several schedule runs, calculate differentials.
2014-07-31 00:35:19 +00:00
for (auto result : getTestDBResultStream()) {
2015-04-27 21:57:04 +00:00
// Get the results from the previous query execution (from RocksDB).
QueryData previous_qd;
auto status = cf.getPreviousQueryResults(previous_qd, db_);
EXPECT_TRUE(status.ok());
EXPECT_EQ(status.toString(), "OK");
// Add the "current" results and output the differentials.
2014-07-31 00:35:19 +00:00
DiffResults dr;
2015-04-27 21:57:04 +00:00
auto s = cf.addNewResults(result.second, dr, true, db_);
2014-07-31 00:35:19 +00:00
EXPECT_TRUE(s.ok());
2015-04-27 21:57:04 +00:00
// Call the diffing utility directly.
DiffResults expected = diff(previous_qd, result.second);
2014-07-31 00:35:19 +00:00
EXPECT_EQ(dr, expected);
2015-04-27 21:57:04 +00:00
// After Query::addNewResults the previous results are now current.
2014-07-31 00:35:19 +00:00
QueryData qd;
2015-04-27 21:57:04 +00:00
cf.getPreviousQueryResults(qd, db_);
2014-07-31 00:35:19 +00:00
EXPECT_EQ(qd, result.second);
}
}
2015-04-27 21:57:04 +00:00
TEST_F(QueryTests, test_get_query_results) {
// Grab an expected set of query data and add it as the previous result.
auto encoded_qd = getSerializedQueryDataJSON();
2014-07-31 00:35:19 +00:00
auto query = getOsqueryScheduledQuery();
2015-04-27 21:57:04 +00:00
auto status = db_->Put(kQueries, "foobar", encoded_qd.first);
EXPECT_TRUE(status.ok());
// Use the Query retrieval API to check the now "previous" result.
QueryData previous_qd;
auto cf = Query("foobar", query);
2015-04-27 21:57:04 +00:00
status = cf.getPreviousQueryResults(previous_qd, db_);
EXPECT_TRUE(status.ok());
2014-07-31 00:35:19 +00:00
}
TEST_F(QueryTests, test_query_name_not_found_in_db) {
2015-04-27 21:57:04 +00:00
// Try to retrieve results from a query that has not executed.
QueryData previous_qd;
2014-07-31 00:35:19 +00:00
auto query = getOsqueryScheduledQuery();
auto cf = Query("not_a_real_query", query);
2015-04-27 21:57:04 +00:00
auto status = cf.getPreviousQueryResults(previous_qd, db_);
EXPECT_TRUE(status.toString() == "Query name not found in database");
EXPECT_TRUE(status.ok());
2014-07-31 00:35:19 +00:00
}
TEST_F(QueryTests, test_is_query_name_in_database) {
auto query = getOsqueryScheduledQuery();
auto cf = Query("foobar", query);
2015-04-27 21:57:04 +00:00
auto encoded_qd = getSerializedQueryDataJSON();
auto status = db_->Put(kQueries, "foobar", encoded_qd.first);
EXPECT_TRUE(status.ok());
// Now test that the query name exists.
EXPECT_TRUE(cf.isQueryNameInDatabase(db_));
2014-07-31 00:35:19 +00:00
}
TEST_F(QueryTests, test_get_stored_query_names) {
auto query = getOsqueryScheduledQuery();
auto cf = Query("foobar", query);
2015-04-27 21:57:04 +00:00
auto encoded_qd = getSerializedQueryDataJSON();
auto status = db_->Put(kQueries, "foobar", encoded_qd.first);
EXPECT_TRUE(status.ok());
// Stored query names is a factory method included alongside every query.
// It will include the set of query names with existing "previous" results.
auto names = cf.getStoredQueryNames(db_);
auto in_vector = std::find(names.begin(), names.end(), "foobar");
2014-07-31 00:35:19 +00:00
EXPECT_NE(in_vector, names.end());
}
2014-08-15 07:25:30 +00:00
}