blob: 68d4d136e4322eff3e378e58b6a81639b6989b3e [file] [log] [blame]
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov80f19562010-11-19 12:00:15 -08002// 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() {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070037 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080038}
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) {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070057 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080058 Terminator::set_exit_blocked(true);
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070059 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080060}
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;
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070071 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080072}
73
74} // namespace chromeos_update_engine