[tracker] libtracker-common (tests/src): More tests and coverage tuning for tracker-file-utils
- From: Ivan Frade <ifrade src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker] libtracker-common (tests/src): More tests and coverage tuning for tracker-file-utils
- Date: Fri, 28 Oct 2011 11:44:43 +0000 (UTC)
commit 6612148057e1cfac4603973d2a087f4cdb4e7a0c
Author: Ivan Frade <ivan frade gmail com>
Date: Fri Oct 28 14:38:28 2011 +0300
libtracker-common (tests/src): More tests and coverage tuning for tracker-file-utils
src/libtracker-common/tracker-file-utils.c | 10 +
tests/libtracker-common/tracker-file-utils-test.c | 331 +++++++++++++++++++--
2 files changed, 318 insertions(+), 23 deletions(-)
---
diff --git a/src/libtracker-common/tracker-file-utils.c b/src/libtracker-common/tracker-file-utils.c
index bc650b6..af71d2b 100644
--- a/src/libtracker-common/tracker-file-utils.c
+++ b/src/libtracker-common/tracker-file-utils.c
@@ -256,11 +256,13 @@ tracker_file_system_get_remaining_space (const gchar *path)
guint64 remaining;
struct __statvfs st;
+//LCOV_EXCL_START
if (__statvfs (path, &st) == -1) {
remaining = 0;
g_critical ("Could not statvfs() '%s': %s",
path,
g_strerror (errno));
+//LCOV_EXCL_STOP
} else {
remaining = st.f_bsize * st.f_bavail;
}
@@ -274,11 +276,13 @@ tracker_file_system_get_remaining_space_percentage (const gchar *path)
gdouble remaining;
struct __statvfs st;
+//LCOV_EXCL_START
if (__statvfs (path, &st) == -1) {
remaining = 0.0;
g_critical ("Could not statvfs() '%s': %s",
path,
g_strerror (errno));
+//LCOV_EXCL_STOP
} else {
remaining = (st.f_bavail * 100.0 / st.f_blocks);
}
@@ -673,6 +677,7 @@ tracker_file_lock (GFile *file)
fd = open (path, O_RDONLY);
if (fd < 0) {
+//LCOV_EXCL_START
gchar *uri;
uri = g_file_get_uri (file);
@@ -681,6 +686,7 @@ tracker_file_lock (GFile *file)
g_free (path);
return FALSE;
+//LCOV_EXCL_STOP
}
retval = flock (fd, LOCK_EX);
@@ -690,12 +696,14 @@ tracker_file_lock (GFile *file)
g_object_ref (file),
GINT_TO_POINTER (fd));
} else {
+//LCOV_EXCL_START
gchar *uri;
uri = g_file_get_uri (file);
g_warning ("Could not lock file '%s'", uri);
g_free (uri);
close (fd);
+//LCOV_EXCL_STOP
}
g_free (path);
@@ -724,6 +732,7 @@ tracker_file_unlock (GFile *file)
retval = flock (fd, LOCK_UN);
if (retval < 0) {
+//LCOV_EXCL_START
gchar *uri;
uri = g_file_get_uri (file);
@@ -731,6 +740,7 @@ tracker_file_unlock (GFile *file)
g_free (uri);
return FALSE;
+//LCOV_EXCL_STOP
}
g_hash_table_remove (file_locks, file);
diff --git a/tests/libtracker-common/tracker-file-utils-test.c b/tests/libtracker-common/tracker-file-utils-test.c
index cad1ddf..31afe03 100644
--- a/tests/libtracker-common/tracker-file-utils-test.c
+++ b/tests/libtracker-common/tracker-file-utils-test.c
@@ -18,8 +18,12 @@
*/
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <glib.h>
+#include <glib/gstdio.h>
#include <gio/gio.h>
#include <libtracker-common/tracker-file-utils.h>
@@ -27,6 +31,24 @@
#include <tracker-test-helpers.h>
+#define TEST_FILENAME "./file-utils-test.txt"
+#define TEST_HIDDEN_FILENAME "./.hidden-file.txt"
+
+static void
+ensure_file_exists (const gchar *filename)
+{
+ if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
+ g_file_set_contents (filename, "Just some stuff", -1, NULL);
+ }
+}
+
+static void
+remove_file (const gchar *filename)
+{
+ g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
+ g_remove (filename);
+}
+
static GSList *
array_as_list (const gchar **array)
{
@@ -56,7 +78,11 @@ string_in_list (GSList *list, const gchar *string)
static void
test_path_list_filter_duplicates (void)
{
- const gchar *input_roots [] = {"/home", "/home/ivan", "/tmp", "/usr/", "/usr/share/local", NULL};
+ const gchar *input_roots [] = {"/home/ivan",
+ "/home",
+ "/tmp",
+ "/usr/",
+ "/usr/share/local", NULL};
GSList *input_as_list = NULL;
GSList *result;
@@ -71,6 +97,34 @@ test_path_list_filter_duplicates (void)
g_assert (string_in_list (result, "/usr"));
g_slist_foreach (input_as_list, (GFunc) g_free, NULL);
+ g_slist_foreach (result, (GFunc) g_free, NULL);
+}
+
+static void
+test_path_list_filter_duplicates_with_exceptions ()
+{
+ const gchar *input_roots [] = { "/home/user/MyDocs",
+ "/home/user/MyDocs/.sounds",
+ "/home/user/MyDocs/visible",
+ NULL};
+ GSList *input_as_list = NULL, *result = NULL;
+
+ input_as_list = array_as_list (input_roots);
+
+ result = tracker_path_list_filter_duplicates (input_as_list, "/home/user/MyDocs", FALSE);
+ g_assert_cmpint (g_slist_length (result), ==, 3);
+ g_assert (string_in_list (result, "/home/user/MyDocs"));
+ g_assert (string_in_list (result, "/home/user/MyDocs/.sounds"));
+ g_assert (string_in_list (result, "/home/user/MyDocs/visible"));
+ g_slist_foreach (result, (GFunc) g_free, NULL);
+
+
+ result = tracker_path_list_filter_duplicates (input_as_list, "/home/user/MyDocs", TRUE);
+ g_assert_cmpint (g_slist_length (result), ==, 1);
+ g_assert (string_in_list (result, "/home/user/MyDocs"));
+ g_slist_foreach (result, (GFunc) g_free, NULL);
+
+ g_slist_foreach (input_as_list, (GFunc) g_free, NULL);
}
static void
@@ -105,35 +159,33 @@ test_path_evaluate_name (void)
result = tracker_path_evaluate_name ("~/all/dir/");
expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir/", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (expected);
+ result = tracker_path_evaluate_name ("just-a-filename");
+ g_assert_cmpstr (result, ==, "just-a-filename");
+
result = tracker_path_evaluate_name ("$HOME/all/dir/");
expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (expected);
result = tracker_path_evaluate_name ("${HOME}/all/dir/");
expected = g_build_path (G_DIR_SEPARATOR_S, home, "/all/dir", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (expected);
result = tracker_path_evaluate_name ("./test/current/dir");
expected = g_build_path (G_DIR_SEPARATOR_S, pwd, "/test/current/dir", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (expected);
result = tracker_path_evaluate_name ("$TEST_TRACKER_DIR/test/dir");
expected = g_build_path (G_DIR_SEPARATOR_S, test, "/test/dir", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (expected);
@@ -141,7 +193,6 @@ test_path_evaluate_name (void)
parent_dir = g_path_get_dirname (pwd);
expected = g_build_path (G_DIR_SEPARATOR_S, parent_dir, "/test/dir", NULL);
g_assert_cmpstr (result, ==, expected);
-
g_free (result);
g_free (parent_dir);
g_free (expected);
@@ -152,6 +203,15 @@ test_path_evaluate_name (void)
result = tracker_path_evaluate_name (NULL);
g_assert (!result);
+
+ g_setenv ("HOME", "", TRUE);
+ result = tracker_path_evaluate_name ("~/but-no-home.txt");
+ g_assert (!result);
+ g_setenv ("HOME", home, TRUE);
+
+ result = tracker_path_evaluate_name ("$UNDEFINED/something");
+ g_assert_cmpstr (result, ==, "/something");
+
result = tracker_path_evaluate_name (tracker_test_helpers_get_nonutf8 ());
g_assert_cmpstr (result, ==, tracker_test_helpers_get_nonutf8 ());
@@ -162,22 +222,23 @@ test_path_evaluate_name (void)
static void
test_file_get_mime_type (void)
{
- gchar *dir_name, *result;
- GFile *dir;
+ gchar *result;
+ GFile *f;
+
+ f = g_file_new_for_path (TEST_FILENAME);
+ result = tracker_file_get_mime_type (f);
+ g_assert_cmpstr (result, ==, "text/plain");
- /* Create test directory */
- dir_name = g_build_filename (g_get_tmp_dir (), "tracker-test", NULL);
- dir = g_file_new_for_path (dir_name);
- g_file_make_directory (dir, NULL, NULL);
+ g_object_unref (f);
+ g_free (result);
- result = tracker_file_get_mime_type (dir);
+ f = g_file_new_for_path ("./file-does-NOT-exist");
+ result = tracker_file_get_mime_type (f);
+ g_assert_cmpstr (result, ==, "unknown");
- g_assert_cmpstr (result, ==, "inode/directory");
+ g_object_unref (f);
+ g_free (result);
- /* Remove test directory */
- g_file_delete (dir, NULL, NULL);
- g_object_unref (dir);
- g_free (dir_name);
}
#define assert_filename_match(a, b) { \
@@ -208,6 +269,202 @@ test_case_match_filename_without_extension ()
assert_filename_match ("", ".");
}
+static void
+test_file_utils_open_close ()
+{
+ FILE *f;
+
+ f = tracker_file_open (TEST_FILENAME);
+ g_assert (f);
+ tracker_file_close (f, TRUE);
+
+ f = tracker_file_open (TEST_FILENAME);
+ g_assert (f);
+ tracker_file_close (f, FALSE);
+
+ f = tracker_file_open ("./file-does-NOT-exist");
+ g_assert (!f);
+}
+
+static void
+test_file_utils_get_size ()
+{
+ goffset size;
+ struct stat st;
+
+ size = tracker_file_get_size (TEST_FILENAME);
+ g_assert_cmpint (size, >, 0);
+
+ stat (TEST_FILENAME, &st);
+ g_assert_cmpint (size, ==, st.st_size);
+
+ /* File doesn't exist */
+ size = tracker_file_get_size ("./file-does-NOT-exist");
+ g_assert_cmpint (size, ==, 0);
+}
+
+static void
+test_file_utils_get_mtime ()
+{
+ guint64 mtime;
+ struct stat st;
+ gchar *pwd, *uri;
+
+ mtime = tracker_file_get_mtime (TEST_FILENAME);
+ g_assert_cmpint (mtime, >, 0);
+
+ stat (TEST_FILENAME, &st);
+ // This comparison could lead a problem in 32/64 bits?
+ g_assert_cmpint (mtime, ==, st.st_mtime);
+
+ pwd = g_get_current_dir ();
+ uri = g_strdup_printf ("file://%s/%s", pwd, TEST_FILENAME);
+ mtime = tracker_file_get_mtime_uri (uri);
+ // This comparison could lead a problem in 32/64 bits?
+ g_assert_cmpint (mtime, ==, st.st_mtime);
+
+ g_free (pwd);
+ g_free (uri);
+
+ mtime = tracker_file_get_mtime_uri ("./file-does-NOT-exist");
+ g_assert_cmpint (mtime, ==, 0);
+}
+
+static void
+test_file_system_get_remaining_space ()
+{
+ guint64 space;
+
+ space = tracker_file_system_get_remaining_space ("/home");
+ g_assert_cmpint (space, >, 0);
+
+ // This is a critical (aborts the process)
+ //space = tracker_file_system_get_remaining_space ("/unlikely/to/have/this/folder");
+}
+
+static void
+test_file_system_get_remaining_space_percentage ()
+{
+ gdouble space;
+
+ space = tracker_file_system_get_remaining_space_percentage ("/home");
+ g_assert_cmpfloat (space, >=, 0);
+ g_assert_cmpfloat (space, <=, 100);
+
+ // This is a critical (aborts the process)
+ //space = tracker_file_system_get_remaining_space_percentage ("/unlikely/to/have/this/folder");
+}
+
+static void
+test_file_system_has_enough_space ()
+{
+ /* Hopefully we will always have 1 byte free... */
+ g_assert (tracker_file_system_has_enough_space ("/home", 1, FALSE));
+ g_assert (tracker_file_system_has_enough_space ("/home", 1, TRUE));
+
+ /* gulong goes only up to 4Gb. Cannot ask for unreasonable amount of space */
+ //g_assert (!tracker_file_system_has_enough_space ("/home", G_MAXULONG, FALSE));
+}
+
+static void
+test_file_exists_and_writable ()
+{
+ const gchar *path = "./test-dir-remove-afterwards";
+
+ if (g_file_test (path, G_FILE_TEST_EXISTS)) {
+ g_remove (path);
+ }
+
+ /* This should create the directory with write access*/
+ g_assert (tracker_path_has_write_access_or_was_created (path));
+ g_assert (g_file_test (path, G_FILE_TEST_EXISTS));
+
+ /* This time exists and has write access */
+ g_assert (tracker_path_has_write_access_or_was_created (path));
+
+ chmod (path, S_IRUSR & S_IRGRP);
+
+ /* Exists but is not writable */
+ g_assert (!tracker_path_has_write_access_or_was_created (path));
+
+ /* Doesn't exist and cannot be created */
+ g_assert (!tracker_path_has_write_access_or_was_created ("/var/log/tracker-test"));
+
+ g_remove (path);
+}
+
+static void
+test_file_utils_lock ()
+{
+ GFile *f, *no_f, *no_native_f;
+
+ f = g_file_new_for_path (TEST_FILENAME);
+ no_f = g_file_new_for_path ("./file-does-NOT-exist");
+ no_native_f = g_file_new_for_uri ("http://cgit.gnome.org/projects.tracker");
+
+ /* Nothing locked */
+ g_assert (tracker_file_unlock (f));
+
+ /* Locking a regular file */
+ g_assert (!tracker_file_is_locked (f));
+
+ g_assert (tracker_file_lock (f));
+ g_assert (tracker_file_is_locked (f));
+
+ /* Try to lock twice */
+ g_assert (tracker_file_lock (f));
+ g_assert (tracker_file_is_locked (f));
+
+ g_assert (tracker_file_unlock (f));
+ g_assert (!tracker_file_is_locked (f));
+
+ /* Unlock not-locked file */
+ g_assert (tracker_file_unlock (no_f));
+
+ /* Lock a non-existent file */
+ /* This causes a warning aborting the test */
+ //g_assert (!tracker_file_lock (no_f));
+
+ /* Lock a non-native file */
+ g_assert (!tracker_file_lock (no_native_f));
+ g_assert (!tracker_file_is_locked (no_native_f));
+
+ g_object_unref (f);
+ g_object_unref (no_f);
+ g_object_unref (no_native_f);
+}
+
+static void
+test_file_utils_is_hidden ()
+{
+ GFile *f;
+
+ ensure_file_exists ("./non-hidden-test-file");
+
+ f = g_file_new_for_path (TEST_HIDDEN_FILENAME);
+ g_assert (tracker_file_is_hidden (f));
+ g_object_unref (f);
+
+ f = g_file_new_for_path ("./non-hidden-test-file");
+ g_assert (!tracker_file_is_hidden (f));
+ g_object_unref (f);
+
+ remove_file ("./non-hidden-test-file");
+}
+
+static void
+test_file_utils_cmp ()
+{
+ GFile *one, *two, *three;
+
+ one = g_file_new_for_path (TEST_FILENAME);
+ two = g_file_new_for_path (TEST_FILENAME);
+ three = g_file_new_for_path (TEST_HIDDEN_FILENAME);
+
+ g_assert (!tracker_file_cmp (one, two));
+ g_assert (tracker_file_cmp (two, three));
+}
+
int
main (int argc, char **argv)
{
@@ -217,18 +474,46 @@ main (int argc, char **argv)
g_test_init (&argc, &argv, NULL);
tracker_locale_init ();
+ ensure_file_exists (TEST_FILENAME);
+ ensure_file_exists (TEST_HIDDEN_FILENAME);
- g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_evaluate_name",
+ g_test_add_func ("/libtracker-common/file-utils/path_evaluate_name",
test_path_evaluate_name);
- g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_list_filter_duplicates",
+ g_test_add_func ("/libtracker-common/file-utils/path_list_filter_duplicates",
test_path_list_filter_duplicates);
- g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/file_get_mime_type",
+ g_test_add_func ("/libtracker-common/file-utils/path_list_filter_duplicates_with_exceptions",
+ test_path_list_filter_duplicates_with_exceptions);
+ g_test_add_func ("/libtracker-common/file-utils/file_get_mime_type",
test_file_get_mime_type);
- g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/case_match_filename_without_extension",
+ g_test_add_func ("/libtracker-common/file-utils/case_match_filename_without_extension",
test_case_match_filename_without_extension);
+ g_test_add_func ("/libtracker-common/file-utils/open_close",
+ test_file_utils_open_close);
+ g_test_add_func ("/libtracker-common/file-utils/get_size",
+ test_file_utils_get_size);
+ g_test_add_func ("/libtracker-common/file-utils/get_mtime",
+ test_file_utils_get_mtime);
+ g_test_add_func ("/libtracker-common/file-utils/get_remaining_space",
+ test_file_system_get_remaining_space);
+ g_test_add_func ("/libtracker-common/file-utils/get_remaining_space_percentage",
+ test_file_system_get_remaining_space_percentage);
+ g_test_add_func ("/libtracker-common/file-utils/has_enough_space",
+ test_file_system_has_enough_space);
+ g_test_add_func ("/libtracker-common/file-utils/has_write_access_or_was_created",
+ test_file_exists_and_writable);
+ g_test_add_func ("/libtracker-common/file-utils/lock",
+ test_file_utils_lock);
+ g_test_add_func ("/libtracker-common/file-utils/is_hidden",
+ test_file_utils_is_hidden);
+ g_test_add_func ("/libtracker-common/file-utils/cmp",
+ test_file_utils_cmp);
+
result = g_test_run ();
+ remove_file (TEST_FILENAME);
+ remove_file (TEST_HIDDEN_FILENAME);
+
tracker_locale_shutdown ();
return result;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]