void test_ncm_obj_array_traps (TestNcmObjArray *test, gconstpointer pdata) { #if GLIB_CHECK_VERSION(2,38,0) g_test_trap_subprocess ("/ncm/obj_array/invalid/add/subprocess", 0, 0); g_test_trap_assert_failed (); g_test_trap_subprocess ("/ncm/obj_array/invalid/set/subprocess", 0, 0); g_test_trap_assert_failed (); #endif }
static void test_sc_do_optional_mount_failure(gconstpointer snap_debug) { if (g_test_subprocess()) { sc_break("mount", broken_mount); if (GPOINTER_TO_INT(snap_debug) == 1) { g_setenv("SNAP_CONFINE_DEBUG", "1", true); } (void)sc_do_optional_mount("/foo", "/bar", "ext4", MS_RDONLY, NULL); g_test_message("expected sc_do_mount not to return"); sc_reset_faults(); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); if (GPOINTER_TO_INT(snap_debug) == 0) { g_test_trap_assert_stderr ("cannot perform operation: mount -t ext4 -o ro /foo /bar: Permission denied\n"); } else { /* with snap_debug the debug output hides the actual mount commands *but* * they are still shown if there was an error */ g_test_trap_assert_stderr ("DEBUG: performing operation: (disabled) use debug build to see details\n" "cannot perform operation: mount -t ext4 -o ro /foo /bar: Permission denied\n"); } }
static void test_dynamic_globalprop_nouser(void) { g_test_trap_subprocess("/qdev/properties/dynamic/global/nouser/subprocess", 0, 0); g_test_trap_assert_passed(); g_test_trap_assert_stderr(""); g_test_trap_assert_stdout(""); }
static void test_static_prop(void) { g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0); g_test_trap_assert_passed(); g_test_trap_assert_stderr(""); g_test_trap_assert_stdout(""); }
static void test_slice_debug (void) { const gchar *oldval; oldval = g_getenv ("G_SLICE"); g_setenv ("G_SLICE", "debug-blocks:always-malloc", TRUE); if (g_test_subprocess ()) { gpointer p, q; p = g_slice_alloc (237); q = g_slice_alloc (259); g_slice_free1 (237, p); g_slice_free1 (259, q); g_slice_debug_tree_statistics (); return; } g_test_trap_subprocess (NULL, 1000000, 0); g_test_trap_assert_passed (); g_test_trap_assert_stderr ("*GSlice: MemChecker: * trunks, * branches, * old branches*"); if (oldval) g_setenv ("G_SLICE", oldval, TRUE); else g_unsetenv ("G_SLICE"); }
/* We test deprecated functionality here */ G_GNUC_BEGIN_IGNORE_DEPRECATIONS #ifdef G_ENABLE_DEBUG static void test_slice_nodebug (void) { const gchar *oldval; oldval = g_getenv ("G_SLICE"); g_unsetenv ("G_SLICE"); if (g_test_subprocess ()) { gpointer p, q; p = g_slice_alloc (237); q = g_slice_alloc (259); g_slice_free1 (237, p); g_slice_free1 (259, q); g_slice_debug_tree_statistics (); return; } g_test_trap_subprocess (NULL, 1000000, 0); g_test_trap_assert_passed (); g_test_trap_assert_stderr ("*GSlice: MemChecker: root=NULL*"); if (oldval) g_setenv ("G_SLICE", oldval, TRUE); }
static void test_sc_is_ns_group_dir_private() { if (geteuid() != 0) { g_test_skip("this test needs to run as root"); return; } const char *ns_dir = sc_test_use_fake_ns_dir(); g_test_queue_destroy(unmount_dir, (char *)ns_dir); if (g_test_subprocess()) { // The temporary directory should not be private initially g_assert_false(sc_is_ns_group_dir_private()); /// do what "mount --bind /foo /foo; mount --make-private /foo" does. int err; err = mount(ns_dir, ns_dir, NULL, MS_BIND, NULL); g_assert_cmpint(err, ==, 0); err = mount(NULL, ns_dir, NULL, MS_PRIVATE, NULL); g_assert_cmpint(err, ==, 0); // The temporary directory should now be private g_assert_true(sc_is_ns_group_dir_private()); return; } g_test_trap_subprocess(NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR); g_test_trap_assert_passed(); }
static void test_sc_initialize_ns_groups() { if (geteuid() != 0) { g_test_skip("this test needs to run as root"); return; } // NOTE: this is g_test_subprocess aware! const char *ns_dir = sc_test_use_fake_ns_dir(); g_test_queue_destroy(unmount_dir, (char *)ns_dir); if (g_test_subprocess()) { // Initialize namespace groups using a fake directory. sc_initialize_ns_groups(); // Check that the fake directory is now a private mount. g_assert_true(sc_is_ns_group_dir_private()); // Check that the lock file did not leak unclosed. // Construct the name of the lock file char *lock_file __attribute__ ((cleanup(sc_cleanup_string))) = NULL; lock_file = g_strdup_printf("%s/%s", sc_ns_dir, SC_NS_LOCK_FILE); // Attempt to open and lock the lock file. int lock_fd __attribute__ ((cleanup(sc_cleanup_close))) = -1; lock_fd = open(lock_file, O_RDWR | O_CLOEXEC | O_NOFOLLOW); g_assert_cmpint(lock_fd, !=, -1); // The non-blocking lock operation should not fail int err = flock(lock_fd, LOCK_EX | LOCK_NB); g_assert_cmpint(err, ==, 0); return; } g_test_trap_subprocess(NULL, 0, G_TEST_SUBPROCESS_INHERIT_STDERR); g_test_trap_assert_passed(); }
static void test_profiler (void) { if (g_test_subprocess ()) { gpointer p; g_mem_set_vtable (glib_mem_profiler_table); p = g_malloc (100); p = g_realloc (p, 200); g_free (p); p = g_malloc0 (1000); g_free (p); p = g_try_malloc (2000); p = g_try_realloc (p, 3000); g_free (p); p = g_malloc (0); p = g_malloc0 (0); p = g_realloc (NULL, 0); p = g_try_malloc (0); p = g_try_realloc (NULL, 0); g_mem_profile (); exit (0); } g_test_trap_subprocess (NULL, 0, 0); g_test_trap_assert_passed (); g_test_trap_assert_stdout ("*GLib Memory statistics*"); }
void test_signal(int signal) { if (g_test_subprocess()) { raise(signal); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_passed(); }
//////// Meta testcases //////////////////////////////////////////////////////// static void crank_test_meta_expected_fail (gconstpointer userdata) { gchar *efail_path = (gchar*) userdata; g_test_trap_subprocess (efail_path, 0, G_TEST_SUBPROCESS_INHERIT_STDERR); g_test_trap_assert_failed (); }
void test_help(void) { if (g_test_subprocess()) { char *test_argv[] = { "", "-h" }; evaluate_options(2, test_argv); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_passed(); g_test_trap_assert_stdout(usage_text("")); }
void test_bind_local_root(bool use_ipv6) { warnx("If this test fails check that you are not root\n"); if (g_test_subprocess()) { test_bind("localhost", 6, use_ipv6); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
void test_wrong_option(void) { if (g_test_subprocess()) { char *test_argv[] = { "", "--nonex-option" }; evaluate_options(2, test_argv); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("*Unrecognised option*"); }
void test_bind_nonexisting(bool use_ipv6) { warnx("If this test fails check if host %s accidentally exists\n", host1); if (g_test_subprocess()) { test_bind(host1, 6, use_ipv6); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
static void test_handler_run_failed(void) { if (g_test_subprocess()) { handler_add("http", "unknown-program %s"); handle_uri(TEST_URI); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("*Can't run *unknown-program*"); }
void test_no_options(void) { if (g_test_subprocess()) { char *test_argv[] = { "" }; evaluate_options(1, test_argv); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr( "*Both ports are required for forwarding.\n\nUsage*"); }
static void test_handler_run_success(void) { if (g_test_subprocess()) { handler_add("http", "echo -n 'handled uri %s'"); handle_uri(TEST_URI); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_passed(); g_test_trap_assert_stdout("handled uri " TEST_URI); }
static void test_dynamic_globalprop(void) { g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0); g_test_trap_assert_passed(); g_test_trap_assert_stderr_unmatched("*prop1*"); g_test_trap_assert_stderr_unmatched("*prop2*"); g_test_trap_assert_stderr("*Warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*"); g_test_trap_assert_stderr_unmatched("*prop4*"); g_test_trap_assert_stderr("*Warning: global nohotplug-type.prop5=105 not used\n*"); g_test_trap_assert_stderr("*Warning: global nondevice-type.prop6 has invalid class name\n*"); g_test_trap_assert_stdout(""); }
static void test_sc_string_init__NULL_buf() { if (g_test_subprocess()) { sc_string_init(NULL, 1); g_test_message("expected sc_string_init not to return"); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("cannot initialize string, buffer is NULL\n"); }
static void test_sc_string_append_char__NULL_buf() { if (g_test_subprocess()) { sc_string_append_char(NULL, 2, 'a'); g_test_message("expected sc_string_append_char not to return"); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("cannot append character: buffer is NULL\n"); }
void test_bind_local(bool use_ipv6) { uint16_t const test_port = 29407; warnx("If this test fails check that TCP port %d is unoccupied\n", test_port); if (g_test_subprocess()) { test_bind("localhost", test_port, use_ipv6); exit(0); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_passed(); }
static void _test_add_data_notnull_good_good(gpointer fixture, gconstpointer user_data) { SettersData *data = fixture; if ( g_test_subprocess() ) { eventd_event_add_data_string(data->event, EVENTD_EVENT_TEST_DATA_NAME, EVENTD_EVENT_TEST_DATA_CONTENT); exit(0); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_passed(); }
static void _test_new_good_null(gpointer fixture, gconstpointer user_data) { if ( ! g_test_undefined() ) return; if ( g_test_subprocess() ) { eventd_event_new_for_uuid_string(EVENTD_EVENT_TEST_UUID, NULL, NULL); exit(0); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
static void _test_add_data_null_good_good(gpointer fixture, gconstpointer user_data) { if ( ! g_test_undefined() ) return; if ( g_test_subprocess() ) { eventd_event_add_data_string(NULL, EVENTD_EVENT_TEST_DATA_NAME, EVENTD_EVENT_TEST_DATA_CONTENT); exit(0); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
static void test_sc_snap_name_validate__respects_error_protocol() { if (g_test_subprocess()) { sc_snap_name_validate("hello world", NULL); g_test_message("expected sc_snap_name_validate to return"); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr ("snap name must use lower case letters, digits or dashes\n"); }
static void test_sc_must_snprintf__fail() { if (g_test_subprocess()) { char buf[5]; sc_must_snprintf(buf, sizeof buf, "12345"); g_test_message("expected sc_must_snprintf not to return"); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("cannot format string: 1234\n"); }
static void test_sc_enable_sanity_timeout() { if (g_test_subprocess()) { sc_enable_sanity_timeout(); debug("waiting..."); usleep(4 * G_USEC_PER_SEC); debug("woke up"); sc_disable_sanity_timeout(); return; } g_test_trap_subprocess(NULL, 5 * G_USEC_PER_SEC, G_TEST_SUBPROCESS_INHERIT_STDERR); g_test_trap_assert_failed(); }
static void test_sc_unlock_ns_mutex_precondition() { sc_test_use_fake_ns_dir(); if (g_test_subprocess()) { struct sc_ns_group *group = sc_alloc_ns_group(); g_test_queue_free(group); // Try to unlock the mutex, this should abort because we never opened the // lock file and don't have a valid file descriptor. sc_unlock_ns_mutex(group); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
static void test_sc_string_quote_NULL_str() { if (g_test_subprocess()) { char buf[16] = { 0 }; sc_string_quote(buf, sizeof buf, NULL); g_test_message("expected sc_string_quote not to return"); g_test_fail(); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("cannot quote string: string is NULL\n"); }