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