[dconf/wip/reorg] tests/: add shm testcase



commit 341cd4350474c166b0a737339b4dc40d7c0a5ca0
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Jul 8 19:04:23 2012 -0400

    tests/: add shm testcase

 tests/.gitignore  |    1 +
 tests/Makefile.am |    6 ++-
 tests/shm.c       |  169 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/tmpdir.c    |   52 ++++++++++++++++
 tests/tmpdir.h    |    9 +++
 5 files changed, 236 insertions(+), 1 deletions(-)
---
diff --git a/tests/.gitignore b/tests/.gitignore
index 0c37e4c..cb7d423 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -9,3 +9,4 @@ gdbus-thread
 gsettings
 gvdb
 paths
+shm
diff --git a/tests/Makefile.am b/tests/Makefile.am
index fc4e551..53142f7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,7 +1,7 @@
 include $(top_srcdir)/Makefile.gtester
 
 AM_CFLAGS = -Wall -Wmissing-prototypes -Wwrite-strings -DSRCDIR=\"$(abs_srcdir)\"
-INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/engine -I$(top_srcdir)/client $(gio_CFLAGS) -I$(top_srcdir)/dbus-1 $(dbus_CFLAGS) -I$(top_srcdir)/gvdb
+INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/engine -I$(top_srcdir)/client $(gio_CFLAGS) -I$(top_srcdir)/dbus-1 $(dbus_CFLAGS) -I$(top_srcdir)/gvdb -I$(top_srcdir)/shm
 
 noinst_LIBRARIES = libdconf-dbus-stub.a
 noinst_PROGRAMS = $(TEST_PROGS) gsettings dbus1
@@ -17,6 +17,10 @@ TEST_PROGS += changeset
 changeset_LDADD = ../common/libdconf-common.a $(glib_LIBS)
 changeset_SOURCES = changeset.c
 
+TEST_PROGS += shm
+shm_LDADD = ../shm/libdconf-shm.a $(glib_LIBS) -ldl
+shm_SOURCES = shm.c tmpdir.h tmpdir.c
+
 engine_LIBS = \
 	../engine/libdconf-engine.a	\
 	../shm/libdconf-shm.a		\
