blob: 8582b99e705a35d08b92353e209dc76702107cb4 [file] [log] [blame]
Darin Petkov80f19562010-11-19 12:00:15 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6#include <gtest/gtest-spi.h>
7
8#include "update_engine/terminator.h"
9
10using std::string;
11using testing::ExitedWithCode;
12
13namespace chromeos_update_engine {
14
15class TerminatorTest : public ::testing::Test {
16 protected:
17 virtual void SetUp() {
18 Terminator::Init();
19 ASSERT_FALSE(Terminator::exit_blocked());
20 ASSERT_FALSE(Terminator::exit_requested());
21 }
Darin Petkov0a9a6ed2010-11-19 15:32:20 -080022 virtual void TearDown() {
23 // Makes sure subsequent non-Terminator tests don't get accidentally
24 // terminated.
25 Terminator::Init();
26 }
Darin Petkov80f19562010-11-19 12:00:15 -080027};
28
29typedef TerminatorTest TerminatorDeathTest;
30
31namespace {
32void UnblockExitThroughUnblocker() {
33 ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
34}
35
36void RaiseSIGTERM() {
37 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(0), "");
38}
39} // namespace {}
40
41TEST_F(TerminatorTest, HandleSignalTest) {
42 Terminator::set_exit_blocked(true);
43 Terminator::HandleSignal(SIGTERM);
44 ASSERT_TRUE(Terminator::exit_requested());
45}
46
47TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) {
48 Terminator::set_exit_blocked(true);
49 ASSERT_TRUE(Terminator::exit_blocked());
50 ASSERT_FALSE(Terminator::exit_requested());
51 UnblockExitThroughUnblocker();
52 ASSERT_FALSE(Terminator::exit_blocked());
53 ASSERT_FALSE(Terminator::exit_requested());
54}
55
56TEST_F(TerminatorDeathTest, ExitTest) {
57 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
58 Terminator::set_exit_blocked(true);
59 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
60}
61
62TEST_F(TerminatorDeathTest, RaiseSignalTest) {
63 RaiseSIGTERM();
64 Terminator::set_exit_blocked(true);
65 EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
66}
67
68TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
69 Terminator::set_exit_blocked(true);
70 Terminator::exit_requested_ = 1;
71 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(0), "");
72}
73
74} // namespace chromeos_update_engine