summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPerberos <[email protected]>2011-11-07 16:46:58 -0300
committerPerberos <[email protected]>2011-11-07 16:46:58 -0300
commit528c1e5ff51e213936e800fc5a9a25da99c0bdf2 (patch)
tree77f8aa456b09367ba81f04d4562fc935f898a951 /tests
downloadpluma-528c1e5ff51e213936e800fc5a9a25da99c0bdf2.tar.bz2
pluma-528c1e5ff51e213936e800fc5a9a25da99c0bdf2.tar.xz
initial
Diffstat (limited to 'tests')
-rwxr-xr-xtests/Makefile.am28
-rwxr-xr-xtests/document-input-stream.c156
-rwxr-xr-xtests/document-loader.c251
-rwxr-xr-xtests/document-output-stream.c134
-rwxr-xr-xtests/document-saver.c735
-rwxr-xr-xtests/setup-document-saver.sh27
-rwxr-xr-xtests/smart-converter.c353
7 files changed, 1684 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100755
index 00000000..f54ce8c7
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,28 @@
+INCLUDES = -g -I$(top_srcdir) -I$(top_srcdir)/gedit $(GEDIT_DEBUG_FLAGS) $(GEDIT_CFLAGS)
+
+noinst_PROGRAMS = $(TEST_PROGS)
+progs_ldadd = $(top_builddir)/gedit/libgedit.la
+
+TEST_PROGS = smart-converter
+smart_converter_SOURCES = smart-converter.c
+smart_converter_LDADD = $(progs_ldadd)
+
+TEST_PROGS += document-input-stream
+document_input_stream_SOURCES = document-input-stream.c
+document_input_stream_LDADD = $(progs_ldadd)
+
+TEST_PROGS += document-output-stream
+document_output_stream_SOURCES = document-output-stream.c
+document_output_stream_LDADD = $(progs_ldadd)
+
+TEST_PROGS += document-loader
+document_loader_SOURCES = document-loader.c
+document_loader_LDADD = $(progs_ldadd)
+
+TEST_PROGS += document-saver
+document_saver_SOURCES = document-saver.c
+document_saver_LDADD = $(progs_ldadd)
+
+TESTS = $(TEST_PROGS)
+
+EXTRA_DIST = setup-document-saver.sh
diff --git a/tests/document-input-stream.c b/tests/document-input-stream.c
new file mode 100755
index 00000000..152a39f9
--- /dev/null
+++ b/tests/document-input-stream.c
@@ -0,0 +1,156 @@
+/*
+ * document-input-stream.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gedit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+
+#include "gedit-document-input-stream.h"
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <glib.h>
+#include <string.h>
+
+static void
+test_consecutive_read (const gchar *inbuf,
+ const gchar *outbuf,
+ GeditDocumentNewlineType type,
+ gsize read_chunk_len)
+{
+ GtkTextBuffer *buf;
+ GInputStream *in;
+ gsize outlen;
+ gssize n, r;
+ GError *err = NULL;
+ gchar *b;
+ gboolean close;
+
+ buf = gtk_text_buffer_new (NULL);
+ gtk_text_buffer_set_text (buf, inbuf, -1);
+
+ b = g_malloc (200);
+ in = gedit_document_input_stream_new (buf, type);
+
+ outlen = strlen (outbuf);
+ n = 0;
+
+ do
+ {
+ r = g_input_stream_read (in, b + n, read_chunk_len, NULL, &err);
+ g_assert_cmpint (r, >=, 0);
+ g_assert_no_error (err);
+
+ n += r;
+ } while (r != 0);
+
+ g_assert_cmpint (n, ==, outlen);
+
+ b[n] = '\0';
+
+ g_assert_cmpstr (b, ==, outbuf);
+
+ close = g_input_stream_close (in, NULL, &err);
+ g_assert (close);
+ g_assert_no_error (err);
+
+ g_object_unref (buf);
+ g_object_unref (in);
+ g_free (b);
+}
+
+static void
+test_empty ()
+{
+ /* empty file should not have a trailing newline */
+ test_consecutive_read ("", "", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 10);
+}
+
+static void
+test_consecutive_cut_char ()
+{
+ /* first \n is read then fo and then is added \r but not \n */
+ test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 8);
+ test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 8);
+}
+
+static void
+test_consecutive_big_read ()
+{
+ test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 200);
+ test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 200);
+
+ test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 200);
+ test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 200);
+
+ test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 200);
+ test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 200);
+
+ test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 200);
+ test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 200);
+}
+
+static void
+test_consecutive_middle_read ()
+{
+ test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 6);
+ test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 6);
+
+ test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 6);
+ test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 6);
+
+ test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 6);
+ test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 6);
+
+ test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 6);
+ test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF, 6);
+}
+
+static void
+test_consecutive_multibyte_cut ()
+{
+ test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 6);
+ test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 6);
+ test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 6);
+}
+
+static void
+test_consecutive_multibyte_big_read ()
+{
+ test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 200);
+ test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", GEDIT_DOCUMENT_NEWLINE_TYPE_CR, 200);
+ test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", GEDIT_DOCUMENT_NEWLINE_TYPE_LF, 200);
+}
+
+int main (int argc,
+ char *argv[])
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/document-input-stream/empty", test_empty);
+
+ g_test_add_func ("/document-input-stream/consecutive_cut_char", test_consecutive_cut_char);
+ g_test_add_func ("/document-input-stream/consecutive_big_read", test_consecutive_big_read);
+ g_test_add_func ("/document-input-stream/consecutive_middle_read", test_consecutive_middle_read);
+
+ g_test_add_func ("/document-input-stream/consecutive_multibyte_cut", test_consecutive_multibyte_cut);
+ g_test_add_func ("/document-input-stream/consecutive_multibyte_big_read", test_consecutive_multibyte_big_read);
+
+ return g_test_run ();
+}
diff --git a/tests/document-loader.c b/tests/document-loader.c
new file mode 100755
index 00000000..e63e7781
--- /dev/null
+++ b/tests/document-loader.c
@@ -0,0 +1,251 @@
+/*
+ * document-loader.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gedit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "gedit-gio-document-loader.h"
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <glib.h>
+#include <string.h>
+
+static gboolean test_completed;
+
+typedef struct
+{
+ const gchar *in_buffer;
+ gint newline_type;
+ GFile *file;
+} LoaderTestData;
+
+static GFile *
+create_document (const gchar *filename,
+ const gchar *contents)
+{
+ GError *error = NULL;
+ GeditDocument *document;
+ gchar *uri;
+
+ if (!g_file_set_contents (filename, contents, -1, &error))
+ {
+ g_assert_no_error (error);
+ }
+
+ return g_file_new_for_path (filename);
+}
+
+static void
+delete_document (GFile *location)
+{
+ if (g_file_query_exists (location, NULL))
+ {
+ GError *err = NULL;
+
+ g_file_delete (location, NULL, &err);
+ g_assert_no_error (err);
+ }
+
+ test_completed = TRUE;
+}
+
+static void
+on_document_loaded (GeditDocument *document,
+ GError *error,
+ LoaderTestData *data)
+{
+ GtkTextIter start, end;
+
+ g_assert_no_error (error);
+
+ if (data->in_buffer != NULL)
+ {
+ gchar *text;
+
+ gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (document), &start, &end);
+ text = gtk_text_iter_get_slice (&start, &end);
+
+ g_assert_cmpstr (text, ==, data->in_buffer);
+
+ g_free (text);
+ }
+
+ if (data->newline_type != -1)
+ {
+ g_assert_cmpint (gedit_document_get_newline_type (document),
+ ==,
+ data->newline_type);
+ }
+
+ delete_document (data->file);
+}
+
+static void
+test_loader (const gchar *filename,
+ const gchar *contents,
+ const gchar *in_buffer,
+ gint newline_type)
+{
+ GFile *file;
+ gchar *uri;
+ GeditDocument *document;
+
+ file = create_document (filename, contents);
+
+ document = gedit_document_new ();
+
+ LoaderTestData *data = g_slice_new (LoaderTestData);
+ data->in_buffer = in_buffer;
+ data->newline_type = newline_type;
+ data->file = file;
+
+ test_completed = FALSE;
+
+ g_signal_connect (document,
+ "loaded",
+ G_CALLBACK (on_document_loaded),
+ data);
+
+ uri = g_file_get_uri (file);
+
+ gedit_document_load (document, uri, gedit_encoding_get_utf8 (), 0, FALSE);
+
+ g_free (uri);
+
+ while (!test_completed)
+ {
+ g_main_context_iteration (NULL, TRUE);
+ }
+
+ g_slice_free (LoaderTestData, data);
+ g_object_unref (file);
+ g_object_unref (document);
+}
+
+static void
+test_end_line_stripping ()
+{
+ test_loader ("document-loader.txt",
+ "hello world\n",
+ "hello world",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "hello world",
+ "hello world",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\nhello world",
+ "\nhello world",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\nhello world\n",
+ "\nhello world",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "hello world\n\n",
+ "hello world\n",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "hello world\r\n",
+ "hello world",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "hello world\r\n\r\n",
+ "hello world\r\n",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\n",
+ "",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\r\n",
+ "",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\n\n",
+ "\n",
+ -1);
+
+ test_loader ("document-loader.txt",
+ "\r\n\r\n",
+ "\r\n",
+ -1);
+}
+
+static void
+test_end_new_line_detection ()
+{
+ test_loader ("document-loader.txt",
+ "hello world\n",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+
+ test_loader ("document-loader.txt",
+ "hello world\r\n",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+
+ test_loader ("document-loader.txt",
+ "hello world\r",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+}
+
+static void
+test_begin_new_line_detection ()
+{
+ test_loader ("document-loader.txt",
+ "\nhello world",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+
+ test_loader ("document-loader.txt",
+ "\r\nhello world",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+
+ test_loader ("document-loader.txt",
+ "\rhello world",
+ NULL,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+}
+
+int main (int argc,
+ char *argv[])
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ gedit_prefs_manager_app_init ();
+
+ g_test_add_func ("/document-loader/end-line-stripping", test_end_line_stripping);
+ g_test_add_func ("/document-loader/end-new-line-detection", test_end_new_line_detection);
+ g_test_add_func ("/document-loader/begin-new-line-detection", test_begin_new_line_detection);
+
+ return g_test_run ();
+}
diff --git a/tests/document-output-stream.c b/tests/document-output-stream.c
new file mode 100755
index 00000000..eb571680
--- /dev/null
+++ b/tests/document-output-stream.c
@@ -0,0 +1,134 @@
+/*
+ * document-output-stream.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gedit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+
+#include "gedit-document-output-stream.h"
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <glib.h>
+#include <string.h>
+
+static void
+test_consecutive_write (const gchar *inbuf,
+ const gchar *outbuf,
+ gsize write_chunk_len,
+ GeditDocumentNewlineType newline_type)
+{
+ GeditDocument *doc;
+ GOutputStream *out;
+ gsize len;
+ gssize n, w;
+ GError *err = NULL;
+ gchar *b;
+ GeditDocumentNewlineType type;
+
+ doc = gedit_document_new ();
+ out = gedit_document_output_stream_new (doc);
+
+ n = 0;
+
+ do
+ {
+ len = MIN (write_chunk_len, strlen (inbuf + n));
+ w = g_output_stream_write (out, inbuf + n, len, NULL, &err);
+ g_assert_cmpint (w, >=, 0);
+ g_assert_no_error (err);
+
+ n += w;
+ } while (w != 0);
+
+ g_object_get (G_OBJECT (doc), "text", &b, NULL);
+
+ g_assert_cmpstr (inbuf, ==, b);
+ g_free (b);
+
+ type = gedit_document_output_stream_detect_newline_type (GEDIT_DOCUMENT_OUTPUT_STREAM (out));
+ g_assert (type == newline_type);
+
+ g_output_stream_close (out, NULL, &err);
+ g_assert_no_error (err);
+
+ g_object_get (G_OBJECT (doc), "text", &b, NULL);
+
+ g_assert_cmpstr (outbuf, ==, b);
+ g_free (b);
+
+ g_assert (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)) == FALSE);
+
+ g_object_unref (doc);
+ g_object_unref (out);
+}
+
+static void
+test_empty ()
+{
+ test_consecutive_write ("", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT);
+ test_consecutive_write ("\r\n", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+ test_consecutive_write ("\r", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+ test_consecutive_write ("\n", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+}
+
+static void
+test_consecutive ()
+{
+ test_consecutive_write ("hello\nhow\nare\nyou", "hello\nhow\nare\nyou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+ test_consecutive_write ("hello\rhow\rare\ryou", "hello\rhow\rare\ryou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+ test_consecutive_write ("hello\r\nhow\r\nare\r\nyou", "hello\r\nhow\r\nare\r\nyou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+}
+
+static void
+test_consecutive_tnewline ()
+{
+ test_consecutive_write ("hello\nhow\nare\nyou\n", "hello\nhow\nare\nyou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+ test_consecutive_write ("hello\rhow\rare\ryou\r", "hello\rhow\rare\ryou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
+ test_consecutive_write ("hello\r\nhow\r\nare\r\nyou\r\n", "hello\r\nhow\r\nare\r\nyou", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
+}
+
+static void
+test_big_char ()
+{
+ test_consecutive_write ("\343\203\200\343\203\200", "\343\203\200\343\203\200", 2,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
+}
+
+int main (int argc,
+ char *argv[])
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ gedit_prefs_manager_app_init ();
+
+ g_test_add_func ("/document-output-stream/empty", test_empty);
+
+ g_test_add_func ("/document-output-stream/consecutive", test_consecutive);
+ g_test_add_func ("/document-output-stream/consecutive_tnewline", test_consecutive_tnewline);
+ g_test_add_func ("/document-output-stream/big-char", test_big_char);
+
+ return g_test_run ();
+}
diff --git a/tests/document-saver.c b/tests/document-saver.c
new file mode 100755
index 00000000..9f44d5a9
--- /dev/null
+++ b/tests/document-saver.c
@@ -0,0 +1,735 @@
+/*
+ * document-saver.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gedit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "gedit-gio-document-loader.h"
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <glib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#define DEFAULT_LOCAL_URI "/tmp/gedit-document-saver-test.txt"
+#define DEFAULT_REMOTE_URI "sftp://localhost/tmp/gedit-document-saver-test.txt"
+#define DEFAULT_CONTENT "hello world!"
+#define DEFAULT_CONTENT_RESULT "hello world!\n"
+
+#define UNOWNED_LOCAL_DIRECTORY "/tmp/gedit-document-saver-unowned"
+#define UNOWNED_LOCAL_URI "/tmp/gedit-document-saver-unowned/gedit-document-saver-test.txt"
+
+#define UNOWNED_REMOTE_DIRECTORY "sftp://localhost/tmp/gedit-document-saver-unowned"
+#define UNOWNED_REMOTE_URI "sftp://localhost/tmp/gedit-document-saver-unowned/gedit-document-saver-test.txt"
+
+#define UNOWNED_GROUP_LOCAL_URI "/tmp/gedit-document-saver-unowned-group.txt"
+#define UNOWNED_GROUP_REMOTE_URI "sftp://localhost/tmp/gedit-document-saver-unowned-group.txt"
+
+static gboolean test_completed;
+static gboolean mount_completed;
+static gboolean mount_success;
+
+typedef struct
+{
+ gchar *uri;
+ const gchar *test_contents;
+ gpointer data;
+} SaverTestData;
+
+static SaverTestData *
+saver_test_data_new (const gchar *uri, const gchar *test_contents, gpointer data)
+{
+ SaverTestData *ret = g_slice_new (SaverTestData);
+
+ ret->uri = g_strdup (uri);
+ ret->test_contents = test_contents;
+ ret->data = data;
+
+ return ret;
+}
+
+static void
+saver_test_data_free (SaverTestData *data)
+{
+ if (data == NULL)
+ {
+ return;
+ }
+
+ g_free (data->uri);
+ g_slice_free (SaverTestData, data);
+}
+
+static GeditDocument *
+create_document (const gchar *contents)
+{
+ GeditDocument *document = gedit_document_new ();
+
+ gtk_text_buffer_set_text (GTK_TEXT_BUFFER (document), contents, -1);
+ return document;
+}
+
+static void
+complete_test_error (GeditDocument *document,
+ GError *error,
+ SaverTestData *data)
+{
+ g_assert_no_error (error);
+}
+
+static const gchar *
+read_file (const gchar *uri)
+{
+ 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
+complete_test (GeditDocument *document,
+ GError *error,
+ SaverTestData *data)
+{
+ test_completed = TRUE;
+
+ if (data && data->test_contents && data->uri)
+ {
+ g_assert_cmpstr (data->test_contents, ==, read_file (data->uri));
+ }
+}
+
+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 && error->code == G_IO_ERROR_ALREADY_MOUNTED)
+ {
+ mount_success = TRUE;
+ 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
+test_saver (const gchar *filename_or_uri,
+ const gchar *contents,
+ GeditDocumentNewlineType newline_type,
+ GeditDocumentSaveFlags save_flags,
+ GCallback saved_callback,
+ SaverTestData *data)
+{
+ GFile *file;
+ gchar *uri;
+ GeditDocument *document;
+ gboolean existed;
+
+ document = create_document (contents);
+ gedit_document_set_newline_type (document, newline_type);
+
+ g_signal_connect (document, "saved", G_CALLBACK (complete_test_error), data);
+
+ if (saved_callback)
+ {
+ g_signal_connect (document, "saved", saved_callback, data);
+ }
+
+ g_signal_connect_after (document, "saved", G_CALLBACK (complete_test), data);
+
+ test_completed = FALSE;
+
+ file = g_file_new_for_commandline_arg (filename_or_uri);
+ uri = g_file_get_uri (file);
+ existed = g_file_query_exists (file, NULL);
+
+ ensure_mounted (file);
+
+ gedit_document_save_as (document, uri, gedit_encoding_get_utf8 (), save_flags);
+
+ while (!test_completed)
+ {
+ g_main_context_iteration (NULL, TRUE);
+ }
+
+ if (!existed)
+ {
+ g_file_delete (file, NULL, NULL);
+ }
+
+ g_free (uri);
+ 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,
+ NULL,
+ saver_test_data_new (filename, nt->result, 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,
+ NULL,
+ saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n", NULL));
+
+ test_saver (DEFAULT_LOCAL_URI,
+ "hello world\r\n",
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ NULL,
+ saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n\n", NULL));
+
+ test_saver (DEFAULT_LOCAL_URI,
+ "hello world\n",
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ NULL,
+ saver_test_data_new (DEFAULT_LOCAL_URI, "hello world\n\n", 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,
+ NULL,
+ saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n", NULL));
+
+ test_saver (DEFAULT_REMOTE_URI,
+ "hello world\r\n",
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ NULL,
+ saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n\n", NULL));
+
+ test_saver (DEFAULT_REMOTE_URI,
+ "hello world\n",
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ NULL,
+ saver_test_data_new (DEFAULT_REMOTE_URI, "hello world\n\n", NULL));
+}
+
+#ifndef G_OS_WIN32
+static void
+check_permissions (GFile *file,
+ guint permissions)
+{
+ GError *error = NULL;
+ GFileInfo *info;
+
+ info = g_file_query_info (file,
+ 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 (GeditDocument *document,
+ GError *error,
+ SaverTestData *data)
+{
+ guint permissions = (guint)GPOINTER_TO_INT (data->data);
+ GFile *file = gedit_document_get_location (document);
+
+ check_permissions (file, permissions);
+
+ g_object_unref (file);
+}
+
+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);
+
+ 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,
+ G_CALLBACK (check_permissions_saved),
+ saver_test_data_new (uri,
+ DEFAULT_CONTENT_RESULT,
+ GINT_TO_POINTER ((gint)permissions)));
+
+ 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,
+ NULL,
+ saver_test_data_new (UNOWNED_LOCAL_URI,
+ DEFAULT_CONTENT_RESULT,
+ NULL));
+}
+
+static void
+test_remote_unowned_directory ()
+{
+ test_saver (UNOWNED_REMOTE_URI,
+ DEFAULT_CONTENT,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ NULL,
+ saver_test_data_new (UNOWNED_REMOTE_URI,
+ DEFAULT_CONTENT_RESULT,
+ 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 (GeditDocument *document,
+ GError *error,
+ SaverTestData *data)
+{
+ GFile *file = g_file_new_for_commandline_arg (data->uri);
+ GError *err = NULL;
+ const gchar *group;
+ guint32 mode;
+
+ GFileInfo *info = g_file_query_info (file,
+ G_FILE_ATTRIBUTE_OWNER_GROUP ","
+ G_FILE_ATTRIBUTE_UNIX_MODE,
+ G_FILE_QUERY_INFO_NONE,
+ NULL,
+ &err);
+
+ g_assert_no_error (err);
+
+ 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 (file);
+ g_object_unref (info);
+}
+
+static void
+test_unowned_group (const gchar *uri)
+{
+ test_saver (uri,
+ DEFAULT_CONTENT,
+ GEDIT_DOCUMENT_NEWLINE_TYPE_LF,
+ 0,
+ G_CALLBACK (test_unowned_group_permissions),
+ saver_test_data_new (uri,
+ DEFAULT_CONTENT_RESULT,
+ NULL));
+}
+
+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;
+}
+
+int main (int argc,
+ char *argv[])
+{
+ gboolean have_unowned;
+ gboolean have_unowned_group;
+
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ gedit_prefs_manager_app_init ();
+
+ g_printf ("\n***\n");
+ have_unowned = check_unowned_directory ();
+ have_unowned_group = check_unowned_group ();
+ g_printf ("***\n\n");
+
+ g_test_add_func ("/document-saver/local", test_local);
+ g_test_add_func ("/document-saver/local-new-line", test_local_newline);
+
+ if (have_unowned)
+ {
+ g_test_add_func ("/document-saver/local-unowned-directory", test_local_unowned_directory);
+ }
+
+ g_test_add_func ("/document-saver/remote", test_remote);
+ g_test_add_func ("/document-saver/remote-new-line", test_remote_newline);
+
+
+ if (have_unowned)
+ {
+ g_test_add_func ("/document-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 ("/document-saver/remote-unowned-group", test_remote_unowned_group); */
+ }
+
+#ifndef G_OS_WIN32
+ g_test_add_func ("/document-saver/local-permissions", test_local_permissions);
+
+ if (have_unowned_group)
+ {
+ g_test_add_func ("/document-saver/local-unowned-group", test_local_unowned_group);
+ }
+
+ g_test_add_func ("/document-saver/remote-permissions", test_remote_permissions);
+#endif
+
+ return g_test_run ();
+}
diff --git a/tests/setup-document-saver.sh b/tests/setup-document-saver.sh
new file mode 100755
index 00000000..4e35d327
--- /dev/null
+++ b/tests/setup-document-saver.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# This script is used to setup some special directory structures, permissions
+# for the saver test
+
+UNOWNED_DIRECTORY="/tmp/gedit-document-saver-unowned"
+UNOWNED_FILE="/tmp/gedit-document-saver-unowned/gedit-document-saver-test.txt"
+
+UNOWNED_GROUP="/tmp/gedit-document-saver-unowned-group.txt"
+
+if [ -f "$UNOWNED_FILE" ]; then
+ sudo rm "$UNOWNED_FILE"
+fi
+
+if [ -d "$UNOWNED_DIRECTORY" ]; then
+ sudo rmdir "$UNOWNED_DIRECTORY"
+fi
+
+mkdir "$UNOWNED_DIRECTORY"
+touch "$UNOWNED_FILE"
+
+sudo chown nobody "$UNOWNED_DIRECTORY"
+
+sudo touch "$UNOWNED_GROUP"
+sudo chgrp root "$UNOWNED_GROUP"
+sudo chmod u+w,g+w,o-rwx "$UNOWNED_GROUP"
+sudo chown $USER "$UNOWNED_GROUP"
diff --git a/tests/smart-converter.c b/tests/smart-converter.c
new file mode 100755
index 00000000..b48240a4
--- /dev/null
+++ b/tests/smart-converter.c
@@ -0,0 +1,353 @@
+/*
+ * smart-converter.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2009 - Ignacio Casal Quinteiro
+ *
+ * gedit is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * gedit 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+
+#include "gedit-smart-charset-converter.h"
+#include "gedit-encodings.h"
+#include <gio/gio.h>
+#include <glib.h>
+#include <string.h>
+
+#define TEXT_TO_CONVERT "this is some text to make the tests"
+#define TEXT_TO_GUESS "hello \xe6\x96\x87 world"
+
+static void
+print_hex (gchar *ptr, gint len)
+{
+ gint i;
+
+ for (i = 0; i < len; ++i)
+ {
+ g_printf ("\\x%02x", (unsigned char)ptr[i]);
+ }
+
+ g_printf ("\n");
+}
+
+static gchar *
+get_encoded_text (const gchar *text,
+ gsize nread,
+ const GeditEncoding *to,
+ const GeditEncoding *from,
+ gsize *bytes_written_aux,
+ gboolean care_about_error)
+{
+ GCharsetConverter *converter;
+ gchar *out, *out_aux;
+ gsize bytes_read, bytes_read_aux;
+ gsize bytes_written;
+ GConverterResult res;
+ GError *err;
+
+ converter = g_charset_converter_new (gedit_encoding_get_charset (to),
+ gedit_encoding_get_charset (from),
+ NULL);
+
+ out = g_malloc (200);
+ out_aux = g_malloc (200);
+ err = NULL;
+ bytes_read_aux = 0;
+ *bytes_written_aux = 0;
+
+ if (nread == -1)
+ {
+ nread = strlen (text);
+ }
+
+ do
+ {
+ res = g_converter_convert (G_CONVERTER (converter),
+ text + bytes_read_aux,
+ nread,
+ out_aux,
+ 200,
+ G_CONVERTER_INPUT_AT_END,
+ &bytes_read,
+ &bytes_written,
+ &err);
+ memcpy (out + *bytes_written_aux, out_aux, bytes_written);
+ bytes_read_aux += bytes_read;
+ *bytes_written_aux += bytes_written;
+ nread -= bytes_read;
+ } while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);
+
+ if (care_about_error)
+ {
+ g_assert_no_error (err);
+ }
+ else if (err)
+ {
+ g_printf ("** You don't care, but there was an error: %s", err->message);
+ return NULL;
+ }
+
+ out[*bytes_written_aux] = '\0';
+
+ if (!g_utf8_validate (out, *bytes_written_aux, NULL) && !care_about_error)
+ {
+ if (!care_about_error)
+ {
+ return NULL;
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+ }
+
+ return out;
+}
+
+static GSList *
+get_all_encodings ()
+{
+ GSList *encs = NULL;
+ gint i = 0;
+
+ while (TRUE)
+ {
+ const GeditEncoding *enc;
+
+ enc = gedit_encoding_get_from_index (i);
+
+ if (enc == NULL)
+ break;
+
+ encs = g_slist_prepend (encs, (gpointer)enc);
+ i++;
+ }
+
+ return encs;
+}
+
+static gchar *
+do_test (const gchar *test_in,
+ const gchar *enc,
+ GSList *encodings,
+ gsize nread,
+ const GeditEncoding **guessed)
+{
+ GeditSmartCharsetConverter *converter;
+ gchar *out, *out_aux;
+ gsize bytes_read, bytes_read_aux;
+ gsize bytes_written, bytes_written_aux;
+ GConverterResult res;
+ GError *err;
+
+ if (enc != NULL)
+ {
+ encodings = NULL;
+ encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset (enc));
+ }
+
+ converter = gedit_smart_charset_converter_new (encodings);
+
+ out = g_malloc (200);
+ out_aux = g_malloc (200);
+ err = NULL;
+ bytes_read_aux = 0;
+ bytes_written_aux = 0;
+
+ do
+ {
+ res = g_converter_convert (G_CONVERTER (converter),
+ test_in + bytes_read_aux,
+ nread,
+ out_aux,
+ 200,
+ G_CONVERTER_INPUT_AT_END,
+ &bytes_read,
+ &bytes_written,
+ &err);
+ memcpy (out + bytes_written_aux, out_aux, bytes_written);
+ bytes_read_aux += bytes_read;
+ bytes_written_aux += bytes_written;
+ nread -= bytes_read;
+ } while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);
+
+ g_assert_no_error (err);
+ out[bytes_written_aux] = '\0';
+
+ if (guessed != NULL)
+ *guessed = gedit_smart_charset_converter_get_guessed (converter);
+
+ return out;
+}
+
+static void
+do_test_roundtrip (const char *str, const char *charset)
+{
+ gsize len;
+ gchar *buf, *p;
+ GInputStream *in, *tmp;
+ GCharsetConverter *c1;
+ GeditSmartCharsetConverter *c2;
+ gsize n, tot;
+ GError *err;
+ GSList *enc = NULL;
+
+ len = strlen(str);
+ buf = g_new0 (char, len);
+
+ in = g_memory_input_stream_new_from_data (str, -1, NULL);
+
+ c1 = g_charset_converter_new (charset, "UTF-8", NULL);
+
+ tmp = in;
+ in = g_converter_input_stream_new (in, G_CONVERTER (c1));
+ g_object_unref (tmp);
+ g_object_unref (c1);
+
+ enc = g_slist_prepend (enc, (gpointer)gedit_encoding_get_from_charset (charset));
+ c2 = gedit_smart_charset_converter_new (enc);
+ g_slist_free (enc);
+
+ tmp = in;
+ in = g_converter_input_stream_new (in, G_CONVERTER (c2));
+ g_object_unref (tmp);
+ g_object_unref (c2);
+
+ tot = 0;
+ p = buf;
+ n = len;
+ while (TRUE)
+ {
+ gssize res;
+
+ err = NULL;
+ res = g_input_stream_read (in, p, n, NULL, &err);
+ g_assert_no_error (err);
+ if (res == 0)
+ break;
+
+ p += res;
+ n -= res;
+ tot += res;
+ }
+
+ g_assert_cmpint (tot, ==, len);
+ g_assert_cmpstr (str, ==, buf);
+
+ g_free (buf);
+ g_object_unref (in);
+}
+
+static void
+test_utf8_utf8 ()
+{
+ gchar *aux;
+
+ aux = do_test (TEXT_TO_CONVERT, "UTF-8", NULL, strlen (TEXT_TO_CONVERT), NULL);
+ g_assert_cmpstr (aux, ==, TEXT_TO_CONVERT);
+
+ aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, NULL);
+ g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
+
+ aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 9, NULL);
+ g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3");
+
+ /* FIXME: Use the utf8 stream for a fallback? */
+ //do_test_with_error ("\xef\xbf\xbezzzzzz", encs, G_IO_ERROR_FAILED);
+}
+
+static void
+test_xxx_xxx ()
+{
+ GSList *encs, *l;
+
+ encs = get_all_encodings ();
+
+ /* Here we just test all encodings it is just to know that the conversions
+ are done ok */
+ for (l = encs; l != NULL; l = g_slist_next (l))
+ {
+ do_test_roundtrip (TEXT_TO_CONVERT, gedit_encoding_get_charset ((const GeditEncoding *)l->data));
+ }
+
+ g_slist_free (encs);
+}
+
+static void
+test_empty ()
+{
+ const GeditEncoding *guessed;
+ gchar *out;
+ GSList *encodings = NULL;
+
+ /* testing the case of an empty file and list of encodings with no
+ utf-8. In this case, the smart converter cannot determine the right
+ encoding (because there is no input), but should still default to
+ utf-8 for the detection */
+ encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset ("UTF-16"));
+ encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset ("ISO-8859-15"));
+
+ out = do_test ("", NULL, encodings, 0, &guessed);
+
+ g_assert_cmpstr (out, ==, "");
+
+ g_assert (guessed == gedit_encoding_get_utf8 ());
+}
+
+static void
+test_guessed ()
+{
+ GSList *encs = NULL;
+ gchar *aux, *aux2, *fail;
+ gsize aux_len, fail_len;
+ const GeditEncoding *guessed;
+
+ aux = get_encoded_text (TEXT_TO_GUESS, -1,
+ gedit_encoding_get_from_charset ("UTF-16"),
+ gedit_encoding_get_from_charset ("UTF-8"),
+ &aux_len,
+ TRUE);
+
+ fail = get_encoded_text (aux, aux_len,
+ gedit_encoding_get_from_charset ("UTF-8"),
+ gedit_encoding_get_from_charset ("ISO-8859-15"),
+ &fail_len,
+ FALSE);
+
+ g_assert (fail == NULL);
+
+ /* ISO-8859-15 should fail */
+ encs = g_slist_append (encs, (gpointer)gedit_encoding_get_from_charset ("ISO-8859-15"));
+ encs = g_slist_append (encs, (gpointer)gedit_encoding_get_from_charset ("UTF-16"));
+
+ aux2 = do_test (aux, NULL, encs, aux_len, &guessed);
+
+ g_assert (guessed == gedit_encoding_get_from_charset ("UTF-16"));
+}
+
+int main (int argc,
+ char *argv[])
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/smart-converter/utf8-utf8", test_utf8_utf8);
+ //g_test_add_func ("/smart-converter/xxx-xxx", test_xxx_xxx);
+ g_test_add_func ("/smart-converter/guessed", test_guessed);
+ g_test_add_func ("/smart-converter/empty", test_empty);
+
+ return g_test_run ();
+}