Add a key-file test library
authorPhilip Withnall <philip.withnall@collabora.co.uk>
Thu, 16 Sep 2010 13:29:03 +0000 (14:29 +0100)
committerTravis Reitter <travis.reitter@collabora.co.uk>
Thu, 4 Nov 2010 00:10:18 +0000 (17:10 -0700)
This contains some of the boilerplate code necessary to test the key-file
backend with arbitrary key files.

Helps bgo#629862.

configure.ac
tests/lib/Makefile.am
tests/lib/key-file/Makefile.am [new file with mode: 0644]
tests/lib/key-file/backend.vala [new file with mode: 0644]

index 30e60fa..1233342 100644 (file)
@@ -223,6 +223,7 @@ AC_CONFIG_FILES([
     tests/telepathy/Makefile
     tests/lib/Makefile
     tests/lib/folks-test-uninstalled.pc
+    tests/lib/key-file/Makefile
     tests/lib/telepathy/Makefile
     tests/lib/telepathy/contactlist/Makefile
     tests/lib/telepathy/contactlist/session.conf
index a4508b6..5bd8edc 100644 (file)
@@ -1,4 +1,5 @@
 SUBDIRS = \
+       key-file \
        telepathy \
        $(NULL)
 
diff --git a/tests/lib/key-file/Makefile.am b/tests/lib/key-file/Makefile.am
new file mode 100644 (file)
index 0000000..880f34b
--- /dev/null
@@ -0,0 +1,31 @@
+VALAFLAGS += \
+       --library=kf-test \
+       --header=kf-test.h \
+       --vapidir=. \
+       $(NULL)
+
+noinst_LTLIBRARIES = libkf-test.la
+
+libkf_test_la_SOURCES = \
+       backend.vala \
+       $(NULL)
+
+libkf_test_la_CFLAGS = \
+       $(GLIB_CFLAGS) \
+       $(NULL)
+
+libkf_test_la_LIBADD = \
+       $(GLIB_LIBS) \
+       $(NULL)
+
+MAINTAINERCLEANFILES = \
+       kf-test.vapi \
+       kf-test.h \
+       $(NULL)
+
+GITIGNOREFILES = \
+       $(libkf_test_la_SOURCES:.vala=.c) \
+       libkf_test_la_vala.stamp \
+       $(NULL)
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/lib/key-file/backend.vala b/tests/lib/key-file/backend.vala
new file mode 100644 (file)
index 0000000..0b241fe
--- /dev/null
@@ -0,0 +1,57 @@
+public class KfTest.Backend
+{
+  private string key_file_name;
+
+  public void set_up (string key_file_contents)
+    {
+      int fd;
+
+      /* Create a temporary file */
+      try
+        {
+          fd = FileUtils.open_tmp ("folks-kf-test-XXXXXX",
+              out this.key_file_name);
+        }
+      catch (FileError e)
+        {
+          error ("Error opening temporary file: %s", e.message);
+        }
+
+      /* Populate it with the given content */
+      IOChannel channel = new IOChannel.unix_new (fd);
+      try
+        {
+          channel.write_chars ((char[]) key_file_contents, null);
+        }
+      catch (ConvertError e)
+        {
+          error ("Error converting for writing to temporary file '%s': %s\n%s",
+              this.key_file_name, e.message, key_file_contents);
+        }
+      catch (IOChannelError e)
+        {
+          error ("Error writing to temporary file '%s': %s", this.key_file_name,
+              e.message);
+        }
+
+      try
+        {
+          channel.shutdown (true);
+        }
+      catch (IOChannelError e) {}
+      FileUtils.close (fd);
+
+      /* Set the environment variable for the key file path to the temporary
+       * file, causing the key-file backend to use it next time it's loaded */
+      Environment.set_variable ("FOLKS_BACKEND_KEY_FILE_PATH",
+          this.key_file_name, true);
+    }
+
+  public void tear_down ()
+    {
+      /* Remove the temporary file */
+      if (this.key_file_name != null)
+        FileUtils.remove (this.key_file_name);
+      this.key_file_name = null;
+    }
+}