d05dafe8a27e27a4c1aacc3852ad1f587fcdf1fb
[folly.git] / folly / experimental / TestUtil.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/experimental/TestUtil.h>
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include <boost/regex.hpp>
23 #include <folly/Exception.h>
24 #include <folly/File.h>
25 #include <folly/FileUtil.h>
26 #include <folly/Memory.h>
27 #include <folly/String.h>
28 #include <folly/portability/Fcntl.h>
29
30 #ifdef _WIN32
31 #include <crtdbg.h> // @manual
32 #endif
33
34 namespace folly {
35 namespace test {
36
37 namespace {
38
39 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
40   if (path.empty()) {
41     path = fs::temp_directory_path();
42   }
43   if (namePrefix.empty()) {
44     path /= fs::unique_path();
45   } else {
46     path /= fs::unique_path(
47         to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
48   }
49   return path;
50 }
51
52 }  // namespace
53
54 TemporaryFile::TemporaryFile(StringPiece namePrefix,
55                              fs::path dir,
56                              Scope scope,
57                              bool closeOnDestruction)
58   : scope_(scope),
59     closeOnDestruction_(closeOnDestruction),
60     fd_(-1),
61     path_(generateUniquePath(std::move(dir), namePrefix)) {
62   fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
63   checkUnixError(fd_, "open failed");
64
65   if (scope_ == Scope::UNLINK_IMMEDIATELY) {
66     boost::system::error_code ec;
67     fs::remove(path_, ec);
68     if (ec) {
69       LOG(WARNING) << "unlink on construction failed: " << ec;
70     } else {
71       path_.clear();
72     }
73   }
74 }
75
76 const fs::path& TemporaryFile::path() const {
77   CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
78   DCHECK(!path_.empty());
79   return path_;
80 }
81
82 TemporaryFile::~TemporaryFile() {
83   if (fd_ != -1 && closeOnDestruction_) {
84     if (close(fd_) == -1) {
85       PLOG(ERROR) << "close failed";
86     }
87   }
88
89   // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
90   // try again here.
91   if (scope_ != Scope::PERMANENT && !path_.empty()) {
92     boost::system::error_code ec;
93     fs::remove(path_, ec);
94     if (ec) {
95       LOG(WARNING) << "unlink on destruction failed: " << ec;
96     }
97   }
98 }
99
100 TemporaryDirectory::TemporaryDirectory(
101     StringPiece namePrefix,
102     fs::path dir,
103     Scope scope)
104     : scope_(scope),
105       path_(std::make_unique<fs::path>(
106           generateUniquePath(std::move(dir), namePrefix))) {
107   fs::create_directory(path());
108 }
109
110 TemporaryDirectory::~TemporaryDirectory() {
111   if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) {
112     boost::system::error_code ec;
113     fs::remove_all(path(), ec);
114     if (ec) {
115       LOG(WARNING) << "recursive delete on destruction failed: " << ec;
116     }
117   }
118 }
119
120 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
121   std::string p = dir_.path().string();
122   ::chdir(p.c_str());
123 }
124
125 ChangeToTempDir::~ChangeToTempDir() {
126   std::string p = initialPath_.string();
127   ::chdir(p.c_str());
128 }
129
130 namespace detail {
131
132 SavedState disableInvalidParameters() {
133 #ifdef _WIN32
134   SavedState ret;
135   ret.previousThreadLocalHandler = _set_thread_local_invalid_parameter_handler(
136       [](const wchar_t*,
137          const wchar_t*,
138          const wchar_t*,
139          unsigned int,
140          uintptr_t) {});
141   ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
142   return ret;
143 #else
144   return SavedState();
145 #endif
146 }
147
148 #ifdef _WIN32
149 void enableInvalidParameters(SavedState state) {
150   _set_thread_local_invalid_parameter_handler(
151       (_invalid_parameter_handler)state.previousThreadLocalHandler);
152   _CrtSetReportMode(_CRT_ASSERT, state.previousCrtReportMode);
153 }
154 #else
155 void enableInvalidParameters(SavedState) {}
156 #endif
157
158 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
159   return boost::regex_match(
160     target.begin(),
161     target.end(),
162     boost::regex(pattern.begin(), pattern.end())
163   );
164 }
165
166 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
167   return !hasPCREPatternMatch(pattern, target);
168 }
169
170 }  // namespace detail
171
172 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
173     : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
174   oldFDCopy_ = dup(fd_);
175   PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
176
177   int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
178   PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
179     << " with " << file_fd;
180   PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
181 }
182
183 void CaptureFD::release() {
184   if (oldFDCopy_ != fd_) {
185     readIncremental();  // Feed chunkCob_
186     PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
187       << oldFDCopy_ << " into " << fd_;
188     PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
189     oldFDCopy_ = fd_;  // Make this call idempotent
190   }
191 }
192
193 CaptureFD::~CaptureFD() {
194   release();
195 }
196
197 std::string CaptureFD::read() const {
198   std::string contents;
199   std::string filename = file_.path().string();
200   PCHECK(folly::readFile(filename.c_str(), contents));
201   return contents;
202 }
203
204 std::string CaptureFD::readIncremental() {
205   std::string filename = file_.path().string();
206   // Yes, I know that I could just keep the file open instead. So sue me.
207   folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
208   auto size = size_t(lseek(f.fd(), 0, SEEK_END) - readOffset_);
209   std::unique_ptr<char[]> buf(new char[size]);
210   auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
211   PCHECK(ssize_t(size) == bytes_read);
212   readOffset_ += off_t(size);
213   chunkCob_(StringPiece(buf.get(), buf.get() + size));
214   return std::string(buf.get(), size);
215 }
216
217 }  // namespace test
218 }  // namespace folly