Blame googletest/test/gtest_premature_exit_test.cc

Packit bd1cd8
// Copyright 2013, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
//
Packit bd1cd8
// Tests that Google Test manipulates the premature-exit-detection
Packit bd1cd8
// file correctly.
Packit bd1cd8
Packit bd1cd8
#include <stdio.h>
Packit bd1cd8
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
using ::testing::InitGoogleTest;
Packit bd1cd8
using ::testing::Test;
Packit bd1cd8
using ::testing::internal::posix::GetEnv;
Packit bd1cd8
using ::testing::internal::posix::Stat;
Packit bd1cd8
using ::testing::internal::posix::StatStruct;
Packit bd1cd8
Packit bd1cd8
namespace {
Packit bd1cd8
Packit bd1cd8
class PrematureExitTest : public Test {
Packit bd1cd8
 public:
Packit bd1cd8
  // Returns true iff the given file exists.
Packit bd1cd8
  static bool FileExists(const char* filepath) {
Packit bd1cd8
    StatStruct stat;
Packit bd1cd8
    return Stat(filepath, &stat) == 0;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
 protected:
Packit bd1cd8
  PrematureExitTest() {
Packit bd1cd8
    premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
Packit bd1cd8
Packit bd1cd8
    // Normalize NULL to "" for ease of handling.
Packit bd1cd8
    if (premature_exit_file_path_ == NULL) {
Packit bd1cd8
      premature_exit_file_path_ = "";
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // Returns true iff the premature-exit file exists.
Packit bd1cd8
  bool PrematureExitFileExists() const {
Packit bd1cd8
    return FileExists(premature_exit_file_path_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  const char* premature_exit_file_path_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
typedef PrematureExitTest PrematureExitDeathTest;
Packit bd1cd8
Packit bd1cd8
// Tests that:
Packit bd1cd8
//   - the premature-exit file exists during the execution of a
Packit bd1cd8
//     death test (EXPECT_DEATH*), and
Packit bd1cd8
//   - a death test doesn't interfere with the main test process's
Packit bd1cd8
//     handling of the premature-exit file.
Packit bd1cd8
TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
Packit bd1cd8
  if (*premature_exit_file_path_ == '\0') {
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  EXPECT_DEATH_IF_SUPPORTED({
Packit bd1cd8
      // If the file exists, crash the process such that the main test
Packit bd1cd8
      // process will catch the (expected) crash and report a success;
Packit bd1cd8
      // otherwise don't crash, which will cause the main test process
Packit bd1cd8
      // to report that the death test has failed.
Packit bd1cd8
      if (PrematureExitFileExists()) {
Packit bd1cd8
        exit(1);
Packit bd1cd8
      }
Packit bd1cd8
    }, "");
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests that the premature-exit file exists during the execution of a
Packit bd1cd8
// normal (non-death) test.
Packit bd1cd8
TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
Packit bd1cd8
  if (*premature_exit_file_path_ == '\0') {
Packit bd1cd8
    return;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  EXPECT_TRUE(PrematureExitFileExists())
Packit bd1cd8
      << " file " << premature_exit_file_path_
Packit bd1cd8
      << " should exist during test execution, but doesn't.";
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace
Packit bd1cd8
Packit bd1cd8
int main(int argc, char **argv) {
Packit bd1cd8
  InitGoogleTest(&argc, argv);
Packit bd1cd8
  const int exit_code = RUN_ALL_TESTS();
Packit bd1cd8
Packit bd1cd8
  // Test that the premature-exit file is deleted upon return from
Packit bd1cd8
  // RUN_ALL_TESTS().
Packit bd1cd8
  const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
Packit bd1cd8
  if (filepath != NULL && *filepath != '\0') {
Packit bd1cd8
    if (PrematureExitTest::FileExists(filepath)) {
Packit bd1cd8
      printf(
Packit bd1cd8
          "File %s shouldn't exist after the test program finishes, but does.",
Packit bd1cd8
          filepath);
Packit bd1cd8
      return 1;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  return exit_code;
Packit bd1cd8
}