诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

305 lines
9.9 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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/io/io_win32.h>
  35. #include <google/protobuf/stubs/strutil.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include <errno.h>
  39. #include <stdlib.h>
  40. #ifdef _MSC_VER
  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. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  53. // them like we do below.
  54. using google::protobuf::io::win32::close;
  55. using google::protobuf::io::win32::dup2;
  56. using google::protobuf::io::win32::dup;
  57. using google::protobuf::io::win32::mkdir;
  58. using google::protobuf::io::win32::open;
  59. #endif
  60. #ifndef O_BINARY
  61. #ifdef _O_BINARY
  62. #define O_BINARY _O_BINARY
  63. #else
  64. #define O_BINARY 0 // If this isn't defined, the platform doesn't need it.
  65. #endif
  66. #endif
  67. string TestSourceDir() {
  68. #ifndef GOOGLE_THIRD_PARTY_PROTOBUF
  69. #ifdef GOOGLE_PROTOBUF_TEST_SOURCE_PATH
  70. return GOOGLE_PROTOBUF_TEST_SOURCE_PATH;
  71. #else
  72. #ifndef _MSC_VER
  73. // automake sets the "srcdir" environment variable.
  74. char* result = getenv("srcdir");
  75. if (result != NULL) {
  76. return result;
  77. }
  78. #endif // _MSC_VER
  79. // Look for the "src" directory.
  80. string prefix = ".";
  81. // Keep looking further up the directory tree until we find
  82. // src/.../descriptor.cc. It is important to look for a particular file,
  83. // keeping in mind that with Bazel builds the directory structure under
  84. // bazel-bin/ looks similar to the main directory tree in the Git repo.
  85. while (!File::Exists(prefix + "/src/google/protobuf/descriptor.cc")) {
  86. if (!File::Exists(prefix)) {
  87. GOOGLE_LOG(FATAL)
  88. << "Could not find protobuf source code. Please run tests from "
  89. "somewhere within the protobuf source package.";
  90. }
  91. prefix += "/..";
  92. }
  93. return prefix + "/src";
  94. #endif // GOOGLE_PROTOBUF_TEST_SOURCE_PATH
  95. #else
  96. return "third_party/protobuf/src";
  97. #endif // GOOGLE_THIRD_PARTY_PROTOBUF
  98. }
  99. namespace {
  100. string GetTemporaryDirectoryName() {
  101. // Tests run under Bazel "should not" use /tmp. Bazel sets this environment
  102. // variable for tests to use instead.
  103. char *from_environment = getenv("TEST_TMPDIR");
  104. if (from_environment != NULL && from_environment[0] != '\0') {
  105. return string(from_environment) + "/protobuf_tmpdir";
  106. }
  107. // tmpnam() is generally not considered safe but we're only using it for
  108. // testing. We cannot use tmpfile() or mkstemp() since we're creating a
  109. // directory.
  110. char b[L_tmpnam + 1]; // HPUX multithread return 0 if s is 0
  111. #pragma GCC diagnostic push
  112. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  113. string result = tmpnam(b);
  114. #pragma GCC diagnostic pop
  115. #ifdef _WIN32
  116. // Avoid a trailing dot by changing it to an underscore. On Win32 the names of
  117. // files and directories can, but should not, end with dot.
  118. //
  119. // In MS-DOS and FAT16 filesystem the filenames were 8dot3 style so it didn't
  120. // make sense to have a name ending in dot without an extension, so the shell
  121. // silently ignored trailing dots. To this day the Win32 API still maintains
  122. // this behavior and silently ignores trailing dots in path arguments of
  123. // functions such as CreateFile{A,W}. Even POSIX API function implementations
  124. // seem to wrap the Win32 API functions (e.g. CreateDirectoryA) and behave
  125. // this way.
  126. // It's possible to avoid this behavior and create files / directories with
  127. // trailing dots (using CreateFileW / CreateDirectoryW and prefixing the path
  128. // with "\\?\") but these will be degenerate in the sense that you cannot
  129. // chdir into such directories (or navigate into them with Windows Explorer)
  130. // nor can you open such files with some programs (e.g. Notepad).
  131. if (result[result.size() - 1] == '.') {
  132. result[result.size() - 1] = '_';
  133. }
  134. // On Win32, tmpnam() returns a file prefixed with '\', but which is supposed
  135. // to be used in the current working directory. WTF?
  136. if (HasPrefixString(result, "\\")) {
  137. result.erase(0, 1);
  138. }
  139. // The Win32 API accepts forward slashes as a path delimiter as long as the
  140. // path doesn't use the "\\?\" prefix.
  141. // Let's avoid confusion and use only forward slashes.
  142. result = StringReplace(result, "\\", "/", true);
  143. #endif // _WIN32
  144. return result;
  145. }
  146. // Creates a temporary directory on demand and deletes it when the process
  147. // quits.
  148. class TempDirDeleter {
  149. public:
  150. TempDirDeleter() {}
  151. ~TempDirDeleter() {
  152. if (!name_.empty()) {
  153. File::DeleteRecursively(name_, NULL, NULL);
  154. }
  155. }
  156. string GetTempDir() {
  157. if (name_.empty()) {
  158. name_ = GetTemporaryDirectoryName();
  159. GOOGLE_CHECK(mkdir(name_.c_str(), 0777) == 0) << strerror(errno);
  160. // Stick a file in the directory that tells people what this is, in case
  161. // we abort and don't get a chance to delete it.
  162. File::WriteStringToFileOrDie("", name_ + "/TEMP_DIR_FOR_PROTOBUF_TESTS");
  163. }
  164. return name_;
  165. }
  166. private:
  167. string name_;
  168. };
  169. TempDirDeleter temp_dir_deleter_;
  170. } // namespace
  171. string TestTempDir() {
  172. return temp_dir_deleter_.GetTempDir();
  173. }
  174. // TODO(kenton): Share duplicated code below. Too busy/lazy for now.
  175. static string stdout_capture_filename_;
  176. static string stderr_capture_filename_;
  177. static int original_stdout_ = -1;
  178. static int original_stderr_ = -1;
  179. void CaptureTestStdout() {
  180. GOOGLE_CHECK_EQ(original_stdout_, -1) << "Already capturing.";
  181. stdout_capture_filename_ = TestTempDir() + "/captured_stdout";
  182. int fd = open(stdout_capture_filename_.c_str(),
  183. O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
  184. GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
  185. original_stdout_ = dup(1);
  186. close(1);
  187. dup2(fd, 1);
  188. close(fd);
  189. }
  190. void CaptureTestStderr() {
  191. GOOGLE_CHECK_EQ(original_stderr_, -1) << "Already capturing.";
  192. stderr_capture_filename_ = TestTempDir() + "/captured_stderr";
  193. int fd = open(stderr_capture_filename_.c_str(),
  194. O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0777);
  195. GOOGLE_CHECK(fd >= 0) << "open: " << strerror(errno);
  196. original_stderr_ = dup(2);
  197. close(2);
  198. dup2(fd, 2);
  199. close(fd);
  200. }
  201. string GetCapturedTestStdout() {
  202. GOOGLE_CHECK_NE(original_stdout_, -1) << "Not capturing.";
  203. close(1);
  204. dup2(original_stdout_, 1);
  205. original_stdout_ = -1;
  206. string result;
  207. File::ReadFileToStringOrDie(stdout_capture_filename_, &result);
  208. remove(stdout_capture_filename_.c_str());
  209. return result;
  210. }
  211. string GetCapturedTestStderr() {
  212. GOOGLE_CHECK_NE(original_stderr_, -1) << "Not capturing.";
  213. close(2);
  214. dup2(original_stderr_, 2);
  215. original_stderr_ = -1;
  216. string result;
  217. File::ReadFileToStringOrDie(stderr_capture_filename_, &result);
  218. remove(stderr_capture_filename_.c_str());
  219. return result;
  220. }
  221. ScopedMemoryLog* ScopedMemoryLog::active_log_ = NULL;
  222. ScopedMemoryLog::ScopedMemoryLog() {
  223. GOOGLE_CHECK(active_log_ == NULL);
  224. active_log_ = this;
  225. old_handler_ = SetLogHandler(&HandleLog);
  226. }
  227. ScopedMemoryLog::~ScopedMemoryLog() {
  228. SetLogHandler(old_handler_);
  229. active_log_ = NULL;
  230. }
  231. const std::vector<string>& ScopedMemoryLog::GetMessages(LogLevel level) {
  232. GOOGLE_CHECK(level == ERROR ||
  233. level == WARNING);
  234. return messages_[level];
  235. }
  236. void ScopedMemoryLog::HandleLog(LogLevel level, const char* filename,
  237. int line, const string& message) {
  238. GOOGLE_CHECK(active_log_ != NULL);
  239. if (level == ERROR || level == WARNING) {
  240. active_log_->messages_[level].push_back(message);
  241. }
  242. }
  243. namespace {
  244. // Force shutdown at process exit so that we can test for memory leaks. To
  245. // actually check for leaks, I suggest using the heap checker included with
  246. // google-perftools. Set it to "draconian" mode to ensure that every last
  247. // call to malloc() has a corresponding free().
  248. struct ForceShutdown {
  249. ~ForceShutdown() {
  250. ShutdownProtobufLibrary();
  251. // Test to shutdown the library twice, which should succeed.
  252. ShutdownProtobufLibrary();
  253. }
  254. } force_shutdown;
  255. } // namespace
  256. } // namespace protobuf
  257. } // namespace google