shell-test.c 4.97 KB
Newer Older
1
#include <config.h>
2 3
#include <stdio.h>
#include <stdlib.h>
4

5 6 7 8 9
#include <dbus/dbus-internals.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-memory.h>
#include <dbus/dbus-shell.h>
#include <dbus/dbus-string.h>
John Palmieri's avatar
John Palmieri committed
10
#include <dbus/dbus-sysdeps.h>
11

12 13 14
static int test_num = 0;
static int num_failed = 0;

15
static dbus_bool_t
16 17 18
test_command_line_internal (dbus_bool_t should_work,
    const char *arg1,
    va_list var_args)
19 20 21 22 23 24 25 26 27
{
  int i, original_argc, shell_argc;
  char **shell_argv;
  char **original_argv;
  char *command_line, *tmp;
  DBusString str;
  DBusList *list = NULL, *node;
  DBusError error;

28 29 30
  if (!_dbus_list_append (&list, (char *)arg1))
    return FALSE;

31 32 33 34 35
  do
    {
      tmp = va_arg (var_args, char *);
      if (!tmp)
        break;
36 37 38 39 40
      if (!_dbus_list_append (&list, tmp))
        {
          _dbus_list_clear (&list);
          return FALSE;
        }
41 42 43 44
    } while (tmp);

  original_argc = _dbus_list_get_length (&list);
  original_argv = dbus_new (char *, original_argc);
45 46 47 48 49 50 51
  if (!_dbus_string_init (&str))
    {
      _dbus_list_clear (&list);
      dbus_free (original_argv);
      return FALSE;
    }

52 53 54 55
  for (i = 0, node = _dbus_list_get_first_link (&list); i < original_argc && node;
       i++, node = _dbus_list_get_next_link (&list, node))
    {
      original_argv[i] = node->data;
56 57 58 59 60 61 62 63
      if ((i > 0 && !_dbus_string_append_byte (&str, ' ')) ||
          !_dbus_string_append (&str, original_argv[i]))
        {
          _dbus_list_clear (&list);
          dbus_free (original_argv);
          _dbus_string_free (&str);
          return FALSE;
        }
64 65 66 67
    }
  
  _dbus_list_clear (&list);
  command_line = _dbus_string_get_data (&str);
68
  printf ("# Testing command line '%s'\n", command_line);
69 70 71 72

  dbus_error_init (&error);
  if (!_dbus_shell_parse_argv (command_line, &shell_argc, &shell_argv, &error))
    {
73 74 75
      printf ("# Error%s parsing command line: %s\n",
          should_work ? "" : " (as expected)",
          error.message ? error.message : "");
76
      dbus_free (original_argv);
77
      _dbus_string_free (&str);
78
      return !should_work;
79 80 81 82 83
    }
  else
    {
      if (shell_argc != original_argc)
        {
84
          printf ("# Number of arguments returned (%d) don't match original (%d)\n",
85
                  shell_argc, original_argc);
86
          dbus_free (original_argv);
87
          dbus_free_string_array (shell_argv);
88 89
          return FALSE;
        } 
90
      printf ("# Number of arguments: %d\n", shell_argc);
91 92 93 94 95 96 97 98 99 100 101
      for (i = 0; i < shell_argc; i++)
        {
          char *unquoted;
          
          unquoted = _dbus_shell_unquote (original_argv[i]);
          if (strcmp (unquoted ? unquoted : "",
                      shell_argv[i] ? shell_argv[i] : ""))
            {
              printf ("Position %d, returned argument (%s) does not match original (%s)\n",
                      i, shell_argv[i], unquoted);
              dbus_free (unquoted);
102
              dbus_free (original_argv);
103
              dbus_free_string_array (shell_argv);
104 105 106 107 108 109 110 111 112
              return FALSE;
            }
          dbus_free (unquoted);
          if (shell_argv[i])
            printf ("Argument %d = %s\n", i, shell_argv[i]);
        }
      
      dbus_free_string_array (shell_argv);
    }
113

114 115 116
  _dbus_string_free (&str);
  dbus_free (original_argv);

117 118 119 120 121 122
  if (!should_work)
    {
      printf ("# Expected an error\n");
      return FALSE;
    }

123 124 125
  return TRUE;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static void
test_command_line (const char *arg1, ...)
{
  va_list var_args;

  va_start (var_args, arg1);

  if (test_command_line_internal (TRUE, arg1, var_args))
    {
      printf ("ok %d\n", ++test_num);
    }
  else
    {
      printf ("not ok %d\n", ++test_num);
      num_failed++;
    }

  va_end (var_args);
}

static void
test_command_line_fails (const char *arg1, ...)
{
  va_list var_args;

  va_start (var_args, arg1);

  if (test_command_line_internal (FALSE, arg1, var_args))
    {
      printf ("ok %d\n", ++test_num);
    }
  else
    {
      printf ("not ok %d\n", ++test_num);
      num_failed++;
    }

  va_end (var_args);
}

/* This test outputs TAP syntax: http://testanything.org/ */
167 168 169
int
main (int argc, char **argv)
{
170 171 172 173 174
  test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL);
  test_command_line ("command", "-s", NULL);
  test_command_line ("/opt/gnome/bin/service-start", NULL);
  test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL);
  test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL);
175
  test_command_line ("ls", "-l", "-a", "--colors", NULL);
176 177 178 179 180 181 182 183 184 185 186
  test_command_line ("rsync-to-server", NULL);
  test_command_line ("test-segfault", "--no-segfault", NULL);
  test_command_line ("evolution", "mailto:pepe@cuco.com", NULL);
  test_command_line ("run", "\"a \n multiline\"", NULL);
  test_command_line_fails ("ls", "\"a wrong string'", NULL);

  /* Tell the TAP driver that we have done all the tests we plan to do.
   * This is how it can distinguish between an unexpected exit and
   * successful completion. */
  printf ("1..%d\n", test_num);

Simon McVittie's avatar
Simon McVittie committed
187
  dbus_shutdown ();
188
  return (num_failed != 0);
189
}