87b2a6291dc6857d7eebdff2304b8e6257f34c51
[platform/upstream/glib.git] / tests / uri-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 typedef struct
36 {
37   char *filename;
38   char *hostname;
39   char *expected_result;
40   GConvertError expected_error; /* If failed */
41 }  ToUriTest;
42
43 ToUriTest
44 to_uri_tests[] = {
45   { "/etc", NULL, "file:///etc"},
46   { "/etc", "", "file:///etc"},
47   { "/etc", "localhost", "file://localhost/etc"},
48 #ifdef G_OS_WIN32
49   { "c:\\windows", NULL, "file:///c:\\windows"},
50   { "c:\\windows", "localhost", "file://localhost/c:\\windows"},
51 #endif
52   { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
53   { "/etc/öäå", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
54   { "/etc/öäå", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
55   { "/etc", "öäå", "file://%C3%B6%C3%A4%C3%A5/etc"},
56   { "/etc", "åäö", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
57   { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
58 };
59
60
61 typedef struct
62 {
63   char *uri;
64   char *expected_filename;
65   char *expected_hostname;
66   GConvertError expected_error; /* If failed */
67 }  FromUriTest;
68
69 FromUriTest
70 from_uri_tests[] = {
71   { "file:///etc", "/etc", NULL},
72   { "file:/etc", "/etc", NULL},
73   { "file://localhost/etc", "/etc", "localhost", },
74   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost", },
75   { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "öäå", },
76   { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
77   { "file://localhost/åäö", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
78   { "file://åäö/etc", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
79   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
80   { "file://some", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
81 };
82
83
84 static gboolean any_failed = FALSE;
85
86 static void
87 run_to_uri_tests (void)
88 {
89   int i;
90   gchar *res;
91   GError *error;
92   
93   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
94     {
95       error = NULL;
96       res = g_filename_to_uri (to_uri_tests[i].filename,
97                                to_uri_tests[i].hostname,
98                                &error);
99
100       if (to_uri_tests[i].expected_result == NULL)
101         {
102           if (res != NULL)
103             {
104               g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
105               any_failed = TRUE;
106             }
107           else
108             {
109               if (error == NULL)
110                 {
111                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
112                   any_failed = TRUE;
113                 }
114               else if (error->domain != G_CONVERT_ERROR)
115                 {
116                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
117                   any_failed = TRUE;
118                 }
119               else if (error->code != to_uri_tests[i].expected_error)
120                 {
121                   g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
122                            i, error->code, to_uri_tests[i].expected_error);
123                   any_failed = TRUE;
124                 }
125             }
126         }
127       else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
128         {
129           g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
130                    i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
131           if (error)
132             g_print ("Error message: %s\n", error->message);
133           any_failed = TRUE;
134         }
135       
136       /* Give some output */
137       g_print (".");
138     }
139 }
140
141 static void
142 run_from_uri_tests (void)
143 {
144   int i;
145   gchar *res;
146   gchar *hostname;
147   GError *error;
148   
149   for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
150     {
151       error = NULL;
152       res = g_filename_from_uri (from_uri_tests[i].uri,
153                                  &hostname,
154                                  &error);
155
156       if (from_uri_tests[i].expected_filename == NULL)
157         {
158           if (res != NULL)
159             {
160               g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
161               any_failed = TRUE;
162             }
163           else
164             {
165               if (error == NULL)
166                 {
167                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
168                   any_failed = TRUE;
169                 }
170               else if (error->domain != G_CONVERT_ERROR)
171                 {
172                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
173                   any_failed = TRUE;
174                 }
175               else if (error->code != from_uri_tests[i].expected_error)
176                 {
177                   g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
178                            i, error->code, from_uri_tests[i].expected_error);
179                   any_failed = TRUE;
180                 }
181             }
182         }
183       else
184         {
185           if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
186             {
187               g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
188                        i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
189               any_failed = TRUE;
190             }
191
192           if (from_uri_tests[i].expected_hostname == NULL)
193             {
194               if (hostname != NULL)
195                 {
196                   g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
197                            i, hostname);
198                   any_failed = TRUE;
199                 }
200             }
201           else if (hostname == NULL ||
202                    strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
203             {
204               g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
205                        i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
206               any_failed = TRUE;
207             }
208         }
209       
210       /* Give some output */
211       g_print (".");
212     }
213   g_print ("\n");
214 }
215
216 int
217 main (int   argc,
218       char *argv[])
219 {
220   run_to_uri_tests ();
221   run_from_uri_tests ();
222
223   return any_failed ? 1 : 0;
224 }