osquery-1/osquery/filesystem/filesystem_tests.cpp

142 lines
4.3 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
* 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-08-04 18:06:45 +00:00
#include <fstream>
#include <stdio.h>
#include <gtest/gtest.h>
#include <boost/property_tree/ptree.hpp>
#include <osquery/filesystem.h>
#include <osquery/logger.h>
2014-08-04 18:06:45 +00:00
2015-02-14 00:52:11 +00:00
#include "osquery/core/test_util.h"
namespace pt = boost::property_tree;
2014-08-15 07:25:30 +00:00
namespace osquery {
2015-02-01 09:11:46 +00:00
class FilesystemTests : public testing::Test {
2015-02-01 09:11:46 +00:00
protected:
2015-02-14 00:52:11 +00:00
void SetUp() { createMockFileStructure(); }
void TearDown() { tearDownMockFileStructure(); }
2015-02-01 09:11:46 +00:00
};
2014-08-04 18:06:45 +00:00
TEST_F(FilesystemTests, test_plugin) {
2015-02-01 09:11:46 +00:00
std::ofstream test_file("/tmp/osquery-fstests-file");
2014-08-13 18:55:29 +00:00
test_file.write("test123\n", sizeof("test123"));
2014-08-04 18:06:45 +00:00
test_file.close();
std::string content;
2015-02-01 09:11:46 +00:00
auto s = readFile("/tmp/osquery-fstests-file", content);
2014-08-04 18:06:45 +00:00
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
2014-08-13 18:55:29 +00:00
EXPECT_EQ(content, "test123\n");
2014-08-04 18:06:45 +00:00
2015-02-01 09:11:46 +00:00
remove("/tmp/osquery-fstests-file");
2014-08-04 18:06:45 +00:00
}
2014-08-14 23:27:20 +00:00
TEST_F(FilesystemTests, test_list_files_in_directory_not_found) {
std::vector<std::string> not_found_vector;
auto not_found = listFilesInDirectory("/foo/bar", not_found_vector);
EXPECT_FALSE(not_found.ok());
EXPECT_EQ(not_found.toString(), "Directory not found: /foo/bar");
2014-08-14 23:27:20 +00:00
}
TEST_F(FilesystemTests, test_wildcard_single_folder_list) {
std::vector<std::string> files;
2015-02-01 09:11:46 +00:00
auto status = resolveFilePattern(kFakeDirectory + "/%", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(
std::find(files.begin(), files.end(), kFakeDirectory + "/roto.txt"),
files.end());
}
TEST_F(FilesystemTests, test_wildcard_dual) {
std::vector<std::string> files;
2015-02-01 09:11:46 +00:00
auto status = resolveFilePattern(kFakeDirectory + "/%/%", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep1/level1.txt"),
files.end());
}
TEST_F(FilesystemTests, test_wildcard_full_recursion) {
std::vector<std::string> files;
2015-02-01 09:11:46 +00:00
auto status = resolveFilePattern(kFakeDirectory + "/%%", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep1/deep2/level2.txt"),
files.end());
}
TEST_F(FilesystemTests, test_wildcard_end_last_component) {
std::vector<std::string> files;
auto status = resolveFilePattern(kFakeDirectory + "/%11/%sh", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep11/not_bash"),
files.end());
}
TEST_F(FilesystemTests, test_wildcard_three_kinds) {
std::vector<std::string> files;
auto status = resolveFilePattern(kFakeDirectory + "/%p11/%/%%", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep11/deep2/deep3/level3.txt"),
files.end());
}
TEST_F(FilesystemTests, test_wildcard_invalid_path) {
std::vector<std::string> files;
auto status = resolveFilePattern("/not_ther_abcdefz/%%", files);
EXPECT_TRUE(status.ok());
EXPECT_EQ(files.size(), 0);
}
TEST_F(FilesystemTests, test_wildcard_filewild) {
std::vector<std::string> files;
auto status = resolveFilePattern(kFakeDirectory + "/deep1%/%", files);
EXPECT_TRUE(status.ok());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep1/level1.txt"),
files.end());
EXPECT_NE(std::find(files.begin(), files.end(),
kFakeDirectory + "/deep11/level1.txt"),
files.end());
}
2014-08-14 23:27:20 +00:00
TEST_F(FilesystemTests, test_list_files_in_directory_not_dir) {
std::vector<std::string> not_dir_vector;
auto not_dir = listFilesInDirectory("/etc/hosts", not_dir_vector);
EXPECT_FALSE(not_dir.ok());
EXPECT_EQ(not_dir.toString(), "Supplied path is not a directory: /etc/hosts");
2014-08-14 23:27:20 +00:00
}
TEST_F(FilesystemTests, test_list_files_in_directorty) {
std::vector<std::string> results;
auto s = listFilesInDirectory("/etc", results);
EXPECT_TRUE(s.ok());
EXPECT_EQ(s.toString(), "OK");
2014-08-15 07:25:30 +00:00
EXPECT_NE(std::find(results.begin(), results.end(), "/etc/hosts"),
results.end());
}
}
2014-08-04 18:06:45 +00:00
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
google::InitGoogleLogging(argv[0]);
return RUN_ALL_TESTS();
}