From ba3931cc1fecf4ebfc7b8d74716775cbeb5da16e Mon Sep 17 00:00:00 2001 From: Teddy Reed Date: Sun, 1 Feb 2015 02:11:46 -0700 Subject: [PATCH] Faster fstests using tmp structures --- osquery/filesystem/filesystem.cpp | 46 ------------- osquery/filesystem/filesystem_tests.cpp | 85 +++++++++---------------- 2 files changed, 30 insertions(+), 101 deletions(-) diff --git a/osquery/filesystem/filesystem.cpp b/osquery/filesystem/filesystem.cpp index 62308c28..a556985f 100644 --- a/osquery/filesystem/filesystem.cpp +++ b/osquery/filesystem/filesystem.cpp @@ -376,50 +376,4 @@ Status isDirectory(const boost::filesystem::path& path) { } return Status(1, "Path is not a directory: " + path.string()); } - -Status parseTomcatUserConfigFromDisk( - const boost::filesystem::path& path, - std::vector >& credentials) { - std::string content; - auto s = readFile(path, content); - if (s.ok()) { - return parseTomcatUserConfig(content, credentials); - } else { - return s; - } -} - -Status parseTomcatUserConfig( - const std::string& content, - std::vector >& credentials) { - std::stringstream ss; - ss << content; - pt::ptree tree; - try { - pt::xml_parser::read_xml(ss, tree); - } catch (const pt::xml_parser_error& e) { - return Status(1, e.what()); - } - try { - for (const auto& i : tree.get_child("tomcat-users")) { - if (i.first == "user") { - try { - std::pair user; - user.first = i.second.get(".username"); - user.second = i.second.get(".password"); - credentials.push_back(user); - } catch (const std::exception& e) { - LOG(ERROR) - << "An error occurred parsing the tomcat users xml: " << e.what(); - return Status(1, e.what()); - } - } - } - } catch (const std::exception& e) { - LOG(ERROR) << "An error occurred while trying to access the tomcat-users" - << " key in the XML content: " << e.what(); - return Status(1, e.what()); - } - return Status(0, "OK"); -} } diff --git a/osquery/filesystem/filesystem_tests.cpp b/osquery/filesystem/filesystem_tests.cpp index 6093d890..caf658bb 100644 --- a/osquery/filesystem/filesystem_tests.cpp +++ b/osquery/filesystem/filesystem_tests.cpp @@ -15,6 +15,7 @@ #include +#include #include #include @@ -24,20 +25,38 @@ namespace pt = boost::property_tree; namespace osquery { -class FilesystemTests : public testing::Test {}; +const std::string kFakeDirectory = "/tmp/osquery-fstests-pattern"; +const std::string kFakeFile = "/tmp/osquery-fstests-pattern/file0"; +const std::string kFakeSubFile = "/tmp/osquery-fstests-pattern/1/file1"; +const std::string kFakeSubSubFile = "/tmp/osquery-fstests-pattern/1/2/file2"; + +class FilesystemTests : public testing::Test { + protected: + void SetUp() { + boost::filesystem::create_directories(kFakeDirectory + "/1/2"); + FILE* fd = fopen(kFakeFile.c_str(), "w"); + fclose(fd); + fd = fopen(kFakeSubFile.c_str(), "w"); + fclose(fd); + fd = fopen(kFakeSubSubFile.c_str(), "w"); + fclose(fd); + } + + void TearDown() { boost::filesystem::remove_all(kFakeDirectory); } +}; TEST_F(FilesystemTests, test_plugin) { - std::ofstream test_file("/tmp/osquery-test-file"); + std::ofstream test_file("/tmp/osquery-fstests-file"); test_file.write("test123\n", sizeof("test123")); test_file.close(); std::string content; - auto s = readFile("/tmp/osquery-test-file", content); + auto s = readFile("/tmp/osquery-fstests-file", content); EXPECT_TRUE(s.ok()); EXPECT_EQ(s.toString(), "OK"); EXPECT_EQ(content, "test123\n"); - remove("/tmp/osquery-test-file"); + remove("/tmp/osquery-fstests-file"); } TEST_F(FilesystemTests, test_list_files_in_directory_not_found) { @@ -49,29 +68,29 @@ TEST_F(FilesystemTests, test_list_files_in_directory_not_found) { // Recursive Tests TEST_F(FilesystemTests, test_wildcard_single_folder_list) { std::vector files; - auto status = resolveFilePattern("/etc/%", files); + auto status = resolveFilePattern(kFakeDirectory + "/%", files); EXPECT_TRUE(status.ok()); - EXPECT_NE(std::find(files.begin(), files.end(), "/etc/hosts"), files.end()); + EXPECT_NE(std::find(files.begin(), files.end(), kFakeFile), files.end()); } TEST_F(FilesystemTests, test_wildcard_dual) { std::vector files; - auto status = resolveFilePattern("/%/%", files); + auto status = resolveFilePattern(kFakeDirectory + "/%/%", files); EXPECT_TRUE(status.ok()); - EXPECT_NE(std::find(files.begin(), files.end(), "/bin/bash"), files.end()); + EXPECT_NE(std::find(files.begin(), files.end(), kFakeSubFile), files.end()); } TEST_F(FilesystemTests, test_wildcard_full_recursion) { std::vector files; - auto status = resolveFilePattern("/usr/%%", files); + auto status = resolveFilePattern(kFakeDirectory + "/%%", files); EXPECT_TRUE(status.ok()); - EXPECT_NE(std::find(files.begin(), files.end(), "/usr/bin/killall"), + EXPECT_NE(std::find(files.begin(), files.end(), kFakeSubSubFile), files.end()); } TEST_F(FilesystemTests, test_wildcard_invalid_path) { std::vector files; - auto status = resolveFilePattern("/not_there/%%", files); + auto status = resolveFilePattern("/foo/bar/%%", files); EXPECT_TRUE(status.ok()); EXPECT_EQ(files.size(), 0); } @@ -92,50 +111,6 @@ TEST_F(FilesystemTests, test_list_files_in_directorty) { EXPECT_NE(std::find(results.begin(), results.end(), "/etc/hosts"), results.end()); } - -TEST_F(FilesystemTests, test_parse_tomcat_user_config) { - // clang-format off - std::string config_content = R"( - - - - - - - - -)"; - // clang-format on - - std::vector> credentials; - auto s = parseTomcatUserConfig(config_content, credentials); - EXPECT_TRUE(s.ok()); - EXPECT_EQ(s.toString(), "OK"); - EXPECT_EQ(credentials.size(), (size_t)1); - EXPECT_EQ(credentials[0].first, "tomcat"); - EXPECT_EQ(credentials[0].second, "tomcat"); -} } int main(int argc, char* argv[]) {