sl@0: /* GLIB - Library of useful routines for C programming sl@0: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald sl@0: * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: sl@0: /* sl@0: * Modified by the GLib Team and others 1997-2000. See the AUTHORS sl@0: * file for a list of people on the GLib Team. See the ChangeLog sl@0: * files for a list of changes. These files are distributed with sl@0: * GLib at ftp://ftp.gtk.org/pub/gtk/. sl@0: */ sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: typedef struct _TestResult TestResult; sl@0: sl@0: struct _TestResult sl@0: { sl@0: gint argc; sl@0: const gchar **argv; sl@0: }; sl@0: sl@0: static const gchar * sl@0: test_command_lines[] = sl@0: { sl@0: /* 0 */ "foo bar", sl@0: /* 1 */ "foo 'bar'", sl@0: /* 2 */ "foo \"bar\"", sl@0: /* 3 */ "foo '' 'bar'", sl@0: /* 4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"", sl@0: /* 5 */ "foo \t \tblah\tfoo\t\tbar baz", sl@0: /* 6 */ "foo ' spaces more spaces lots of spaces in this ' \t", sl@0: /* 7 */ "foo \\\nbar", sl@0: /* 8 */ "foo '' ''", sl@0: /* 9 */ "foo \\\" la la la", sl@0: /* 10 */ "foo \\ foo woo woo\\ ", sl@0: /* 11 */ "foo \"yada yada \\$\\\"\"", sl@0: /* 12 */ "foo \"c:\\\\\"", sl@0: NULL sl@0: }; sl@0: sl@0: static const gchar *result0[] = { "foo", "bar", NULL }; sl@0: static const gchar *result1[] = { "foo", "bar", NULL }; sl@0: static const gchar *result2[] = { "foo", "bar", NULL }; sl@0: static const gchar *result3[] = { "foo", "", "bar", NULL }; sl@0: static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL }; sl@0: static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL }; sl@0: static const gchar *result6[] = { "foo", " spaces more spaces lots of spaces in this ", NULL }; sl@0: static const gchar *result7[] = { "foo", "bar", NULL }; sl@0: static const gchar *result8[] = { "foo", "", "", NULL }; sl@0: static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL }; sl@0: static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL }; sl@0: static const gchar *result11[] = { "foo", "yada yada $\"", NULL }; sl@0: static const gchar *result12[] = { "foo", "c:\\", NULL }; sl@0: sl@0: static const TestResult sl@0: correct_results[] = sl@0: { sl@0: { G_N_ELEMENTS (result0) - 1, result0 }, sl@0: { G_N_ELEMENTS (result1) - 1, result1 }, sl@0: { G_N_ELEMENTS (result2) - 1, result2 }, sl@0: { G_N_ELEMENTS (result3) - 1, result3 }, sl@0: { G_N_ELEMENTS (result4) - 1, result4 }, sl@0: { G_N_ELEMENTS (result5) - 1, result5 }, sl@0: { G_N_ELEMENTS (result6) - 1, result6 }, sl@0: { G_N_ELEMENTS (result7) - 1, result7 }, sl@0: { G_N_ELEMENTS (result8) - 1, result8 }, sl@0: { G_N_ELEMENTS (result9) - 1, result9 }, sl@0: { G_N_ELEMENTS (result10) - 1, result10 }, sl@0: { G_N_ELEMENTS (result11) - 1, result11 }, sl@0: { G_N_ELEMENTS (result12) - 1, result12 } sl@0: }; sl@0: sl@0: static void sl@0: print_test (const gchar *cmdline, gint argc, gchar **argv, sl@0: const TestResult *result) sl@0: { sl@0: gint i; sl@0: sl@0: fprintf (stderr, "Command line was: '%s'\n", cmdline); sl@0: sl@0: fprintf (stderr, "Expected result (%d args):\n", result->argc); sl@0: sl@0: i = 0; sl@0: while (result->argv[i]) sl@0: { sl@0: fprintf (stderr, " %3d '%s'\n", i, result->argv[i]); sl@0: ++i; sl@0: } sl@0: sl@0: fprintf (stderr, "Actual result (%d args):\n", argc); sl@0: sl@0: i = 0; sl@0: while (argv[i]) sl@0: { sl@0: fprintf (stderr, " %3d '%s'\n", i, argv[i]); sl@0: ++i; sl@0: } sl@0: } sl@0: sl@0: static void sl@0: do_argv_test (const gchar *cmdline, const TestResult *result) sl@0: { sl@0: gint argc; sl@0: gchar **argv; sl@0: GError *err; sl@0: gint i; sl@0: sl@0: err = NULL; sl@0: if (!g_shell_parse_argv (cmdline, &argc, &argv, &err)) sl@0: { sl@0: fprintf (stderr, "Error parsing command line that should work fine: %s\n", sl@0: err->message); sl@0: sl@0: exit (1); sl@0: } sl@0: sl@0: if (argc != result->argc) sl@0: { sl@0: fprintf (stderr, "Expected and actual argc don't match\n"); sl@0: print_test (cmdline, argc, argv, result); sl@0: exit (1); sl@0: } sl@0: sl@0: i = 0; sl@0: while (argv[i]) sl@0: { sl@0: if (strcmp (argv[i], result->argv[i]) != 0) sl@0: { sl@0: fprintf (stderr, "Expected and actual arg %d do not match\n", i); sl@0: print_test (cmdline, argc, argv, result); sl@0: exit (1); sl@0: } sl@0: sl@0: ++i; sl@0: } sl@0: sl@0: if (argv[i] != NULL) sl@0: { sl@0: fprintf (stderr, "argv didn't get NULL-terminated\n"); sl@0: exit (1); sl@0: } sl@0: g_strfreev (argv); sl@0: } sl@0: sl@0: static void sl@0: run_tests (void) sl@0: { sl@0: gint i; sl@0: sl@0: i = 0; sl@0: while (test_command_lines[i]) sl@0: { sl@0: do_argv_test (test_command_lines[i], &correct_results[i]); sl@0: ++i; sl@0: } sl@0: } sl@0: sl@0: static gboolean any_test_failed = FALSE; sl@0: sl@0: #define CHECK_STRING_RESULT(expression, expected_value) \ sl@0: check_string_result (#expression, __FILE__, __LINE__, expression, expected_value) sl@0: sl@0: static void sl@0: check_string_result (const char *expression, sl@0: const char *file_name, sl@0: int line_number, sl@0: char *result, sl@0: const char *expected) sl@0: { sl@0: gboolean match; sl@0: sl@0: if (expected == NULL) sl@0: match = result == NULL; sl@0: else sl@0: match = result != NULL && strcmp (result, expected) == 0; sl@0: sl@0: if (!match) sl@0: { sl@0: if (!any_test_failed) sl@0: fprintf (stderr, "\n"); sl@0: sl@0: fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number); sl@0: fprintf (stderr, " evaluated: %s\n", expression); sl@0: fprintf (stderr, " expected: %s\n", expected == NULL ? "NULL" : expected); sl@0: fprintf (stderr, " got: %s\n", result == NULL ? "NULL" : result); sl@0: sl@0: any_test_failed = TRUE; sl@0: } sl@0: sl@0: g_free (result); sl@0: } sl@0: sl@0: static char * sl@0: test_shell_unquote (const char *str) sl@0: { sl@0: char *result; sl@0: GError *error; sl@0: sl@0: error = NULL; sl@0: result = g_shell_unquote (str, &error); sl@0: if (error == NULL) sl@0: return result; sl@0: sl@0: /* Leaks the error, which is no big deal and easy to fix if we sl@0: * decide it matters. sl@0: */ sl@0: sl@0: if (error->domain != G_SHELL_ERROR) sl@0: return g_strdup ("error in domain other than G_SHELL_ERROR"); sl@0: sl@0: /* It would be nice to check the error message too, but that's sl@0: * localized, so it's too much of a pain. sl@0: */ sl@0: switch (error->code) sl@0: { sl@0: case G_SHELL_ERROR_BAD_QUOTING: sl@0: return g_strdup ("G_SHELL_ERROR_BAD_QUOTING"); sl@0: case G_SHELL_ERROR_EMPTY_STRING: sl@0: return g_strdup ("G_SHELL_ERROR_EMPTY_STRING"); sl@0: case G_SHELL_ERROR_FAILED: sl@0: return g_strdup ("G_SHELL_ERROR_FAILED"); sl@0: default: sl@0: return g_strdup ("bad error code in G_SHELL_ERROR domain"); sl@0: } sl@0: } sl@0: sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: #ifdef __SYMBIAN32__ sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: sl@0: sl@0: run_tests (); sl@0: sl@0: CHECK_STRING_RESULT (g_shell_quote (""), "''"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("("), "'('"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''"); sl@0: CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'"); sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote (""), ""); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("a"), "a"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'('"), "("); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a"); sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), ""); sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING"); sl@0: sl@0: #if defined (_MSC_VER) && (_MSC_VER <= 1200) sl@0: /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */ sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\""); sl@0: #else sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\""); sl@0: #endif sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING"); sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n"); sl@0: sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r"); sl@0: CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n"); sl@0: sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("shell-test"); sl@0: #endif /* EMULATOR */ sl@0: return any_test_failed ? 1 : 0; sl@0: }