os/ossrv/glib/tsrc/BC/tests/shell-test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/shell-test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,313 @@
     1.4 +/* GLIB - Library of useful routines for C programming
     1.5 + * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
     1.6 + * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.7 + * This library is free software; you can redistribute it and/or
     1.8 + * modify it under the terms of the GNU Lesser General Public
     1.9 + * License as published by the Free Software Foundation; either
    1.10 + * version 2 of the License, or (at your option) any later version.
    1.11 + *
    1.12 + * This library is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 + * Lesser General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Lesser General Public
    1.18 + * License along with this library; if not, write to the
    1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 + * Boston, MA 02111-1307, USA.
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 + * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
    1.25 + * file for a list of people on the GLib Team.  See the ChangeLog
    1.26 + * files for a list of changes.  These files are distributed with
    1.27 + * GLib at ftp://ftp.gtk.org/pub/gtk/. 
    1.28 + */
    1.29 +
    1.30 +#undef G_DISABLE_ASSERT
    1.31 +#undef G_LOG_DOMAIN
    1.32 +
    1.33 +#include <glib.h>
    1.34 +#include <stdio.h>
    1.35 +#include <string.h>
    1.36 +#include <stdlib.h>
    1.37 +
    1.38 +#ifdef SYMBIAN
    1.39 +#include "mrt2_glib2_test.h"
    1.40 +#endif /*SYMBIAN*/
    1.41 +
    1.42 +
    1.43 +
    1.44 +typedef struct _TestResult TestResult;
    1.45 +
    1.46 +
    1.47 +
    1.48 +
    1.49 +struct _TestResult
    1.50 +{
    1.51 +  gint argc;
    1.52 +  const gchar **argv;
    1.53 +};
    1.54 +
    1.55 +static const gchar *
    1.56 +test_command_lines[] =
    1.57 +{
    1.58 +  /*  0 */ "foo bar",
    1.59 +  /*  1 */ "foo 'bar'",
    1.60 +  /*  2 */ "foo \"bar\"",
    1.61 +  /*  3 */ "foo '' 'bar'",
    1.62 +  /*  4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
    1.63 +  /*  5 */ "foo \t \tblah\tfoo\t\tbar  baz",
    1.64 +  /*  6 */ "foo '    spaces more spaces lots of     spaces in this   '  \t",
    1.65 +  /*  7 */ "foo \\\nbar",
    1.66 +  /*  8 */ "foo '' ''",
    1.67 +  /*  9 */ "foo \\\" la la la",
    1.68 +  /* 10 */ "foo \\ foo woo woo\\ ",
    1.69 +  /* 11 */ "foo \"yada yada \\$\\\"\"",
    1.70 +  /* 12 */ "foo \"c:\\\\\"",
    1.71 +  NULL
    1.72 +};
    1.73 +
    1.74 +static const gchar *result0[] = { "foo", "bar", NULL };
    1.75 +static const gchar *result1[] = { "foo", "bar", NULL };
    1.76 +static const gchar *result2[] = { "foo", "bar", NULL };
    1.77 +static const gchar *result3[] = { "foo", "", "bar", NULL };
    1.78 +static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
    1.79 +static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
    1.80 +static const gchar *result6[] = { "foo", "    spaces more spaces lots of     spaces in this   ", NULL };
    1.81 +static const gchar *result7[] = { "foo", "bar", NULL };
    1.82 +static const gchar *result8[] = { "foo", "", "", NULL };
    1.83 +static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
    1.84 +static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
    1.85 +static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
    1.86 +static const gchar *result12[] = { "foo", "c:\\", NULL };
    1.87 +
    1.88 +static const TestResult
    1.89 +correct_results[] =
    1.90 +{
    1.91 +  { G_N_ELEMENTS (result0) - 1, result0 },
    1.92 +  { G_N_ELEMENTS (result1) - 1, result1 },
    1.93 +  { G_N_ELEMENTS (result2) - 1, result2 },
    1.94 +  { G_N_ELEMENTS (result3) - 1, result3 },
    1.95 +  { G_N_ELEMENTS (result4) - 1, result4 },
    1.96 +  { G_N_ELEMENTS (result5) - 1, result5 },
    1.97 +  { G_N_ELEMENTS (result6) - 1, result6 },
    1.98 +  { G_N_ELEMENTS (result7) - 1, result7 },
    1.99 +  { G_N_ELEMENTS (result8) - 1, result8 },
   1.100 +  { G_N_ELEMENTS (result9) - 1, result9 },
   1.101 +  { G_N_ELEMENTS (result10) - 1, result10 },
   1.102 +  { G_N_ELEMENTS (result11) - 1, result11 },
   1.103 +  { G_N_ELEMENTS (result12) - 1, result12 }
   1.104 +};
   1.105 +
   1.106 +static void
   1.107 +print_test (const gchar *cmdline, gint argc, gchar **argv,
   1.108 +            const TestResult *result)
   1.109 +{
   1.110 +  gint i;
   1.111 +  
   1.112 +  g_print("Command line was: '%s'\n", cmdline);
   1.113 +
   1.114 +  g_print("Expected result (%d args):\n", result->argc);
   1.115 +  
   1.116 +  i = 0;
   1.117 +  while (result->argv[i])
   1.118 +    {
   1.119 +      g_print(" %3d '%s'\n", i, result->argv[i]);
   1.120 +      ++i;
   1.121 +    }  
   1.122 +
   1.123 +  g_print("Actual result (%d args):\n", argc);
   1.124 +  
   1.125 +  i = 0;
   1.126 +  while (argv[i])
   1.127 +    {
   1.128 +      g_print(" %3d '%s'\n", i, argv[i]);
   1.129 +      ++i;
   1.130 +    }
   1.131 +}
   1.132 +
   1.133 +static void
   1.134 +do_argv_test (const gchar *cmdline, const TestResult *result)
   1.135 +{
   1.136 +  gint argc;
   1.137 +  gchar **argv;
   1.138 +  GError *err;
   1.139 +  gint i;
   1.140 +
   1.141 +  err = NULL;
   1.142 +  if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
   1.143 +    {
   1.144 +      g_print("Error parsing command line that should work fine: %s\n",
   1.145 +               err->message);
   1.146 +      g_assert(FALSE && "shell-test");
   1.147 +      exit (1);
   1.148 +    }
   1.149 +  
   1.150 +  if (argc != result->argc)
   1.151 +    {
   1.152 +      g_print("Expected and actual argc don't match\n");
   1.153 +      print_test (cmdline, argc, argv, result);
   1.154 +      exit (1);
   1.155 +    }
   1.156 +
   1.157 +  i = 0;
   1.158 +  while (argv[i])
   1.159 +    {
   1.160 +      if (strcmp (argv[i], result->argv[i]) != 0)
   1.161 +        {
   1.162 +          g_print("Expected and actual arg %d do not match\n", i);
   1.163 +          print_test (cmdline, argc, argv, result);
   1.164 +          exit (1);
   1.165 +        }
   1.166 +      
   1.167 +      ++i;
   1.168 +    }
   1.169 +
   1.170 +  if (argv[i] != NULL)
   1.171 +    {
   1.172 +      g_print("argv didn't get NULL-terminated\n");
   1.173 +      exit (1);
   1.174 +    }
   1.175 +}
   1.176 +
   1.177 +static void
   1.178 +run_tests (void)
   1.179 +{
   1.180 +  gint i;
   1.181 +  
   1.182 +  i = 0;
   1.183 +  while (test_command_lines[i])
   1.184 +    {
   1.185 +      do_argv_test (test_command_lines[i], &correct_results[i]);
   1.186 +      ++i;
   1.187 +    }
   1.188 +}
   1.189 +
   1.190 +static gboolean any_test_failed = FALSE;
   1.191 +
   1.192 +#define CHECK_STRING_RESULT(expression, expected_value) \
   1.193 + check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
   1.194 +
   1.195 +static void
   1.196 +check_string_result (const char *expression,
   1.197 +		     const char *file_name,
   1.198 +		     int line_number,
   1.199 +		     char *result,
   1.200 +		     const char *expected)
   1.201 +{
   1.202 +  gboolean match;
   1.203 +  
   1.204 +  if (expected == NULL)
   1.205 +    match = result == NULL;
   1.206 +  else
   1.207 +    match = result != NULL && strcmp (result, expected) == 0;
   1.208 +  
   1.209 +  if (!match)
   1.210 +    {
   1.211 +      if (!any_test_failed)
   1.212 +	g_print("\n");
   1.213 +      
   1.214 +      g_print("FAIL: check failed in %s, line %d\n", file_name, line_number);
   1.215 +      g_print("evaluated: %s\n", expression);
   1.216 +      g_print("expected: %s\n", expected == NULL ? "NULL" : expected);
   1.217 +      g_print("got: %s\n", result == NULL ? "NULL" : result);
   1.218 +      g_assert(FALSE && "shell-test");
   1.219 +      any_test_failed = TRUE;
   1.220 +    }
   1.221 +  
   1.222 +  g_free (result);
   1.223 +}
   1.224 +
   1.225 +static char *
   1.226 +test_shell_unquote (const char *str)
   1.227 +{
   1.228 +  char *result;
   1.229 +  GError *error;
   1.230 +
   1.231 +  error = NULL;
   1.232 +  result = g_shell_unquote (str, &error);
   1.233 +  if (error == NULL)
   1.234 +    return result;
   1.235 +
   1.236 +  /* Leaks the error, which is no big deal and easy to fix if we
   1.237 +   * decide it matters.
   1.238 +   */
   1.239 +
   1.240 +  if (error->domain != G_SHELL_ERROR)
   1.241 +    return g_strdup ("error in domain other than G_SHELL_ERROR");
   1.242 +
   1.243 +  /* It would be nice to check the error message too, but that's
   1.244 +   * localized, so it's too much of a pain.
   1.245 +   */
   1.246 +  switch (error->code)
   1.247 +    {
   1.248 +    case G_SHELL_ERROR_BAD_QUOTING:
   1.249 +      return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
   1.250 +    case G_SHELL_ERROR_EMPTY_STRING:
   1.251 +      return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
   1.252 +    case G_SHELL_ERROR_FAILED:
   1.253 +      return g_strdup ("G_SHELL_ERROR_FAILED");
   1.254 +    default:
   1.255 +      return g_strdup ("bad error code in G_SHELL_ERROR domain");
   1.256 +    }
   1.257 +}
   1.258 +
   1.259 +int
   1.260 +main (int   argc,
   1.261 +      char *argv[])
   1.262 +{
   1.263 +  #ifdef SYMBIAN
   1.264 +  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);
   1.265 +  g_set_print_handler(mrtPrintHandler);
   1.266 +  #endif /*SYMBIAN*/
   1.267 +	  
   1.268 +
   1.269 +  run_tests ();
   1.270 +  
   1.271 +  CHECK_STRING_RESULT (g_shell_quote (""), "''");
   1.272 +  CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
   1.273 +  CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
   1.274 +  CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
   1.275 +  CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
   1.276 +  CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
   1.277 +  CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
   1.278 +  
   1.279 +  CHECK_STRING_RESULT (test_shell_unquote (""), "");
   1.280 +  CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
   1.281 +  CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
   1.282 +  CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
   1.283 +  CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
   1.284 +  CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
   1.285 +  CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
   1.286 +  CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
   1.287 +
   1.288 +  CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
   1.289 +  CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
   1.290 +
   1.291 +  CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
   1.292 +
   1.293 +#if defined (_MSC_VER) && (_MSC_VER <= 1200)
   1.294 +  /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
   1.295 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
   1.296 +#else
   1.297 +  CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
   1.298 +#endif
   1.299 +
   1.300 +  CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
   1.301 +  CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
   1.302 +
   1.303 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
   1.304 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
   1.305 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
   1.306 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
   1.307 +
   1.308 +  CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
   1.309 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
   1.310 +  CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
   1.311 +
   1.312 +#ifdef SYMBIAN
   1.313 +  testResultXml("shell-test");
   1.314 +#endif /* EMULATOR */	
   1.315 +  return any_test_failed ? 1 : 0;
   1.316 +}