PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/google/protobuf/testing/googletest.cc

http://protobuf.googlecode.com/
C++ | 255 lines | 207 code | 9 blank | 39 comment | 6 complexity | f6a5e83bea3f644d9fd2c4899bf3c30a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  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
  14. // distribution.
  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.
  18. //
  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.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // emulates google3/testing/base/public/googletest.cc
  32. #include <google/protobuf/testing/googletest.h>
  33. #include <google/protobuf/testing/file.h>
  34. #include <google/protobuf/stubs/strutil.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <errno.h>
  38. #include <stdlib.h>
  39. #ifdef _MSC_VER
  40. #include <io.h>
  41. #include <direct.h>
  42. #else
  43. #include <unistd.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <fcntl.h>
  47. #include <iostream>
  48. #include <fstream>
  49. namespace google {
  50. namespace protobuf {
  51. #ifdef _WIN32
  52. #define mkdir(name, mode) mkdir(name)
  53. #endif
  54. #ifndef O_BINARY
  55. #ifdef _O_BINARY
  56. #define O_BINARY _O_BINARY
  57. #else
  58. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  59. #endif
  60. #endif
  61. string TestSourceDir() {
  62. #ifdef _MSC_VER
  63. // Look for the "src" directory.
  64. string prefix = ".";
  65. while (!File::Exists(prefix + "/src/google/protobuf")) {
  66. if (!File::Exists(prefix)) {
  67. GOOGLE_LOG(FATAL)
  68. << "Could not find protobuf source code. Please run tests from "
  69. "somewhere within the protobuf source package.";
  70. }
  71. prefix += "/..";
  72. }
  73. return prefix + "/src";
  74. #else
  75. // automake sets the "srcdir" environment variable.
  76. char* result = getenv("srcdir");
  77. if (result == NULL) {
  78. // Otherwise, the test must be run from the source directory.
  79. return ".";
  80. } else {
  81. return result;
  82. }
  83. #endif
  84. }
  85. namespace {
  86. string GetTemporaryDirectoryName() {
  87. // tmpnam() is generally not considered safe but we're only using it for
  88. // testing. We cannot use tmpfile() or mkstemp() since we're creating a
  89. // directory.
  90. char b[L_tmpnam + 1]; // HPUX multithread return 0 if s is 0
  91. string result = tmpnam(b);
  92. #ifdef _WIN32
  93. // On Win32, tmpnam() returns a file prefixed with '\', but which is supposed
  94. // to be used in the current working directory. WTF?
  95. if (HasPrefixString(result, "\\")) {
  96. result.erase(0, 1);
  97. }
  98. #endif // _WIN32
  99. return result;
  100. }
  101. // Creates a temporary directory on demand and deletes it when the process
  102. // quits.
  103. class TempDirDeleter {
  104. public:
  105. TempDirDeleter() {}
  106. ~TempDirDeleter() {
  107. if (!name_.empty()) {
  108. File::DeleteRecursively(name_, NULL, NULL);
  109. }
  110. }
  111. string GetTempDir() {
  112. if (name_.empty()) {
  113. name_ = GetTemporaryDirectoryName();
  114. GOOGLE_CHECK(mkdir(name_.c_str(), 0777) == 0) << strerror(errno);
  115. // Stick a file in the directory that tells people what this is, in case
  116. // we abort and don't get a chance to delete it.
  117. File::WriteStringToFileOrDie("", name_ + "/TEMP_DIR_FOR_PROTOBUF_TESTS");
  118. }
  119. return name_;
  120. }
  121. private:
  122. string name_;
  123. };
  124. TempDirDeleter temp_dir_deleter_;
  125. } // namespace
  126. string TestTempDir() {
  127. return temp_dir_deleter_.GetTempDir();
  128. }
  129. // TODO(kenton): Share duplicated code below. Too busy/lazy for now.
  130. static string stdout_capture_filename_;
  131. static string stderr_capture_filename_;
  132. static int original_stdout_ = -1;
  133. static int original_stderr_ = -1;
  134. void CaptureTestStdout() {
  135. GOOGLE_CHECK_EQ(original_stdout_, -1) << "Already capturing.";
  136. stdout_capture_filename_ = TestTempDir() + "/captured_stdout";
  137. int fd = open(stdout_capture_filename_.c_str(),
  138. O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
  139. GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
  140. original_stdout_ = dup(1);
  141. close(1);
  142. dup2(fd, 1);
  143. close(fd);
  144. }
  145. void CaptureTestStderr() {
  146. GOOGLE_CHECK_EQ(original_stderr_, -1) << "Already capturing.";
  147. stderr_capture_filename_ = TestTempDir() + "/captured_stderr";
  148. int fd = open(stderr_capture_filename_.c_str(),
  149. O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
  150. GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
  151. original_stderr_ = dup(2);
  152. close(2);
  153. dup2(fd, 2);
  154. close(fd);
  155. }
  156. string GetCapturedTestStdout() {
  157. GOOGLE_CHECK_NE(original_stdout_, -1) << "Not capturing.";
  158. close(1);
  159. dup2(original_stdout_, 1);
  160. original_stdout_ = -1;
  161. string result;
  162. File::ReadFileToStringOrDie(stdout_capture_filename_, &result);
  163. remove(stdout_capture_filename_.c_str());
  164. return result;
  165. }
  166. string GetCapturedTestStderr() {
  167. GOOGLE_CHECK_NE(original_stderr_, -1) << "Not capturing.";
  168. close(2);
  169. dup2(original_stderr_, 2);
  170. original_stderr_ = -1;
  171. string result;
  172. File::ReadFileToStringOrDie(stderr_capture_filename_, &result);
  173. remove(stderr_capture_filename_.c_str());
  174. return result;
  175. }
  176. ScopedMemoryLog* ScopedMemoryLog::active_log_ = NULL;
  177. ScopedMemoryLog::ScopedMemoryLog() {
  178. GOOGLE_CHECK(active_log_ == NULL);
  179. active_log_ = this;
  180. old_handler_ = SetLogHandler(&HandleLog);
  181. }
  182. ScopedMemoryLog::~ScopedMemoryLog() {
  183. SetLogHandler(old_handler_);
  184. active_log_ = NULL;
  185. }
  186. const vector<string>& ScopedMemoryLog::GetMessages(LogLevel level) {
  187. GOOGLE_CHECK(level == ERROR ||
  188. level == WARNING);
  189. return messages_[level];
  190. }
  191. void ScopedMemoryLog::HandleLog(LogLevel level, const char* filename,
  192. int line, const string& message) {
  193. GOOGLE_CHECK(active_log_ != NULL);
  194. if (level == ERROR || level == WARNING) {
  195. active_log_->messages_[level].push_back(message);
  196. }
  197. }
  198. namespace {
  199. // Force shutdown at process exit so that we can test for memory leaks. To
  200. // actually check for leaks, I suggest using the heap checker included with
  201. // google-perftools. Set it to "draconian" mode to ensure that every last
  202. // call to malloc() has a corresponding free().
  203. struct ForceShutdown {
  204. ~ForceShutdown() {
  205. ShutdownProtobufLibrary();
  206. }
  207. } force_shutdown;
  208. } // namespace
  209. } // namespace protobuf
  210. } // namespace google