| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/terminator.h" | 
| Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 |  | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> | 
 | 20 | #include <gtest/gtest-spi.h> | 
 | 21 |  | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 22 | using testing::ExitedWithCode; | 
 | 23 |  | 
 | 24 | namespace chromeos_update_engine { | 
 | 25 |  | 
 | 26 | class TerminatorTest : public ::testing::Test { | 
 | 27 |  protected: | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 28 |   void SetUp() override { | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 29 |     Terminator::Init(); | 
 | 30 |     ASSERT_FALSE(Terminator::exit_blocked()); | 
 | 31 |     ASSERT_FALSE(Terminator::exit_requested()); | 
 | 32 |   } | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 33 |   void TearDown() override { | 
| Darin Petkov | 0a9a6ed | 2010-11-19 15:32:20 -0800 | [diff] [blame] | 34 |     // Makes sure subsequent non-Terminator tests don't get accidentally | 
 | 35 |     // terminated. | 
 | 36 |     Terminator::Init(); | 
 | 37 |   } | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 38 | }; | 
 | 39 |  | 
 | 40 | typedef TerminatorTest TerminatorDeathTest; | 
 | 41 |  | 
 | 42 | namespace { | 
 | 43 | void UnblockExitThroughUnblocker() { | 
 | 44 |   ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker(); | 
 | 45 | } | 
 | 46 |  | 
 | 47 | void RaiseSIGTERM() { | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 48 |   ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), ""); | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 49 | } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 50 | }  // namespace | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 51 |  | 
 | 52 | TEST_F(TerminatorTest, HandleSignalTest) { | 
 | 53 |   Terminator::set_exit_blocked(true); | 
 | 54 |   Terminator::HandleSignal(SIGTERM); | 
 | 55 |   ASSERT_TRUE(Terminator::exit_requested()); | 
 | 56 | } | 
 | 57 |  | 
 | 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 |  | 
 | 67 | TEST_F(TerminatorDeathTest, ExitTest) { | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 68 |   ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), ""); | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 69 |   Terminator::set_exit_blocked(true); | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 70 |   ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), ""); | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 71 | } | 
 | 72 |  | 
 | 73 | TEST_F(TerminatorDeathTest, RaiseSignalTest) { | 
 | 74 |   RaiseSIGTERM(); | 
 | 75 |   Terminator::set_exit_blocked(true); | 
 | 76 |   EXPECT_FATAL_FAILURE(RaiseSIGTERM(), ""); | 
 | 77 | } | 
 | 78 |  | 
 | 79 | TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) { | 
 | 80 |   Terminator::set_exit_blocked(true); | 
 | 81 |   Terminator::exit_requested_ = 1; | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 82 |   ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), ""); | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 83 | } | 
 | 84 |  | 
 | 85 | }  // namespace chromeos_update_engine |