Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: cleanup per-test environment after each fuzz case. #4253

Merged
merged 3 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/fuzz/fuzz_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ PerTestEnvironment::PerTestEnvironment()
TestEnvironment::createPath(test_tmpdir_);
}

PerTestEnvironment::~PerTestEnvironment() { TestEnvironment::removePath(test_tmpdir_); }

void Runner::setupEnvironment(int argc, char** argv, spdlog::level::level_enum default_log_level) {
Event::Libevent::Global::initialize();

Expand Down
1 change: 1 addition & 0 deletions test/fuzz/fuzz_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Fuzz {
class PerTestEnvironment {
public:
PerTestEnvironment();
~PerTestEnvironment();

std::string temporaryPath(const std::string& path) const { return test_tmpdir_ + "/" + path; }

Expand Down
13 changes: 13 additions & 0 deletions test/test_common/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ void TestEnvironment::createParentPath(const std::string& path) {
RELEASE_ASSERT(::system(("mkdir -p $(dirname " + path + ")").c_str()) == 0, "");
#endif
}

void TestEnvironment::removePath(const std::string& path) {
RELEASE_ASSERT(StringUtil::startsWith(path.c_str(), TestEnvironment::temporaryDirectory()), "");
#ifdef __cpp_lib_experimental_filesystem
// We don't want to rely on rm etc. if we can avoid it, since it might not
// exist in some environments such as ClusterFuzz.
std::experimental::filesystem::remove_all(std::experimental::filesystem::path(path));
#else
// No support on this system for std::experimental::filesystem.
RELEASE_ASSERT(::system(("rm -rf " + path).c_str()) == 0, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is test only, but this still scares me. Are we adequately isolated from all possible pilot errors here? Maybe just add a really simple RELEASE_ASSERT check that the first character of path isn't .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to validate a TestEnvironmet::temporaryDirectory() prefix; it's not fool proof but it should catch most issues.

#endif
}

absl::optional<std::string> TestEnvironment::getOptionalEnvVar(const std::string& var) {
const char* path = ::getenv(var.c_str());
if (path == nullptr) {
Expand Down
6 changes: 6 additions & 0 deletions test/test_common/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,11 @@ class TestEnvironment {
* @param path.
*/
static void createParentPath(const std::string& path);

/**
* Remove a path on the filesystem (rm -rf ... equivalent).
* @param path.
*/
static void removePath(const std::string& path);
};
} // namespace Envoy