dbus-auth-script.c 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-auth-script.c Test DBusAuth using a special script file (internal to D-BUS implementation)
 * 
 * Copyright (C) 2003 Red Hat, Inc.
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <config.h>

#ifdef DBUS_BUILD_TESTS

#include "dbus-auth-script.h"
#include "dbus-auth.h"
#include "dbus-string.h"
#include "dbus-hash.h"
#include "dbus-internals.h"
#include "dbus-marshal.h"

/**
 * @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
 * @ingroup  DBusInternals
 * @brief DBusAuth unit test scripting
 *
 * The code in here is used for unit testing, it loads
 * up a script that tests DBusAuth.
 *
 * @{
 */

45 46 47
/* this is slightly different from the other append_quoted_string
 * in dbus-message-builder.c
 */
48 49 50 51 52
static dbus_bool_t
append_quoted_string (DBusString       *dest,
                      const DBusString *quoted)
{
  dbus_bool_t in_quotes = FALSE;
53
  dbus_bool_t in_backslash = FALSE;
54 55 56 57 58 59 60 61
  int i;

  i = 0;
  while (i < _dbus_string_get_length (quoted))
    {
      unsigned char b;

      b = _dbus_string_get_byte (quoted, i);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

      if (in_backslash)
        {
          unsigned char a;
          
          if (b == 'r')
            a = '\r';
          else if (b == 'n')
            a = '\n';
          else if (b == '\\')
            a = '\\';
          else
            {
              _dbus_warn ("bad backslashed byte %c\n", b);
              return FALSE;
            }

          if (!_dbus_string_append_byte (dest, a))
            return FALSE;
          
          in_backslash = FALSE;
        }
      else if (b == '\\')
        {
          in_backslash = TRUE;
        }
      else if (in_quotes)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 167 168 169 170 171 172
        {
          if (b == '\'')
            in_quotes = FALSE;
          else
            {
              if (!_dbus_string_append_byte (dest, b))
                return FALSE;
            }
        }
      else
        {
          if (b == '\'')
            in_quotes = TRUE;
          else if (b == ' ' || b == '\n' || b == '\t')
            break; /* end on whitespace if not quoted */
          else
            {
              if (!_dbus_string_append_byte (dest, b))
                return FALSE;
            }
        }
      
      ++i;
    }

  return TRUE;
}

static dbus_bool_t
same_first_word (const DBusString *a,
                 const DBusString *b)
{
  int first_a_blank, first_b_blank;

  _dbus_string_find_blank (a, 0, &first_a_blank);
  _dbus_string_find_blank (b, 0, &first_b_blank);

  if (first_a_blank != first_b_blank)
    return FALSE;

  return _dbus_string_equal_len (a, b, first_a_blank);
}

static DBusAuthState
auth_state_from_string (const DBusString *str)
{ 
  if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
    return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
  else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
    return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
  else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
    return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
  else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
    return DBUS_AUTH_STATE_NEED_DISCONNECT;
  else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED_WITH_UNUSED_BYTES"))
    return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
  else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
    return DBUS_AUTH_STATE_AUTHENTICATED;
  else
    return -1;
}

static const char*
auth_state_to_string (DBusAuthState state)
{
  switch (state)
    {
    case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
      return "WAITING_FOR_INPUT";
    case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
      return "WAITING_FOR_MEMORY";
    case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
      return "HAVE_BYTES_TO_SEND";
    case DBUS_AUTH_STATE_NEED_DISCONNECT:
      return "NEED_DISCONNECT";
    case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
      return "AUTHENTICATED_WITH_UNUSED_BYTES";
    case DBUS_AUTH_STATE_AUTHENTICATED:
      return "AUTHENTICATED";
    }

  return "unknown";
}

173 174 175 176 177 178 179 180 181 182
/**
 * Runs an "auth script" which is a script for testing the
 * authentication protocol. Scripts send and receive data, and then
 * include assertions about the state of both ends of the connection
 * after processing the data. A script succeeds if these assertions
 * hold.
 *
 * @param filename the file containing the script to run
 * @returns #TRUE if the script succeeds, #FALSE otherwise
 */
