gtest_premature_exit_test.cc
Go to the documentation of this file.
00001 // Copyright 2013, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 //
00032 // Tests that Google Test manipulates the premature-exit-detection
00033 // file correctly.
00034 
00035 #include <stdio.h>
00036 
00037 #include "gtest/gtest.h"
00038 
00039 using ::testing::InitGoogleTest;
00040 using ::testing::Test;
00041 using ::testing::internal::posix::GetEnv;
00042 using ::testing::internal::posix::Stat;
00043 using ::testing::internal::posix::StatStruct;
00044 
00045 namespace {
00046 
00047 // Is the TEST_PREMATURE_EXIT_FILE environment variable expected to be
00048 // set?
00049 const bool kTestPrematureExitFileEnvVarShouldBeSet = false;
00050 
00051 class PrematureExitTest : public Test {
00052  public:
00053   // Returns true iff the given file exists.
00054   static bool FileExists(const char* filepath) {
00055     StatStruct stat;
00056     return Stat(filepath, &stat) == 0;
00057   }
00058 
00059  protected:
00060   PrematureExitTest() {
00061     premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
00062 
00063     // Normalize NULL to "" for ease of handling.
00064     if (premature_exit_file_path_ == NULL) {
00065       premature_exit_file_path_ = "";
00066     }
00067   }
00068 
00069   // Returns true iff the premature-exit file exists.
00070   bool PrematureExitFileExists() const {
00071     return FileExists(premature_exit_file_path_);
00072   }
00073 
00074   const char* premature_exit_file_path_;
00075 };
00076 
00077 typedef PrematureExitTest PrematureExitDeathTest;
00078 
00079 // Tests that:
00080 //   - the premature-exit file exists during the execution of a
00081 //     death test (EXPECT_DEATH*), and
00082 //   - a death test doesn't interfere with the main test process's
00083 //     handling of the premature-exit file.
00084 TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
00085   if (*premature_exit_file_path_ == '\0') {
00086     return;
00087   }
00088 
00089   EXPECT_DEATH_IF_SUPPORTED({
00090       // If the file exists, crash the process such that the main test
00091       // process will catch the (expected) crash and report a success;
00092       // otherwise don't crash, which will cause the main test process
00093       // to report that the death test has failed.
00094       if (PrematureExitFileExists()) {
00095         exit(1);
00096       }
00097     }, "");
00098 }
00099 
00100 // Tests that TEST_PREMATURE_EXIT_FILE is set where it's expected to
00101 // be set.
00102 TEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
00103   GTEST_INTENTIONAL_CONST_COND_PUSH_()
00104   if (kTestPrematureExitFileEnvVarShouldBeSet) {
00105   GTEST_INTENTIONAL_CONST_COND_POP_()
00106     const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00107     ASSERT_TRUE(filepath != NULL);
00108     ASSERT_NE(*filepath, '\0');
00109   }
00110 }
00111 
00112 // Tests that the premature-exit file exists during the execution of a
00113 // normal (non-death) test.
00114 TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
00115   if (*premature_exit_file_path_ == '\0') {
00116     return;
00117   }
00118 
00119   EXPECT_TRUE(PrematureExitFileExists())
00120       << " file " << premature_exit_file_path_
00121       << " should exist during test execution, but doesn't.";
00122 }
00123 
00124 }  // namespace
00125 
00126 int main(int argc, char **argv) {
00127   InitGoogleTest(&argc, argv);
00128   const int exit_code = RUN_ALL_TESTS();
00129 
00130   // Test that the premature-exit file is deleted upon return from
00131   // RUN_ALL_TESTS().
00132   const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
00133   if (filepath != NULL && *filepath != '\0') {
00134     if (PrematureExitTest::FileExists(filepath)) {
00135       printf(
00136           "File %s shouldn't exist after the test program finishes, but does.",
00137           filepath);
00138       return 1;
00139     }
00140   }
00141 
00142   return exit_code;
00143 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04