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_LSErrorLog(TestData *fixture, gconstpointer user_data) { LSError lserror; LSErrorInit(&lserror); _LSErrorSetNoPrint(&lserror, LS_ERROR_CODE_UNKNOWN_ERROR, LS_ERROR_TEXT_UNKNOWN_ERROR); if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LOG_LSERROR("LS_TEST_ERROR", &lserror); exit(0); } gchar *expected_stderr = g_strdup_printf("{\"ERROR_CODE\":%d,\"ERROR\":\"%s\",\"FUNC\":\"%s\",\"FILE\":\"%s\",\"LINE\":%d" "} LUNASERVICE ERROR\n", lserror.error_code, lserror.message, lserror.func, lserror.file, lserror.line); g_test_trap_assert_stderr(expected_stderr); g_free(expected_stderr); if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LOG_LSERROR("LS_TEST_ERROR", NULL); exit(0); } g_test_trap_assert_stderr("lserror is NULL. Did you pass in a LSError?\n"); LSErrorFree(&lserror); }
static void test_LSErrorPrint(TestData *fixture, gconstpointer user_data) { LSError error; LSErrorInit(&error); _LSErrorSetNoPrint(&error, LS_ERROR_CODE_UNKNOWN_ERROR, LS_ERROR_TEXT_UNKNOWN_ERROR); if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LSErrorPrint(&error, stderr); exit(0); } gchar *expected_stderr = g_strdup_printf("LUNASERVICE ERROR %d: %s (%s @ %s:%d)\n", error.error_code, error.message, error.func, error.file, error.line); g_test_trap_assert_stderr(expected_stderr); g_free(expected_stderr); if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LSErrorPrint(NULL, stderr); exit(0); } g_test_trap_assert_stderr("LUNASERVICE ERROR: lserror is NULL. Did you pass in a LSError?"); LSErrorFree(&error); }
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_schema (gpointer data) { SchemaTest *test = (SchemaTest *) data; if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { gchar *filename = g_strconcat (test->name, ".gschema.xml", NULL); gchar *path = g_build_filename (SRCDIR, "schema-tests", filename, NULL); gchar *argv[] = { "../glib-compile-schemas", "--dry-run", "--schema-file", path, (gchar *)test->opt, NULL }; gchar *envp[] = { NULL }; execve (argv[0], argv, envp); g_free (filename); g_free (path); } if (test->err) { g_test_trap_assert_failed (); g_test_trap_assert_stderr (test->err); } else g_test_trap_assert_passed(); }
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_unset_display (void) { if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) { GdkDisplayManager *manager; g_unsetenv ("DISPLAY"); g_assert (!gdk_init_check (NULL, NULL)); manager = gdk_display_manager_get (); g_assert (manager != NULL); g_assert (gdk_display_manager_get_default_display (manager) == NULL); exit (0); } g_test_trap_assert_passed (); if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) { g_unsetenv ("DISPLAY"); gdk_init (NULL, NULL); exit (0); } g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*cannot open display*"); }
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_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(""); }
/* Test g_warn macros */ static void test_warnings (void) { if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { g_warn_if_reached (); } g_test_trap_assert_failed(); g_test_trap_assert_stderr ("*WARNING*test_warnings*should not be reached*"); if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { g_warn_if_fail (FALSE); } g_test_trap_assert_failed(); g_test_trap_assert_stderr ("*WARNING*test_warnings*runtime check failed*"); }
void test_failure(){ if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)){ dictionary *dd = dictionary_new(); dictionary_add(dd, NULL, "blank"); } g_test_trap_assert_failed(); g_test_trap_assert_stderr("NULL is not a valid key.\n"); }
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*"); }
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_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"); }
static void test_LSDebugLogIncoming(TestData *fixture, gconstpointer user_data) { const char *expected_stdout = "RX: where token <<0>> sender: com.name.service sender_unique: com.name.service.0\n"; const char *expected_verbose_stdout = "RX: where token <<0>> sender: com.name.service sender_unique: com.name.service.0 payload: {}\n"; setenv("G_MESSAGES_DEBUG", "all", 1); PmLogSetContextLevel(PmLogGetLibContext(), kPmLogLevel_Debug); if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LSDebugLogIncoming("where", GINT_TO_POINTER(1)); exit(0); } g_test_trap_assert_stderr_unmatched(expected_stdout); // enable DEBUG_TRACING _ls_debug_tracing = 1; if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LSDebugLogIncoming("where", GINT_TO_POINTER(1)); exit(0); } g_test_trap_assert_stderr(expected_stdout); // enable DEBUG_VERBOSE _ls_debug_tracing = 2; if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDERR)) { LSDebugLogIncoming("where", GINT_TO_POINTER(1)); exit(0); } g_test_trap_assert_stderr(expected_verbose_stdout); _ls_debug_tracing = 0; }
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_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_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_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"); }
void test_wrong_port2(void) { if (g_test_subprocess()) { char *test_argv[] = { "", "-p", "6", "-q", "DEF", }; int test_argc = 5; evaluate_options(test_argc, test_argv); } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); g_test_trap_assert_stderr("*Second port invalid*"); }
// Check that `src' cannot be NULL. static void test_sc_string_append__NULL_str() { if (g_test_subprocess()) { char buf[4]; sc_string_append(buf, sizeof buf, NULL); g_test_message("expected sc_string_append 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 string: string is NULL\n"); }
static void test_sc_string_append_char__uninitialized_buf() { if (g_test_subprocess()) { char buf[2] = { 0xFF, 0xFF }; sc_string_append_char(buf, sizeof buf, '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: dst is unterminated\n"); }
static void test_sc_string_append_char__invalid_zero() { if (g_test_subprocess()) { char buf[2] = { 0 }; sc_string_append_char(buf, sizeof buf, '\0'); 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: cannot append string terminator\n"); }
static void test_double_vtable (void) { if (g_test_subprocess ()) { GMemVTable vtable = { malloc, realloc, free, NULL, NULL, NULL }; g_mem_set_vtable (&vtable); g_mem_set_vtable (&vtable); exit (0); } g_test_trap_subprocess (NULL, 0, 0); g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*can only be set once*"); }
static void test_sc_string_append_char__overflow() { if (g_test_subprocess()) { char buf[1] = { 0 }; sc_string_append_char(buf, sizeof buf, '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: not enough space\n"); }
/* void ClockPrint() */ static void test_ClockPrint(void) { /* ClockPrint uses internally ClockPrintTime() so we'll stress test that one and check here only the print procedure. */ /* Fork this test to capture stdout. */ if (g_test_trap_fork(1000000, G_TEST_TRAP_SILENCE_STDERR)) { ClockPrint(); exit(0); } g_test_trap_assert_passed(); /* Consider regular expression to check for more specific "%lds.%ldms". */ g_test_trap_assert_stderr("*s.*ms*"); }
static void test_sc_string_init__empty_buf() { if (g_test_subprocess()) { char buf[1] = { 0xFF }; sc_string_init(buf, 0); 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 too small\n"); }
static void test_incomplete_vtable (void) { if (g_test_subprocess ()) { GMemVTable vtable = { malloc, realloc, NULL, NULL, NULL, NULL }; gpointer p; g_mem_set_vtable (&vtable); p = g_malloc0 (1000); g_free (p); exit (0); } g_test_trap_subprocess (NULL, 0, 0); g_test_trap_assert_failed (); g_test_trap_assert_stderr ("*lacks one of*"); }