Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 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 | |
| 10 | using std::string; |
| 11 | using testing::ExitedWithCode; |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
| 15 | class 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 Petkov | 0a9a6ed | 2010-11-19 15:32:20 -0800 | [diff] [blame] | 22 | virtual void TearDown() { |
| 23 | // Makes sure subsequent non-Terminator tests don't get accidentally |
| 24 | // terminated. |
| 25 | Terminator::Init(); |
| 26 | } |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | typedef TerminatorTest TerminatorDeathTest; |
| 30 | |
| 31 | namespace { |
| 32 | void UnblockExitThroughUnblocker() { |
| 33 | ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker(); |
| 34 | } |
| 35 | |
| 36 | void RaiseSIGTERM() { |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 37 | ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(2), ""); |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 38 | } |
| 39 | } // namespace {} |
| 40 | |
| 41 | TEST_F(TerminatorTest, HandleSignalTest) { |
| 42 | Terminator::set_exit_blocked(true); |
| 43 | Terminator::HandleSignal(SIGTERM); |
| 44 | ASSERT_TRUE(Terminator::exit_requested()); |
| 45 | } |
| 46 | |
| 47 | TEST_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 | |
| 56 | TEST_F(TerminatorDeathTest, ExitTest) { |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 57 | ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), ""); |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 58 | Terminator::set_exit_blocked(true); |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 59 | ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(2), ""); |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST_F(TerminatorDeathTest, RaiseSignalTest) { |
| 63 | RaiseSIGTERM(); |
| 64 | Terminator::set_exit_blocked(true); |
| 65 | EXPECT_FATAL_FAILURE(RaiseSIGTERM(), ""); |
| 66 | } |
| 67 | |
| 68 | TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) { |
| 69 | Terminator::set_exit_blocked(true); |
| 70 | Terminator::exit_requested_ = 1; |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 71 | ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(2), ""); |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | } // namespace chromeos_update_engine |