GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-gnc-uri-utils.c
1 /***************************************************************************
2  * test-gnc-uri-utils.c
3  *
4  * Thu Sep 29 22:48:57 2005
5  * Copyright 2005 GnuCash team
6  ****************************************************************************/
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  */
23 
24 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <glib.h>
29 #include "qof.h"
30 #include "test-stuff.h"
31 #include "gnc-uri-utils.h"
32 
34 {
35  gchar *uri;
36  gboolean want_password;
37  gchar *protocol;
38  gchar *hostname;
39  gchar *username;
40  gchar *password;
41  gchar *path;
42  gint32 port;
43  gchar *created_uri;
44  gchar *normalized_uri;
45  gboolean is_file_protocol;
46 };
47 
48 typedef struct test_strings_struct test_strings;
49 
50 test_strings strs[] =
51 {
52 #ifndef G_OS_WIN32
53  /* basic file tests in posix like environment */
54  {
55  "/test/path/file.gnucash", FALSE,
56  "file", NULL, NULL, NULL, "/test/path/file.gnucash", 0,
57  "file:///test/path/file.gnucash",
58  "file:///test/path/file.gnucash", TRUE
59  },
60  {
61  "file:///test/path/file.gnucash", FALSE,
62  "file", NULL, NULL, NULL, "/test/path/file.gnucash", 0,
63  "file:///test/path/file.gnucash",
64  "file:///test/path/file.gnucash", TRUE
65  },
66  {
67  "xml:///test/path/file.gnucash", FALSE,
68  "xml", NULL, NULL, NULL, "/test/path/file.gnucash", 0,
69  "xml:///test/path/file.gnucash",
70  "xml:///test/path/file.gnucash", TRUE
71  },
72  {
73  "sqlite3:///test/path/file.gnucash", FALSE,
74  "sqlite3", NULL, NULL, NULL, "/test/path/file.gnucash", 0,
75  "sqlite3:///test/path/file.gnucash",
76  "sqlite3:///test/path/file.gnucash", TRUE
77  },
78 #else
79  /* basic file tests in windows environment */
80  {
81  "c:\\test\\path\\file.gnucash", FALSE,
82  "file", NULL, NULL, NULL, "c:\\test\\path\\file.gnucash", 0,
83  "file://c:\\test\\path\\file.gnucash",
84  "file://c:\\test\\path\\file.gnucash", TRUE
85  },
86  {
87  "file://c:\\test\\path\\file.gnucash", FALSE,
88  "file", NULL, NULL, NULL, "c:\\test\\path\\file.gnucash", 0,
89  "file://c:\\test\\path\\file.gnucash",
90  "file://c:\\test\\path\\file.gnucash", TRUE
91  },
92  {
93  "xml://c:\\test\\path\\file.gnucash", FALSE,
94  "xml", NULL, NULL, NULL, "c:\\test\\path\\file.gnucash", 0,
95  "xml://c:\\test\\path\\file.gnucash",
96  "xml://c:\\test\\path\\file.gnucash", TRUE
97  },
98  {
99  "sqlite3://c:\\test\\path\\file.gnucash", FALSE,
100  "sqlite3", NULL, NULL, NULL, "c:\\test\\path\\file.gnucash", 0,
101  "sqlite3://c:\\test\\path\\file.gnucash",
102  "sqlite3://c:\\test\\path\\file.gnucash", TRUE
103  },
104 #endif
105  /* basic database tests */
106  {
107  "mysql://www.gnucash.org/gnucash", FALSE,
108  "mysql", "www.gnucash.org", NULL, NULL, "gnucash", 0,
109  "mysql://www.gnucash.org/gnucash",
110  "mysql://www.gnucash.org/gnucash", FALSE
111  },
112  {
113  "mysql://www.gnucash.org/gnucash", TRUE,
114  "mysql", "www.gnucash.org", NULL, NULL, "gnucash", 0,
115  "mysql://www.gnucash.org/gnucash",
116  "mysql://www.gnucash.org/gnucash", FALSE
117  },
118  {
119  "mysql://[email protected]/gnucash", FALSE,
120  "mysql", "www.gnucash.org", "dbuser", NULL, "gnucash", 0,
121  "mysql://[email protected]/gnucash",
122  "mysql://[email protected]/gnucash", FALSE
123  },
124  {
125  "mysql://[email protected]/gnucash", TRUE,
126  "mysql", "www.gnucash.org", "dbuser", NULL, "gnucash", 0,
127  "mysql://[email protected]/gnucash",
128  "mysql://[email protected]/gnucash", FALSE
129  },
130  {
131  "mysql://dbuser:[email protected]/gnucash", FALSE,
132  "mysql", "www.gnucash.org", "dbuser", "dbpass", "gnucash", 0,
133  "mysql://dbuser:[email protected]/gnucash",
134  "mysql://[email protected]/gnucash", FALSE
135  },
136  {
137  "mysql://dbuser:[email protected]/gnucash", TRUE,
138  "mysql", "www.gnucash.org", "dbuser", "dbpass", "gnucash", 0,
139  "mysql://dbuser:[email protected]/gnucash",
140  "mysql://dbuser:[email protected]/gnucash", FALSE
141  },
142  {
143  "postgres://www.gnucash.org/gnucash", FALSE,
144  "postgres", "www.gnucash.org", NULL, NULL, "gnucash", 0,
145  "postgres://www.gnucash.org/gnucash",
146  "postgres://www.gnucash.org/gnucash", FALSE
147  },
148  {
149  "postgres://www.gnucash.org/gnucash", TRUE,
150  "postgres", "www.gnucash.org", NULL, NULL, "gnucash", 0,
151  "postgres://www.gnucash.org/gnucash",
152  "postgres://www.gnucash.org/gnucash", FALSE
153  },
154  {
155  "postgres://[email protected]/gnucash", FALSE,
156  "postgres", "www.gnucash.org", "dbuser", NULL, "gnucash", 0,
157  "postgres://[email protected]/gnucash",
158  "postgres://[email protected]/gnucash", FALSE
159  },
160  {
161  "postgres://[email protected]/gnucash", TRUE,
162  "postgres", "www.gnucash.org", "dbuser", NULL, "gnucash", 0,
163  "postgres://[email protected]/gnucash",
164  "postgres://[email protected]/gnucash", FALSE
165  },
166  {
167  "postgres://dbuser:[email protected]/gnucash", FALSE,
168  "postgres", "www.gnucash.org", "dbuser", "dbpass", "gnucash", 0,
169  "postgres://dbuser:[email protected]/gnucash",
170  "postgres://[email protected]/gnucash", FALSE
171  },
172  {
173  "postgres://dbuser:[email protected]/gnucash", TRUE,
174  "postgres", "www.gnucash.org", "dbuser", "dbpass", "gnucash", 0,
175  "postgres://dbuser:[email protected]/gnucash",
176  "postgres://dbuser:[email protected]/gnucash", FALSE
177  },
178  /* password with lots of ugly characters in it (potentially conflicting with uri syntax) */
179  {
180  "postgres://dbuser:*#bad35:@[email protected]/gnucash", TRUE,
181  "postgres", "www.gnucash.org", "dbuser", "*#bad35:@xx", "gnucash", 0,
182  "postgres://dbuser:*#bad35:@[email protected]/gnucash",
183  "postgres://dbuser:*#bad35:@[email protected]/gnucash", FALSE
184  },
185  /* uri with custom port number, and hide password in normalized uri */
186  {
187  "postgres://dbuser:[email protected]:744/gnucash", FALSE,
188  "postgres", "www.gnucash.org", "dbuser", "dbpass", "gnucash", 744,
189  "postgres://dbuser:[email protected]:744/gnucash",
190  "postgres://[email protected]:744/gnucash", FALSE
191  },
192  /* TODO Figure out how to write tests that actually verify the relative
193  * pathname resolution. The above tests only test absolute pathnames */
194  { NULL, FALSE, NULL, NULL, NULL, NULL, NULL, 0, NULL, FALSE },
195 };
196 
197 int
198 main(int argc, char **argv)
199 {
200  int i;
201 
202  qof_init();
203 
204  /* TEST: gnc_uri_get_components */
205  for (i = 0; strs[i].uri != NULL; i++)
206  {
207  gchar *tprotocol = NULL;
208  gchar *thostname = NULL;
209  gchar *tusername = NULL;
210  gchar *tpassword = NULL;
211  gchar *tpath = NULL;
212  gint32 tport = 0;
213  gboolean testresult;
214 
215  gnc_uri_get_components( strs[i].uri, &tprotocol, &thostname,
216  &tport, &tusername, &tpassword, &tpath );
217  testresult = ( g_strcmp0 ( tprotocol, strs[i].protocol ) == 0 ) &
218  ( g_strcmp0 ( thostname, strs[i].hostname ) == 0 ) &
219  ( g_strcmp0 ( tusername, strs[i].username ) == 0 ) &
220  ( g_strcmp0 ( tpassword, strs[i].password ) == 0 ) &
221  ( g_strcmp0 ( tpath, strs[i].path ) == 0 ) &
222  ( tport == strs[i].port );
223  do_test_args(testresult,
224  "gnc_uri_get_components",
225  __FILE__, __LINE__,
226  "\n %s:\n"
227  " Expected: %s, %s, %s, %s, %s, %d\n"
228  " Got : %s, %s, %s, %s, %s, %d\n",
229  strs[i].uri, strs[i].protocol, strs[i].hostname,
230  strs[i].username, strs[i].password, strs[i].path, strs[i].port,
231  tprotocol, thostname, tusername, tpassword, tpath, tport);
232  g_free(tprotocol);
233  g_free(thostname);
234  g_free(tusername);
235  g_free(tpassword);
236  g_free(tpath);
237  }
238 
239  /* TEST: gnc_uri_get_protocol */
240  for (i = 0; strs[i].uri != NULL; i++)
241  {
242  gchar *tprotocol = NULL;
243  gboolean testresult;
244 
245  tprotocol = gnc_uri_get_protocol( strs[i].uri );
246  testresult = ( g_strcmp0 ( tprotocol, strs[i].protocol ) == 0 );
247  do_test_args(testresult,
248  "gnc_uri_get_protocol",
249  __FILE__, __LINE__,
250  "\n %s:\n"
251  " Expected: %s\n"
252  " Got : %s\n",
253  strs[i].uri, strs[i].protocol, tprotocol );
254  g_free(tprotocol);
255  }
256 
257  /* TEST: gnc_uri_get_path */
258  for (i = 0; strs[i].uri != NULL; i++)
259  {
260  gchar *tpath = NULL;
261  gboolean testresult;
262 
263  tpath = gnc_uri_get_path( strs[i].uri );
264  testresult = ( g_strcmp0 ( tpath, strs[i].path ) == 0 );
265  do_test_args(testresult,
266  "gnc_uri_get_path",
267  __FILE__, __LINE__,
268  "\n %s:\n"
269  " Expected: %s\n"
270  " Got : %s\n",
271  strs[i].uri, strs[i].path, tpath );
272  g_free(tpath);
273  }
274 
275  /* TEST: gnc_uri_create_uri */
276  for (i = 0; strs[i].uri != NULL; i++)
277  {
278  gchar *turi = NULL;
279  gboolean testresult;
280 
281  turi = gnc_uri_create_uri( strs[i].protocol, strs[i].hostname, strs[i].port,
282  strs[i].username, strs[i].password, strs[i].path );
283  testresult = ( g_strcmp0 ( turi, strs[i].created_uri ) == 0 );
284  do_test_args(testresult,
285  "gnc_uri_create_uri",
286  __FILE__, __LINE__,
287  "\n %s, %s, %s, %s, %s, %d:\n"
288  " Expected: %s\n"
289  " Got : %s\n",
290  strs[i].protocol, strs[i].hostname,
291  strs[i].username, strs[i].password, strs[i].path, strs[i].port,
292  strs[i].created_uri, turi);
293  g_free(turi);
294  }
295 
296  /* TEST: gnc_uri_normalize_uri */
297  for (i = 0; strs[i].uri != NULL; i++)
298  {
299  gchar *turi = NULL;
300  gboolean testresult;
301 
302  turi = gnc_uri_normalize_uri( strs[i].uri, strs[i].want_password );
303  testresult = ( g_strcmp0 ( turi, strs[i].normalized_uri ) == 0 );
304  do_test_args(testresult,
305  "gnc_uri_normalize_uri",
306  __FILE__, __LINE__,
307  "\n %s:\n"
308  " Expected: %s\n"
309  " Got : %s\n",
310  strs[i].uri, strs[i].normalized_uri, turi );
311  g_free(turi);
312  }
313 
314  /* TEST: gnc_uri_is_file_protocol */
315  for (i = 0; strs[i].uri != NULL; i++)
316  {
317  gboolean tis_file_protocol;
318  gboolean testresult;
319 
320  tis_file_protocol = gnc_uri_is_file_protocol( strs[i].protocol );
321  testresult = ( tis_file_protocol == strs[i].is_file_protocol );
322  do_test_args(testresult,
323  "gnc_uri_is_file_protocol",
324  __FILE__, __LINE__,
325  "\n %s:\n"
326  " Expected: %s\n"
327  " Got : %s\n",
328  strs[i].uri, strs[i].is_file_protocol, tis_file_protocol );
329  }
330 
331  /* TEST: gnc_uri_is_file_uri */
332  for (i = 0; strs[i].uri != NULL; i++)
333  {
334  gboolean tis_file_uri;
335  gboolean testresult;
336 
337  tis_file_uri = gnc_uri_is_file_uri( strs[i].uri );
338  testresult = ( tis_file_uri == strs[i].is_file_protocol );
339  do_test_args(testresult,
340  "gnc_uri_is_file_uri",
341  __FILE__, __LINE__,
342  "\n %s:\n"
343  " Expected: %s\n"
344  " Got : %s\n",
345  strs[i].uri, strs[i].is_file_protocol, tis_file_uri );
346  }
347 
348  print_test_results();
349  return get_rv();
350 }
gboolean gnc_uri_is_file_protocol(const gchar *protocol)
Definition: gnc-uri-utils.c:58
gboolean gnc_uri_is_file_uri(const gchar *uri)
Definition: gnc-uri-utils.c:71
gchar * gnc_uri_get_path(const gchar *uri)
gchar * gnc_uri_normalize_uri(const gchar *uri, gboolean allow_password)
void gnc_uri_get_components(const gchar *uri, gchar **protocol, gchar **hostname, gint32 *port, gchar **username, gchar **password, gchar **path)
Definition: gnc-uri-utils.c:82
gchar * gnc_uri_create_uri(const gchar *protocol, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
Utility functions for convert uri in separate components and back.
void qof_init(void)
Initialise the Query Object Framework.
gchar * gnc_uri_get_protocol(const gchar *uri)