blob: 4d96a5d78f5eab29d41ab12371807e3046f6cfe9 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Darin Petkov80f19562010-11-19 12:00:15 -080016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/terminator.h"
18
Darin Petkov80f19562010-11-19 12:00:15 -080019#include <gtest/gtest.h>
20#include <gtest/gtest-spi.h>
21
Darin Petkov80f19562010-11-19 12:00:15 -080022using testing::ExitedWithCode;
23
24namespace chromeos_update_engine {
25
26class TerminatorTest : public ::testing::Test {
27 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080028 void SetUp() override {
Darin Petkov80f19562010-11-19 12:00:15 -080029 Terminator::Init();
30 ASSERT_FALSE(Terminator::exit_blocked());
31 ASSERT_FALSE(Terminator::exit_requested());
32 }
Alex Deymo610277e2014-11-11 21:18:11 -080033 void TearDown() override {
Darin Petkov0a9a6ed2010-11-19 15:32:20 -080034 // Makes sure subsequent non-Terminator tests don't get accidentally
35 // terminated.
36 Terminator::Init();
37 }
Darin Petkov80f19562010-11-19 12:00:15 -080038};
39
40typedef TerminatorTest TerminatorDeathTest;
41
42namespace {
43void UnblockExitThroughUnblocker() {
44 ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
45}
46
47void RaiseSIGTERM() {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070048 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080049}
Alex Vakulenkod2779df2014-06-16 13:19:00 -070050} // namespace
Darin Petkov80f19562010-11-19 12:00:15 -080051
52TEST_F(TerminatorTest, HandleSignalTest) {
53 Terminator::set_exit_blocked(true);
54 Terminator::HandleSignal(SIGTERM);
55 ASSERT_TRUE(Terminator::exit_requested());
56}
57
58TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) {
59 Terminator::set_exit_blocked(true);
60 ASSERT_TRUE(Terminator::exit_blocked());
61 ASSERT_FALSE(Terminator::exit_requested());
62 UnblockExitThroughUnblocker();
63 ASSERT_FALSE(Terminator::exit_blocked());
64 ASSERT_FALSE(Terminator::exit_requested());
65}
66
67TEST_F(TerminatorDeathTest, ExitTest) {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070068 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080069 Terminator::set_exit_blocked(true);
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070070 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080071}
72
73TEST_F(TerminatorDeathTest, RaiseSignalTest) {
74 RaiseSIGTERM();
75 Terminator::set_exit_blocked(true);
76 EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
77}
78
79TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
80 Terminator::set_exit_blocked(true);
81 Terminator::exit_requested_ = 1;
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070082 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), "");
Darin Petkov80f19562010-11-19 12:00:15 -080083}
84
85} // namespace chromeos_update_engine