blob: e1fb5734ae0b23215bc00fa9e79f243b3cfec758 [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 }
22};
23
24typedef TerminatorTest TerminatorDeathTest;
25
26namespace {
27void UnblockExitThroughUnblocker() {
28 ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
29}
30
31void RaiseSIGTERM() {
32 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(0), "");
33}
34} // namespace {}
35
36TEST_F(TerminatorTest, HandleSignalTest) {
37 Terminator::set_exit_blocked(true);
38 Terminator::HandleSignal(SIGTERM);
39 ASSERT_TRUE(Terminator::exit_requested());
40}
41
42TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) {
43 Terminator::set_exit_blocked(true);
44 ASSERT_TRUE(Terminator::exit_blocked());
45 ASSERT_FALSE(Terminator::exit_requested());
46 UnblockExitThroughUnblocker();
47 ASSERT_FALSE(Terminator::exit_blocked());
48 ASSERT_FALSE(Terminator::exit_requested());
49}
50
51TEST_F(TerminatorDeathTest, ExitTest) {
52 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
53 Terminator::set_exit_blocked(true);
54 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), "");
55}
56
57TEST_F(TerminatorDeathTest, RaiseSignalTest) {
58 RaiseSIGTERM();
59 Terminator::set_exit_blocked(true);
60 EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
61}
62
63TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
64 Terminator::set_exit_blocked(true);
65 Terminator::exit_requested_ = 1;
66 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(0), "");
67}
68
69} // namespace chromeos_update_engine