1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: kenton@google.com (Kenton Varda)
32 // emulates google3/testing/base/public/googletest.cc
34 #include <google/protobuf/testing/googletest.h>
35 #include <google/protobuf/testing/file.h>
36 #include <google/protobuf/stubs/strutil.h>
38 #include <sys/types.h>
56 #define mkdir(name, mode) mkdir(name)
61 #define O_BINARY _O_BINARY
63 #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
67 string TestSourceDir() {
69 // Look for the "src" directory.
72 while (!File::Exists(prefix + "/src/google/protobuf")) {
73 if (!File::Exists(prefix)) {
75 << "Could not find protobuf source code. Please run tests from "
76 "somewhere within the protobuf source package.";
80 return prefix + "/src";
82 // automake sets the "srcdir" environment variable.
83 char* result = getenv("srcdir");
85 // Otherwise, the test must be run from the source directory.
95 string GetTemporaryDirectoryName() {
96 // tmpnam() is generally not considered safe but we're only using it for
97 // testing. We cannot use tmpfile() or mkstemp() since we're creating a
99 char b[L_tmpnam + 1]; // HPUX multithread return 0 if s is 0
100 string result = tmpnam(b);
102 // On Win32, tmpnam() returns a file prefixed with '\', but which is supposed
103 // to be used in the current working directory. WTF?
104 if (HasPrefixString(result, "\\")) {
111 // Creates a temporary directory on demand and deletes it when the process
113 class TempDirDeleter {
117 if (!name_.empty()) {
118 File::DeleteRecursively(name_, NULL, NULL);
122 string GetTempDir() {
124 name_ = GetTemporaryDirectoryName();
125 GOOGLE_CHECK(mkdir(name_.c_str(), 0777) == 0) << strerror(errno);
127 // Stick a file in the directory that tells people what this is, in case
128 // we abort and don't get a chance to delete it.
129 File::WriteStringToFileOrDie("", name_ + "/TEMP_DIR_FOR_PROTOBUF_TESTS");
138 TempDirDeleter temp_dir_deleter_;
142 string TestTempDir() {
143 return temp_dir_deleter_.GetTempDir();
146 // TODO(kenton): Share duplicated code below. Too busy/lazy for now.
148 static string stdout_capture_filename_;
149 static string stderr_capture_filename_;
150 static int original_stdout_ = -1;
151 static int original_stderr_ = -1;
153 void CaptureTestStdout() {
154 GOOGLE_CHECK_EQ(original_stdout_, -1) << "Already capturing.";
156 stdout_capture_filename_ = TestTempDir() + "/captured_stdout";
158 int fd = open(stdout_capture_filename_.c_str(),
159 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
160 GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
162 original_stdout_ = dup(1);
168 void CaptureTestStderr() {
169 GOOGLE_CHECK_EQ(original_stderr_, -1) << "Already capturing.";
171 stderr_capture_filename_ = TestTempDir() + "/captured_stderr";
173 int fd = open(stderr_capture_filename_.c_str(),
174 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
175 GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
177 original_stderr_ = dup(2);
183 string GetCapturedTestStdout() {
184 GOOGLE_CHECK_NE(original_stdout_, -1) << "Not capturing.";
187 dup2(original_stdout_, 1);
188 original_stdout_ = -1;
191 File::ReadFileToStringOrDie(stdout_capture_filename_, &result);
193 remove(stdout_capture_filename_.c_str());
198 string GetCapturedTestStderr() {
199 GOOGLE_CHECK_NE(original_stderr_, -1) << "Not capturing.";
202 dup2(original_stderr_, 2);
203 original_stderr_ = -1;
206 File::ReadFileToStringOrDie(stderr_capture_filename_, &result);
208 remove(stderr_capture_filename_.c_str());
213 ScopedMemoryLog* ScopedMemoryLog::active_log_ = NULL;
215 ScopedMemoryLog::ScopedMemoryLog() {
216 GOOGLE_CHECK(active_log_ == NULL);
218 old_handler_ = SetLogHandler(&HandleLog);
221 ScopedMemoryLog::~ScopedMemoryLog() {
222 SetLogHandler(old_handler_);
226 const vector<string>& ScopedMemoryLog::GetMessages(LogLevel level) {
227 GOOGLE_CHECK(level == ERROR ||
229 return messages_[level];
232 void ScopedMemoryLog::HandleLog(LogLevel level, const char* filename,
233 int line, const string& message) {
234 GOOGLE_CHECK(active_log_ != NULL);
235 if (level == ERROR || level == WARNING) {
236 active_log_->messages_[level].push_back(message);
242 // Force shutdown at process exit so that we can test for memory leaks. To
243 // actually check for leaks, I suggest using the heap checker included with
244 // google-perftools. Set it to "draconian" mode to ensure that every last
245 // call to malloc() has a corresponding free().
246 struct ForceShutdown {
248 ShutdownProtobufLibrary();
254 } // namespace protobuf
255 } // namespace google