Merge pull request #694 from theopolis/fast_fs_tests

[Fix #690] Faster fstests using tmp structures
This commit is contained in:
Teddy Reed 2015-02-01 01:19:43 -08:00
commit 5ee9804685
2 changed files with 30 additions and 101 deletions

View File

@ -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<std::pair<std::string, std::string> >& 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<std::pair<std::string, std::string> >& 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<std::string, std::string> user;
user.first = i.second.get<std::string>("<xmlattr>.username");
user.second = i.second.get<std::string>("<xmlattr>.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");
}
}

View File

@ -15,6 +15,7 @@
#include <gtest/gtest.h>
#include <boost/filesystem/operations.hpp>
#include <boost/property_tree/ptree.hpp>
#include <osquery/filesystem.h>
@ -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<std::string> 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<std::string> 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<std::string> 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<std::string> 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"(
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<tomcat-users>
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary.
-->
<!--
NOTE: The sample user and role entries below are wrapped in a comment
and thus are ignored when reading this file. Do not forget to remove
<!.. ..> that surrounds them.
-->
<role rolename="tomcat"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
</tomcat-users>
)";
// clang-format on
std::vector<std::pair<std::string, std::string>> 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[]) {