void test_local (void) { void *parent; void *_ptr; TEST_GROUP ("nih_local"); /* Make sure that when a variable goes out of scope, it's freed. */ TEST_FEATURE ("with variable going out of scope"); do { nih_local void *ptr; ptr = nih_alloc (NULL, 100); nih_alloc_set_destructor (ptr, destructor_called); destructor_was_called = 0; } while (0); TEST_TRUE (destructor_was_called); /* Make sure that if a variable is referenced while in scope, it * is not freed. */ TEST_FEATURE ("with referenced variable"); parent = nih_alloc (NULL, 100); do { nih_local void *ptr; ptr = nih_alloc (NULL, 100); nih_ref (ptr, parent); _ptr = ptr; nih_alloc_set_destructor (ptr, destructor_called); destructor_was_called = 0; } while (0); TEST_FALSE (destructor_was_called); TEST_ALLOC_PARENT (_ptr, parent); nih_free (parent); /* Make sure we don't need to allocate the variable. */ TEST_FEATURE ("with NULL variable"); do { nih_local void *ptr = NULL; } while (0); }
/** * test_checks: * * Perform any checks necessary before real tests are run. **/ void test_checks (void) { int ret; TEST_GROUP ("test environment"); /* * Warn (*) if overlayfs detected. * * (*) - Don't fail in the hope that one day someone might fix * overlayfs. */ TEST_FEATURE ("checking for overlayfs"); if (check_for_overlayfs ()) { nih_warn ("Found overlayfs mounts"); nih_warn ("This environment will probably cause tests to fail mysteriously!!"); nih_warn ("See bug LP:#882147 for further details."); } #ifdef ENABLE_CGROUPS if (file_exists ("/sys/fs/cgroup/cgmanager/sock")) { TEST_FEATURE ("checking for cgmanager"); ret = connect_to_cgmanager (); switch (ret) { case -2: nih_warn ("Found no cgroup manager"); goto out_skip; case -1: nih_warn ("Error connecting to cgmanager"); goto out_skip; case 0: print_my_cgroup (); break; default: nih_warn ("Unknown error from connect_to_cgmanager: %d", ret); goto out_skip; } TEST_FEATURE ("cgroup sandbox"); if (check_cgroup_sandbox() != 0) nih_warn ("Could not create cgroup sandbox"); } else { nih_warn ("Skipping CGManager tests, CGManager socket not found"); } out_skip: disconnect_cgmanager(); if (ret) nih_warn ("Skipping CGManager tests, CGManager not properly configured"); #endif /* ENABLE_CGROUPS */ }
void test_libupstart (void) { nih_local NihDBusProxy *upstart = NULL; nih_local char *version = NULL; int ret; pid_t upstart_pid; pid_t dbus_pid; char xdg_runtime_dir[PATH_MAX]; nih_local char *orig_xdg_runtime_dir = NULL; nih_local char *session_file = NULL; nih_local char *path = NULL; TEST_GROUP ("libupstart"); TEST_FEATURE ("version"); TEST_FILENAME (xdg_runtime_dir); TEST_EQ (mkdir (xdg_runtime_dir, 0755), 0); /* Take care to avoid disrupting users environment by saving and * restoring this variable (assuming the tests all pass...). */ orig_xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR"); if (orig_xdg_runtime_dir) orig_xdg_runtime_dir = NIH_MUST (nih_strdup (NULL, orig_xdg_runtime_dir)); assert0 (setenv ("XDG_RUNTIME_DIR", xdg_runtime_dir, 1)); /*******************************************************************/ /* Create a private Session Init instance to connect to */ TEST_DBUS (dbus_pid); START_UPSTART (upstart_pid, TRUE); upstart = upstart_open (NULL); TEST_NE_P (upstart, NULL); /* Basic test (that does not change the state of the system * running this test) to see if we can query version of running * Upstart instance. */ ret = upstart_get_version_sync (NULL, upstart, &version); TEST_EQ (ret, 0); nih_message ("Running instance version: '%s'", version); assert0 (fnmatch ("init (upstart*)", version, 0x0)); STOP_UPSTART (upstart_pid); TEST_DBUS_END (dbus_pid); /*******************************************************************/ if (orig_xdg_runtime_dir) { /* restore */ setenv ("XDG_RUNTIME_DIR", orig_xdg_runtime_dir, 1); } else { assert0 (unsetenv ("XDG_RUNTIME_DIR")); } session_file = get_session_file (xdg_runtime_dir, upstart_pid); unlink (session_file); /* Remove the directory tree the Session Init created */ path = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions", xdg_runtime_dir)); assert0 (rmdir (path)); path = NIH_MUST (nih_sprintf (NULL, "%s/upstart", xdg_runtime_dir)); assert0 (rmdir (path)); assert0 (rmdir (xdg_runtime_dir)); }
void test_cgroup_job_start (void) { char confdir[PATH_MAX]; char logdir[PATH_MAX]; char flagfile[PATH_MAX]; nih_local char *cmd = NULL; pid_t dbus_pid = 0; pid_t upstart_pid = 0; char **output; size_t lines; size_t len; nih_local char *logfile = NULL; nih_local char *logfile_name = NULL; nih_local char *contents = NULL; if (geteuid ()) { printf ("INFO: skipping %s tests as not running as root\n", __func__); fflush (NULL); return; } TEST_GROUP ("cgroup manager handling"); TEST_FILENAME (confdir); TEST_EQ (mkdir (confdir, 0755), 0); TEST_FILENAME (logdir); TEST_EQ (mkdir (logdir, 0755), 0); TEST_FILENAME (flagfile); /* Use the "secret" interface */ TEST_EQ (setenv ("UPSTART_CONFDIR", confdir, 1), 0); TEST_EQ (setenv ("UPSTART_LOGDIR", logdir, 1), 0); TEST_DBUS (dbus_pid); /*******************************************************************/ TEST_FEATURE ("Ensure startup job does not start until cgmanager available"); contents = nih_sprintf (NULL, "start on startup\n" "\n" "cgroup memory mem-%s\n" "\n" "exec echo hello\n", __func__); TEST_NE_P (contents, NULL); CREATE_FILE (confdir, "cgroup.conf", contents); logfile_name = NIH_MUST (nih_sprintf (NULL, "%s/%s", logdir, "cgroup.log")); start_upstart_common (&upstart_pid, FALSE, FALSE, confdir, logdir, NULL); cmd = nih_sprintf (NULL, "%s status %s 2>&1", get_initctl (), "cgroup"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); /* job should *NOT* start on startup */ TEST_EQ_STR (output[0], "cgroup stop/waiting"); nih_free (output); TEST_FALSE (file_exists (logfile_name)); cmd = nih_sprintf (NULL, "%s notify-cgroup-manager-address %s 2>&1", get_initctl (), CGMANAGER_DBUS_SOCK); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 0); WAIT_FOR_FILE (logfile_name); logfile = nih_file_read (NULL, logfile_name, &len); TEST_NE_P (logfile, NULL); TEST_EQ_STR (logfile, "hello\r\n"); DELETE_FILE (confdir, "cgroup.conf"); assert0 (unlink (logfile_name)); /*******************************************************************/ TEST_FEATURE ("Ensure bogus cgroups don't crash init"); contents = nih_sprintf (NULL, "cgroup name\n" "\n" "exec echo hello\n"); TEST_NE_P (contents, NULL); CREATE_FILE (confdir, "cgroup-name.conf", contents); logfile_name = NIH_MUST (nih_sprintf (NULL, "%s/%s", logdir, "cgroup-name.log")); cmd = nih_sprintf (NULL, "%s status %s 2>&1", get_initctl (), "cgroup-name"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); /* job is not running yet */ TEST_EQ_STR (output[0], "cgroup-name stop/waiting"); nih_free (output); TEST_FALSE (file_exists (logfile_name)); cmd = nih_sprintf (NULL, "%s start %s 2>&1", get_initctl (), "cgroup-name"); TEST_NE_P (cmd, NULL); RUN_COMMAND (NULL, cmd, &output, &lines); TEST_EQ (lines, 1); TEST_EQ_STR (output[0], "initctl: Job failed to start"); DELETE_FILE (confdir, "cgroup-name.conf"); /*******************************************************************/ STOP_UPSTART (upstart_pid); TEST_DBUS_END (dbus_pid); TEST_EQ (rmdir (confdir), 0); TEST_EQ (rmdir (logdir), 0); /*******************************************************************/ }