source: trunk/third/gnome-vfs2/test/test-escape.c @ 18317

Revision 18317, 13.1 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18316, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3/* test-async-cancel.c - Test program for the GNOME Virtual File System.
4
5   Copyright (C) 1999 Free Software Foundation
6
7   The Gnome Library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Library General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   The Gnome Library 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.  See the GNU
15   Library General Public License for more details.
16
17   You should have received a copy of the GNU Library General Public
18   License along with the Gnome Library; see the file COPYING.LIB.  If not,
19   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.
21
22   Author: Darin Adler <darin@eazel.com>
23*/
24
25#include <config.h>
26
27#include <glib/gstrfuncs.h>
28#include <glib/gmessages.h>
29#include <libgnomevfs/gnome-vfs-init.h>
30#include <libgnomevfs/gnome-vfs-utils.h>
31#include <signal.h>
32#include <stdlib.h>
33#include <string.h>
34
35#define TEST_ASSERT(expression, message) \
36        G_STMT_START { if (!(expression)) test_failed message; } G_STMT_END
37
38static void
39stop_after_log (const char *domain, GLogLevelFlags level,
40        const char *message, gpointer data)
41{
42        void (* saved_handler) (int);
43       
44        g_log_default_handler (domain, level, message, data);
45
46        saved_handler = signal (SIGINT, SIG_IGN);
47        raise (SIGINT);
48        signal (SIGINT, saved_handler);
49}
50
51static void
52make_asserts_break (const char *domain)
53{
54        g_log_set_handler
55                (domain,
56                 (GLogLevelFlags) (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING),
57                 stop_after_log, NULL);
58}
59
60static gboolean at_least_one_test_failed = FALSE;
61
62static void
63test_failed (const char *format, ...)
64{
65        va_list arguments;
66        char *message;
67
68        va_start (arguments, format);
69        message = g_strdup_vprintf (format, arguments);
70        va_end (arguments);
71
72        g_message ("test failed: %s", message);
73        at_least_one_test_failed = TRUE;
74}
75
76static void
77test_escape (const char *input, const char *expected_output)
78{
79        char *output;
80
81        output = gnome_vfs_escape_string (input);
82
83        if (strcmp (output, expected_output) != 0) {
84                test_failed ("escaping %s resulted in %s instead of %s",
85                             input, output, expected_output);
86        }
87
88        g_free (output);
89}
90
91static void
92test_escape_path (const char *input, const char *expected_output)
93{
94        char *output;
95
96        output = gnome_vfs_escape_path_string (input);
97
98        if (strcmp (output, expected_output) != 0) {
99                test_failed ("escaping path %s resulted in %s instead of %s",
100                             input, output, expected_output);
101        }
102
103        g_free (output);
104}
105
106static void
107test_unescape (const char *input, const char *illegal, const char *expected_output)
108{
109        char *output;
110
111        output = gnome_vfs_unescape_string (input, illegal);
112        if (expected_output == NULL) {
113                if (output != NULL) {
114                        test_failed ("unescaping %s resulted in %s instead of NULL",
115                                     input, illegal, output);
116                }
117        } else {
118                if (output == NULL) {
119                        test_failed ("unescaping %s resulted in NULL instead of %s",
120                                     input, illegal, expected_output);
121                } else if (strcmp (output, expected_output) != 0) {
122                        test_failed ("unescaping %s with %s illegal resulted in %s instead of %s",
123                                     input, illegal, output, expected_output);
124                }
125        }
126
127        g_free (output);
128}
129
130static void
131test_unescape_display (const char *input, const char *expected_output)
132{
133        char *output;
134
135        output = gnome_vfs_unescape_string_for_display (input);
136        if (expected_output == NULL) {
137                if (output != NULL) {
138                        test_failed ("unescaping %s for display resulted in %s instead of NULL",
139                                     input, output);
140                }
141        } else {
142                if (output == NULL) {
143                        test_failed ("unescaping %s for display resulted in NULL instead of %s",
144                                     input, expected_output);
145                } else if (strcmp (output, expected_output) != 0) {
146                        test_failed ("unescaping %s for display resulted in %s instead of %s",
147                                     input, output, expected_output);
148                }
149        }
150
151        g_free (output);
152}
153
154int
155main (int argc, char **argv)
156{
157        make_asserts_break ("GnomeVFS");
158
159        gnome_vfs_init ();
160
161        test_escape ("", "");
162
163        test_escape ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
164        test_escape ("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
165        test_escape ("0123456789", "0123456789");
166        test_escape ("-_.!~*'()", "-_.!~*'()");
167
168        test_escape ("\x01\x02\x03\x04\x05\x06\x07", "%01%02%03%04%05%06%07");
169        test_escape ("\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", "%08%09%0A%0B%0C%0D%0E%0F");
170        test_escape (" \"#$%&+,/", "%20%22%23%24%25%26%2B%2C%2F");
171        test_escape (":;<=>?@", "%3A%3B%3C%3D%3E%3F%40");
172        test_escape ("[\\]^`", "%5B%5C%5D%5E%60");
173        test_escape ("{|}\x7F", "%7B%7C%7D%7F");
174
175        test_escape ("\x80\x81\x82\x83\x84\x85\x86\x87", "%80%81%82%83%84%85%86%87");
176        test_escape ("\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F", "%88%89%8A%8B%8C%8D%8E%8F");
177        test_escape ("\x90\x91\x92\x93\x94\x95\x96\x97", "%90%91%92%93%94%95%96%97");
178        test_escape ("\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F", "%98%99%9A%9B%9C%9D%9E%9F");
179        test_escape ("\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7", "%A0%A1%A2%A3%A4%A5%A6%A7");
180        test_escape ("\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF", "%A8%A9%AA%AB%AC%AD%AE%AF");
181        test_escape ("\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7", "%B0%B1%B2%B3%B4%B5%B6%B7");
182        test_escape ("\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF", "%B8%B9%BA%BB%BC%BD%BE%BF");
183        test_escape ("\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7", "%C0%C1%C2%C3%C4%C5%C6%C7");
184        test_escape ("\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF", "%C8%C9%CA%CB%CC%CD%CE%CF");
185        test_escape ("\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7", "%D0%D1%D2%D3%D4%D5%D6%D7");
186        test_escape ("\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF", "%D8%D9%DA%DB%DC%DD%DE%DF");
187        test_escape ("\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7", "%E0%E1%E2%E3%E4%E5%E6%E7");
188        test_escape ("\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF", "%E8%E9%EA%EB%EC%ED%EE%EF");
189        test_escape ("\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7", "%F0%F1%F2%F3%F4%F5%F6%F7");
190        test_escape ("\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", "%F8%F9%FA%FB%FC%FD%FE%FF");
191
192        test_escape_path ("", "");
193
194        test_escape_path ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
195        test_escape_path ("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
196        test_escape_path ("0123456789", "0123456789");
197        test_escape_path ("-_.!~*'()/", "-_.!~*'()/");
198
199        test_escape_path ("\x01\x02\x03\x04\x05\x06\x07", "%01%02%03%04%05%06%07");
200        test_escape_path ("\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", "%08%09%0A%0B%0C%0D%0E%0F");
201        test_escape_path (" \"#$%&+,", "%20%22%23%24%25&%2B%2C");
202        test_escape_path (":;<=>?@", "%3A%3B%3C=%3E?%40");
203        test_escape_path ("[\\]^`", "%5B%5C%5D%5E%60");
204        test_escape_path ("{|}\x7F", "%7B%7C%7D%7F");
205
206        test_escape_path ("\x80\x81\x82\x83\x84\x85\x86\x87", "%80%81%82%83%84%85%86%87");
207        test_escape_path ("\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F", "%88%89%8A%8B%8C%8D%8E%8F");
208        test_escape_path ("\x90\x91\x92\x93\x94\x95\x96\x97", "%90%91%92%93%94%95%96%97");
209        test_escape_path ("\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F", "%98%99%9A%9B%9C%9D%9E%9F");
210        test_escape_path ("\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7", "%A0%A1%A2%A3%A4%A5%A6%A7");
211        test_escape_path ("\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF", "%A8%A9%AA%AB%AC%AD%AE%AF");
212        test_escape_path ("\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7", "%B0%B1%B2%B3%B4%B5%B6%B7");
213        test_escape_path ("\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF", "%B8%B9%BA%BB%BC%BD%BE%BF");
214        test_escape_path ("\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7", "%C0%C1%C2%C3%C4%C5%C6%C7");
215        test_escape_path ("\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF", "%C8%C9%CA%CB%CC%CD%CE%CF");
216        test_escape_path ("\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7", "%D0%D1%D2%D3%D4%D5%D6%D7");
217        test_escape_path ("\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF", "%D8%D9%DA%DB%DC%DD%DE%DF");
218        test_escape_path ("\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7", "%E0%E1%E2%E3%E4%E5%E6%E7");
219        test_escape_path ("\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF", "%E8%E9%EA%EB%EC%ED%EE%EF");
220        test_escape_path ("\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7", "%F0%F1%F2%F3%F4%F5%F6%F7");
221        test_escape_path ("\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF", "%F8%F9%FA%FB%FC%FD%FE%FF");
222
223        test_unescape ("", NULL, "");
224        test_unescape ("", "", "");
225        test_unescape ("", "/", "");
226        test_unescape ("", "/:", "");
227
228        test_unescape ("/", "/", "/");
229        test_unescape ("%2f", NULL, "/");
230        test_unescape ("%2F", NULL, "/");
231        test_unescape ("%2F", "/", NULL);
232        test_unescape ("%", NULL, NULL);
233        test_unescape ("%1", NULL, NULL);
234        test_unescape ("%aa", NULL, "\xAA");
235        test_unescape ("%ag", NULL, NULL);
236        test_unescape ("%g", NULL, NULL);
237        test_unescape ("%%", NULL, NULL);
238        test_unescape ("%1%1", NULL, NULL);
239        test_unescape ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
240        test_unescape ("abcdefghijklmnopqrstuvwxyz", NULL, "abcdefghijklmnopqrstuvwxyz");
241        test_unescape ("0123456789", NULL, "0123456789");
242        test_unescape ("-_.!~*'()", NULL, "-_.!~*'()");
243
244        test_unescape ("%01%02%03%04%05%06%07", NULL, "\x01\x02\x03\x04\x05\x06\x07");
245        test_unescape ("%08%09%0A%0B%0C%0D%0E%0F", NULL, "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F");
246        test_unescape ("%20%22%23%24%25%26%2B%2C%2F", NULL, " \"#$%&+,/");
247        test_unescape ("%3A%3B%3C%3D%3E%3F%40", NULL, ":;<=>?@");
248        test_unescape ("%5B%5C%5D%5E%60", NULL, "[\\]^`");
249        test_unescape ("%7B%7C%7D%7F", NULL, "{|}\x7F");
250       
251        test_unescape ("%80%81%82%83%84%85%86%87", NULL, "\x80\x81\x82\x83\x84\x85\x86\x87");
252        test_unescape ("%88%89%8A%8B%8C%8D%8E%8F", NULL, "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F");
253        test_unescape ("%90%91%92%93%94%95%96%97", NULL, "\x90\x91\x92\x93\x94\x95\x96\x97");
254        test_unescape ("%98%99%9A%9B%9C%9D%9E%9F", NULL, "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F");
255        test_unescape ("%A0%A1%A2%A3%A4%A5%A6%A7", NULL, "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7");
256        test_unescape ("%A8%A9%AA%AB%AC%AD%AE%AF", NULL, "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF");
257        test_unescape ("%B0%B1%B2%B3%B4%B5%B6%B7", NULL, "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7");
258        test_unescape ("%B8%B9%BA%BB%BC%BD%BE%BF", NULL, "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
259        test_unescape ("%C0%C1%C2%C3%C4%C5%C6%C7", NULL, "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7");
260        test_unescape ("%C8%C9%CA%CB%CC%CD%CE%CF", NULL, "\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF");
261        test_unescape ("%D0%D1%D2%D3%D4%D5%D6%D7", NULL, "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7");
262        test_unescape ("%D8%D9%DA%DB%DC%DD%DE%DF", NULL, "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF");
263        test_unescape ("%E0%E1%E2%E3%E4%E5%E6%E7", NULL, "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7");
264        test_unescape ("%E8%E9%EA%EB%EC%ED%EE%EF", NULL, "\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF");
265        test_unescape ("%F0%F1%F2%F3%F4%F5%F6%F7", NULL, "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7");
266        test_unescape ("%F8%F9%FA%FB%FC%FD%FE%FF", NULL, "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF");
267       
268        test_unescape_display ("ABC%00DEF", "ABC%00DEF");
269        test_unescape_display ("%20%22%23%24%25%26%2B%00%2C%2F", " \"#$%&+%00,/");
270        test_unescape_display ("", "");
271        test_unescape_display ("%1%1", "%1%1");
272        test_unescape_display ("/", "/");
273        test_unescape_display ("%2f", "/");
274        test_unescape_display ("%2F", "/");
275        test_unescape_display ("%", "%");
276        test_unescape_display ("%1", "%1");
277        test_unescape_display ("%aa", "\xAA");
278        test_unescape_display ("%ag", "%ag");
279        test_unescape_display ("%g", "%g");
280        test_unescape_display ("%%", "%%");
281        test_unescape_display ("%%20", "% ");
282        test_unescape_display ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
283        test_unescape_display ("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
284        test_unescape_display ("0123456789", "0123456789");
285        test_unescape_display ("-_.!~*'()", "-_.!~*'()");
286
287        test_unescape_display ("%01%02%03%04%05%06%07", "\x01\x02\x03\x04\x05\x06\x07");
288        test_unescape_display ("%08%09%0A%0B%0C%0D%0E%0F", "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F");
289        test_unescape_display ("%20%22%23%24%25%26%2B%2C%2F", " \"#$%&+,/");
290        test_unescape_display ("%3A%3B%3C%3D%3E%3F%40", ":;<=>?@");
291        test_unescape_display ("%5B%5C%5D%5E%60", "[\\]^`");
292        test_unescape_display ("%7B%7C%7D%7F", "{|}\x7F");
293       
294        test_unescape_display ("%80%81%82%83%84%85%86%87", "\x80\x81\x82\x83\x84\x85\x86\x87");
295        test_unescape_display ("%88%89%8A%8B%8C%8D%8E%8F", "\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F");
296        test_unescape_display ("%90%91%92%93%94%95%96%97", "\x90\x91\x92\x93\x94\x95\x96\x97");
297        test_unescape_display ("%98%99%9A%9B%9C%9D%9E%9F", "\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F");
298        test_unescape_display ("%A0%A1%A2%A3%A4%A5%A6%A7", "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7");
299        test_unescape_display ("%A8%A9%AA%AB%AC%AD%AE%AF", "\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF");
300        test_unescape_display ("%B0%B1%B2%B3%B4%B5%B6%B7", "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7");
301        test_unescape_display ("%B8%B9%BA%BB%BC%BD%BE%BF", "\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
302        test_unescape_display ("%C0%C1%C2%C3%C4%C5%C6%C7", "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7");
303        test_unescape_display ("%C8%C9%CA%CB%CC%CD%CE%CF", "\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF");
304        test_unescape_display ("%D0%D1%D2%D3%D4%D5%D6%D7", "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7");
305        test_unescape_display ("%D8%D9%DA%DB%DC%DD%DE%DF", "\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF");
306        test_unescape_display ("%E0%E1%E2%E3%E4%E5%E6%E7", "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7");
307        test_unescape_display ("%E8%E9%EA%EB%EC%ED%EE%EF", "\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF");
308        test_unescape_display ("%F0%F1%F2%F3%F4%F5%F6%F7", "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7");
309        test_unescape_display ("%F8%F9%FA%FB%FC%FD%FE%FF", "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF");
310
311        /* Report to "make check" on whether it all worked or not. */
312        return at_least_one_test_failed ? EXIT_FAILURE : EXIT_SUCCESS;
313}
Note: See TracBrowser for help on using the repository browser.