source: trunk/third/gnome-vfs/test/test-callback.c @ 17128

Revision 17128, 8.7 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17127, which included commits to RCS files with non-trunk default branches.
Line 
1#include <stdio.h>
2#include <signal.h>
3#include <gnome.h>
4#include <gnome-vfs.h>
5#include <gnome-vfs-standard-callbacks.h>
6#include <gnome-vfs-module-callback.h>
7#include <gmodule.h>
8
9static gboolean authentication_callback_called = FALSE;
10
11/* For this test case to function, these two URI's should
12 * require a username/password (set in authentication_username, authentication_password below)
13 * and AUTHENTICATION_URI_CHILD should be a child of AUTHENTICATION_URI
14 */
15#define AUTHENTICATION_URI_CHILD "http://anarchy.noisehavoc.org/poweredby.png"
16#define AUTHENTICATION_URI "http://anarchy.noisehavoc.org/"
17
18static const char *authentication_username = "mjs";
19static const char *authentication_password = "foobar42";
20
21static void /* GnomeVFSModuleCallback */
22authentication_callback (gconstpointer in, size_t in_size, gpointer out, size_t out_size, gpointer user_data)
23{
24        GnomeVFSModuleCallbackAuthenticationIn *in_real;
25        GnomeVFSModuleCallbackAuthenticationOut *out_real;
26
27        /* printf ("in authentication_callback\n"); */
28       
29        g_return_if_fail (sizeof (GnomeVFSModuleCallbackAuthenticationIn) == in_size
30                && sizeof (GnomeVFSModuleCallbackAuthenticationOut) == out_size);
31
32        g_return_if_fail (in != NULL);
33        g_return_if_fail (out != NULL);
34
35        in_real = (GnomeVFSModuleCallbackAuthenticationIn *)in;
36        out_real = (GnomeVFSModuleCallbackAuthenticationOut *)out;
37
38        /* printf ("in uri: %s realm: %s\n", in_real->uri, in_real->realm); */
39       
40        out_real->username = g_strdup (authentication_username);
41        out_real->password = g_strdup (authentication_password);
42
43        authentication_callback_called = TRUE;
44}
45
46static gboolean destroy_notify_occurred = FALSE;
47
48static void /*GDestroyNotify*/
49destroy_notify (gpointer user_data)
50{
51        destroy_notify_occurred = TRUE;
52}
53
54static volatile gboolean open_callback_occurred = FALSE;
55
56static GnomeVFSResult open_callback_result_expected = GNOME_VFS_OK;
57
58static void /* GnomeVFSAsyncOpenCallback */
59open_callback (GnomeVFSAsyncHandle *handle,
60               GnomeVFSResult result,
61               gpointer callback_data)
62{
63        g_assert (result == open_callback_result_expected);
64
65        open_callback_occurred = TRUE;
66}
67
68static volatile gboolean close_callback_occurred = FALSE;
69
70static void /* GnomeVFSAsyncOpenCallback */
71close_callback (GnomeVFSAsyncHandle *handle,
72                GnomeVFSResult result,
73                gpointer callback_data)
74{
75        close_callback_occurred = TRUE;
76}
77
78static void
79stop_after_log (const char *domain, GLogLevelFlags level,
80        const char *message, gpointer data)
81{
82        void (* saved_handler) (int);
83       
84        g_log_default_handler (domain, level, message, data);
85
86        saved_handler = signal (SIGINT, SIG_IGN);
87        raise (SIGINT);
88        signal (SIGINT, saved_handler);
89}
90
91static void
92make_asserts_break (const char *domain)
93{
94        g_log_set_handler
95                (domain,
96                 (GLogLevelFlags) (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING),
97                 stop_after_log, NULL);
98}
99
100static void (*flush_credentials_func)(void);
101
102int
103main (int argc, char **argv)
104{
105        GnomeVFSHandle *handle;
106        GnomeVFSResult result;
107        GnomeVFSAsyncHandle *async_handle;
108        char *module_path;
109        char *authentication_uri, *authentication_uri_child;
110        GModule *module;
111        guint i;
112
113        gnome_init ("test-callback", "0.1", argc, argv);
114
115        make_asserts_break ("GLib");
116        make_asserts_break ("GnomeVFS");
117
118       /* make the stupid "SaveYourself" warning not come up */
119        gnome_client_disconnect (gnome_master_client ());
120
121        if (argc == 2) {
122                authentication_uri = argv[1];
123                authentication_uri_child = g_strdup_printf("%s/./", authentication_uri);
124        } else if (argc == 3) {
125                authentication_uri = argv[1];
126                authentication_uri_child = argv[2];
127        } else {
128                authentication_uri = AUTHENTICATION_URI;
129                authentication_uri_child = AUTHENTICATION_URI_CHILD;
130        }
131
132        gnome_vfs_init ();
133
134        /* Load http module so we can snag the test hook */
135        module_path = g_module_build_path (MODULES_PATH, "http");
136        module = g_module_open (module_path, G_MODULE_BIND_LAZY);
137        g_free (module_path);
138        module_path = NULL;
139
140        if (module == NULL) {
141                fprintf (stderr, "Couldn't load http module \n");
142                exit (-1);
143        }
144
145        g_module_symbol (module, "http_authentication_test_flush_credentials", (gpointer *) &flush_credentials_func);
146
147        if (flush_credentials_func == NULL) {
148                fprintf (stderr, "Couldn't find http_authentication_test_flush_credentials\n");
149                exit (-1);
150        }
151
152        /* Test 1: Attempt to access a URI requiring authentication w/o a callback registered */
153
154        result = gnome_vfs_open (&handle, authentication_uri, GNOME_VFS_OPEN_READ);
155        g_assert (result == GNOME_VFS_ERROR_ACCESS_DENIED);
156        handle = NULL;
157
158        /* Test 2: Attempt an async open that requires http authentication */
159
160        gnome_vfs_module_callback_set_default (GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
161                                               authentication_callback,
162                                               NULL,
163                                               NULL);
164
165        authentication_callback_called = FALSE;
166       
167        open_callback_occurred = FALSE;
168        open_callback_result_expected = GNOME_VFS_OK;
169
170        gnome_vfs_async_open (
171                &async_handle,
172                authentication_uri,
173                GNOME_VFS_OPEN_READ,
174                open_callback,
175                NULL);
176
177        while (!open_callback_occurred) {
178                gtk_main_iteration_do (TRUE);
179        }
180
181        close_callback_occurred = FALSE;
182        gnome_vfs_async_close (async_handle, close_callback, NULL);
183
184        while (!close_callback_occurred) {
185                gtk_main_iteration_do (TRUE);
186        }
187
188        g_assert (authentication_callback_called);
189
190        /* Test 3: Attempt a sync call to the same location;
191         * credentials should be stored so the authentication_callback function
192         * should not be called
193         */
194       
195        authentication_callback_called = FALSE;
196        result = gnome_vfs_open (&handle, authentication_uri, GNOME_VFS_OPEN_READ);
197        g_assert (result == GNOME_VFS_OK);
198        gnome_vfs_close (handle);
199        handle = NULL;
200        /* The credentials should be in the cache, so we shouldn't have been called */
201        g_assert (authentication_callback_called == FALSE);
202
203        /* Test 4: Attempt a sync call to something deeper in the namespace.
204         * which should work without a callback too
205         */
206
207        authentication_callback_called = FALSE;
208        result = gnome_vfs_open (&handle, authentication_uri_child, GNOME_VFS_OPEN_READ);
209        g_assert (result == GNOME_VFS_OK);
210        gnome_vfs_close (handle);
211        handle = NULL;
212        /* The credentials should be in the cache, so we shouldn't have been called */
213        g_assert (authentication_callback_called == FALSE);
214
215        /* Test 5: clear the credential store and try again in reverse order */
216
217        flush_credentials_func();
218
219        authentication_callback_called = FALSE;
220        result = gnome_vfs_open (&handle, authentication_uri_child, GNOME_VFS_OPEN_READ);
221        g_assert (result == GNOME_VFS_OK);
222        gnome_vfs_close (handle);
223        handle = NULL;
224        g_assert (authentication_callback_called == TRUE);
225
226        /* Test 6: Try something higher in the namespace, which should
227         * cause the callback to happen again
228         */
229
230        authentication_callback_called = FALSE;
231        result = gnome_vfs_open (&handle, authentication_uri, GNOME_VFS_OPEN_READ);
232        g_assert (result == GNOME_VFS_OK);
233        gnome_vfs_close (handle);
234        handle = NULL;
235        g_assert (authentication_callback_called == TRUE);
236
237        /* Test 7: Try same URL as in test 4, make sure callback doesn't get called */
238
239        authentication_callback_called = FALSE;
240        result = gnome_vfs_open (&handle, authentication_uri_child, GNOME_VFS_OPEN_READ);
241        g_assert (result == GNOME_VFS_OK);
242        gnome_vfs_close (handle);
243        handle = NULL;
244        g_assert (authentication_callback_called == FALSE);
245
246        /* Test 8: clear the credential store ensure that passing a username as NULL
247         * cancels the operation, resulting in a ACCESS_DENIED error */
248
249        flush_credentials_func();
250
251        authentication_username = NULL;
252
253        authentication_callback_called = FALSE;
254        result = gnome_vfs_open (&handle, authentication_uri_child, GNOME_VFS_OPEN_READ);
255        g_assert (result == GNOME_VFS_ERROR_ACCESS_DENIED);
256        handle = NULL;
257        g_assert (authentication_callback_called == TRUE);
258
259
260        /* Test 9: exercise the "destroy notify" functionality */
261        /* Note that job doesn't end until a "close" is called, so the inherited
262         * callback isn't released until then
263         */
264
265        flush_credentials_func();
266        authentication_username = "foo";
267
268        gnome_vfs_module_callback_push (GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
269                                        authentication_callback,
270                                        NULL,
271                                        destroy_notify);
272
273        authentication_callback_called = FALSE;
274       
275        open_callback_occurred = FALSE;
276        open_callback_result_expected = GNOME_VFS_OK;
277
278        destroy_notify_occurred = FALSE;
279
280        gnome_vfs_async_open (
281                &async_handle,
282                authentication_uri,
283                GNOME_VFS_OPEN_READ,
284                open_callback,
285                NULL);
286
287        gnome_vfs_module_callback_pop (GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION);
288
289        g_assert (!destroy_notify_occurred);
290
291        while (!open_callback_occurred) {
292                gtk_main_iteration_do (TRUE);
293        }
294
295        close_callback_occurred = FALSE;
296        gnome_vfs_async_close (async_handle, close_callback, NULL);
297
298        while (!close_callback_occurred) {
299                gtk_main_iteration_do (TRUE);
300        }
301
302        for (i = 0 ; i<100 ; i++) {
303                gtk_main_iteration_do (FALSE);
304                usleep (10);
305        }
306
307        g_assert (authentication_callback_called);
308        g_assert (destroy_notify_occurred);
309
310        gnome_vfs_shutdown ();
311
312        return 0;
313}
Note: See TracBrowser for help on using the repository browser.