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

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