Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | } |
| 22 | }; |
| 23 | |
| 24 | typedef TerminatorTest TerminatorDeathTest; |
| 25 | |
| 26 | namespace { |
| 27 | void UnblockExitThroughUnblocker() { |
| 28 | ScopedTerminatorExitUnblocker unblocker = ScopedTerminatorExitUnblocker(); |
| 29 | } |
| 30 | |
| 31 | void RaiseSIGTERM() { |
| 32 | ASSERT_EXIT(raise(SIGTERM), ExitedWithCode(0), ""); |
| 33 | } |
| 34 | } // namespace {} |
| 35 | |
| 36 | TEST_F(TerminatorTest, HandleSignalTest) { |
| 37 | Terminator::set_exit_blocked(true); |
| 38 | Terminator::HandleSignal(SIGTERM); |
| 39 | ASSERT_TRUE(Terminator::exit_requested()); |
| 40 | } |
| 41 | |
| 42 | TEST_F(TerminatorTest, ScopedTerminatorExitUnblockerTest) { |
| 43 | Terminator::set_exit_blocked(true); |
| 44 | ASSERT_TRUE(Terminator::exit_blocked()); |
| 45 | ASSERT_FALSE(Terminator::exit_requested()); |
| 46 | UnblockExitThroughUnblocker(); |
| 47 | ASSERT_FALSE(Terminator::exit_blocked()); |
| 48 | ASSERT_FALSE(Terminator::exit_requested()); |
| 49 | } |
| 50 | |
| 51 | TEST_F(TerminatorDeathTest, ExitTest) { |
| 52 | ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), ""); |
| 53 | Terminator::set_exit_blocked(true); |
| 54 | ASSERT_EXIT(Terminator::Exit(), ExitedWithCode(0), ""); |
| 55 | } |
| 56 | |
| 57 | TEST_F(TerminatorDeathTest, RaiseSignalTest) { |
| 58 | RaiseSIGTERM(); |
| 59 | Terminator::set_exit_blocked(true); |
| 60 | EXPECT_FATAL_FAILURE(RaiseSIGTERM(), ""); |
| 61 | } |
| 62 | |
| 63 | TEST_F(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest) { |
| 64 | Terminator::set_exit_blocked(true); |
| 65 | Terminator::exit_requested_ = 1; |
| 66 | ASSERT_EXIT(UnblockExitThroughUnblocker(), ExitedWithCode(0), ""); |
| 67 | } |
| 68 | |
| 69 | } // namespace chromeos_update_engine |