Fix up windows (cygwin) specific problem in carver tests (#5437)

Summary:
Pull Request resolved: https://github.com/facebook/osquery/pull/5437

- test should not assume that fs::temp_directory_path() is always the same
- test should clean everithin up in TearDown() method
- tests should no depend on the order (test_decompression previously was depending on test_compression)

Reviewed By: mkareta

Differential Revision: D14064645

fbshipit-source-id: 653e2061c3de8e3fc30a4f0fc553831f22e62fb7
This commit is contained in:
Alexander Kindyakov 2019-02-13 09:59:24 -08:00 committed by Facebook Github Bot
parent 59437ee9a5
commit 40a4276b4d
2 changed files with 50 additions and 37 deletions

View File

@ -12,11 +12,6 @@ osquery_cxx_test(
srcs = [ srcs = [
"carver_tests.cpp", "carver_tests.cpp",
], ],
env = {
"TEST_CONF_FILES_DIR": "$(location {})".format(
osquery_target("tools/tests:config_files"),
),
},
visibility = ["PUBLIC"], visibility = ["PUBLIC"],
deps = [ deps = [
osquery_target("osquery/config/tests:test_utils"), osquery_target("osquery/config/tests:test_utils"),

View File

@ -38,24 +38,14 @@ std::string genGuid() {
class CarverTests : public testing::Test { class CarverTests : public testing::Test {
public: public:
CarverTests() {
fs::create_directories(fs::temp_directory_path() / "files_to_carve/");
writeTextFile(fs::temp_directory_path() / "files_to_carve/secrets.txt",
"This is a message I'd rather no one saw.");
writeTextFile(fs::temp_directory_path() / "files_to_carve/evil.exe",
"MZP\x00\x02\x00\x00\x00\x04\x00\x0f\x00\xff\xff");
auto paths =
platformGlob((fs::temp_directory_path() / "files_to_carve/*").string());
for (const auto& p : paths) {
carvePaths.insert(p);
}
}
std::set<std::string>& getCarvePaths() { std::set<std::string>& getCarvePaths() {
return carvePaths; return carvePaths;
} }
fs::path const& getWorkingDir() const {
return working_dir_;
}
protected: protected:
void SetUp() override { void SetUp() override {
Initializer::platformSetup(); Initializer::platformSetup();
@ -65,12 +55,34 @@ class CarverTests : public testing::Test {
FLAGS_disable_database = true; FLAGS_disable_database = true;
DatabasePlugin::setAllowOpen(true); DatabasePlugin::setAllowOpen(true);
DatabasePlugin::initPlugin(); DatabasePlugin::initPlugin();
working_dir_ =
fs::temp_directory_path() /
fs::unique_path("osquery.carver_tests.working_dir.%%%%.%%%%");
fs::create_directories(working_dir_);
files_to_carve_dir_ = working_dir_ / "files_to_carve";
fs::create_directories(files_to_carve_dir_);
writeTextFile(files_to_carve_dir_ / "secrets.txt",
"This is a message I'd rather no one saw.");
writeTextFile(files_to_carve_dir_ / "evil.exe",
"MZP\x00\x02\x00\x00\x00\x04\x00\x0f\x00\xff\xff");
auto paths = platformGlob((files_to_carve_dir_ / "*").string());
for (const auto& p : paths) {
carvePaths.insert(p);
}
} }
void TearDown() override { void TearDown() override {
fs::remove_all(fs::temp_directory_path() / "/files_to_carve/"); fs::remove_all(files_to_carve_dir_);
fs::remove_all(working_dir_);
} }
private: private:
fs::path working_dir_;
fs::path files_to_carve_dir_;
std::set<std::string> carvePaths; std::set<std::string> carvePaths;
}; };
@ -104,25 +116,31 @@ TEST_F(CarverTests, test_carve_files_locally) {
EXPECT_GT(tar.size(), 0U); EXPECT_GT(tar.size(), 0U);
} }
TEST_F(CarverTests, test_compression) { TEST_F(CarverTests, test_compression_decompression) {
auto s = osquery::compress(getTestConfigDirectory() / "test.config", auto const test_data_file = getWorkingDir() / "test.data";
fs::temp_directory_path() / fs::path("test.zst")); writeTextFile(test_data_file, R"raw_text(
EXPECT_TRUE(s.ok()); 2TItVMSvAY8OFlbYnx1O1NSsuehfNhNiV4Qw4IPP6exA47HVzAlEXZI3blanlAd2
} JSxCUr+3boxWMwsgW2jJPzypSKvfXB9EDbFKiDjVueniBfiAepwta57pZ9tQDnJA
uRioApcqYSWL14OJrnPQFHel5FpXylmVdIkiz()cT82JsOPZmh56vDn62Kk/mU7V
RltGAYEpKmi8e71fuB8d/S6Lau{}AmL1153X7E+4d1G1UfiQa7Q02uVjxLLE5FEj
JTDjVqIQNhi50Pt4J4RVopYzy1AZGwPHLhwFVIPH0s/LmzVW+xbT8/V2UMSzK4XB
oqADd9Ckcdtplx3k7bcLU[U04j8WWUtUccmB+4e2KS]i3x7WDKviPY/sWy9xFapv
)raw_text");
{
auto s = osquery::compress(test_data_file,
getWorkingDir() / fs::path("test.zst"));
ASSERT_TRUE(s.ok()) << s.what();
}
{
auto s =
osquery::decompress(getWorkingDir() / fs::path("test.zst"),
getWorkingDir() / fs::path("test.data.extract"));
ASSERT_TRUE(s.ok()) << s.what();
}
TEST_F(CarverTests, test_decompression) {
std::cout << fs::temp_directory_path() << "\n";
std::cout << (getTestConfigDirectory() / "test.config") << "\n";
auto s = osquery::decompress(
fs::temp_directory_path() / fs::path("test.zst"),
fs::temp_directory_path() / fs::path("test.config.extract"));
EXPECT_TRUE(s.ok());
EXPECT_EQ( EXPECT_EQ(
hashFromFile(HashType::HASH_TYPE_SHA256, hashFromFile(HashType::HASH_TYPE_SHA256,
(fs::temp_directory_path() / fs::path("test.config.extract")) (getWorkingDir() / fs::path("test.data.extract")).string()),
.string()), hashFromFile(HashType::HASH_TYPE_SHA256, test_data_file.string()));
hashFromFile(
HashType::HASH_TYPE_SHA256,
(getTestConfigDirectory() / fs::path("test.config")).string()));
} }
} // namespace osquery } // namespace osquery