blob: 5a6f4e27ad944861dfa7f8ff5f81cd94ab1c981f [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
Alex Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/terminator.h"
6
Darin Petkov80f19562010-11-19 12:00:15 -08007#include <gtest/gtest.h>
8#include <gtest/gtest-spi.h>
9
Darin Petkov80f19562010-11-19 12:00:15 -080010using testing::ExitedWithCode;
11
12namespace chromeos_update_engine {
13
14class TerminatorTest : public ::testing::Test {
15 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080016 void SetUp() override {
Darin Petkov80f19562010-11-19 12:00:15 -080017 Terminator::Init();
18 ASSERT_FALSE(Terminator::exit_blocked());
19 ASSERT_FALSE(Terminator::exit_requested());
20 }
Alex Deymo610277e2014-11-11 21:18:11 -080021 void TearDown() override {
Darin Petkov0a9a6ed2010-11-19 15:32:20 -080022 // Makes sure subsequent non-Terminator tests don't get accidentally
23 // terminated.
24 Terminator::Init();
25 }
Darin Petkov80f19562010-11-19 12:00:15 -080026};
27
28typedef TerminatorTest TerminatorDeathTest;
29
30namespace {
31void UnblockExitThroughUnblocker() {
32 ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
33}
34
35void RaiseSIGTERM() {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070036 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080037}
Alex Vakulenkod2779df2014-06-16 13:19:00 -070038} // namespace
Darin Petkov80f19562010-11-19 12:00:15 -080039
40TEST_F(TerminatorTest, HandleSignalTest) {
41 Terminator::set_exit_blocked(true);
42 Terminator::HandleSignal(SIGTERM);
43 ASSERT_TRUE(Terminator::exit_requested());
44}
45
46TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) {
47 Terminator::set_exit_blocked(true);
48 ASSERT_TRUE(Terminator::exit_blocked());
49 ASSERT_FALSE(Terminator::exit_requested());
50 UnblockExitThroughUnblocker();
51 ASSERT_FALSE(Terminator::exit_blocked());
52 ASSERT_FALSE(Terminator::exit_requested());
53}
54
55TEST_F(TerminatorDeathTest, ExitTest) {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070056 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080057 Terminator::set_exit_blocked(true);
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070058 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080059}
60
61TEST_F(TerminatorDeathTest, RaiseSignalTest) {
62 RaiseSIGTERM();
63 Terminator::set_exit_blocked(true);
64 EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
65}
66
67TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
68 Terminator::set_exit_blocked(true);
69 Terminator::exit_requested_ = 1;
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070070 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080071}
72
73} // namespace chromeos_update_engine