[gnome-keyring/trust-store] [testing] Move scratch file functions into testing module.



commit a21aca10e2c4f06342ee31ab5c3ebea1d2198b8e
Author: Stef Walter <stefw collabora co uk>
Date:   Wed Nov 24 21:21:41 2010 +0000

    [testing] Move scratch file functions into testing module.
    
     * So they can be used in various places.
     * Fix the touch function to set both times properly.

 pkcs11/xdg-store/tests/test-xdg-module.c |   91 +++--------------------------
 testing/testing.c                        |   48 ++++++++++++++++
 testing/testing.h                        |    8 +++
 3 files changed, 66 insertions(+), 81 deletions(-)
---
diff --git a/pkcs11/xdg-store/tests/test-xdg-module.c b/pkcs11/xdg-store/tests/test-xdg-module.c
index 1fc0943..cc26566 100644
--- a/pkcs11/xdg-store/tests/test-xdg-module.c
+++ b/pkcs11/xdg-store/tests/test-xdg-module.c
@@ -2,6 +2,7 @@
 /* test-xdg-module.c: A test PKCS#11 module implementation
 
    Copyright (C) 2010 Stefan Walter
+   Copyright (C) 2010 Collabora Ltd
 
    The Gnome Keyring Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public License as
@@ -39,78 +40,6 @@ static GMutex *mutex = NULL;
 GkmModule*  _gkm_xdg_store_get_module_for_testing (void);
 GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
 
-static void
-copy_scratch_file (const gchar *basename)
-{
-	gchar *filename;
-	gchar *data;
-	gsize n_data;
-
-	filename = testing_data_filename (basename);
-	if (!g_file_get_contents (filename, &data, &n_data, NULL)) {
-		g_warning ("couldn't read: %s", filename);
-		g_return_if_reached ();
-	}
-	g_free (filename);
-
-	filename = testing_scratch_filename (basename);
-	if (!g_file_set_contents (filename, data, n_data, NULL))
-		g_return_if_reached ();
-	g_free (filename);
-	g_free (data);
-}
-
-static void
-empty_scratch_file (const gchar *basename)
-{
-	GError *err = NULL;
-	gchar *filename;
-
-	filename = testing_scratch_filename (basename);
-	if (!g_file_set_contents (filename, "", 0, &err))
-		g_assert_no_error (err);
-
-	g_free (filename);
-}
-
-static void
-touch_scratch_file (const gchar *basename, gint future)
-{
-	GError *err = NULL;
-	gchar *filename;
-	struct timeval tv;
-
-	filename = testing_scratch_filename (basename);
-
-	gettimeofday (&tv, NULL);
-	tv.tv_sec += future;
-
-	if (utimes (filename, &tv) < 0) {
-		err = g_error_new_literal (G_FILE_ERROR, g_file_error_from_errno (errno),
-		                           g_strerror (errno));
-		g_assert_no_error (err);
-	}
-
-	g_free (filename);
-}
-
-static void
-remove_scratch_file (const gchar *basename)
-{
-	GError *err = NULL;
-	gchar *filename;
-
-	filename = testing_scratch_filename (basename);
-	if (g_unlink (filename) < 0) {
-		err = g_error_new_literal (G_FILE_ERROR, g_file_error_from_errno (errno),
-		                           g_strerror (errno));
-		g_assert_no_error (err);
-	}
-
-	g_free (filename);
-}
-
-
 GkmModule*
 test_xdg_module_initialize_and_enter (void)
 {
@@ -127,11 +56,11 @@ test_xdg_module_initialize_and_enter (void)
 	args.flags = CKF_OS_LOCKING_OK;
 
 	/* Copy files from test-data to scratch */
-	copy_scratch_file ("test-refer-1.trust");
-	copy_scratch_file ("test-certificate-1.cer");
-	empty_scratch_file ("invalid-without-ext");
-	empty_scratch_file ("test-file.unknown");
-	empty_scratch_file ("test-invalid.trust");
+	testing_data_to_scratch ("test-refer-1.trust", NULL);
+	testing_data_to_scratch ("test-certificate-1.cer", NULL);
+	testing_scratch_empty ("invalid-without-ext");
+	testing_scratch_empty ("test-file.unknown");
+	testing_scratch_empty ("test-invalid.trust");
 
 	funcs = gkm_xdg_store_get_functions ();
 	rv = (funcs->C_Initialize) (&args);