183 184 185 186
dbus_bool_t
_dbus_auth_script_run (const DBusString *filename)
{
  DBusString file;
187
  DBusError error;
188 189 190 191 192 193
  DBusString line;
  dbus_bool_t retval;
  int line_no;
  DBusAuth *auth;
  DBusString from_auth;
  DBusAuthState state;
194
  DBusString context;
195 196 197
  
  retval = FALSE;
  auth = NULL;
198 199

  _dbus_string_init_const (&context, "org_freedesktop_test");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  
  if (!_dbus_string_init (&file, _DBUS_INT_MAX))
    return FALSE;

  if (!_dbus_string_init (&line, _DBUS_INT_MAX))
    {
      _dbus_string_free (&file);
      return FALSE;
    }

  if (!_dbus_string_init (&from_auth, _DBUS_INT_MAX))
    {
      _dbus_string_free (&file);
      _dbus_string_free (&line);
      return FALSE;
    }
216 217 218

  dbus_error_init (&error);
  if (!_dbus_file_get_contents (&file, filename, &error))    {
219 220 221
      const char *s;
      _dbus_string_get_const_data (filename, &s);
      _dbus_warn ("Getting contents of %s failed: %s\n",
222 223
                  s, error.message);
      dbus_error_free (&error);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
      goto out;
    }

  state = DBUS_AUTH_STATE_NEED_DISCONNECT;
  line_no = 0;
 next_iteration:
  while (_dbus_string_pop_line (&file, &line))
    {      
      line_no += 1;

      _dbus_string_delete_leading_blanks (&line);

      if (auth != NULL)
        {
          while ((state = _dbus_auth_do_work (auth)) ==
                 DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
            {
              const DBusString *tmp;
              if (_dbus_auth_get_bytes_to_send (auth, &tmp))
                {
                  int count = _dbus_string_get_length (tmp);

                  if (_dbus_string_copy (tmp, 0, &from_auth,
                                         _dbus_string_get_length (&from_auth)))
                    _dbus_auth_bytes_sent (auth, count);
                }
            }
        }
      
      if (_dbus_string_get_length (&line) == 0)
        {
          /* empty line */
          goto next_iteration;
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "#"))
        {
          /* Ignore this comment */
          goto next_iteration;
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "CLIENT"))
        {
267 268
          DBusCredentials creds;
          
269 270 271 272 273 274 275 276 277 278 279 280
          if (auth != NULL)
            {
              _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
              goto out;
            }

          auth = _dbus_auth_client_new ();
          if (auth == NULL)
            {
              _dbus_warn ("no memory to create DBusAuth\n");
              goto out;
            }
281 282 283

          _dbus_credentials_from_current_process (&creds);
          _dbus_auth_set_credentials (auth, &creds);
284 285 286 287
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "SERVER"))
        {
288 289
          DBusCredentials creds;
          
290 291 292 293 294 295 296 297 298 299 300 301
          if (auth != NULL)
            {
              _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
              goto out;
            }

          auth = _dbus_auth_server_new ();
          if (auth == NULL)
            {
              _dbus_warn ("no memory to create DBusAuth\n");
              goto out;
            }
302 303 304

          _dbus_credentials_from_current_process (&creds);
          _dbus_auth_set_credentials (auth, &creds);
305
          _dbus_auth_set_context (auth, &context);
306 307 308 309 310 311 312
        }
      else if (auth == NULL)
        {
          _dbus_warn ("must specify CLIENT or SERVER\n");
          goto out;

        }
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      else if (_dbus_string_starts_with_c_str (&line,
                                               "NO_CREDENTIALS"))
        {
          DBusCredentials creds = { -1, -1, -1 };
          _dbus_auth_set_credentials (auth, &creds);
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "ROOT_CREDENTIALS"))
        {
          DBusCredentials creds = { -1, 0, 0 };
          _dbus_auth_set_credentials (auth, &creds);          
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "SILLY_CREDENTIALS"))
        {
          DBusCredentials creds = { -1, 4312, 1232 };
          _dbus_auth_set_credentials (auth, &creds);          
        }
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      else if (_dbus_string_starts_with_c_str (&line,
                                               "SEND"))
        {
          DBusString to_send;
          
          _dbus_string_delete_first_word (&line);

          if (!_dbus_string_init (&to_send, _DBUS_INT_MAX))
            {
              _dbus_warn ("no memory to allocate string\n");
              goto out;
            }

          if (!append_quoted_string (&to_send, &line))
            {
              _dbus_warn ("failed to append quoted string line %d\n",
                          line_no);
              _dbus_string_free (&to_send);
              goto out;
            }

352 353 354 355 356 357 358 359 360 361 362 363 364
          {
            const char *s4;
            _dbus_string_get_const_data (&to_send, &s4);
            _dbus_verbose ("Sending '%s'\n", s4);
          }
          
          if (!_dbus_string_append (&to_send, "\r\n"))
            {
              _dbus_warn ("failed to append \r\n from line %d\n",
                          line_no);
              _dbus_string_free (&to_send);
              goto out;
            }
365

366
          /* Replace USERID_BASE64 with our username in base64 */
367 368 369 370
          {
            int where;
            
            if (_dbus_string_find (&to_send, 0,
371
                                   "USERID_BASE64", &where))
372 373 374 375 376
              {
                DBusString username;

                if (!_dbus_string_init (&username, _DBUS_INT_MAX))
                  {
377
                    _dbus_warn ("no memory for userid\n");
378 379 380 381 382
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                if (!_dbus_string_append_our_uid (&username))
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
                  {
                    _dbus_warn ("no memory for userid\n");
                    _dbus_string_free (&username);
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                _dbus_string_delete (&to_send, where, strlen ("USERID_BASE64"));
                
                if (!_dbus_string_base64_encode (&username, 0,
                                                 &to_send, where))
                  {
                    _dbus_warn ("no memory to subst USERID_BASE64\n");
                    _dbus_string_free (&username);
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                _dbus_string_free (&username);
              }
            else if (_dbus_string_find (&to_send, 0,
                                        "USERNAME_BASE64", &where))
              {
                DBusString username;
                const DBusString *u;
                
                if (!_dbus_string_init (&username, _DBUS_INT_MAX))
                  {
                    _dbus_warn ("no memory for username\n");
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                if (!_dbus_user_info_from_current_process (&u, NULL, NULL) ||
                    !_dbus_string_copy (u, 0, &username,
                                        _dbus_string_get_length (&username)))
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
                  {
                    _dbus_warn ("no memory for username\n");
                    _dbus_string_free (&username);
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                _dbus_string_delete (&to_send, where, strlen ("USERNAME_BASE64"));
                
                if (!_dbus_string_base64_encode (&username, 0,
                                                 &to_send, where))
                  {
                    _dbus_warn ("no memory to subst USERNAME_BASE64\n");
                    _dbus_string_free (&username);
                    _dbus_string_free (&to_send);
                    goto out;
                  }

                _dbus_string_free (&username);
              }
          }
440

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
          {
            DBusString *buffer;

            _dbus_auth_get_buffer (auth, &buffer);
            if (!_dbus_string_copy (&to_send, 0,
                                    buffer, _dbus_string_get_length (buffer)))
              {
                _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
                _dbus_string_free (&to_send);
                _dbus_auth_return_buffer (auth, buffer, 0);
                goto out;
              }

            _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
          }
          
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
          _dbus_string_free (&to_send);
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "EXPECT_STATE"))
        {
          DBusAuthState expected;
          
          _dbus_string_delete_first_word (&line);

          expected = auth_state_from_string (&line);
          if (expected < 0)
            {
              _dbus_warn ("bad auth state given to EXPECT_STATE\n");
              goto parse_failed;
            }

          if (expected != state)
            {
              _dbus_warn ("expected auth state %s but got %s on line %d\n",
                          auth_state_to_string (expected),
                          auth_state_to_string (state),
                          line_no);
              goto out;
            }
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "EXPECT_COMMAND"))
        {
          DBusString received;
          
          _dbus_string_delete_first_word (&line);

          if (!_dbus_string_init (&received, _DBUS_INT_MAX))
            {
              _dbus_warn ("no mem to allocate string received\n");
              goto out;
            }

          if (!_dbus_string_pop_line (&from_auth, &received))
            {
              const char *command;
              _dbus_string_get_const_data (&line, &command);
              _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
                          command, line_no);
              _dbus_string_free (&received);
              goto out;
            }

          if (!same_first_word (&received, &line))
            {
              const char *s1, *s2;
              _dbus_string_get_const_data (&line, &s1);
              _dbus_string_get_const_data (&received, &s2);
510 511
              _dbus_warn ("line %d expected command '%s' and got '%s'\n",
                          line_no, s1, s2);
512 513 514 515 516 517
              _dbus_string_free (&received);
              goto out;
            }
          
          _dbus_string_free (&received);
        }
518 519 520 521
      else if (_dbus_string_starts_with_c_str (&line,
                                               "EXPECT_UNUSED"))
        {
          DBusString expected;
522
          const DBusString *unused;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
          
          _dbus_string_delete_first_word (&line);

          if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
            {
              _dbus_warn ("no mem to allocate string expected\n");
              goto out;
            }

          if (!append_quoted_string (&expected, &line))
            {
              _dbus_warn ("failed to append quoted string line %d\n",
                          line_no);
              _dbus_string_free (&expected);
              goto out;
            }

540
          _dbus_auth_get_unused_bytes (auth, &unused);
541
          
542
          if (_dbus_string_equal (&expected, unused))
543 544 545 546 547 548 549
            {
              _dbus_string_free (&expected);
            }
          else
            {
              const char *e1, *h1;
              _dbus_string_get_const_data (&expected, &e1);
550
              _dbus_string_get_const_data (unused, &h1);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
              _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
                          e1, h1);
              _dbus_string_free (&expected);
              goto out;
            }
        }
      else if (_dbus_string_starts_with_c_str (&line,
                                               "EXPECT"))
        {
          DBusString expected;
          
          _dbus_string_delete_first_word (&line);

          if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
            {
              _dbus_warn ("no mem to allocate string expected\n");
              goto out;
            }

          if (!append_quoted_string (&expected, &line))
            {
              _dbus_warn ("failed to append quoted string line %d\n",
                          line_no);
              _dbus_string_free (&expected);
              goto out;
            }

          if (_dbus_string_equal_len (&expected, &from_auth,
                                      _dbus_string_get_length (&expected)))
            {
              _dbus_string_delete (&from_auth, 0,
                                   _dbus_string_get_length (&expected));
              _dbus_string_free (&expected);
            }
          else
            {
              const char *e1, *h1;
              _dbus_string_get_const_data (&expected, &e1);
              _dbus_string_get_const_data (&from_auth, &h1);
              _dbus_warn ("Expected exact string '%s' and have '%s'\n",
                          e1, h1);
              _dbus_string_free (&expected);
              goto out;
            }
        }
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
      else
        goto parse_failed;

      goto next_iteration; /* skip parse_failed */
      
    parse_failed:
      {
        const char *s;
        _dbus_string_get_const_data (&line, &s);
        _dbus_warn ("couldn't process line %d \"%s\"\n",
                    line_no, s);
        goto out;
      }
    }

  if (auth != NULL &&
      state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
    {
      _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
      goto out;
    }

  if (_dbus_string_get_length (&from_auth) > 0)
    {
      const char *s;
      _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
      _dbus_string_get_const_data (&from_auth, &s);
      _dbus_warn ("Leftover data: %s\n", s);
      goto out;
    }
  
  retval = TRUE;
  
 out:
  if (auth)
    _dbus_auth_unref (auth);

  _dbus_string_free (&file);
634
  _dbus_string_free (&line);
635 636 637 638 639 640 641
  _dbus_string_free (&from_auth);
  
  return retval;
}

/** @} */
#endif /* DBUS_BUILD_TESTS */