Blame gio/tests/g-file.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 * Copyright (C) 2008 Red Hat, Inc.
Packit ae235b
 * Authors: Tomas Bzatek <tbzatek@redhat.com>
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <glib/glib.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
struct TestPathsWithOper {
Packit ae235b
  const char *path1;
Packit ae235b
  gboolean equal;
Packit ae235b
  gboolean use_uri;
Packit ae235b
  const char *path2;
Packit ae235b
  const char *path3;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
/* TODO:
Packit ae235b
 *   - test on Windows
Packit ae235b
 * 
Packit ae235b
 **/
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_new_null (void)
Packit ae235b
{
Packit ae235b
  const char *paths[] = {"/",
Packit ae235b
			 "/tmp///",
Packit ae235b
			 "/non-existent-file",
Packit ae235b
			 "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
Packit ae235b
			 NULL
Packit ae235b
  };
Packit ae235b
  const char *uris[] = {"file:///",
Packit ae235b
			"file:///tmp///",
Packit ae235b
			"non-existent-uri:///some-dir/",
Packit ae235b
			"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88",
Packit ae235b
			NULL
Packit ae235b
  };
Packit ae235b
  
Packit ae235b
  GFile *file = NULL;
Packit ae235b
  
Packit ae235b
  int i = 0;
Packit ae235b
  while (paths[i])
Packit ae235b
    {
Packit ae235b
      file = g_file_new_for_path (paths[i++]);
Packit ae235b
      g_assert (file != NULL);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  i = 0;
Packit ae235b
  while (uris[i])
Packit ae235b
    {
Packit ae235b
      file = g_file_new_for_uri (uris[i++]);
Packit ae235b
      g_assert (file != NULL);
Packit ae235b
      g_object_unref(file);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
Packit ae235b
{
Packit ae235b
  GFile *file1 = NULL;
Packit ae235b
  GFile *file2 = NULL;
Packit ae235b
  gboolean equal;
Packit ae235b
Packit ae235b
  if (use_uri)
Packit ae235b
    {
Packit ae235b
      file1 = g_file_new_for_uri (path1);
Packit ae235b
      file2 = g_file_new_for_uri (path2);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      file1 = g_file_new_for_path (path1);
Packit ae235b
      file2 = g_file_new_for_path (path2);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert (file1 != NULL);
Packit ae235b
  g_assert (file2 != NULL);
Packit ae235b
  
Packit ae235b
  equal = g_file_equal (file1, file2);
Packit ae235b
  
Packit ae235b
  g_object_unref (file1);
Packit ae235b
  g_object_unref (file2);
Packit ae235b
  
Packit ae235b
  return equal;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_new_for_path (void)
Packit ae235b
{
Packit ae235b
  const struct TestPathsWithOper cmp_paths[] =
Packit ae235b
    {
Packit ae235b
      {"/", TRUE, 0, "/./"},
Packit ae235b
      {"//", TRUE, 0, "//"},
Packit ae235b
      {"//", TRUE, 0, "//./"},
Packit ae235b
      {"/", TRUE, 0, "/.//"},
Packit ae235b
      {"/", TRUE, 0, "/././"},
Packit ae235b
      {"/tmp", TRUE, 0, "/tmp/d/../"},
Packit ae235b
      {"/", TRUE, 0, "/somedir/../"},
Packit ae235b
      {"/", FALSE, 0, "/somedir/.../"},
Packit ae235b
      {"//tmp/dir1", TRUE, 0, "//tmp/dir1"},
Packit ae235b
      {"/tmp/dir1", TRUE, 0, "///tmp/dir1"},
Packit ae235b
      {"/tmp/dir1", TRUE, 0, "////tmp/dir1"},
Packit ae235b
      {"/tmp/dir1", TRUE, 0, "/tmp/./dir1"},
Packit ae235b
      {"/tmp/dir1", TRUE, 0, "/tmp//dir1"},
Packit ae235b
      {"/tmp/dir1", TRUE, 0, "/tmp///dir1///"},
Packit ae235b
      {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, 0, "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/"}
Packit ae235b
    };
Packit ae235b
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
Packit ae235b
    {
Packit ae235b
      gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
Packit ae235b
      g_assert_cmpint (equal, ==, cmp_paths[i].equal);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_new_for_uri (void)
Packit ae235b
{
Packit ae235b
  const struct TestPathsWithOper cmp_uris[] = {
Packit ae235b
    {"file:///", TRUE, 0, "file:///./"},
Packit ae235b
    {"file:////", TRUE, 0, "file:////"},
Packit ae235b
    {"file:////", TRUE, 0, "file:////./"},
Packit ae235b
    {"file:///", TRUE, 0, "file:///.//"},
Packit ae235b
    {"file:///", TRUE, 0, "file:///././"},
Packit ae235b
    {"file:///tmp", TRUE, 0, "file:///tmp/d/../"},
Packit ae235b
    {"file:///", TRUE, 0, "file:///somedir/../"},
Packit ae235b
    {"file:///", FALSE, 0, "file:///somedir/.../"},
Packit ae235b
    {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1"},
Packit ae235b
    {"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1"},
Packit ae235b
    {"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1"},
Packit ae235b
    {"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///"},
Packit ae235b
    {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, 0, "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/"}
Packit ae235b
  };
Packit ae235b
  
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
Packit ae235b
    {
Packit ae235b
      gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
Packit ae235b
      g_assert_cmpint (equal, ==, cmp_uris[i].equal);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
dup_equals (const gboolean use_uri, const char *path)
Packit ae235b
{
Packit ae235b
  GFile *file1 = NULL;
Packit ae235b
  GFile *file2 = NULL;
Packit ae235b
  gboolean equal;
Packit ae235b
  
Packit ae235b
  if (use_uri) 
Packit ae235b
    file1 = g_file_new_for_uri (path);
Packit ae235b
  else
Packit ae235b
    file1 = g_file_new_for_path (path);
Packit ae235b
	
Packit ae235b
  g_assert (file1 != NULL);
Packit ae235b
  
Packit ae235b
  file2 = g_file_dup (file1);
Packit ae235b
  
Packit ae235b
  g_assert (file2 != NULL);
Packit ae235b
  
Packit ae235b
  equal = g_file_equal (file1, file2);
Packit ae235b
  
Packit ae235b
  g_object_unref (file1);
Packit ae235b
  g_object_unref (file2);
Packit ae235b
	
Packit ae235b
  return equal;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_dup (void)
Packit ae235b
{
Packit ae235b
  const struct TestPathsWithOper dup_paths[] =
Packit ae235b
    {
Packit ae235b
      {"/", 0, FALSE, ""},
Packit ae235b
      {"file:///", 0, TRUE, ""},
Packit ae235b
      {"totalnonsense", 0, FALSE, ""},
Packit ae235b
      {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, ""},
Packit ae235b
      {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", 0, TRUE, ""},
Packit ae235b
    };
Packit ae235b
  
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
Packit ae235b
    {
Packit ae235b
      gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
Packit ae235b
      g_assert (equal == TRUE);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
Packit ae235b
{
Packit ae235b
  GFile *file1 = NULL;
Packit ae235b
  GFile *file2 = NULL;
Packit ae235b
  char *parsed_name;
Packit ae235b
  gboolean is_utf8_valid;
Packit ae235b
  gboolean equal;
Packit ae235b
  
Packit ae235b
  if (use_uri)
Packit ae235b
    file1 = g_file_new_for_uri (path);
Packit ae235b
  else
Packit ae235b
    file1 = g_file_new_for_path (path);
Packit ae235b
	
Packit ae235b
  g_assert (file1 != NULL);
Packit ae235b
Packit ae235b
  parsed_name = g_file_get_parse_name (file1);
Packit ae235b
  
Packit ae235b
  g_assert (parsed_name != NULL);
Packit ae235b
  
Packit ae235b
  /* UTF-8 validation */
Packit ae235b
  is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
Packit ae235b
  g_assert (is_utf8_valid == TRUE);
Packit ae235b
Packit ae235b
  if (result_parse_name)
Packit ae235b
    g_assert_cmpstr (parsed_name, ==, result_parse_name);
Packit ae235b
  
Packit ae235b
  file2 = g_file_parse_name (parsed_name);
Packit ae235b
  
Packit ae235b
  g_assert (file2 != NULL);
Packit ae235b
Packit ae235b
  equal = g_file_equal (file1, file2);
Packit ae235b
	
Packit ae235b
  g_object_unref (file1);
Packit ae235b
  g_object_unref (file2);
Packit ae235b
  
Packit ae235b
  g_free (parsed_name);
Packit ae235b
  
Packit ae235b
  return equal;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_get_parse_name_utf8 (void)
Packit ae235b
{
Packit ae235b
  const struct TestPathsWithOper strings[] =
Packit ae235b
    {
Packit ae235b
      {G_DIR_SEPARATOR_S, 0, FALSE, G_DIR_SEPARATOR_S},
Packit ae235b
      {"file:///", 0, TRUE, G_DIR_SEPARATOR_S},
Packit ae235b
      {"totalnonsense", 0, FALSE, NULL},
Packit ae235b
      {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, NULL /* Depends on local file encoding */},
Packit ae235b
      {"file:///invalid%08/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, TRUE, "file:///invalid%08/UTF-8%20p\xc5\x99\xc3\xadli\xc5\xa1%20\xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd%20k\xc5\xaf\xc5\x88"},
Packit ae235b
    };
Packit ae235b
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (strings); i++)
Packit ae235b
    {
Packit ae235b
      gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
Packit ae235b
      g_assert (equal == TRUE);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
resolve_arg (const gboolean is_uri_only, const char *arg)
Packit ae235b
{
Packit ae235b
  GFile *file1 = NULL;
Packit ae235b
  char *uri = NULL;
Packit ae235b
  char *path = NULL;
Packit ae235b
  char *s = NULL;
Packit ae235b
  
Packit ae235b
  file1 = g_file_new_for_commandline_arg (arg);
Packit ae235b
  g_assert (file1 != NULL);
Packit ae235b
	
Packit ae235b
  /*  Test if we get URI string */
Packit ae235b
  uri = g_file_get_uri (file1);
Packit ae235b
  g_assert_cmpstr (uri, !=, NULL);
Packit ae235b
  g_printerr ("%s\n",uri);
Packit ae235b
	
Packit ae235b
  /*  Test if we get correct value of the local path */
Packit ae235b
  path = g_file_get_path (file1);
Packit ae235b
  if (is_uri_only) 
Packit ae235b
    g_assert_cmpstr (path, ==, NULL);
Packit ae235b
  else
Packit ae235b
    g_assert (g_path_is_absolute (path) == TRUE);
Packit ae235b
Packit ae235b
  /*  Get the URI scheme and compare it with expected one */
Packit ae235b
  s = g_file_get_uri_scheme (file1);
Packit ae235b
	
Packit ae235b
  g_object_unref (file1);
Packit ae235b
  g_free (uri);
Packit ae235b
  g_free (path);
Packit ae235b
  
Packit ae235b
  return s;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_new_for_commandline_arg (void)
Packit ae235b
{
Packit ae235b
  /*  TestPathsWithOper.use_uri represents IsURIOnly here */
Packit ae235b
  const struct TestPathsWithOper arg_data[] =
Packit ae235b
    {
Packit ae235b
      {"./", 0, FALSE, "file"},
Packit ae235b
      {"../", 0, FALSE, "file"},
Packit ae235b
      {"/tmp", 0, FALSE, "file"},
Packit ae235b
      {"//UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, "file"},
Packit ae235b
      {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, FALSE, "file"},
Packit ae235b
#if 0
Packit ae235b
      {"http://www.gtk.org/", 0, TRUE, "http"},
Packit ae235b
      {"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp"},
Packit ae235b
#endif
Packit ae235b
    };
Packit ae235b
  GFile *file;
Packit ae235b
  char *resolved;
Packit ae235b
  char *cwd;
Packit ae235b
  guint i;
Packit ae235b
  
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
Packit ae235b
    {
Packit ae235b
      char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
Packit ae235b
      g_assert_cmpstr (s, ==, arg_data[i].path2);
Packit ae235b
      g_free (s);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* Manual test for getting correct cwd */
Packit ae235b
  file = g_file_new_for_commandline_arg ("./");
Packit ae235b
  resolved = g_file_get_path (file);
Packit ae235b
  cwd = g_get_current_dir ();
Packit ae235b
  g_assert_cmpstr (resolved, ==, cwd);
Packit ae235b
  g_object_unref (file);
Packit ae235b
  g_free (resolved);
Packit ae235b
  g_free (cwd);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char*
Packit ae235b
get_relative_path (const gboolean use_uri, const gboolean should_have_prefix, const char *dir1, const char *dir2)
Packit ae235b
{
Packit ae235b
  GFile *file1 = NULL;
Packit ae235b
  GFile *file2 = NULL;
Packit ae235b
  GFile *file3 = NULL;
Packit ae235b
  gboolean has_prefix = FALSE;
Packit ae235b
  char *relative_path = NULL;
Packit ae235b
  
Packit ae235b
  if (use_uri)
Packit ae235b
    {
Packit ae235b
      file1 = g_file_new_for_uri (dir1);
Packit ae235b
      file2 = g_file_new_for_uri (dir2);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      file1 = g_file_new_for_path (dir1);
Packit ae235b
      file2 = g_file_new_for_path (dir2);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_assert (file1 != NULL);
Packit ae235b
  g_assert (file2 != NULL);
Packit ae235b
  
Packit ae235b
  has_prefix = g_file_has_prefix (file2, file1);
Packit ae235b
  g_printerr ("%s %s\n", dir1, dir2);
Packit ae235b
  g_assert (has_prefix == should_have_prefix);
Packit ae235b
Packit ae235b
  relative_path = g_file_get_relative_path (file1, file2);
Packit ae235b
  if (should_have_prefix)
Packit ae235b
    {
Packit ae235b
      g_assert (relative_path != NULL);
Packit ae235b
      
Packit ae235b
      file3 = g_file_resolve_relative_path (file1, relative_path);
Packit ae235b
      g_assert (g_file_equal (file2, file3) == TRUE);
Packit ae235b
    }
Packit ae235b
	
Packit ae235b
  if (file1)
Packit ae235b
    g_object_unref (file1);
Packit ae235b
  if (file2)
Packit ae235b
    g_object_unref (file2);
Packit ae235b
  if (file3)
Packit ae235b
    g_object_unref (file3);
Packit ae235b
Packit ae235b
  return relative_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_has_prefix (void)
Packit ae235b
{
Packit ae235b
  /*  TestPathsWithOper.equal represents here if the dir belongs to the directory structure  */
Packit ae235b
  const struct TestPathsWithOper dirs[] =
Packit ae235b
    {
Packit ae235b
      /* path1            equal  uri     path2    path3  */
Packit ae235b
      {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"/tmp/", FALSE, FALSE, "/something/", NULL},
Packit ae235b
      {"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
Packit ae235b
      {"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, FALSE, "/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/dir2", "dir2"},
Packit ae235b
      {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
Packit ae235b
      {"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
Packit ae235b
      {"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
Packit ae235b
      {"file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, TRUE, "file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/dir2", "dir2"},
Packit ae235b
#if 0
Packit ae235b
      {"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
Packit ae235b
      {"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
Packit ae235b
#endif
Packit ae235b
    };
Packit ae235b
  
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (dirs); i++)
Packit ae235b
    {
Packit ae235b
      char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
Packit ae235b
      if (dirs[i].equal) 
Packit ae235b
	g_assert_cmpstr (s, ==, dirs[i].path3);
Packit ae235b
      g_free (s);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
Packit ae235b
			const char *path, const char *dir_holder)
Packit ae235b
{
Packit ae235b
  GFile *files[6] = {NULL};
Packit ae235b
  guint i;
Packit ae235b
  
Packit ae235b
  if (use_uri)
Packit ae235b
    {
Packit ae235b
      files[0] = g_file_new_for_uri (path);
Packit ae235b
      files[1] = g_file_new_for_uri (path);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      files[0] = g_file_new_for_path (path);
Packit ae235b
      files[1] = g_file_new_for_path (path);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert (files[0] != NULL);
Packit ae235b
  g_assert (files[1] != NULL);
Packit ae235b
  
Packit ae235b
  files[2] = g_file_get_child (files[1], dir_holder); 
Packit ae235b
  g_assert (files[2] != NULL);
Packit ae235b
  
Packit ae235b
  files[3] = g_file_get_parent (files[2]);
Packit ae235b
  g_assert (files[3] != NULL);
Packit ae235b
  g_assert (g_file_equal (files[3], files[0]) == TRUE);
Packit ae235b
  
Packit ae235b
  files[4] = g_file_get_parent (files[3]);
Packit ae235b
  /*  Don't go lower beyond the root */
Packit ae235b
  if (under_root_descending) 
Packit ae235b
    g_assert (files[4] == NULL);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert (files[4] != NULL);
Packit ae235b
      
Packit ae235b
      files[5] = g_file_get_child (files[4], dir_holder); 
Packit ae235b
      g_assert (files[5] != NULL);
Packit ae235b
      g_assert (g_file_equal (files[5], files[0]) == TRUE);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (files); i++)
Packit ae235b
    {
Packit ae235b
      if (files[i])
Packit ae235b
	g_object_unref (files[i]);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_g_file_get_parent_child (void)
Packit ae235b
{
Packit ae235b
  const struct TestPathsWithOper paths[] =
Packit ae235b
    {
Packit ae235b
      /* path     root_desc   uri  dir_holder */
Packit ae235b
      {"/dir1/dir", FALSE, FALSE, "dir"},
Packit ae235b
      {"/dir", FALSE, FALSE, "dir"},
Packit ae235b
      {"/", TRUE, FALSE, "dir"},
Packit ae235b
      {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/", FALSE, FALSE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
Packit ae235b
      {"file:///dir1/dir", FALSE, TRUE, "dir"},
Packit ae235b
      {"file:///dir", FALSE, TRUE, "dir"},
Packit ae235b
      {"file:///", TRUE, TRUE, "dir"},
Packit ae235b
      {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", FALSE, TRUE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"},
Packit ae235b
      {"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings"},
Packit ae235b
    };
Packit ae235b
Packit ae235b
  guint i;
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (paths); i++)
Packit ae235b
    roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
  
Packit ae235b
  
Packit ae235b
  /*  Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result  */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
Packit ae235b
  
Packit ae235b
  /*  Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
Packit ae235b
Packit ae235b
  /*  Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
Packit ae235b
Packit ae235b
  /*  Testing g_file_dup() equals original file via g_file_equal()  */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
Packit ae235b
Packit ae235b
  /*  Testing g_file_get_parse_name() to return correct UTF-8 string    */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
Packit ae235b
  
Packit ae235b
  /*  Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess   */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
Packit ae235b
  
Packit ae235b
  /*  Testing g_file_has_prefix(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths         */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_has_prefix", test_g_file_has_prefix);
Packit ae235b
  
Packit ae235b
  /*  Testing g_file_get_parent() and g_file_get_child()            */
Packit ae235b
  g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);
Packit ae235b
  
Packit ae235b
  return g_test_run();
Packit ae235b
}