github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/cmd/libsnap-confine-private/panic-test.c (about)

     1  /*
     2   * Copyright (C) 2019 Canonical Ltd
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  
    18  #include "panic.h"
    19  #include "panic.c"
    20  
    21  #include <glib.h>
    22  
    23  static void test_panic(void)
    24  {
    25  	if (g_test_subprocess()) {
    26  		errno = 0;
    27  		sc_panic("death message");
    28  		g_test_message("expected die not to return");
    29  		g_test_fail();
    30  		return;
    31  	}
    32  	g_test_trap_subprocess(NULL, 0, 0);
    33  	g_test_trap_assert_failed();
    34  	g_test_trap_assert_stderr("death message\n");
    35  }
    36  
    37  static void test_panic_with_errno(void)
    38  {
    39  	if (g_test_subprocess()) {
    40  		errno = EPERM;
    41  		sc_panic("death message");
    42  		g_test_message("expected die not to return");
    43  		g_test_fail();
    44  		return;
    45  	}
    46  	g_test_trap_subprocess(NULL, 0, 0);
    47  	g_test_trap_assert_failed();
    48  	g_test_trap_assert_stderr("death message: Operation not permitted\n");
    49  }
    50  
    51  static void custom_panic_msg(const char *fmt, va_list ap, int errno_copy)
    52  {
    53  	fprintf(stderr, "PANIC: ");
    54  	vfprintf(stderr, fmt, ap);
    55  	fprintf(stderr, " (errno: %d)", errno_copy);
    56  	fprintf(stderr, "\n");
    57  }
    58  
    59  static void custom_panic_exit(void)
    60  {
    61  	fprintf(stderr, "EXITING\n");
    62  	exit(2);
    63  }
    64  
    65  static void test_panic_customization(void)
    66  {
    67  	if (g_test_subprocess()) {
    68  		sc_set_panic_msg_fn(custom_panic_msg);
    69  		sc_set_panic_exit_fn(custom_panic_exit);
    70  		errno = 123;
    71  		sc_panic("death message");
    72  		g_test_message("expected die not to return");
    73  		g_test_fail();
    74  		return;
    75  	}
    76  	g_test_trap_subprocess(NULL, 0, 0);
    77  	g_test_trap_assert_failed();
    78  	g_test_trap_assert_stderr("PANIC: death message (errno: 123)\n"
    79  				  "EXITING\n");
    80  	// NOTE: g_test doesn't offer facilities to observe the exit code.
    81  }
    82  
    83  static void __attribute__((constructor)) init(void)
    84  {
    85  	g_test_add_func("/panic/panic", test_panic);
    86  	g_test_add_func("/panic/panic_with_errno", test_panic_with_errno);
    87  	g_test_add_func("/panic/panic_customization", test_panic_customization);
    88  }