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); }
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_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_open_ns_group_graceful() { sc_set_ns_dir("/nonexistent"); g_test_queue_destroy((GDestroyNotify) sc_set_ns_dir, SC_NS_DIR); struct sc_ns_group *group = sc_open_ns_group("foo", SC_NS_FAIL_GRACEFULLY); g_assert_null(group); }
void test_timeout_reset (guint factor) { GTimer *timer = g_timer_new (); g_test_message ("Resetting test timeout (reference: %p; factor: %u)", timer, factor); set_timeout (factor); g_test_queue_destroy (report_and_destroy, timer); }
// 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; }
static char *drive_create(void) { int fd, ret; char *t_path = g_strdup("/tmp/qtest.XXXXXX"); /* Create a temporary raw image */ fd = mkstemp(t_path); g_assert_cmpint(fd, >=, 0); ret = ftruncate(fd, TEST_IMAGE_SIZE); g_assert_cmpint(ret, ==, 0); close(fd); g_test_queue_destroy(drive_destroy, t_path); return t_path; }
void test_user_grain_value_save(void) { GwyUserGrainValue *usergrainvalue = make_test_grain_value(); GwyResource *resource = GWY_RESOURCE(usergrainvalue); GError *error = NULL; gwy_resource_set_filename(resource, "Bloat3"); g_assert(gwy_resource_save(resource, &error)); g_assert_no_error(error); g_test_queue_destroy((GDestroyNotify)g_unlink, "Bloat3"); resource_check_file(resource, "Bloat3"); GWY_OBJECT_UNREF(usergrainvalue); user_grain_value_load_check("Bloat3", "Bloat", "The Bloat Group", "V_0/L_b0^3", -1, 0, 1, GWY_GRAIN_VALUE_SAME_UNITS_LATERAL, FALSE); }
// Initialize a namespace group. // // The group is automatically destroyed at the end of the test. static struct sc_ns_group *sc_test_open_ns_group(const char *group_name) { // Initialize a namespace group struct sc_ns_group *group = NULL; if (group_name == NULL) { group_name = "test-group"; } group = sc_open_ns_group(group_name, 0); g_test_queue_destroy((GDestroyNotify) sc_close_ns_group, group); // Check if the returned group data looks okay 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_cmpstr(group->name, ==, group_name); return group; }
static void test_parse_mountinfo_entry__no_tags() { const char *line = "1 2 3:4 root mount-dir mount-opts - fs-type mount-source super-opts"; struct mountinfo_entry *entry = parse_mountinfo_entry(line); g_assert_nonnull(entry); g_test_queue_destroy((GDestroyNotify) free_mountinfo_entry, entry); g_assert_cmpint(entry->mount_id, ==, 1); g_assert_cmpint(entry->parent_id, ==, 2); g_assert_cmpint(entry->dev_major, ==, 3); g_assert_cmpint(entry->dev_minor, ==, 4); g_assert_cmpstr(entry->root, ==, "root"); g_assert_cmpstr(entry->mount_dir, ==, "mount-dir"); g_assert_cmpstr(entry->mount_opts, ==, "mount-opts"); g_assert_cmpstr(entry->optional_fields, ==, ""); g_assert_cmpstr(entry->fs_type, ==, "fs-type"); g_assert_cmpstr(entry->mount_source, ==, "mount-source"); g_assert_cmpstr(entry->super_opts, ==, "super-opts"); g_assert_null(entry->next); }
static void test_parse_mountinfo_entry__snapd_mnt() { const char *line = "256 104 0:3 mnt:[4026532509] /run/snapd/ns/hello-world.mnt rw - nsfs nsfs rw"; struct mountinfo_entry *entry = parse_mountinfo_entry(line); g_assert_nonnull(entry); g_test_queue_destroy((GDestroyNotify) free_mountinfo_entry, entry); g_assert_cmpint(entry->mount_id, ==, 256); g_assert_cmpint(entry->parent_id, ==, 104); g_assert_cmpint(entry->dev_major, ==, 0); g_assert_cmpint(entry->dev_minor, ==, 3); g_assert_cmpstr(entry->root, ==, "mnt:[4026532509]"); g_assert_cmpstr(entry->mount_dir, ==, "/run/snapd/ns/hello-world.mnt"); g_assert_cmpstr(entry->mount_opts, ==, "rw"); g_assert_cmpstr(entry->optional_fields, ==, ""); g_assert_cmpstr(entry->fs_type, ==, "nsfs"); g_assert_cmpstr(entry->mount_source, ==, "nsfs"); g_assert_cmpstr(entry->super_opts, ==, "rw"); g_assert_null(entry->next); }
// Parse the /run/snapd/ns bind mount (over itself) // Note that /run is itself a tmpfs mount point. static void test_parse_mountinfo_entry__snapd_ns() { const char *line = "104 23 0:19 /snapd/ns /run/snapd/ns rw,nosuid,noexec,relatime - tmpfs tmpfs rw,size=99840k,mode=755"; struct mountinfo_entry *entry = parse_mountinfo_entry(line); g_assert_nonnull(entry); g_test_queue_destroy((GDestroyNotify) free_mountinfo_entry, entry); g_assert_cmpint(entry->mount_id, ==, 104); g_assert_cmpint(entry->parent_id, ==, 23); g_assert_cmpint(entry->dev_major, ==, 0); g_assert_cmpint(entry->dev_minor, ==, 19); g_assert_cmpstr(entry->root, ==, "/snapd/ns"); g_assert_cmpstr(entry->mount_dir, ==, "/run/snapd/ns"); g_assert_cmpstr(entry->mount_opts, ==, "rw,nosuid,noexec,relatime"); g_assert_cmpstr(entry->optional_fields, ==, ""); g_assert_cmpstr(entry->fs_type, ==, "tmpfs"); g_assert_cmpstr(entry->mount_source, ==, "tmpfs"); g_assert_cmpstr(entry->super_opts, ==, "rw,size=99840k,mode=755"); g_assert_null(entry->next); }
/** * gtk_test_create_widget: * @widget_type: a valid widget type. * @first_property_name: (allow-none): Name of first property to set or %NULL * @...: value to set the first property to, followed by more * name-value pairs, terminated by %NULL * * This function wraps g_object_new() for widget types. * It'll automatically show all created non window widgets, also * g_object_ref_sink() them (to keep them alive across a running test) * and set them up for destruction during the next test teardown phase. * * Returns: (transfer none): a newly created widget. * * Since: 2.14 */ GtkWidget* gtk_test_create_widget (GType widget_type, const gchar *first_property_name, ...) { GtkWidget *widget; va_list var_args; g_return_val_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET), NULL); va_start (var_args, first_property_name); widget = (GtkWidget*) g_object_new_valist (widget_type, first_property_name, var_args); va_end (var_args); if (widget) { if (!GTK_IS_WINDOW (widget)) gtk_widget_show (widget); g_object_ref_sink (widget); g_test_queue_unref (widget); g_test_queue_destroy ((GDestroyNotify) gtk_widget_destroy, widget); } return widget; }
static void test_parse_mountinfo_entry__sysfs() { const char *line = "19 25 0:18 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw"; struct mountinfo_entry *entry = parse_mountinfo_entry(line); g_assert_nonnull(entry); g_test_queue_destroy((GDestroyNotify) free_mountinfo_entry, entry); g_assert_cmpint(entry->mount_id, ==, 19); g_assert_cmpint(entry->parent_id, ==, 25); g_assert_cmpint(entry->dev_major, ==, 0); g_assert_cmpint(entry->dev_minor, ==, 18); g_assert_cmpstr(entry->root, ==, "/"); g_assert_cmpstr(entry->mount_dir, ==, "/sys"); g_assert_cmpstr(entry->mount_opts, ==, "rw,nosuid,nodev,noexec,relatime"); g_assert_cmpstr(entry->optional_fields, ==, "shared:7"); g_assert_cmpstr(entry->fs_type, ==, "sysfs"); g_assert_cmpstr(entry->mount_source, ==, "sysfs"); g_assert_cmpstr(entry->super_opts, ==, "rw"); g_assert_null(entry->next); }
static void test_accessor_funcs() { const char *line = "256 104 0:3 mnt:[4026532509] /run/snapd/ns/hello-world.mnt rw - nsfs nsfs rw"; struct mountinfo_entry *entry = parse_mountinfo_entry(line); g_assert_nonnull(entry); g_test_queue_destroy((GDestroyNotify) free_mountinfo_entry, entry); g_assert_cmpint(mountinfo_entry_mount_id(entry), ==, 256); g_assert_cmpint(mountinfo_entry_parent_id(entry), ==, 104); g_assert_cmpint(mountinfo_entry_dev_major(entry), ==, 0); g_assert_cmpint(mountinfo_entry_dev_minor(entry), ==, 3); g_assert_cmpstr(mountinfo_entry_root(entry), ==, "mnt:[4026532509]"); g_assert_cmpstr(mountinfo_entry_mount_dir(entry), ==, "/run/snapd/ns/hello-world.mnt"); g_assert_cmpstr(mountinfo_entry_mount_opts(entry), ==, "rw"); g_assert_cmpstr(mountinfo_entry_optional_fields(entry), ==, ""); g_assert_cmpstr(mountinfo_entry_fs_type(entry), ==, "nsfs"); g_assert_cmpstr(mountinfo_entry_mount_source(entry), ==, "nsfs"); g_assert_cmpstr(mountinfo_entry_super_opts(entry), ==, "rw"); }
void test_user_grain_value_load(void) { static const gchar usergrainvalue_v2[] = "Gwyddion resource GwyUserGrainValue\n" "formula atan((z_max - z_min)/D_max)\n" "ident sloppiness_2\n" "power_x 0\n" "power_y 0\n" "power_z 0\n" "is_angle 1\n" "same_units 1\n"; static const gchar usergrainvalue_v3[] = "Gwyddion3 resource GwyUserGrainValue\n" "name Sloppiness v3\n" "group Group that hopefully does not exist\n" "formula atan((z_max - z_min)/D_max)\n" "ident sloppiness_2\n" "power_x 0\n" "power_y 0\n" "power_z 0\n" "is_angle 1\n" "same_units 1\n"; static const gchar usergrainvalue_v3_ugly[] = "Gwyddion3 resource GwyUserGrainValue\n" " name Sloppiness³ \t\n" " ident\t \tsloppiness_2 \n" " formula\tatan((z_max - z_min)/D_max)\n" " \tis_angle \t1\n" "\t \n" "\tsame_units\t1\t \t\n"; GError *error = NULL; // Version2 resource g_assert(g_file_set_contents("Sloppiness", usergrainvalue_v2, -1, &error)); g_test_queue_destroy((GDestroyNotify)g_unlink, "Sloppiness"); g_assert_no_error(error); user_grain_value_load_check("Sloppiness", "Sloppiness", "User", "atan((z_max - z_min)/D_max)", 0, 0, 0, GWY_GRAIN_VALUE_SAME_UNITS_LATERAL, TRUE); // Version3 resource g_assert(g_file_set_contents("Sloppiness3", usergrainvalue_v3, -1, &error)); g_test_queue_destroy((GDestroyNotify)g_unlink, "Sloppiness3"); g_assert_no_error(error); user_grain_value_load_check("Sloppiness3", "Sloppiness v3", "Group that hopefully does not exist", "atan((z_max - z_min)/D_max)", 0, 0, 0, GWY_GRAIN_VALUE_SAME_UNITS_LATERAL, TRUE); // Version3 ugly resource g_assert(g_file_set_contents("Ugly-Slop", usergrainvalue_v3_ugly, -1, &error)); g_test_queue_destroy((GDestroyNotify)g_unlink, "Ugly-Slop"); g_assert_no_error(error); user_grain_value_load_check("Ugly-Slop", "Sloppiness³", "User", "atan((z_max - z_min)/D_max)", 0, 0, 0, GWY_GRAIN_VALUE_SAME_UNITS_LATERAL, TRUE); }