gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gio / tests / vfs.c
1
2 /* Unit tests for GVfs
3  * Copyright (C) 2011 Red Hat, Inc
4  * Author: Matthias Clasen
5  *
6  * SPDX-License-Identifier: LicenseRef-old-glib-tests
7  *
8  * This work is provided "as is"; redistribution and modification
9  * in whole or in part, in any medium, physical or electronic is
10  * permitted without restriction.
11  *
12  * This work is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * In no event shall the authors or contributors be liable for any
17  * direct, indirect, incidental, special, exemplary, or consequential
18  * damages (including, but not limited to, procurement of substitute
19  * goods or services; loss of use, data, or profits; or business
20  * interruption) however caused and on any theory of liability, whether
21  * in contract, strict liability, or tort (including negligence or
22  * otherwise) arising in any way out of the use of this software, even
23  * if advised of the possibility of such damage.
24  */
25
26 #include <gio/gio.h>
27
28 static GFile *
29 test_vfs_parse_name (GVfs       *vfs,
30                      const char *parse_name,
31                      gpointer    user_data)
32 {
33   GFile *file = NULL;
34
35   if (g_strcmp0 ((parse_name), "testfile") == 0)
36     {
37       file = g_file_new_for_uri ("file:///");
38       g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
39     }
40
41   return file;
42 }
43
44 static GFile *
45 test_vfs_lookup (GVfs       *vfs,
46                  const char *uri,
47                  gpointer    user_data)
48 {
49   GFile *file;
50   file = g_file_new_for_uri ("file:///");
51   g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
52
53   return file;
54 }
55
56 static void
57 test_register_scheme (void)
58 {
59   GVfs *vfs;
60   GFile *file;
61   const gchar * const *schemes;
62   gboolean res;
63
64   vfs = g_vfs_get_default ();
65   g_assert_nonnull (vfs);
66   g_assert_true (g_vfs_is_active (vfs));
67
68   schemes = g_vfs_get_supported_uri_schemes (vfs);
69   g_assert_false (g_strv_contains (schemes, "test"));
70
71   res = g_vfs_unregister_uri_scheme (vfs, "test");
72   g_assert_false (res);
73
74   res = g_vfs_register_uri_scheme (vfs, "test",
75                                    test_vfs_lookup, NULL, NULL,
76                                    test_vfs_parse_name, NULL, NULL);
77   g_assert_true (res);
78
79   schemes = g_vfs_get_supported_uri_schemes (vfs);
80   g_assert_true (g_strv_contains (schemes, "test"));
81
82   file = g_file_new_for_uri ("test:///foo");
83   g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
84   g_object_unref (file);
85
86   file = g_file_parse_name ("testfile");
87   g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
88   g_object_unref (file);
89
90   res = g_vfs_register_uri_scheme (vfs, "test",
91                                    test_vfs_lookup, NULL, NULL,
92                                    test_vfs_parse_name, NULL, NULL);
93   g_assert_false (res);
94
95   res = g_vfs_unregister_uri_scheme (vfs, "test");
96   g_assert_true (res);
97
98   file = g_file_new_for_uri ("test:///foo");
99   g_assert_null (g_object_get_data (G_OBJECT (file), "testfile"));
100   g_object_unref (file);
101 }
102
103 static void
104 test_local (void)
105 {
106   GVfs *vfs;
107   GFile *file;
108   gchar **schemes;
109
110   vfs = g_vfs_get_local ();
111   g_assert_true (g_vfs_is_active (vfs));
112
113   file = g_vfs_get_file_for_uri (vfs, "not a good uri");
114   g_assert_true (G_IS_FILE (file));
115   g_object_unref (file);
116
117   schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs);
118
119   g_assert_cmpuint (g_strv_length (schemes), >, 0);
120   g_assert_cmpstr (schemes[0], ==, "file");
121 }
122
123 static void
124 test_resource_malformed_escaping (void)
125 {
126   GVfs *vfs;
127   GFile *file;
128
129   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3090");
130   g_test_summary ("Test that g_vfs_get_file_for_uri() returns an invalid file for an invalid URI");
131
132   vfs = g_vfs_get_local ();
133   g_assert_true (g_vfs_is_active (vfs));
134
135   file = g_vfs_get_file_for_uri (vfs, "resource:///%not-valid-escaping/gtk.css");
136   g_assert_true (G_IS_FILE (file));
137
138   /* This only returns %NULL if the file was constructed with an invalid URI: */
139   g_assert_null (g_file_get_uri_scheme (file));
140
141   g_object_unref (file);
142 }
143
144 int
145 main (int argc, char *argv[])
146 {
147   g_test_init (&argc, &argv, NULL);
148
149   g_test_add_func ("/gvfs/local", test_local);
150   g_test_add_func ("/gvfs/register-scheme", test_register_scheme);
151   g_test_add_func ("/gvfs/resource/malformed-escaping", test_resource_malformed_escaping);
152
153   return g_test_run ();
154 }