/* -*- mode: C; c-file-style: "gnu" -*- */
-/* dbus-auth-script.c Test DBusAuth using a special script file (internal to D-BUS implementation)
+/* 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
+ * Licensed under the Academic Free License version 2.1
*
* 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
#include "dbus-string.h"
#include "dbus-hash.h"
#include "dbus-internals.h"
-#include "dbus-marshal.h"
+#include "dbus-userdb.h"
/**
* @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
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 "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";
}
+static char **
+split_string (DBusString *str)
+{
+ int i, j, k, count, end;
+ char **array;
+
+ end = _dbus_string_get_length (str);
+
+ i = 0;
+ _dbus_string_skip_blank (str, i, &i);
+ for (count = 0; i < end; count++)
+ {
+ _dbus_string_find_blank (str, i, &i);
+ _dbus_string_skip_blank (str, i, &i);
+ }
+
+ array = dbus_new0 (char *, count + 1);
+ if (array == NULL)
+ return NULL;
+
+ i = 0;
+ _dbus_string_skip_blank (str, i, &i);
+ for (k = 0; k < count; k++)
+ {
+ _dbus_string_find_blank (str, i, &j);
+
+ array[k] = dbus_malloc (j - i + 1);
+ if (array[k] == NULL)
+ {
+ dbus_free_string_array (array);
+ return NULL;
+ }
+ memcpy (array[k],
+ _dbus_string_get_const_data_len (str, i, j - i), j - i);
+ array[k][j - i] = '\0';
+
+ _dbus_string_skip_blank (str, j, &i);
+ }
+ array[k] = NULL;
+
+ return array;
+}
+
/**
* Runs an "auth script" which is a script for testing the
* authentication protocol. Scripts send and receive data, and then
DBusString from_auth;
DBusAuthState state;
DBusString context;
+ DBusString guid;
retval = FALSE;
auth = NULL;
+ _dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
_dbus_string_init_const (&context, "org_freedesktop_test");
- if (!_dbus_string_init (&file, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&file))
return FALSE;
- if (!_dbus_string_init (&line, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&line))
{
_dbus_string_free (&file);
return FALSE;
}
- if (!_dbus_string_init (&from_auth, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&from_auth))
{
_dbus_string_free (&file);
_dbus_string_free (&line);
dbus_error_init (&error);
if (!_dbus_file_get_contents (&file, filename, &error)) {
- const char *s;
- _dbus_string_get_const_data (filename, &s);
_dbus_warn ("Getting contents of %s failed: %s\n",
- s, error.message);
+ _dbus_string_get_const_data (filename), error.message);
dbus_error_free (&error);
goto out;
}
goto out;
}
+ /* test ref/unref */
+ _dbus_auth_ref (auth);
+ _dbus_auth_unref (auth);
+
_dbus_credentials_from_current_process (&creds);
_dbus_auth_set_credentials (auth, &creds);
}
goto out;
}
- auth = _dbus_auth_server_new ();
+ auth = _dbus_auth_server_new (&guid);
if (auth == NULL)
{
_dbus_warn ("no memory to create DBusAuth\n");
goto out;
}
+ /* test ref/unref */
+ _dbus_auth_ref (auth);
+ _dbus_auth_unref (auth);
+
_dbus_credentials_from_current_process (&creds);
_dbus_auth_set_credentials (auth, &creds);
_dbus_auth_set_context (auth, &context);
_dbus_auth_set_credentials (auth, &creds);
}
else if (_dbus_string_starts_with_c_str (&line,
+ "ALLOWED_MECHS"))
+ {
+ char **mechs;
+
+ _dbus_string_delete_first_word (&line);
+ mechs = split_string (&line);
+ _dbus_auth_set_mechanisms (auth, (const char **) mechs);
+ dbus_free_string_array (mechs);
+ }
+ 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))
+ if (!_dbus_string_init (&to_send))
{
_dbus_warn ("no memory to allocate string\n");
goto out;
goto out;
}
- {
- const char *s4;
- _dbus_string_get_const_data (&to_send, &s4);
- _dbus_verbose ("Sending '%s'\n", s4);
- }
+ _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
if (!_dbus_string_append (&to_send, "\r\n"))
{
goto out;
}
- /* Replace USERID_BASE64 with our username in base64 */
+ /* Replace USERID_HEX with our username in hex */
{
int where;
if (_dbus_string_find (&to_send, 0,
- "USERID_BASE64", &where))
+ "USERID_HEX", &where))
{
DBusString username;
- if (!_dbus_string_init (&username, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&username))
{
_dbus_warn ("no memory for userid\n");
_dbus_string_free (&to_send);
goto out;
}
- if (!_dbus_string_append_our_uid (&username))
+ if (!_dbus_string_append_uint (&username,
+ _dbus_getuid ()))
{
_dbus_warn ("no memory for userid\n");
_dbus_string_free (&username);
goto out;
}
- _dbus_string_delete (&to_send, where, strlen ("USERID_BASE64"));
+ _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
- if (!_dbus_string_base64_encode (&username, 0,
- &to_send, where))
+ if (!_dbus_string_hex_encode (&username, 0,
+ &to_send, where))
{
- _dbus_warn ("no memory to subst USERID_BASE64\n");
+ _dbus_warn ("no memory to subst USERID_HEX\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))
+ "USERNAME_HEX", &where))
{
DBusString username;
const DBusString *u;
- if (!_dbus_string_init (&username, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&username))
{
_dbus_warn ("no memory for username\n");
_dbus_string_free (&to_send);
goto out;
}
- if (!_dbus_user_info_from_current_process (&u, NULL, NULL) ||
+ if (!_dbus_username_from_current_process (&u) ||
!_dbus_string_copy (u, 0, &username,
_dbus_string_get_length (&username)))
{
goto out;
}
- _dbus_string_delete (&to_send, where, strlen ("USERNAME_BASE64"));
+ _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
- if (!_dbus_string_base64_encode (&username, 0,
- &to_send, where))
+ if (!_dbus_string_hex_encode (&username, 0,
+ &to_send, where))
{
- _dbus_warn ("no memory to subst USERNAME_BASE64\n");
+ _dbus_warn ("no memory to subst USERNAME_HEX\n");
_dbus_string_free (&username);
_dbus_string_free (&to_send);
goto out;
_dbus_string_free (&username);
}
}
-
- if (!_dbus_auth_bytes_received (auth, &to_send))
- {
- _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);
- goto out;
- }
+ {
+ 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));
+ }
+
_dbus_string_free (&to_send);
}
else if (_dbus_string_starts_with_c_str (&line,
_dbus_string_delete_first_word (&line);
- if (!_dbus_string_init (&received, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&received))
{
_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_get_const_data (&line), 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);
_dbus_warn ("line %d expected command '%s' and got '%s'\n",
- line_no, s1, s2);
+ line_no,
+ _dbus_string_get_const_data (&line),
+ _dbus_string_get_const_data (&received));
_dbus_string_free (&received);
goto out;
}
"EXPECT_UNUSED"))
{
DBusString expected;
- DBusString unused;
+ const DBusString *unused;
_dbus_string_delete_first_word (&line);
- if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&expected))
{
_dbus_warn ("no mem to allocate string expected\n");
goto out;
goto out;
}
- if (!_dbus_string_init (&unused, _DBUS_INT_MAX))
- {
- _dbus_warn ("no mem to allocate string unused\n");
- _dbus_string_free (&expected);
- goto out;
- }
-
- if (!_dbus_auth_get_unused_bytes (auth, &unused))
- {
- _dbus_warn ("couldn't get unused bytes\n");
- _dbus_string_free (&expected);
- _dbus_string_free (&unused);
- goto out;
- }
+ _dbus_auth_get_unused_bytes (auth, &unused);
- if (_dbus_string_equal (&expected, &unused))
+ if (_dbus_string_equal (&expected, unused))
{
+ _dbus_auth_delete_unused_bytes (auth);
_dbus_string_free (&expected);
- _dbus_string_free (&unused);
}
else
{
- const char *e1, *h1;
- _dbus_string_get_const_data (&expected, &e1);
- _dbus_string_get_const_data (&unused, &h1);
_dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
- e1, h1);
+ _dbus_string_get_const_data (&expected),
+ _dbus_string_get_const_data (unused));
_dbus_string_free (&expected);
- _dbus_string_free (&unused);
goto out;
}
}
_dbus_string_delete_first_word (&line);
- if (!_dbus_string_init (&expected, _DBUS_INT_MAX))
+ if (!_dbus_string_init (&expected))
{
_dbus_warn ("no mem to allocate string expected\n");
goto out;
}
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_get_const_data (&expected),
+ _dbus_string_get_const_data (&from_auth));
_dbus_string_free (&expected);
goto out;
}
parse_failed:
{
- const char *s;
- _dbus_string_get_const_data (&line, &s);
_dbus_warn ("couldn't process line %d \"%s\"\n",
- line_no, s);
+ line_no, _dbus_string_get_const_data (&line));
goto out;
}
}
if (auth != NULL &&
- state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
+ state == DBUS_AUTH_STATE_AUTHENTICATED)
{
- _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
- goto out;
+ const DBusString *unused;
+
+ _dbus_auth_get_unused_bytes (auth, &unused);
+
+ if (_dbus_string_get_length (unused) > 0)
+ {
+ _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);
+ _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
goto out;
}