1 //
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 //
16
17 #include "update_engine/common/terminator.h"
18
19 #include <gtest/gtest.h>
20 #include <gtest/gtest-spi.h>
21
22 using testing::ExitedWithCode;
23
24 namespace chromeos_update_engine {
25
26 class TerminatorTest : public ::testing::Test {
27 protected:
SetUp()28 void SetUp() override {
29 Terminator::Init();
30 ASSERT_FALSE(Terminator::exit_blocked());
31 ASSERT_FALSE(Terminator::exit_requested());
32 }
TearDown()33 void TearDown() override {
34 // Makes sure subsequent non-Terminator tests don't get accidentally
35 // terminated.
36 Terminator::Init();
37 }
38 };
39
40 typedef TerminatorTest TerminatorDeathTest;
41
42 namespace {
UnblockExitThroughUnblocker()43 void UnblockExitThroughUnblocker() {
44 ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker();
45 }
46
RaiseSIGTERM()47 void RaiseSIGTERM() {
48 ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), "");
49 }
50 } // namespace
51
TEST_F(TerminatorTest,HandleSignalTest)52 TEST_F(TerminatorTest, HandleSignalTest) {
53 Terminator::set_exit_blocked(true);
54 Terminator::HandleSignal(SIGTERM);
55 ASSERT_TRUE(Terminator::exit_requested());
56 }
57
TEST_F(TerminatorTest,ScopedTerminatorExitUnblockerTest)58 TEST_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
TEST_F(TerminatorDeathTest,ExitTest)67 TEST_F(TerminatorDeathTest, ExitTest) {
68 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
69 Terminator::set_exit_blocked(true);
70 ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), "");
71 }
72
TEST_F(TerminatorDeathTest,RaiseSignalTest)73 TEST_F(TerminatorDeathTest, RaiseSignalTest) {
74 RaiseSIGTERM();
75 Terminator::set_exit_blocked(true);
76 EXPECT_FATAL_FAILURE(RaiseSIGTERM(), "");
77 }
78
TEST_F(TerminatorDeathTest,ScopedTerminatorExitUnblockerExitTest)79 TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) {
80 Terminator::set_exit_blocked(true);
81 Terminator::exit_requested_ = 1;
82 ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), "");
83 }
84
85 } // namespace chromeos_update_engine
86