7d7ba81eaefa28a4c4a652328298d19e8d6363c4
[folly.git] / folly / experimental / TestUtil.cpp
1 /*
2  * Copyright 2015 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 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include <boost/regex.hpp>
25 #include <folly/Conv.h>
26 #include <folly/Exception.h>
27 #include <folly/File.h>
28 #include <folly/FileUtil.h>
29 #include <folly/String.h>
30
31 #ifndef _MSC_VER
32 extern char** environ;
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 const fs::path& TemporaryFile::path() const {
78   CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
79   DCHECK(!path_.empty());
80   return path_;
81 }
82
83 TemporaryFile::~TemporaryFile() {
84   if (fd_ != -1 && closeOnDestruction_) {
85     if (close(fd_) == -1) {
86       PLOG(ERROR) << "close failed";
87     }
88   }
89
90   // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
91   // try again here.
92   if (scope_ != Scope::PERMANENT && !path_.empty()) {
93     boost::system::error_code ec;
94     fs::remove(path_, ec);
95     if (ec) {
96       LOG(WARNING) << "unlink on destruction failed: " << ec;
97     }
98   }
99 }
100
101 TemporaryDirectory::TemporaryDirectory(StringPiece namePrefix,
102                                        fs::path dir,
103                                        Scope scope)
104   : scope_(scope),
105     path_(generateUniquePath(std::move(dir), namePrefix)) {
106   fs::create_directory(path_);
107 }
108
109 TemporaryDirectory::~TemporaryDirectory() {
110   if (scope_ == Scope::DELETE_ON_DESTRUCTION) {
111     boost::system::error_code ec;
112     fs::remove_all(path_, ec);
113     if (ec) {
114       LOG(WARNING) << "recursive delete on destruction failed: " << ec;
115     }
116   }
117 }
118
119 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
120   std::string p = dir_.path().string();
121   ::chdir(p.c_str());
122 }
123
124 ChangeToTempDir::~ChangeToTempDir() {
125   std::string p = initialPath_.string();
126   ::chdir(p.c_str());
127 }
128
129 namespace detail {
130
131 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
132   return boost::regex_match(
133     target.begin(),
134     target.end(),
135     boost::regex(pattern.begin(), pattern.end())
136   );
137 }
138
139 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
140   return !hasPCREPatternMatch(pattern, target);
141 }
142
143 }  // namespace detail
144
145 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
146     : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
147   oldFDCopy_ = dup(fd_);
148   PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
149
150   int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
151   PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
152     << " with " << file_fd;
153   PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
154 }
155
156 void CaptureFD::release() {
157   if (oldFDCopy_ != fd_) {
158     readIncremental();  // Feed chunkCob_
159     PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
160       << oldFDCopy_ << " into " << fd_;
161     PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
162     oldFDCopy_ = fd_;  // Make this call idempotent
163   }
164 }
165
166 CaptureFD::~CaptureFD() {
167   release();
168 }
169
170 std::string CaptureFD::read() const {
171   std::string contents;
172   std::string filename = file_.path().string();
173   PCHECK(folly::readFile(filename.c_str(), contents));
174   return contents;
175 }
176
177 std::string CaptureFD::readIncremental() {
178   std::string filename = file_.path().string();
179   // Yes, I know that I could just keep the file open instead. So sue me.
180   folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
181   auto size = lseek(f.fd(), 0, SEEK_END) - readOffset_;
182   std::unique_ptr<char[]> buf(new char[size]);
183   auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
184   PCHECK(size == bytes_read);
185   readOffset_ += size;
186   chunkCob_(StringPiece(buf.get(), buf.get() + size));
187   return std::string(buf.get(), size);
188 }
189
190 static std::map<std::string, std::string> getEnvVarMap() {
191   std::map<std::string, std::string> data;
192   for (auto it = environ; *it != nullptr; ++it) {
193     std::string key, value;
194     split("=", *it, key, value);
195     if (key.empty()) {
196       continue;
197     }
198     CHECK(!data.count(key)) << "already contains: " << key;
199     data.emplace(move(key), move(value));
200   }
201   return data;
202 }
203
204 EnvVarSaver::EnvVarSaver() {
205   saved_ = getEnvVarMap();
206 }
207
208 EnvVarSaver::~EnvVarSaver() {
209   for (const auto& kvp : getEnvVarMap()) {
210     if (saved_.count(kvp.first)) {
211       continue;
212     }
213     PCHECK(0 == unsetenv(kvp.first.c_str()));
214   }
215   for (const auto& kvp : saved_) {
216     PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
217   }
218 }
219
220 }  // namespace test
221 }  // namespace folly