osquery-1/osquery/database/tests/db_handle_tests.cpp
2015-04-27 09:40:31 -07:00

93 lines
2.5 KiB
C++

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* 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.
*
*/
#include <algorithm>
#include <boost/filesystem/operations.hpp>
#include <gtest/gtest.h>
#include <osquery/database/db_handle.h>
#include <osquery/logger.h>
#include <osquery/tables.h>
const std::string kTestingDBHandlePath = "/tmp/rocksdb-osquery-dbhandletests";
namespace osquery {
class DBHandleTests : public testing::Test {
public:
void SetUp() {
// Setup a testing DB instance
db = DBHandle::getInstanceAtPath(kTestingDBHandlePath);
cfh_queries = DBHandle::getInstance()->getHandleForColumnFamily(kQueries);
cfh_foobar =
DBHandle::getInstance()->getHandleForColumnFamily("foobartest");
}
void TearDown() { boost::filesystem::remove_all(kTestingDBHandlePath); }
public:
rocksdb::ColumnFamilyHandle* cfh_queries;
rocksdb::ColumnFamilyHandle* cfh_foobar;
std::shared_ptr<DBHandle> db;
};
TEST_F(DBHandleTests, test_singleton_on_disk) {
auto db1 = DBHandle::getInstance();
auto db2 = DBHandle::getInstance();
EXPECT_EQ(db1, db2);
}
TEST_F(DBHandleTests, test_get_handle_for_column_family) {
ASSERT_TRUE(cfh_queries != nullptr);
ASSERT_TRUE(cfh_foobar == nullptr);
}
TEST_F(DBHandleTests, test_get) {
db->getDB()->Put(
rocksdb::WriteOptions(), cfh_queries, "test_query_123", "{}");
std::string r;
std::string key = "test_query_123";
auto s = db->Get(kQueries, key, r);
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
EXPECT_EQ(r, "{}");
}
TEST_F(DBHandleTests, test_put) {
auto s = db->Put(kQueries, "test_put", "bar");
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
}
TEST_F(DBHandleTests, test_delete) {
db->Put(kQueries, "test_delete", "baz");
auto s = db->Delete(kQueries, "test_delete");
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
}
TEST_F(DBHandleTests, test_scan) {
db->Put(kQueries, "test_scan_foo1", "baz");
db->Put(kQueries, "test_scan_foo2", "baz");
db->Put(kQueries, "test_scan_foo3", "baz");
std::vector<std::string> keys;
std::vector<std::string> expected = {
"test_scan_foo1", "test_scan_foo2", "test_scan_foo3"};
auto s = db->Scan(kQueries, keys);
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
for (const auto& i : expected) {
EXPECT_NE(std::find(keys.begin(), keys.end(), i), keys.end());
}
}
}