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