1 // Copyright 2010 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "utils/signals/programmer.hpp" 30 31 extern "C" { 32 #include <signal.h> 33 #include <unistd.h> 34 } 35 36 #include <atf-c++.hpp> 37 38 #include "utils/sanity.hpp" 39 40 namespace signals = utils::signals; 41 42 43 namespace { 44 45 46 namespace sigchld { 47 48 49 static bool happened_1; 50 static bool happened_2; 51 52 53 void handler_1(const int signo) { 54 PRE(signo == SIGCHLD); 55 happened_1 = true; 56 } 57 58 59 void handler_2(const int signo) { 60 PRE(signo == SIGCHLD); 61 happened_2 = true; 62 } 63 64 65 } // namespace sigchld 66 67 68 } // anonymous namespace 69 70 71 ATF_TEST_CASE_WITHOUT_HEAD(program_unprogram); 72 ATF_TEST_CASE_BODY(program_unprogram) 73 { 74 signals::programmer programmer(SIGCHLD, sigchld::handler_1); 75 sigchld::happened_1 = false; 76 ::kill(::getpid(), SIGCHLD); 77 ATF_REQUIRE(sigchld::happened_1); 78 79 programmer.unprogram(); 80 sigchld::happened_1 = false; 81 ::kill(::getpid(), SIGCHLD); 82 ATF_REQUIRE(!sigchld::happened_1); 83 } 84 85 86 ATF_TEST_CASE_WITHOUT_HEAD(scope); 87 ATF_TEST_CASE_BODY(scope) 88 { 89 { 90 signals::programmer programmer(SIGCHLD, sigchld::handler_1); 91 sigchld::happened_1 = false; 92 ::kill(::getpid(), SIGCHLD); 93 ATF_REQUIRE(sigchld::happened_1); 94 } 95 96 sigchld::happened_1 = false; 97 ::kill(::getpid(), SIGCHLD); 98 ATF_REQUIRE(!sigchld::happened_1); 99 } 100 101 102 ATF_TEST_CASE_WITHOUT_HEAD(nested); 103 ATF_TEST_CASE_BODY(nested) 104 { 105 signals::programmer programmer_1(SIGCHLD, sigchld::handler_1); 106 sigchld::happened_1 = false; 107 sigchld::happened_2 = false; 108 ::kill(::getpid(), SIGCHLD); 109 ATF_REQUIRE(sigchld::happened_1); 110 ATF_REQUIRE(!sigchld::happened_2); 111 112 signals::programmer programmer_2(SIGCHLD, sigchld::handler_2); 113 sigchld::happened_1 = false; 114 sigchld::happened_2 = false; 115 ::kill(::getpid(), SIGCHLD); 116 ATF_REQUIRE(!sigchld::happened_1); 117 ATF_REQUIRE(sigchld::happened_2); 118 119 programmer_2.unprogram(); 120 sigchld::happened_1 = false; 121 sigchld::happened_2 = false; 122 ::kill(::getpid(), SIGCHLD); 123 ATF_REQUIRE(sigchld::happened_1); 124 ATF_REQUIRE(!sigchld::happened_2); 125 126 programmer_1.unprogram(); 127 sigchld::happened_1 = false; 128 sigchld::happened_2 = false; 129 ::kill(::getpid(), SIGCHLD); 130 ATF_REQUIRE(!sigchld::happened_1); 131 ATF_REQUIRE(!sigchld::happened_2); 132 } 133 134 135 ATF_INIT_TEST_CASES(tcs) 136 { 137 ATF_ADD_TEST_CASE(tcs, program_unprogram); 138 ATF_ADD_TEST_CASE(tcs, scope); 139 ATF_ADD_TEST_CASE(tcs, nested); 140 } 141