glib/tests/shell-test.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     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 #ifdef __SYMBIAN32__
       
    36 #include "mrt2_glib2_test.h"
       
    37 #endif /*__SYMBIAN32__*/
       
    38 
       
    39 typedef struct _TestResult TestResult;
       
    40 
       
    41 struct _TestResult
       
    42 {
       
    43   gint argc;
       
    44   const gchar **argv;
       
    45 };
       
    46 
       
    47 static const gchar *
       
    48 test_command_lines[] =
       
    49 {
       
    50   /*  0 */ "foo bar",
       
    51   /*  1 */ "foo 'bar'",
       
    52   /*  2 */ "foo \"bar\"",
       
    53   /*  3 */ "foo '' 'bar'",
       
    54   /*  4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
       
    55   /*  5 */ "foo \t \tblah\tfoo\t\tbar  baz",
       
    56   /*  6 */ "foo '    spaces more spaces lots of     spaces in this   '  \t",
       
    57   /*  7 */ "foo \\\nbar",
       
    58   /*  8 */ "foo '' ''",
       
    59   /*  9 */ "foo \\\" la la la",
       
    60   /* 10 */ "foo \\ foo woo woo\\ ",
       
    61   /* 11 */ "foo \"yada yada \\$\\\"\"",
       
    62   /* 12 */ "foo \"c:\\\\\"",
       
    63   NULL
       
    64 };
       
    65 
       
    66 static const gchar *result0[] = { "foo", "bar", NULL };
       
    67 static const gchar *result1[] = { "foo", "bar", NULL };
       
    68 static const gchar *result2[] = { "foo", "bar", NULL };
       
    69 static const gchar *result3[] = { "foo", "", "bar", NULL };
       
    70 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
       
    71 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
       
    72 static const gchar *result6[] = { "foo", "    spaces more spaces lots of     spaces in this   ", NULL };
       
    73 static const gchar *result7[] = { "foo", "bar", NULL };
       
    74 static const gchar *result8[] = { "foo", "", "", NULL };
       
    75 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
       
    76 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
       
    77 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
       
    78 static const gchar *result12[] = { "foo", "c:\\", NULL };
       
    79 
       
    80 static const TestResult
       
    81 correct_results[] =
       
    82 {
       
    83   { G_N_ELEMENTS (result0) - 1, result0 },
       
    84   { G_N_ELEMENTS (result1) - 1, result1 },
       
    85   { G_N_ELEMENTS (result2) - 1, result2 },
       
    86   { G_N_ELEMENTS (result3) - 1, result3 },
       
    87   { G_N_ELEMENTS (result4) - 1, result4 },
       
    88   { G_N_ELEMENTS (result5) - 1, result5 },
       
    89   { G_N_ELEMENTS (result6) - 1, result6 },
       
    90   { G_N_ELEMENTS (result7) - 1, result7 },
       
    91   { G_N_ELEMENTS (result8) - 1, result8 },
       
    92   { G_N_ELEMENTS (result9) - 1, result9 },
       
    93   { G_N_ELEMENTS (result10) - 1, result10 },
       
    94   { G_N_ELEMENTS (result11) - 1, result11 },
       
    95   { G_N_ELEMENTS (result12) - 1, result12 }
       
    96 };
       
    97 
       
    98 static void
       
    99 print_test (const gchar *cmdline, gint argc, gchar **argv,
       
   100             const TestResult *result)
       
   101 {
       
   102   gint i;
       
   103   
       
   104   fprintf (stderr, "Command line was: '%s'\n", cmdline);
       
   105 
       
   106   fprintf (stderr, "Expected result (%d args):\n", result->argc);
       
   107   
       
   108   i = 0;
       
   109   while (result->argv[i])
       
   110     {
       
   111       fprintf (stderr, " %3d '%s'\n", i, result->argv[i]);
       
   112       ++i;
       
   113     }  
       
   114 
       
   115   fprintf (stderr, "Actual result (%d args):\n", argc);
       
   116   
       
   117   i = 0;
       
   118   while (argv[i])
       
   119     {
       
   120       fprintf (stderr, " %3d '%s'\n", i, argv[i]);
       
   121       ++i;
       
   122     }
       
   123 }
       
   124 
       
   125 static void
       
   126 do_argv_test (const gchar *cmdline, const TestResult *result)
       
   127 {
       
   128   gint argc;
       
   129   gchar **argv;
       
   130   GError *err;
       
   131   gint i;
       
   132 
       
   133   err = NULL;
       
   134   if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
       
   135     {
       
   136       fprintf (stderr, "Error parsing command line that should work fine: %s\n",
       
   137                err->message);
       
   138       
       
   139       exit (1);
       
   140     }
       
   141   
       
   142   if (argc != result->argc)
       
   143     {
       
   144       fprintf (stderr, "Expected and actual argc don't match\n");
       
   145       print_test (cmdline, argc, argv, result);
       
   146       exit (1);
       
   147     }
       
   148 
       
   149   i = 0;
       
   150   while (argv[i])
       
   151     {
       
   152       if (strcmp (argv[i], result->argv[i]) != 0)
       
   153         {
       
   154           fprintf (stderr, "Expected and actual arg %d do not match\n", i);
       
   155           print_test (cmdline, argc, argv, result);
       
   156           exit (1);
       
   157         }
       
   158       
       
   159       ++i;
       
   160     }
       
   161 
       
   162   if (argv[i] != NULL)
       
   163     {
       
   164       fprintf (stderr, "argv didn't get NULL-terminated\n");
       
   165       exit (1);
       
   166     }
       
   167   g_strfreev (argv);
       
   168 }
       
   169 
       
   170 static void
       
   171 run_tests (void)
       
   172 {
       
   173   gint i;
       
   174   
       
   175   i = 0;
       
   176   while (test_command_lines[i])
       
   177     {
       
   178       do_argv_test (test_command_lines[i], &correct_results[i]);
       
   179       ++i;
       
   180     }
       
   181 }
       
   182 
       
   183 static gboolean any_test_failed = FALSE;
       
   184 
       
   185 #define CHECK_STRING_RESULT(expression, expected_value) \
       
   186  check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
       
   187 
       
   188 static void
       
   189 check_string_result (const char *expression,
       
   190 		     const char *file_name,
       
   191 		     int line_number,
       
   192 		     char *result,
       
   193 		     const char *expected)
       
   194 {
       
   195   gboolean match;
       
   196   
       
   197   if (expected == NULL)
       
   198     match = result == NULL;
       
   199   else
       
   200     match = result != NULL && strcmp (result, expected) == 0;
       
   201   
       
   202   if (!match)
       
   203     {
       
   204       if (!any_test_failed)
       
   205 	fprintf (stderr, "\n");
       
   206       
       
   207       fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number);
       
   208       fprintf (stderr, "      evaluated: %s\n", expression);
       
   209       fprintf (stderr, "       expected: %s\n", expected == NULL ? "NULL" : expected);
       
   210       fprintf (stderr, "            got: %s\n", result == NULL ? "NULL" : result);
       
   211       
       
   212       any_test_failed = TRUE;
       
   213     }
       
   214   
       
   215   g_free (result);
       
   216 }
       
   217 
       
   218 static char *
       
   219 test_shell_unquote (const char *str)
       
   220 {
       
   221   char *result;
       
   222   GError *error;
       
   223 
       
   224   error = NULL;
       
   225   result = g_shell_unquote (str, &error);
       
   226   if (error == NULL)
       
   227     return result;
       
   228 
       
   229   /* Leaks the error, which is no big deal and easy to fix if we
       
   230    * decide it matters.
       
   231    */
       
   232 
       
   233   if (error->domain != G_SHELL_ERROR)
       
   234     return g_strdup ("error in domain other than G_SHELL_ERROR");
       
   235 
       
   236   /* It would be nice to check the error message too, but that's
       
   237    * localized, so it's too much of a pain.
       
   238    */
       
   239   switch (error->code)
       
   240     {
       
   241     case G_SHELL_ERROR_BAD_QUOTING:
       
   242       return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
       
   243     case G_SHELL_ERROR_EMPTY_STRING:
       
   244       return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
       
   245     case G_SHELL_ERROR_FAILED:
       
   246       return g_strdup ("G_SHELL_ERROR_FAILED");
       
   247     default:
       
   248       return g_strdup ("bad error code in G_SHELL_ERROR domain");
       
   249     }
       
   250 }
       
   251 
       
   252 int
       
   253 main (int   argc,
       
   254       char *argv[])
       
   255 {
       
   256   #ifdef __SYMBIAN32__
       
   257   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   258   g_set_print_handler(mrtPrintHandler);
       
   259   #endif /*__SYMBIAN32__*/
       
   260 	  
       
   261 
       
   262   run_tests ();
       
   263   
       
   264   CHECK_STRING_RESULT (g_shell_quote (""), "''");
       
   265   CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
       
   266   CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
       
   267   CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
       
   268   CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
       
   269   CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
       
   270   CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
       
   271   
       
   272   CHECK_STRING_RESULT (test_shell_unquote (""), "");
       
   273   CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
       
   274   CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
       
   275   CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
       
   276   CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
       
   277   CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
       
   278   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
       
   279   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
       
   280 
       
   281   CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
       
   282   CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
       
   283 
       
   284   CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
       
   285 
       
   286 #if defined (_MSC_VER) && (_MSC_VER <= 1200)
       
   287   /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
       
   288   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
       
   289 #else
       
   290   CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
       
   291 #endif
       
   292 
       
   293   CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
       
   294   CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
       
   295 
       
   296   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
       
   297   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
       
   298   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
       
   299   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
       
   300 
       
   301   CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
       
   302   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
       
   303   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
       
   304 
       
   305 #ifdef __SYMBIAN32__
       
   306   testResultXml("shell-test");
       
   307 #endif /* EMULATOR */	
       
   308   return any_test_failed ? 1 : 0;
       
   309 }