[gtksourceview/test-file-saver: 2/2] Unit tests for the FileSaver (unfinished!)



commit a97700fad8871c61a771643201900f2c8b1d01ad
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Dec 13 17:47:08 2013 +0100

    Unit tests for the FileSaver (unfinished!)

 tests/Makefile.am       |    7 +
 tests/test-file-saver.c |  749 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 756 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b2835fe..65856f6 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -86,6 +86,13 @@ test_completion_words_LDADD =                                        \
        $(DEP_LIBS)                                             \
        $(TESTS_LIBS)
 
+UNIT_TEST_PROGS += test-file-saver
+test_file_saver_SOURCES = test-file-saver.c
+test_file_saver_LDADD =                                                \
+       $(top_builddir)/gtksourceview/libgtksourceview-3.0.la   \
+       $(DEP_LIBS)                                             \
+       $(TESTS_LIBS)
+
 UNIT_TEST_PROGS += test-language
 test_language_SOURCES =                \
        test-language.c
diff --git a/tests/test-file-saver.c b/tests/test-file-saver.c
new file mode 100644
index 0000000..66eb5b3
--- /dev/null
+++ b/tests/test-file-saver.c
@@ -0,0 +1,749 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* test-file-saver.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ * Copyright (C) 2013 - Sébastien Wilmet
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <gtksourceview/gtksource.h>
+
+/* linux/bsd has it. others such as Solaris, do not */
+#ifndef ACCESSPERMS
+#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
+#endif
+
+#define DEFAULT_LOCAL_URI "/tmp/gtksourceview-file-saver-test.txt"
+#define DEFAULT_REMOTE_URI "sftp://localhost/tmp/gtksourceview-file-saver-test.txt";
+#define DEFAULT_CONTENT "hello world!"
+#define DEFAULT_CONTENT_RESULT "hello world!\n"
+
+#define UNOWNED_LOCAL_DIRECTORY "/tmp/gtksourceview-file-saver-unowned"
+#define UNOWNED_LOCAL_URI "/tmp/gtksourceview-file-saver-unowned/gtksourceview-file-saver-test.txt"
+
+#define UNOWNED_REMOTE_DIRECTORY "sftp://localhost/tmp/gtksourceview-file-saver-unowned";
+#define UNOWNED_REMOTE_URI 
"sftp://localhost/tmp/gtksourceview-file-saver-unowned/gtksourceview-file-saver-test.txt";
+
+#define UNOWNED_GROUP_LOCAL_URI "/tmp/gtksourceview-file-saver-unowned-group.txt"
+#define UNOWNED_GROUP_REMOTE_URI "sftp://localhost/tmp/gtksourceview-file-saver-unowned-group.txt";
+
+static gboolean test_completed;
+static gboolean mount_completed;
+static gboolean mount_success;
+
+typedef struct
+{
+       GFile *location;
+       const gchar *test_contents;
+       gpointer data;
+       GCallback saved_callback;
+
+       guint file_existed : 1;
+} SaverTestData;
+
+static SaverTestData *
+saver_test_data_new (GFile       *location,
+                    const gchar *test_contents,
+                    gpointer     data,
+                    GCallback    saved_callback)
+{
+       SaverTestData *ret = g_slice_new (SaverTestData);
+
+       /* FIXME g_object_ref or g_file_dup? */
+       ret->location = g_object_ref (location);
+       ret->test_contents = test_contents;
+       ret->data = data;
+       ret->saved_callback = saved_callback;
+
+       return ret;
+}
+
+static void
+saver_test_data_free (SaverTestData *data)
+{
+       if (data != NULL)
+       {
+               g_object_unref (data->location);
+               g_free (data->uri);
+               g_slice_free (SaverTestData, data);
+       }
+}
+
+static GtkSourceBuffer *
+create_buffer (const gchar *contents)
+{
+       GtkSourceBuffer *buffer = gtk_source_buffer_new (NULL);
+
+       gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), contents, -1);
+       return buffer;
+}
+
+static const gchar *
+read_file (const gchar *uri)
+{
+       /* TODO use g_file_load_contents() */
+       GFile *file = g_file_new_for_commandline_arg (uri);
+       GError *error = NULL;
+       static gchar buffer[4096];
+       gsize read;
+
+       GInputStream *stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
+
+       g_assert_no_error (error);
+
+       g_input_stream_read_all (stream, buffer, sizeof (buffer) - 1, &read, NULL, &error);
+       g_assert_no_error (error);
+
+       buffer[read] = '\0';
+
+       g_input_stream_close (stream, NULL, NULL);
+
+       g_object_unref (stream);
+       g_object_unref (file);
+
+       return buffer;
+}
+
+static void
+mount_ready_callback (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      data)
+{
+       GError *error = NULL;
+       mount_success = g_file_mount_enclosing_volume_finish (G_FILE (object),
+                                                             result,
+                                                             &error);
+
+       if (error != NULL && error->code == G_IO_ERROR_ALREADY_MOUNTED)
+       {
+               mount_success = TRUE;
+               g_error_free (error);
+       }
+       else if (error != NULL && error->code == G_IO_ERROR_NOT_SUPPORTED)
+       {
+               g_error_free (error);
+       }
+       else
+       {
+               g_assert_no_error (error);
+       }
+
+       mount_completed = TRUE;
+}
+
+static gboolean
+ensure_mounted (GFile *file)
+{
+       GMountOperation *mo;
+
+       mount_success = FALSE;
+       mount_completed = FALSE;
+
+       if (g_file_is_native (file))
+       {
+               return TRUE;
+       }
+
+       mo = gtk_mount_operation_new (NULL);
+
+       g_file_mount_enclosing_volume (file,
+                                      G_MOUNT_MOUNT_NONE,
+                                      mo,
+                                      NULL,
+                                      mount_ready_callback,
+                                      NULL);
+
+       while (!mount_completed)
+       {
+               g_main_context_iteration (NULL, TRUE);
+       }
+
+       g_object_unref (mo);
+
+       return mount_success;
+}
+
+static void
+file_saved_cb (GtkSourceFile *file,
+              GAsyncResult  *result,
+              SaverTestData *data)
+{
+       GError *error = NULL;
+
+       g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+       gtk_source_file_save_finish (file, result, &error);
+
+       g_assert_no_error (error);
+
+       if (data != NULL &&
+           data->test_contents != NULL &&
+           data->uri != NULL)
+       {
+               g_assert_cmpstr (data->test_contents, ==, read_file (data->uri));
+       }
+
+       if (data->saved_callback != NULL)
+       {
+               data->saved_callback (file, data);
+       }
+
+       if (!data->file_existed)
+       {
+               /* TODO store the GFile in data */
+               g_file_delete (location, NULL, NULL);
+       }
+}
+
+static void
+test_saver (const gchar            *filename_or_uri,
+           const gchar            *contents,
+           GtkSourceNewlineType    newline_type,
+           GtkSourceFileSaveFlags  save_flags,
+           SaverTestData          *data)
+{
+       GFile *location;
+       GtkSourceBuffer *buffer;
+       GtkSourceFile *file;
+       gboolean existed;
+
+       g_assert (data != NULL);
+
+       location = g_file_new_for_commandline_arg (filename_or_uri);
+       buffer = create_buffer (contents);
+       file = gtk_source_file_new (location, buffer);
+
+       gtk_source_file_set_newline_type (file, newline_type);
+       gtk_source_file_set_encoding (file, gtk_source_encoding_get_utf8 ());
+
+       data->file_existed = g_file_query_exists (location, NULL);
+
+       if (!ensure_mounted (location))
+       {
+               saver_test_data_free (data);
+               return;
+       }
+
+       gtk_source_file_save_async (file,
+                                   save_flags,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   (GAsyncReadyCallback) file_saved_cb,
+                                   data);
+
+       while (!test_completed)
+       {
+               g_main_context_iteration (NULL, TRUE);
+       }
+
+       g_object_unref (file);
+
+       saver_test_data_free (data);
+}
+
+typedef struct
+{
+       GeditDocumentNewlineType type;
+       const gchar *text;
+       const gchar *result;
+} NewLineTestData;
+
+static NewLineTestData newline_test_data[] = {
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\nhello\nworld", "\nhello\nworld\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\nhello\nworld\n", "\nhello\nworld\n\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\nhello\nworld\n\n", "\nhello\nworld\n\n\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\r\nhello\r\nworld", "\nhello\nworld\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\r\nhello\r\nworld\r\n", "\nhello\nworld\n\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\rhello\rworld", "\nhello\nworld\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\rhello\rworld\r", "\nhello\nworld\n\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\nhello\r\nworld", "\nhello\nworld\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_LF, "\nhello\r\nworld\r", "\nhello\nworld\n\n"},
+
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\nhello\nworld", "\r\nhello\r\nworld\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\nhello\nworld\n", "\r\nhello\r\nworld\r\n\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\nhello\nworld\n\n", "\r\nhello\r\nworld\r\n\r\n\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\r\nhello\r\nworld", "\r\nhello\r\nworld\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\r\nhello\r\nworld\r\n", "\r\nhello\r\nworld\r\n\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\rhello\rworld", "\r\nhello\r\nworld\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\rhello\rworld\r", "\r\nhello\r\nworld\r\n\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\nhello\r\nworld", "\r\nhello\r\nworld\r\n"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, "\nhello\r\nworld\r", "\r\nhello\r\nworld\r\n\r\n"},
+
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\nhello\nworld", "\rhello\rworld\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\nhello\nworld\n", "\rhello\rworld\r\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\nhello\nworld\n\n", "\rhello\rworld\r\r\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\r\nhello\r\nworld", "\rhello\rworld\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\r\nhello\r\nworld\r\n", "\rhello\rworld\r\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\rhello\rworld", "\rhello\rworld\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\rhello\rworld\r", "\rhello\rworld\r\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\nhello\r\nworld", "\rhello\rworld\r"},
+       {GEDIT_DOCUMENT_NEWLINE_TYPE_CR, "\nhello\r\nworld\r", "\rhello\rworld\r\r"}
+};
+
+static void
+test_new_line (const gchar *filename, GeditDocumentSaveFlags save_flags)
+{
+       gint i;
+       gint num = sizeof (newline_test_data) / sizeof (NewLineTestData);
+
+       for (i = 0; i < num; ++i)
+       {
+               NewLineTestData *nt = &(newline_test_data[i]);
+
+               test_saver (filename,
+                           nt->text,
+                           nt->type,
+                           save_flags,
+                           saver_test_data_new (filename, nt->result, NULL, NULL));
+       }
+}
+
+static void
+test_local_newline ()
+{
+       test_new_line (DEFAULT_LOCAL_URI, 0);
+}
+
+static void
+test_local ()
+{
+       test_saver (DEFAULT_LOCAL_URI,
+                   "hello world",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n", NULL, NULL));
+
+       test_saver (DEFAULT_LOCAL_URI,
+                   "hello world\r\n",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n\n", NULL, NULL));
+
+       test_saver (DEFAULT_LOCAL_URI,
+                   "hello world\n",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n\n", NULL, NULL));
+}
+
+static void
+test_remote_newline ()
+{
+       test_new_line (DEFAULT_REMOTE_URI, 0);
+}
+
+static void
+test_remote ()
+{
+       test_saver (DEFAULT_REMOTE_URI,
+                   "hello world",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n", NULL, NULL));
+
+       test_saver (DEFAULT_REMOTE_URI,
+                   "hello world\r\n",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n\n", NULL, NULL));
+
+       test_saver (DEFAULT_REMOTE_URI,
+                   "hello world\n",
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n\n", NULL, NULL));
+}
+
+#ifndef G_OS_WIN32
+static void
+check_permissions (GFile *location,
+                   guint  permissions)
+{
+       GError *error = NULL;
+       GFileInfo *info;
+
+       info = g_file_query_info (location,
+                                 G_FILE_ATTRIBUTE_UNIX_MODE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 &error);
+
+       g_assert_no_error (error);
+
+       g_assert_cmpint (permissions,
+                        ==,
+                        g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & ACCESSPERMS);
+
+       g_object_unref (info);
+}
+
+static void
+check_permissions_saved (GtkSourceFile *file,
+                         SaverTestData *data)
+{
+       guint permissions = (guint)GPOINTER_TO_INT (data->data);
+       GFile *location = gtk_source_file_get_location (file);
+
+       check_permissions (location, permissions);
+
+       g_object_unref (location);
+}
+
+static void
+test_permissions (const gchar *uri,
+                  guint        permissions)
+{
+       GError *error = NULL;
+       GFile *file = g_file_new_for_commandline_arg (uri);
+       GFileOutputStream *stream;
+       GFileInfo *info;
+       guint mode;
+
+       g_file_delete (file, NULL, NULL);
+       stream = g_file_create (file, 0, NULL, &error);
+
+       if (error && error->code == G_IO_ERROR_NOT_SUPPORTED)
+       {
+               g_error_free (error);
+               return;
+       }
+
+       g_assert_no_error (error);
+
+       g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
+       g_object_unref (stream);
+
+       info = g_file_query_info (file,
+                                 G_FILE_ATTRIBUTE_UNIX_MODE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 &error);
+
+       g_assert_no_error (error);
+
+       mode = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE);
+       g_object_unref (info);
+
+       g_file_set_attribute_uint32 (file,
+                                    G_FILE_ATTRIBUTE_UNIX_MODE,
+                                    (mode & ~ACCESSPERMS) | permissions,
+                                    G_FILE_QUERY_INFO_NONE,
+                                    NULL,
+                                    &error);
+       g_assert_no_error (error);
+
+       check_permissions (file, permissions);
+
+       test_saver (uri,
+                   DEFAULT_CONTENT,
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (uri,
+                                        DEFAULT_CONTENT_RESULT,
+                                        GINT_TO_POINTER ((gint)permissions),
+                                        G_CALLBACK (check_permissions_saved)));
+
+       g_file_delete (file, NULL, NULL);
+       g_object_unref (file);
+}
+
+static void
+test_local_permissions ()
+{
+       test_permissions (DEFAULT_LOCAL_URI, 0600);
+       test_permissions (DEFAULT_LOCAL_URI, 0660);
+       test_permissions (DEFAULT_LOCAL_URI, 0666);
+       test_permissions (DEFAULT_LOCAL_URI, 0760);
+}
+#endif
+
+static void
+test_local_unowned_directory ()
+{
+       test_saver (UNOWNED_LOCAL_URI,
+                   DEFAULT_CONTENT,
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (UNOWNED_LOCAL_URI, DEFAULT_CONTENT_RESULT, NULL, NULL));
+}
+
+static void
+test_remote_unowned_directory ()
+{
+       test_saver (UNOWNED_REMOTE_URI,
+                   DEFAULT_CONTENT,
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (UNOWNED_REMOTE_URI, DEFAULT_CONTENT_RESULT, NULL, NULL));
+}
+
+#ifndef G_OS_WIN32
+static void
+test_remote_permissions ()
+{
+       test_permissions (DEFAULT_REMOTE_URI, 0600);
+       test_permissions (DEFAULT_REMOTE_URI, 0660);
+       test_permissions (DEFAULT_REMOTE_URI, 0666);
+       test_permissions (DEFAULT_REMOTE_URI, 0760);
+}
+
+static void
+test_unowned_group_permissions (GtkSourceFile *file,
+                                SaverTestData *data)
+{
+       GFile *location = g_file_new_for_commandline_arg (data->uri);
+       GError *error = NULL;
+       const gchar *group;
+       guint32 mode;
+
+       GFileInfo *info = g_file_query_info (location,
+                                            G_FILE_ATTRIBUTE_OWNER_GROUP ","
+                                            G_FILE_ATTRIBUTE_UNIX_MODE,
+                                            G_FILE_QUERY_INFO_NONE,
+                                            NULL,
+                                            &error);
+
+       g_assert_no_error (error);
+
+       group = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP);
+       g_assert_cmpstr (group, ==, "root");
+
+       mode = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE);
+
+       g_assert_cmpint (mode & ACCESSPERMS, ==, 0660);
+
+       g_object_unref (location);
+       g_object_unref (info);
+}
+
+static void
+test_unowned_group (const gchar *uri)
+{
+       test_saver (uri,
+                   DEFAULT_CONTENT,
+                   GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+                   0,
+                   saver_test_data_new (uri,
+                                        DEFAULT_CONTENT_RESULT,
+                                        NULL,
+                                        G_CALLBACK (test_unowned_group_permissions)));
+}
+
+static void
+test_local_unowned_group ()
+{
+       test_unowned_group (UNOWNED_GROUP_LOCAL_URI);
+}
+
+static void
+test_remote_unowned_group ()
+{
+       test_unowned_group (UNOWNED_GROUP_REMOTE_URI);
+}
+
+#endif
+
+static gboolean
+check_unowned_directory ()
+{
+       GFile *unowned = g_file_new_for_path (UNOWNED_LOCAL_DIRECTORY);
+       GFile *unowned_file;
+       GFileInfo *info;
+       GError *error = NULL;
+
+       g_printf ("*** Checking for unowned directory test... ");
+
+       info = g_file_query_info (unowned,
+                                 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 &error);
+
+       if (error)
+       {
+               g_object_unref (unowned);
+               g_printf ("NO: directory does not exist\n");
+
+               g_error_free (error);
+               return FALSE;
+       }
+
+       if (g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+       {
+               g_object_unref (unowned);
+
+               g_printf ("NO: directory is writable\n");
+               g_object_unref (info);
+               return FALSE;
+       }
+
+       g_object_unref (info);
+       g_object_unref (unowned);
+
+       unowned_file = g_file_new_for_commandline_arg (UNOWNED_LOCAL_URI);
+
+       info = g_file_query_info (unowned_file,
+                                 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 &error);
+
+       if (error)
+       {
+               g_object_unref (unowned_file);
+               g_error_free (error);
+
+               g_printf ("NO: file does not exist\n");
+               return FALSE;
+       }
+
+       if (!g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+       {
+               g_object_unref (unowned_file);
+
+               g_printf ("NO: file is not writable\n");
+               g_object_unref (info);
+               return FALSE;
+       }
+
+       g_object_unref (info);
+       g_object_unref (unowned_file);
+
+       g_printf ("YES\n");
+       return TRUE;
+}
+
+static gboolean
+check_unowned_group ()
+{
+       GFile *unowned = g_file_new_for_path (UNOWNED_GROUP_LOCAL_URI);
+       GFileInfo *info;
+       GError *error = NULL;
+
+       g_printf ("*** Checking for unowned group test... ");
+
+       info = g_file_query_info (unowned,
+                                 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE ","
+                                 G_FILE_ATTRIBUTE_OWNER_GROUP ","
+                                 G_FILE_ATTRIBUTE_UNIX_MODE,
+                                 G_FILE_QUERY_INFO_NONE,
+                                 NULL,
+                                 &error);
+
+       if (error)
+       {
+               g_object_unref (unowned);
+               g_printf ("NO: file does not exist\n");
+
+               g_error_free (error);
+               return FALSE;
+       }
+
+       if (!g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+       {
+               g_object_unref (unowned);
+
+               g_printf ("NO: file is not writable\n");
+               g_object_unref (info);
+               return FALSE;
+       }
+
+       if (g_strcmp0 (g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_OWNER_GROUP),
+                      "root") != 0)
+       {
+               g_object_unref (unowned);
+
+               g_printf ("NO: group is not root (%s)\n", g_file_info_get_attribute_string (info, 
G_FILE_ATTRIBUTE_OWNER_GROUP));
+               g_object_unref (info);
+               return FALSE;
+       }
+
+#ifndef G_OS_WIN32
+       if ((g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_MODE) & ACCESSPERMS) != 0660)
+       {
+               g_object_unref (unowned);
+
+               g_printf ("NO: file has wrong permissions\n");
+               g_object_unref (info);
+               return FALSE;
+       }
+#endif
+
+       g_object_unref (info);
+       g_object_unref (unowned);
+
+       g_printf ("YES\n");
+       return TRUE;
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+       gboolean have_unowned;
+       gboolean have_unowned_group;
+
+       g_test_init (&argc, &argv, NULL);
+       gtk_init (&argc, &argv);
+
+       g_printf ("\n***\n");
+       have_unowned = check_unowned_directory ();
+       have_unowned_group = check_unowned_group ();
+       g_printf ("***\n\n");
+
+       g_test_add_func ("/file-saver/local", test_local);
+       g_test_add_func ("/file-saver/local-new-line", test_local_newline);
+
+       if (have_unowned)
+       {
+               g_test_add_func ("/file-saver/local-unowned-directory", test_local_unowned_directory);
+       }
+
+       g_test_add_func ("/file-saver/remote", test_remote);
+       g_test_add_func ("/file-saver/remote-new-line", test_remote_newline);
+
+
+       if (have_unowned)
+       {
+               g_test_add_func ("/file-saver/remote-unowned-directory", test_remote_unowned_directory);
+       }
+
+       if (have_unowned_group)
+       {
+               /* FIXME: there is a bug in gvfs sftp which doesn't pass this test */
+               /* g_test_add_func ("/file-saver/remote-unowned-group", test_remote_unowned_group); */
+       }
+
+#ifndef G_OS_WIN32
+       g_test_add_func ("/file-saver/local-permissions", test_local_permissions);
+
+       if (have_unowned_group)
+       {
+               g_test_add_func ("/file-saver/local-unowned-group", test_local_unowned_group);
+       }
+
+       g_test_add_func ("/file-saver/remote-permissions", test_remote_permissions);
+#endif
+
+       return g_test_run ();
+}


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