blob: b0f419188d7dbdc97fd67491ba42936c8d570866 [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() {
Darin Petkov8e07f1e2010-11-22 10:57:37 -080037 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(1), "");
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) {
Darin Petkov8e07f1e2010-11-22 10:57:37 -080057 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(1), "");
Darin Petkov80f19562010-11-19 12:00:15 -080058 Terminator::set_exit_blocked(true);
Darin Petkov8e07f1e2010-11-22 10:57:37 -080059 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(1), "");
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;
Darin Petkov8e07f1e2010-11-22 10:57:37 -080071 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(1), "");
Darin Petkov80f19562010-11-19 12:00:15 -080072}
73
74} // namespace chromeos_update_engine