File: vfs.c

package info (click to toggle)
glib2.0 2.84.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 66,144 kB
  • sloc: ansic: 538,877; python: 9,624; sh: 1,572; xml: 1,482; perl: 1,222; cpp: 535; makefile: 316; javascript: 11
file content (154 lines) | stat: -rw-r--r-- 4,492 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

/* Unit tests for GVfs
 * Copyright (C) 2011 Red Hat, Inc
 * Author: Matthias Clasen
 *
 * SPDX-License-Identifier: LicenseRef-old-glib-tests
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <gio/gio.h>

static GFile *
test_vfs_parse_name (GVfs       *vfs,
                     const char *parse_name,
                     gpointer    user_data)
{
  GFile *file = NULL;

  if (g_strcmp0 ((parse_name), "testfile") == 0)
    {
      file = g_file_new_for_uri ("file:///");
      g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
    }

  return file;
}

static GFile *
test_vfs_lookup (GVfs       *vfs,
                 const char *uri,
                 gpointer    user_data)
{
  GFile *file;
  file = g_file_new_for_uri ("file:///");
  g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));

  return file;
}

static void
test_register_scheme (void)
{
  GVfs *vfs;
  GFile *file;
  const gchar * const *schemes;
  gboolean res;

  vfs = g_vfs_get_default ();
  g_assert_nonnull (vfs);
  g_assert_true (g_vfs_is_active (vfs));

  schemes = g_vfs_get_supported_uri_schemes (vfs);
  g_assert_false (g_strv_contains (schemes, "test"));

  res = g_vfs_unregister_uri_scheme (vfs, "test");
  g_assert_false (res);

  res = g_vfs_register_uri_scheme (vfs, "test",
                                   test_vfs_lookup, NULL, NULL,
                                   test_vfs_parse_name, NULL, NULL);
  g_assert_true (res);

  schemes = g_vfs_get_supported_uri_schemes (vfs);
  g_assert_true (g_strv_contains (schemes, "test"));

  file = g_file_new_for_uri ("test:///foo");
  g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
  g_object_unref (file);

  file = g_file_parse_name ("testfile");
  g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
  g_object_unref (file);

  res = g_vfs_register_uri_scheme (vfs, "test",
                                   test_vfs_lookup, NULL, NULL,
                                   test_vfs_parse_name, NULL, NULL);
  g_assert_false (res);

  res = g_vfs_unregister_uri_scheme (vfs, "test");
  g_assert_true (res);

  file = g_file_new_for_uri ("test:///foo");
  g_assert_null (g_object_get_data (G_OBJECT (file), "testfile"));
  g_object_unref (file);
}

static void
test_local (void)
{
  GVfs *vfs;
  GFile *file;
  gchar **schemes;

  vfs = g_vfs_get_local ();
  g_assert_true (g_vfs_is_active (vfs));

  file = g_vfs_get_file_for_uri (vfs, "not a good uri");
  g_assert_true (G_IS_FILE (file));
  g_object_unref (file);

  schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs);

  g_assert_cmpuint (g_strv_length (schemes), >, 0);
  g_assert_cmpstr (schemes[0], ==, "file");
}

static void
test_resource_malformed_escaping (void)
{
  GVfs *vfs;
  GFile *file;

  g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3090");
  g_test_summary ("Test that g_vfs_get_file_for_uri() returns an invalid file for an invalid URI");

  vfs = g_vfs_get_local ();
  g_assert_true (g_vfs_is_active (vfs));

  file = g_vfs_get_file_for_uri (vfs, "resource:///%not-valid-escaping/gtk.css");
  g_assert_true (G_IS_FILE (file));

  /* This only returns %NULL if the file was constructed with an invalid URI: */
  g_assert_null (g_file_get_uri_scheme (file));

  g_object_unref (file);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/gvfs/local", test_local);
  g_test_add_func ("/gvfs/register-scheme", test_register_scheme);
  g_test_add_func ("/gvfs/resource/malformed-escaping", test_resource_malformed_escaping);

  return g_test_run ();
}