github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/libsnap-confine-private/fault-injection-test.c (about)

     1  /*
     2   * Copyright (C) 2017 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 "fault-injection.h"
    19  #include "fault-injection.c"
    20  
    21  #include <errno.h>
    22  #include <glib.h>
    23  
    24  static bool broken(struct sc_fault_state *state, void *ptr)
    25  {
    26  	return true;
    27  }
    28  
    29  static bool broken_alter_msg(struct sc_fault_state *state, void *ptr)
    30  {
    31  	char **s = ptr;
    32  	*s = "broken";
    33  	return true;
    34  }
    35  
    36  static void test_fault_injection(void)
    37  {
    38  	g_assert_false(sc_faulty("foo", NULL));
    39  
    40  	sc_break("foo", broken);
    41  	g_assert_true(sc_faulty("foo", NULL));
    42  
    43  	sc_reset_faults();
    44  	g_assert_false(sc_faulty("foo", NULL));
    45  
    46  	const char *msg = NULL;
    47  	if (!sc_faulty("foo", &msg)) {
    48  		msg = "working";
    49  	}
    50  	g_assert_cmpstr(msg, ==, "working");
    51  
    52  	sc_break("foo", broken_alter_msg);
    53  	if (!sc_faulty("foo", &msg)) {
    54  		msg = "working";
    55  	}
    56  	g_assert_cmpstr(msg, ==, "broken");
    57  	sc_reset_faults();
    58  }
    59  
    60  static void __attribute__((constructor)) init(void)
    61  {
    62  	g_test_add_func("/fault-injection", test_fault_injection);
    63  }