1aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Copyright 2013, Google Inc.
2aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// All rights reserved.
3aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
4aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Redistribution and use in source and binary forms, with or without
5aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// modification, are permitted provided that the following conditions are
6aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// met:
7aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
8aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Redistributions of source code must retain the above copyright
9aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// notice, this list of conditions and the following disclaimer.
10aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Redistributions in binary form must reproduce the above
11aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// copyright notice, this list of conditions and the following disclaimer
12aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// in the documentation and/or other materials provided with the
13aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// distribution.
14aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Neither the name of Google Inc. nor the names of its
15aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// contributors may be used to endorse or promote products derived from
16aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// this software without specific prior written permission.
17aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
18aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
30aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Author: wan@google.com (Zhanyong Wan)
31aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
32aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Tests that Google Test manipulates the premature-exit-detection
33aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// file correctly.
34aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
35aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include <stdio.h>
36aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
37aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include "gtest/gtest.h"
38aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
39aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterusing ::testing::InitGoogleTest;
40aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterusing ::testing::Test;
41aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterusing ::testing::internal::posix::GetEnv;
42aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterusing ::testing::internal::posix::Stat;
43aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterusing ::testing::internal::posix::StatStruct;
44aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
45aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchternamespace {
46aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
47aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Is the TEST_PREMATURE_EXIT_FILE environment variable expected to be
48aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// set?
49aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterconst bool kTestPrematureExitFileEnvVarShouldBeSet = false;
50aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
51aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterclass PrematureExitTest : public Test {
52aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter public:
53aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // Returns true iff the given file exists.
54aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  static bool FileExists(const char* filepath) {
55aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    StatStruct stat;
56aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    return Stat(filepath, &stat) == 0;
57aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
58aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
59aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter protected:
60aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  PrematureExitTest() {
61aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
62aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
63aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    // Normalize NULL to "" for ease of handling.
64aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    if (premature_exit_file_path_ == NULL) {
65aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      premature_exit_file_path_ = "";
66aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    }
67aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
68aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
69aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // Returns true iff the premature-exit file exists.
70aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  bool PrematureExitFileExists() const {
71aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    return FileExists(premature_exit_file_path_);
72aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
73aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
74aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  const char* premature_exit_file_path_;
75aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter};
76aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
77aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchtertypedef PrematureExitTest PrematureExitDeathTest;
78aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
79aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Tests that:
80aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//   - the premature-exit file exists during the execution of a
81aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     death test (EXPECT_DEATH*), and
82aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//   - a death test doesn't interfere with the main test process's
83aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     handling of the premature-exit file.
84aa46da279e2426caf1e103eb079dfec8124c5feeCourtney GoeltzenleuchterTEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
85aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  if (*premature_exit_file_path_ == '\0') {
86aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    return;
87aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
88aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
89aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  EXPECT_DEATH_IF_SUPPORTED({
90aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      // If the file exists, crash the process such that the main test
91aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      // process will catch the (expected) crash and report a success;
92aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      // otherwise don't crash, which will cause the main test process
93aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      // to report that the death test has failed.
94aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      if (PrematureExitFileExists()) {
95aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter        exit(1);
96aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      }
97aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    }, "");
98aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
99aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
100aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Tests that TEST_PREMATURE_EXIT_FILE is set where it's expected to
101aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// be set.
102aa46da279e2426caf1e103eb079dfec8124c5feeCourtney GoeltzenleuchterTEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
103aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  if (kTestPrematureExitFileEnvVarShouldBeSet) {
104aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
105aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    ASSERT_TRUE(filepath != NULL);
106aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    ASSERT_NE(*filepath, '\0');
107aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
108aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
109aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
110aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Tests that the premature-exit file exists during the execution of a
111aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// normal (non-death) test.
112aa46da279e2426caf1e103eb079dfec8124c5feeCourtney GoeltzenleuchterTEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
113aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  if (*premature_exit_file_path_ == '\0') {
114aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    return;
115aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
116aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
117aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  EXPECT_TRUE(PrematureExitFileExists())
118aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      << " file " << premature_exit_file_path_
119aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      << " should exist during test execution, but doesn't.";
120aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
121aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
122aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}  // namespace
123aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
124aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterint main(int argc, char **argv) {
125aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  InitGoogleTest(&argc, argv);
126aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  const int exit_code = RUN_ALL_TESTS();
127aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
128aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // Test that the premature-exit file is deleted upon return from
129aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // RUN_ALL_TESTS().
130aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
131aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  if (filepath != NULL && *filepath != '\0') {
132aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    if (PrematureExitTest::FileExists(filepath)) {
133aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      printf(
134aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter          "File %s shouldn't exist after the test program finishes, but does.",
135aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter          filepath);
136aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter      return 1;
137aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter    }
138aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  }
139aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
140aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  return exit_code;
141aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
142