diff --git a/tests/shm.c b/tests/shm.c
new file mode 100644
index 0000000..cb60560
--- /dev/null
+++ b/tests/shm.c
@@ -0,0 +1,169 @@
+#define _GNU_SOURCE
+#include <dconf-paths.h>
+#include <glib/gstdio.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <dlfcn.h>
+
+#include "dconf-shm.h"
+#include "tmpdir.h"
+
+static void
+test_mkdir_fail (void)
+{
+  guint8 *shm;
+
+  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+    {
+      gchar *evil;
+      gint fd;
+
+      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
+
+      evil = g_build_filename (g_get_user_runtime_dir (), "dconf", NULL);
+      fd = open (evil, O_WRONLY | O_CREAT, 0600);
+      close (fd);
+
+      shm = dconf_shm_open ("foo");
+      g_assert (shm == NULL);
+
+      g_unlink (evil);
+      g_free (evil);
+
+      exit (0);
+    }
+  g_test_trap_assert_passed ();
+  g_test_trap_assert_stderr ("*unable to create directory*");
+}
+
+static void
+test_close_null (void)
+{
+  dconf_shm_close (NULL);
+}
+
+static void
+test_open_and_flag (void)
+{
+  guint8 *shm;
+
+  shm = dconf_shm_open ("foo");
+  g_assert (shm != NULL);
+  g_assert (!dconf_shm_is_flagged (shm));
+  dconf_shm_flag ("foo");
+  g_assert (dconf_shm_is_flagged (shm));
+  dconf_shm_close (shm);
+}
+
+static void
+test_invalid_name (void)
+{
+  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+    {
+      guint8 *shm;
+
+      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
+
+      shm = dconf_shm_open ("foo/bar");
+      g_assert (shm == NULL);
+      g_assert (dconf_shm_is_flagged (shm));
+      exit (0);
+    }
+  g_test_trap_assert_passed ();
+  g_test_trap_assert_stderr ("*unable to create*foo/bar*");
+}
+
+static void
+test_flag_nonexistent (void)
+{
+  dconf_shm_flag ("does-not-exist");
+}
+
+static gboolean should_fail_pwrite;
+/* interpose */
+ssize_t
+pwrite (int fd, const void *buf, size_t count, off_t offset)
+{
+  static ssize_t (* real_pwrite) (int, const void *, size_t, off_t);
+
+  if (!real_pwrite)
+    real_pwrite = dlsym (RTLD_NEXT, "pwrite");
+
+  if (should_fail_pwrite)
+    {
+      errno = ENOSPC;
+      return -1;
+    }
+
+  return (* real_pwrite) (fd, buf, count, offset);
+}
+
+static void
+test_out_of_space_open (void)
+{
+  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+    {
+      guint8 *shm;
+
+      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
+      should_fail_pwrite = TRUE;
+
+      shm = dconf_shm_open ("foo");
+      g_assert (shm == NULL);
+      g_assert (dconf_shm_is_flagged (shm));
+      exit (0);
+    }
+  g_test_trap_assert_passed ();
+  g_test_trap_assert_stderr ("*failed to allocate*foo*");
+}
+
+static void
+test_out_of_space_flag (void)
+{
+  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+    {
+      g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
+      should_fail_pwrite = TRUE;
+
+      dconf_shm_flag ("foo");
+      exit (0);
+    }
+  g_test_trap_assert_passed ();
+}
+
+int
+main (int argc, char **argv)
+{
+  gchar *temp;
+  gint status;
+
+  temp = dconf_test_create_tmpdir ();
+
+  g_setenv ("XDG_RUNTIME_DIR", temp, TRUE);
+  /* This currently works, but it is possible that one day GLib will
+   * read the XDG_RUNTIME_DIR variable (and cache its value) as a
+   * side-effect of the dconf_test_create_tmpdir() call above.
+   *
+   * This assert will quickly uncover the problem in that case...
+   */
+  g_assert_cmpstr (g_get_user_runtime_dir (), ==, temp);
+
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/shm/mkdir-fail", test_mkdir_fail);
+  g_test_add_func ("/shm/close-null", test_close_null);
+  g_test_add_func ("/shm/open-and-flag", test_open_and_flag);
+  g_test_add_func ("/shm/invalid-name", test_invalid_name);
+  g_test_add_func ("/shm/flag-nonexistent", test_flag_nonexistent);
+  g_test_add_func ("/shm/out-of-space-open", test_out_of_space_open);
+  g_test_add_func ("/shm/out-of-space-flag", test_out_of_space_flag);
+
+  status = g_test_run ();
+
+  dconf_test_remove_tmpdir (temp);
+
+  return status;
+}
diff --git a/tests/tmpdir.c b/tests/tmpdir.c
new file mode 100644
index 0000000..405d37b
--- /dev/null
+++ b/tests/tmpdir.c
@@ -0,0 +1,52 @@
+#include "tmpdir.h"
+
+#include <glib/gstdio.h>
+#include <dconf-paths.h>
+#include <string.h>
+
+gchar *
+dconf_test_create_tmpdir (void)
+{
+  GError *error = NULL;
+  gchar *temp;
+
+  temp = g_dir_make_tmp ("dconf-testcase.XXXXXX", &error);
+  g_assert_no_error (error);
+  g_assert (temp != NULL);
+
+  return temp;
+}
+
+static void
+rm_rf (const gchar *file)
+{
+  GDir *dir;
+
+  dir = g_dir_open (file, 0, NULL);
+  if (dir)
+    {
+      const gchar *basename;
+
+      while ((basename = g_dir_read_name (dir)))
+        {
+          gchar *fullname;
+
+          fullname = g_build_filename (file, basename, NULL);
+          rm_rf (fullname);
+          g_free (fullname);
+        }
+
+      g_rmdir (file);
+    }
+
+  else
+    /* excess paranoia -- only unlink if we're really really sure */
+    if (strstr (file, "/dconf-testcase") && !strstr (file, ".."))
+      g_unlink (file);
+}
+
+void
+dconf_test_remove_tmpdir (const gchar *tmpdir)
+{
+  rm_rf (tmpdir);
+}
diff --git a/tests/tmpdir.h b/tests/tmpdir.h
new file mode 100644
index 0000000..6e9dae8
--- /dev/null
+++ b/tests/tmpdir.h
@@ -0,0 +1,9 @@
+#ifndef __dconf_tmpdir_h__
+#define __dconf_tmpdir_h__
+
+#include <glib.h>
+
+gchar *dconf_test_create_tmpdir (void);
+void   dconf_test_remove_tmpdir (const gchar *tmpdir);
+
+#endif /* __dconf_tmpdir_h__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]