cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

assert-signal.h (3204B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#ifndef GUAC_LIBGUAC_TESTS_ASSERT_SIGNAL_H
     21#define GUAC_LIBGUAC_TESTS_ASSERT_SIGNAL_H
     22
     23#include <CUnit/CUnit.h>
     24#include <signal.h>
     25#include <stdlib.h>
     26#include <sys/wait.h>
     27#include <unistd.h>
     28
     29/**
     30 * Verifies that the given test terminates the calling process with the given
     31 * signal.
     32 *
     33 * @param sig
     34 *     The signal that is expected to terminate the calling process.
     35 *
     36 * @param test
     37 *     The test that is expected to terminate the calling process with the
     38 *     given signal.
     39 */
     40#define ASSERT_SIGNALLED(sig, test) \
     41    do {                                                                      \
     42                                                                              \
     43        /* Fork to ensure test can safely terminate */                        \
     44        pid_t _child = fork();                                                \
     45        CU_ASSERT_NOT_EQUAL_FATAL(_child, -1);                                \
     46                                                                              \
     47        /* Run test strictly within child process */                          \
     48        if (_child == 0) {                                                    \
     49            do { test; } while (0);                                           \
     50            exit(0);                                                          \
     51        }                                                                     \
     52                                                                              \
     53        /* Wait for child process to terminate */                             \
     54        int _status = 0;                                                      \
     55        CU_ASSERT_EQUAL_FATAL(waitpid(_child, &_status, 0), _child);          \
     56                                                                              \
     57        /* Verify process terminated with expected signal */                  \
     58        if (WIFSIGNALED(_status)) {                                           \
     59            CU_ASSERT_EQUAL(WTERMSIG(_status), (sig));                        \
     60        }                                                                     \
     61        else                                                                  \
     62            CU_FAIL("Process did not terminate due to a signal");             \
     63                                                                              \
     64    } while (0)
     65
     66#endif
     67