diff options
author | Diego Escalante Urrelo <diegoe@igalia.com> | 2012-04-02 08:27:09 +0800 |
---|---|---|
committer | Diego Escalante Urrelo <diegoe@igalia.com> | 2012-04-30 08:15:32 +0800 |
commit | d1ba46b3f4a4e2afbffe5a0ab2543462caea0bbe (patch) | |
tree | 3de229f977528d10f5e7280251907dcb2e8fa3c8 /tests | |
parent | cebb6d5f7baf01520e02d2434612c57fe878acea (diff) | |
download | gsoc2013-epiphany-d1ba46b3f4a4e2afbffe5a0ab2543462caea0bbe.tar.gz gsoc2013-epiphany-d1ba46b3f4a4e2afbffe5a0ab2543462caea0bbe.tar.zst gsoc2013-epiphany-d1ba46b3f4a4e2afbffe5a0ab2543462caea0bbe.zip |
tests: add ephy-file-helpers-test
Test API for file switch, tmp file creation, downloads, desktop
directories.
https://bugzilla.gnome.org/show_bug.cgi?id=673337
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 7 | ||||
-rw-r--r-- | tests/data/user-dirs.dirs | 2 | ||||
-rw-r--r-- | tests/ephy-file-helpers-test.c | 392 |
3 files changed, 401 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index cb0944267..c116dbbdf 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,6 +2,7 @@ noinst_PROGRAMS = \ test-ephy-download \ test-ephy-embed-single \ test-ephy-embed-utils \ + test-ephy-file-helpers \ test-ephy-history \ test-ephy-location-entry \ test-ephy-search-entry \ @@ -119,6 +120,12 @@ test_ephy_embed_single_SOURCES = \ test_ephy_embed_utils_SOURCES = \ ephy-embed-utils-test.c +test_ephy_file_helpers_SOURCES = \ + ephy-file-helpers-test.c +test_ephy_file_helpers_CPPFLAGS = \ + -DTOP_SRC_DIR=\"$(abs_top_srcdir)\" \ + $(AM_CPPFLAGS) + test_ephy_history_SOURCES = \ ephy-history-test.c diff --git a/tests/data/user-dirs.dirs b/tests/data/user-dirs.dirs new file mode 100644 index 000000000..f816f8939 --- /dev/null +++ b/tests/data/user-dirs.dirs @@ -0,0 +1,2 @@ +XDG_DESKTOP_DIR="/tmp/ephy-xdg-desktop" +XDG_DOWNLOAD_DIR="/tmp/ephy-xdg-downloads" diff --git a/tests/ephy-file-helpers-test.c b/tests/ephy-file-helpers-test.c new file mode 100644 index 000000000..bc7fbaeac --- /dev/null +++ b/tests/ephy-file-helpers-test.c @@ -0,0 +1,392 @@ +/* vim: set sw=2 ts=2 sts=2 et: */ +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * ephy-file-helpers-test.c + * This file is part of Epiphany + * + * Copyright © 2012 - Igalia S.L. + * + * Epiphany 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. + * + * Epiphany 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 Epiphany; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "config.h" +#include "ephy-debug.h" +#include "ephy-file-helpers.h" +#include "ephy-settings.h" + +#include <glib.h> +#include <gtk/gtk.h> + +typedef struct { + const char *dir; + gboolean private; + gboolean keep_tmp; +} FileInitTest; + +static const FileInitTest private_tests[] = +{ + { NULL, TRUE, FALSE }, + { NULL, TRUE, TRUE } +}; + +static void +test_ephy_file_helpers_init () +{ + int i; + + for (i = 0; i < G_N_ELEMENTS (private_tests); i++) { + FileInitTest test; + + char *tmp_dir = NULL; + char *dot_dir = NULL; + + test = private_tests[i]; + g_test_message ("INIT: dir: %s; private: %s; keep-tmp: %s;", + test.dir, + test.private ? "TRUE" : "FALSE", + test.keep_tmp ? "TRUE" : "FALSE"); + + g_assert (ephy_dot_dir () == NULL); + g_assert (ephy_file_helpers_init (NULL, test.private, test.keep_tmp, NULL)); + + tmp_dir = g_strdup (ephy_file_tmp_dir ()); + dot_dir = g_strdup (ephy_dot_dir ()); + + g_assert (tmp_dir != NULL); + g_assert (dot_dir != NULL); + + /* These should always exist after init. */ + g_assert (g_file_test (tmp_dir, G_FILE_TEST_EXISTS)); + g_assert (g_file_test (dot_dir, G_FILE_TEST_EXISTS)); + + ephy_file_helpers_shutdown (); + + /* Private profiles have their dot_dir inside tmp_dir. */ + g_assert (g_file_test (tmp_dir, G_FILE_TEST_EXISTS) == test.keep_tmp); + g_assert (g_file_test (dot_dir, G_FILE_TEST_EXISTS) == test.keep_tmp); + + /* Cleanup tmp-dir left behind. */ + if (test.keep_tmp) { + GFile *file; + + file = g_file_new_for_path (tmp_dir); + /* As a safety measure, only try recursive delete on paths + * prefixed with /tmp. */ + if (g_str_has_prefix (tmp_dir, "/tmp")) + g_assert (ephy_file_delete_dir_recursively (file, NULL)); + else + g_warning ("INIT: dangerous path returned as tmp_dir: %s", tmp_dir); + + g_object_unref (file); + } + + g_free (tmp_dir); + g_free (dot_dir); + } +} + +typedef struct { + const char *key_value; + const char *expected; + GUserDirectory user_dir; +} DownloadsDirTest; + +static const DownloadsDirTest downloads_tests[] = +{ + { "Desktop", NULL, G_USER_DIRECTORY_DESKTOP }, + + { "Downloads", NULL, G_USER_DIRECTORY_DOWNLOAD }, + { "invalid-keyword", NULL, G_USER_DIRECTORY_DOWNLOAD }, + + { "/tmp/Downloads", "/tmp/Downloads", -1 }, +}; + +static void +test_ephy_file_get_downloads_dir () +{ + int i; + + ephy_file_helpers_init (NULL, TRUE, FALSE, NULL); + + for (i = 0; i < G_N_ELEMENTS (downloads_tests); i++) { + DownloadsDirTest test; + char *downloads_dir = NULL; + const char *expected_dir = NULL; + + test = downloads_tests[i]; + + if (test.expected != NULL) + expected_dir = test.expected; + else + expected_dir = g_get_user_special_dir (test.user_dir); + + g_test_message ("DOWNLOADS: key_value: %s; expected: %s;", + test.key_value, + expected_dir); + + g_settings_set_string (EPHY_SETTINGS_STATE, + EPHY_PREFS_STATE_DOWNLOAD_DIR, + test.key_value); + + downloads_dir = ephy_file_get_downloads_dir (); + g_assert_cmpstr (downloads_dir, ==, expected_dir); + + g_free (downloads_dir); + } + + ephy_file_helpers_shutdown (); +} + +typedef struct { + const char *dir; + gboolean exists; + gboolean can_create; + gboolean can_delete; +} DirTest; + +static const DirTest dir_tests[] = +{ + { "/tmp", TRUE, FALSE, FALSE }, + { "/tmp/ephy-test-dir", FALSE, TRUE, TRUE }, + /* Unreadable */ + { "/root/ephy-test-dir", FALSE, FALSE, FALSE }, + /* Unwritable */ + { "/non-existant/ephy-test-dir", FALSE, FALSE, FALSE }, +}; + +static void +test_ephy_file_create_delete_dir () +{ + int i; + + ephy_file_helpers_init (NULL, TRUE, FALSE, NULL); + + for (i = 0; i < G_N_ELEMENTS (dir_tests); i++) { + GFile *file = NULL; + DirTest test; + + test = dir_tests[i]; + + g_test_message ("DIR: testing for %s", test.dir); + + g_assert (g_file_test (test.dir, G_FILE_TEST_EXISTS) == test.exists); + g_assert (ephy_ensure_dir_exists (test.dir, NULL) == (test.exists || test.can_create)); + g_assert (g_file_test (test.dir, G_FILE_TEST_EXISTS) == (test.exists || test.can_create)); + + file = g_file_new_for_path (test.dir); + + g_assert (ephy_file_delete_dir_recursively (file, NULL) == test.can_delete); + + if (test.exists) + g_assert (g_file_test (test.dir, G_FILE_TEST_EXISTS) != test.can_delete); + + g_object_unref (file); + } + + ephy_file_helpers_shutdown (); +} + +static void +test_ephy_file_desktop_dir () +{ + char *desktop_dir; + const char *xdg_desktop; + + ephy_file_helpers_init (NULL, TRUE, FALSE, NULL); + + desktop_dir = ephy_file_desktop_dir (); + xdg_desktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP); + g_assert_cmpstr (desktop_dir, ==, xdg_desktop); + + g_free (desktop_dir); + + ephy_file_helpers_shutdown (); +} + +static void +test_ephy_file_create_delete_tmp () +{ + char *tmp_file = NULL; + char *tmp_path = NULL; + char *tmp_uri = NULL; + char *tmp_path_prefix = NULL; + + ephy_file_helpers_init (NULL, TRUE, FALSE, NULL); + + /* Fails because filename has no "XXXXXX" keyword. */ + tmp_file = ephy_file_tmp_filename ("test-ephy", NULL); + tmp_path = g_build_filename (ephy_file_tmp_dir (), "test-ephy", NULL); + + g_assert (tmp_file == NULL); + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS) == FALSE); + + g_free (tmp_path); + g_free (tmp_file); + + /* Simple creation. */ + tmp_file = ephy_file_tmp_filename ("test-ephy-XXXXXX", NULL); + tmp_path = g_build_filename (ephy_file_tmp_dir (), tmp_file, NULL); + + g_test_message ("TMP: %s", tmp_path); + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS) == FALSE); + g_assert (g_file_set_contents (tmp_path, "test", -1, NULL)); + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS)); + + tmp_uri = g_filename_to_uri (tmp_path, NULL, NULL); + ephy_file_delete_uri (tmp_uri); + + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS) == FALSE); + + g_free (tmp_uri); + g_free (tmp_path); + g_free (tmp_file); + + /* Creation with suffix. */ + tmp_file = ephy_file_tmp_filename ("test-ephy-XXXXXX", "test-ext"); + tmp_path = g_build_filename (ephy_file_tmp_dir (), tmp_file, NULL); + + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS) == FALSE); + g_assert (g_file_set_contents (tmp_path, "test", -1, NULL)); + g_assert (g_file_test (tmp_path, G_FILE_TEST_EXISTS)); + + tmp_path_prefix = g_build_filename (ephy_file_tmp_dir (), "test-ephy-", NULL); + + g_test_message ("TMP: %s", tmp_path); + g_assert (g_str_has_suffix (tmp_path, "test-ext")); + g_assert (g_str_has_prefix (tmp_path, tmp_path_prefix)); + + tmp_uri = g_filename_to_uri (tmp_path, NULL, NULL); + ephy_file_delete_uri (tmp_uri); + + g_assert (g_file_test (tmp_file, G_FILE_TEST_EXISTS) == FALSE); + + g_free (tmp_uri); + g_free (tmp_path_prefix); + g_free (tmp_path); + g_free (tmp_file); + + ephy_file_helpers_shutdown (); +} + +static void +test_ephy_file_switch_temp_file () +{ + char *tmp_file; + + GFile *orig; + char *orig_path; + + GFile *dest; + char *dest_path; + char *file_cont = NULL; + + ephy_file_helpers_init (NULL, TRUE, FALSE, NULL); + + /* Empty dest */ + tmp_file = ephy_file_tmp_filename ("test-dest-XXXXXX", NULL); + dest_path = g_build_filename (ephy_file_tmp_dir (), tmp_file, NULL); + g_free (tmp_file); + + g_assert (g_file_test (dest_path, G_FILE_TEST_EXISTS) == FALSE); + dest = g_file_new_for_path (dest_path); + + tmp_file = ephy_file_tmp_filename ("test-orig-XXXXXX", NULL); + orig_path = g_build_filename (ephy_file_tmp_dir (), tmp_file, NULL); + g_free (tmp_file); + + g_assert (g_file_test (orig_path, G_FILE_TEST_EXISTS) == FALSE); + orig = g_file_new_for_path (orig_path); + + g_file_set_contents (orig_path, "orig", -1, NULL); + g_assert (g_file_test (orig_path, G_FILE_TEST_EXISTS)); + + g_test_message ("SWITCH: %s to %s", orig_path, dest_path); + + g_assert (ephy_file_switch_temp_file (dest, orig)); + g_assert (g_file_test (orig_path, G_FILE_TEST_EXISTS) == FALSE); + g_assert (g_file_test (dest_path, G_FILE_TEST_EXISTS)); + + g_assert (g_file_get_contents (dest_path, &file_cont, NULL, NULL)); + g_assert_cmpstr ("orig", ==, file_cont); + g_free (file_cont); + + ephy_file_delete_uri (g_file_get_uri (dest)); + g_assert (g_file_test (dest_path, G_FILE_TEST_EXISTS) == FALSE); + + /* Full replace */ + g_file_set_contents (dest_path, "dest", -1, NULL); + g_assert (g_file_test (dest_path, G_FILE_TEST_EXISTS)); + + g_file_set_contents (orig_path, "orig", -1, NULL); + g_assert (g_file_test (orig_path, G_FILE_TEST_EXISTS)); + + g_test_message ("SWITCH REPLACE: %s to %s", orig_path, dest_path); + g_assert (ephy_file_switch_temp_file (dest, orig)); + g_assert (g_file_test (dest_path, G_FILE_TEST_EXISTS)); + g_assert (g_file_test (orig_path, G_FILE_TEST_EXISTS) == FALSE); + + g_assert (g_file_get_contents (dest_path, &file_cont, NULL, NULL)); + g_assert_cmpstr ("orig", ==, file_cont); + g_free (file_cont); + + g_free (orig_path); + g_free (dest_path); + + g_object_unref (orig); + g_object_unref (dest); + + ephy_file_helpers_shutdown (); +} + +int +main (int argc, char *argv[]) +{ + int ret; + + /* This should affect only this test, we use it so we can test for + * default directory changes. */ + g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); + /* Set our custom user-dirs.dirs, to control the output of + * g_get_user_special_dir. The values there are the ones we should + * check for in the test. */ + g_setenv ("XDG_CONFIG_HOME", TOP_SRC_DIR "/tests/data/", TRUE); + + gtk_test_init (&argc, &argv); + + ephy_debug_init (); + + g_test_add_func ("/lib/ephy-file-helpers/init", + test_ephy_file_helpers_init); + + g_test_add_func ("/lib/ephy-file-helpers/get_downloads_dir", + test_ephy_file_get_downloads_dir); + + g_test_add_func ("/lib/ephy-file-helpers/create_delete_dir", + test_ephy_file_create_delete_dir); + + g_test_add_func ("/lib/ephy-file-helpers/desktop_dir", + test_ephy_file_desktop_dir); + + g_test_add_func ("/lib/ephy-file-helpers/create_delete_tmp", + test_ephy_file_create_delete_tmp); + + g_test_add_func ("/lib/ephy-file-helpers/switch_temp_file", + test_ephy_file_switch_temp_file); + + ret = g_test_run (); + + return ret; +} |