source: trunk/third/gnome-vfs/test/test-escape.c @ 15858

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