static void set_fake_cookie_dir() { char *ctx_dir = NULL; ctx_dir = g_dir_make_tmp(NULL, NULL); g_assert_nonnull(ctx_dir); g_test_queue_free(ctx_dir); g_test_queue_destroy((GDestroyNotify) rm_rf_tmp, ctx_dir); g_test_queue_destroy((GDestroyNotify) set_cookie_dir, SC_COOKIE_DIR); set_cookie_dir(ctx_dir); }
// Check that allocating a namespace group sets up internal data structures to // safe values. static void test_sc_alloc_ns_group() { struct sc_ns_group *group = NULL; group = sc_alloc_ns_group(); g_test_queue_free(group); g_assert_nonnull(group); g_assert_cmpint(group->dir_fd, ==, -1); g_assert_cmpint(group->lock_fd, ==, -1); g_assert_cmpint(group->event_fd, ==, -1); g_assert_cmpint(group->child, ==, 0); g_assert_cmpint(group->should_populate, ==, false); g_assert_null(group->name); }
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(); }
// Use temporary directory for namespace groups. // // The directory is automatically reset to the real value at the end of the // test. static const char *sc_test_use_fake_ns_dir() { char *ns_dir = NULL; if (g_test_subprocess()) { // Check if the environment variable is set. If so then someone is already // managing the temporary directory and we should not create a new one. ns_dir = getenv("SNAP_CONFINE_NS_DIR"); g_assert_nonnull(ns_dir); } else { ns_dir = g_dir_make_tmp(NULL, NULL); g_assert_nonnull(ns_dir); g_test_queue_free(ns_dir); g_assert_cmpint(setenv("SNAP_CONFINE_NS_DIR", ns_dir, 0), ==, 0); g_test_queue_destroy((GDestroyNotify) unsetenv, "SNAP_CONFINE_NS_DIR"); g_test_queue_destroy((GDestroyNotify) rm_rf_tmp, ns_dir); } g_test_queue_destroy((GDestroyNotify) sc_set_ns_dir, SC_NS_DIR); sc_set_ns_dir(ns_dir); return ns_dir; }