@@ -247,7 +176,7 @@ TESTING_TEST (xdg_module_find_twice_is_same)
 	gkm_assert_cmpulong (n_objects, >, 0);
 
 	/* Update the time on the file */
-	touch_scratch_file ("test-refer-1.trust", 1);
+	testing_scratch_touch ("test-refer-1.trust", 1);
 
 	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
@@ -277,8 +206,8 @@ TESTING_TEST (xdg_module_file_becomes_invalid)
 	gkm_assert_cmpulong (n_objects, >, 0);
 
 	/* Overwrite the file with empty */
-	empty_scratch_file ("test-refer-1.trust");
-	touch_scratch_file ("test-refer-1.trust", 2);
+	testing_scratch_empty ("test-refer-1.trust");
+	testing_scratch_touch ("test-refer-1.trust", 2);
 
 	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
@@ -308,7 +237,7 @@ TESTING_TEST (xdg_module_file_remove)
 	gkm_assert_cmpulong (n_objects, >, 0);
 
 	/* This file goes away */
-	remove_scratch_file ("test-refer-1.trust");
+	testing_scratch_remove ("test-refer-1.trust");
 
 	rv = gkm_session_C_FindObjectsInit (session, NULL, 0);
 	gkm_assert_cmprv (rv, ==, CKR_OK);
diff --git a/testing/testing.c b/testing/testing.c
index d4a5463..1ebee17 100644
--- a/testing/testing.c
+++ b/testing/testing.c
@@ -2,6 +2,7 @@
 /* test-helpers.c: Common functions called from gtest unit tests
 
    Copyright (C) 2008 Stefan Walter
+   Copyright (C) 2010 Collabora Ltd
 
    The Gnome Keyring Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public License as
@@ -32,6 +33,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <time.h>
+#include <sys/time.h>
 
 #include "testing.h"
 
@@ -175,6 +178,51 @@ testing_data_to_scratch (const gchar *basename, const gchar *newname)
 	g_free (data);
 }
 
+
+void
+testing_scratch_empty (const gchar *basename)
+{
+	GError *err = NULL;
+	gchar *filename;
+	filename = testing_scratch_filename (basename);
+	if (!g_file_set_contents (filename, "", 0, &err))
+		g_error ("couldn't write to file: %s: %s", filename,
+		         err && err->message ? err->message : "");
+	g_free (filename);
+}
+
+void
+testing_scratch_touch (const gchar *basename, gint future)
+{
+	gchar *filename;
+	struct timeval tv[2];
+
+	filename = testing_scratch_filename (basename);
+
+	/* Initialize the access and modification times */
+	gettimeofday (tv, NULL);
+	tv[0].tv_sec += future;
+	memcpy (tv + 1, tv, sizeof (struct timeval));
+
+	if (utimes (filename, tv) < 0)
+		g_error ("couldn't update file time: %s: %s", filename, g_strerror (errno));
+
+	g_free (filename);
+}
+
+void
+testing_scratch_remove (const gchar *basename)
+{
+	gchar *filename;
+
+	filename = testing_scratch_filename (basename);
+	if (g_unlink (filename) < 0)
+		g_error ("couldn't delete file: %s: %s", filename, g_strerror (errno));
+
+	g_free (filename);
+}
+
+
 #if WITH_P11_TESTS
 
 static void
diff --git a/testing/testing.h b/testing/testing.h
index ed93ae6..dfaafbb 100644
--- a/testing/testing.h
+++ b/testing/testing.h
@@ -2,6 +2,7 @@
 /* testing.h: Declarations for common functions called from gtest unit tests
 
    Copyright (C) 2008 Stefan Walter
+   Copyright (C) 2010 Collabora Ltd
 
    The Gnome Keyring Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public License as
@@ -54,6 +55,13 @@ void             testing_data_to_scratch          (const gchar *basename,
 
 gchar*           testing_scratch_filename         (const gchar *basename);
 
+void             testing_scratch_empty            (const gchar *basename);
+
+void             testing_scratch_touch            (const gchar *basename,
+                                                   gint future);
+
+void             testing_scratch_remove           (const gchar *basename);
+
 gchar*           testing_data_filename            (const gchar *basename);
 
 #ifdef CRYPTOKI_VERSION_MAJOR



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