<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- perl -*-
#
# Copyright (C) 2004-2011 Daniel P. Berrange
#
# This program is free software; You can redistribute it and/or modify
# it under the same terms as Perl itself. Either:
#
# a) the GNU General Public License as published by the Free
#   Software Foundation; either version 2, or (at your option) any
#   later version,
#
# or
#
# b) the "Artistic License"
#
# The file "COPYING" distributed along with this file provides full
# details of the terms and conditions of the two licenses.

=pod

=head1 NAME

Net::DBus::Test::MockConnection - Fake a connection to the bus unit testing

=head1 SYNOPSIS

  use Net::DBus;

  my $bus = Net::DBus-&gt;test

  # Register a service, and the objec to be tested
  use MyObject
  my $service = $bus-&gt;export_service("org.example.MyService");
  my $object = MyObject-&gt;new($service);


  # Acquire the service &amp; do tests
  my $remote_service = $bus-&gt;get_service('org.example.MyService');
  my $remote_object = $service-&gt;get_object("/org/example/MyObjct");

  # This traverses the mock connection, eventually
  # invoking 'testSomething' on the $object above.
  $remote_object-&gt;testSomething()

=head1 DESCRIPTION

This object provides a fake implementation of the L&lt;Net::DBus::Binding::Connection&gt;
enabling a pure 'in-memory' message bus to be mocked up. This is intended to
facilitate creation of unit tests for services which would otherwise need to
call out to other object on a live message bus. It is used as a companion to
the L&lt;Net::DBus::Test::MockObject&gt; module which is how fake objects are to be
provided on the fake bus.

=head1 METHODS

=over 4

=cut

package Net::DBus::Test::MockConnection;

use strict;
use warnings;

use Net::DBus::Error;
use Net::DBus::Test::MockMessage;
use Net::DBus::Binding::Message::MethodCall;
use Net::DBus::Binding::Message::MethodReturn;
use Net::DBus::Binding::Message::Error;
use Net::DBus::Binding::Message::Signal;

=item my $con = Net::DBus::Test::MockConnection-&gt;new()

Create a new mock connection object instance. It is not usually
necessary to create instances of this object directly, instead
the C&lt;test&gt; method on the L&lt;Net::DBus&gt; object can be used to
get a handle to a test bus.

=cut

sub new {
    my $class = shift;
    my $self = {};

    $self-&gt;{replies} = [];
    $self-&gt;{signals} = [];
    $self-&gt;{objects} = {};
    $self-&gt;{objectTrees} = {};
    $self-&gt;{filters} = [];

    bless $self, $class;

    return $self;
}

=item $con-&gt;send($message)

Send a message over the mock connection. If the message is
a method call, it will be dispatched straight to any corresponding
mock object registered. If the message is an error or method return
it will be made available as a return value for the C&lt;send_with_reply_and_block&gt;
method. If the message is a signal it will be queued up for processing
by the C&lt;dispatch&gt; method.

=cut


sub send {
    my $self = shift;
    my $msg = shift;

    if ($msg-&gt;get_type() == &amp;Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_CALL) {
	$self-&gt;_call_method($msg);
    } elsif ($msg-&gt;get_type() == &amp;Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN ||
	     $msg-&gt;get_type() == &amp;Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
	push @{$self-&gt;{replies}}, $msg;
    } elsif ($msg-&gt;get_type() == &amp;Net::DBus::Binding::Message::MESSAGE_TYPE_SIGNAL) {
	push @{$self-&gt;{signals}}, $msg;
    } else {
	die "unhandled type of message " . ref($msg);
    }
}


=item $bus-&gt;request_name($service_name)

Pretend to send a request to the bus registering the well known
name specified in the C&lt;$service_name&gt; parameter. In reality
this is just a no-op giving the impression that the name was
successfully registered.

=cut

sub request_name {
    my $self = shift;
    my $name = shift;
    my $flags = shift;

    # XXX do we care about this for test cases? probably not...
    # ....famous last words
}

=item my $reply = $con-&gt;send_with_reply_and_block($msg)

Send a message over the mock connection and wait for a
reply. The C&lt;$msg&gt; should be an instance of C&lt;Net::DBus::Binding::Message::MethodCall&gt;
and the return C&lt;$reply&gt; will be an instance of C&lt;Net::DBus::Binding::Message::MethodReturn&gt;.
It is also possible that an error will be thrown, with
the thrown error being blessed into the C&lt;Net::DBus::Error&gt;
class.

=cut

sub send_with_reply_and_block {
    my $self = shift;
    my $msg = shift;
    my $timeout = shift;

    $self-&gt;send($msg);

    if ($#{$self-&gt;{replies}} == -1) {
	die "no reply for " . $msg-&gt;get_path . "-&gt;" . $msg-&gt;get_member . " received within timeout";
    }

    my $reply = shift @{$self-&gt;{replies}};
    if ($#{$self-&gt;{replies}} != -1) {
	die "too many replies received";
    }

    if ($reply-&gt;get_type() == &amp;Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
	my $iter = $reply-&gt;iterator;
	my $desc = $iter-&gt;get_string;
	die Net::DBus::Error-&gt;new(name =&gt; $reply-&gt;get_error_name,
				  message =&gt; $desc);
    }

    return $reply;
}

=item $con-&gt;dispatch;

Dispatches any pending messages in the incoming queue
to their message handlers. This method should be called
by test suites whenever they anticipate that there are
pending signals to be dealt with.

=cut

sub dispatch {
    my $self = shift;

    my @signals = @{$self-&gt;{signals}};
    $self-&gt;{signals} = [];
    foreach my $msg (@signals) {
	foreach my $cb (@{$self-&gt;{filters}}) {
	    # XXX we should worry about return value...
	    &amp;$cb($self, $msg);
	}
    }
}

=item $con-&gt;add_filter($coderef);

Adds a filter to the connection which will be invoked whenever a
message is received. The C&lt;$coderef&gt; should be a reference to a
subroutine, which returns a true value if the message should be
filtered out, or a false value if the normal message dispatch
should be performed.

=cut

sub add_filter {
    my $self = shift;
    my $cb = shift;

    push @{$self-&gt;{filters}}, $cb;
}

=item $bus-&gt;add_match($rule)

Register a signal match rule with the bus controller, allowing
matching broadcast signals to routed to this client. In reality
this is just a no-op giving the impression that the match was
successfully registered.

=cut

sub add_match {
    my $self = shift;
    my $rule = shift;

    # XXX do we need to implement anything ? probably not
    # nada
}

=item $bus-&gt;remove_match($rule)

Unregister a signal match rule with the bus controller, preventing
further broadcast signals being routed to this client. In reality
this is just a no-op giving the impression that the match was
successfully unregistered.

=cut

sub remove_match {
    my $self = shift;
    my $rule = shift;

    # XXX do we need to implement anything ? probably not
    # nada
}


=item $con-&gt;register_object_path($path, \&amp;handler)

Registers a handler for messages whose path matches
that specified in the C&lt;$path&gt; parameter. The supplied
code reference will be invoked with two parameters, the
connection object on which the message was received,
and the message to be processed (an instance of the
C&lt;Net::DBus::Binding::Message&gt; class).

=cut

sub register_object_path {
    my $self = shift;
    my $path = shift;
    my $code = shift;

    $self-&gt;{objects}-&gt;{$path} = $code;
}

=item $con-&gt;register_fallback($path, \&amp;handler)

Registers a handler for messages whose path starts with
the prefix specified in the C&lt;$path&gt; parameter. The supplied
code reference will be invoked with two parameters, the
connection object on which the message was received,
and the message to be processed (an instance of the
C&lt;Net::DBus::Binding::Message&gt; class).

=cut

sub register_fallback {
    my $self = shift;
    my $path = shift;
    my $code = shift;

    $self-&gt;{objects}-&gt;{$path} = $code;
    $self-&gt;{objectTrees}-&gt;{$path} = $code;
}

=item $con-&gt;unregister_object_path($path)

Unregisters the handler associated with the object path C&lt;$path&gt;. The
handler would previously have been registered with the C&lt;register_object_path&gt;
or C&lt;register_fallback&gt; methods.

=cut

sub unregister_object_path {
    my $self = shift;
    my $path = shift;

    delete $self-&gt;{objects}-&gt;{$path};
}

sub _call_method {
    my $self = shift;
    my $msg = shift;

    if (exists $self-&gt;{objects}-&gt;{$msg-&gt;get_path}) {
	my $cb = $self-&gt;{objects}-&gt;{$msg-&gt;get_path};
	&amp;$cb($self, $msg);
    } else {
	foreach my $path (reverse sort { $a cmp $b } keys %{$self-&gt;{objectTrees}}) {
	    if ((index $msg-&gt;get_path, $path) == 0) {
		my $cb = $self-&gt;{objects}-&gt;{$path};
		&amp;$cb($self, $msg);
		return;
	    }
	}
	if ($msg-&gt;get_path eq "/org/freedesktop/DBus") {
	    if ($msg-&gt;get_member eq "GetNameOwner") {
		my $reply = $self-&gt;make_method_return_message($msg);
		my $iter = $reply-&gt;iterator(1);
		$iter-&gt;append(":1.1");
		$self-&gt;send($reply);
	    }
	}
    }
}

=item my $msg = $con-&gt;make_error_message($replyto, $name, $description)

Creates a new message, representing an error which occurred during
the handling of the method call object passed in as the C&lt;$replyto&gt;
parameter. The C&lt;$name&gt; parameter is the formal name of the error
condition, while the C&lt;$description&gt; is a short piece of text giving
more specific information on the error.

=cut

sub make_error_message {
    my $self = shift;
    my $replyto = shift;
    my $name = shift;
    my $description = shift;

    if (1) {
	return Net::DBus::Test::MockMessage-&gt;new_error(replyto =&gt; $replyto,
						       error_name =&gt; $name,
						       error_description =&gt; $description);
    } else {
	return Net::DBus::Binding::Message::Error-&gt;new(replyto =&gt; $replyto,
						       name =&gt; $name,
						       description =&gt; $description);
    }
}

=item my $call = $con-&gt;make_method_call_message(
  $service_name, $object_path, $interface, $method_name);

Create a message representing a call on the object located at
the path C&lt;$object_path&gt; within the client owning the well-known
name given by C&lt;$service_name&gt;. The method to be invoked has
the name C&lt;$method_name&gt; within the interface specified by the
C&lt;$interface&gt; parameter.

=cut

sub make_method_call_message {
    my $self = shift;
    my $service_name = shift;
    my $object_path = shift;
    my $interface = shift;
    my $method_name = shift;

    if (1) {
	return Net::DBus::Test::MockMessage-&gt;new_method_call(destination =&gt; $service_name,
							     path =&gt; $object_path,
							     interface =&gt; $interface,
							     member =&gt; $method_name);
    } else {
	return Net::DBus::Binding::Message::MethodCall-&gt;new(service_name =&gt; $service_name,
							    object_path =&gt; $object_path,
							    interface =&gt; $interface,
							    method_name =&gt; $method_name);
    }
}

=item my $msg = $con-&gt;make_method_return_message($replyto)

Create a message representing a reply to the method call message passed in
the C&lt;$replyto&gt; parameter.

=cut


sub make_method_return_message {
    my $self = shift;
    my $replyto = shift;

    if (1) {
	return Net::DBus::Test::MockMessage-&gt;new_method_return(replyto =&gt; $replyto);
    } else {
	return Net::DBus::Binding::Message::MethodReturn-&gt;new(call =&gt; $replyto);
    }
}


=item my $msg = $con-&gt;make_signal_message($object_path, $interface, $signal_name);

Creates a new message, representing a signal [to be] emitted by
the object located under the path given by the C&lt;$object_path&gt;
parameter. The name of the signal is given by the C&lt;$signal_name&gt;
parameter, and is scoped to the interface given by the
C&lt;$interface&gt; parameter.

=cut

sub make_signal_message {
    my $self = shift;
    my $object_path = shift;
    my $interface = shift;
    my $signal_name = shift;

    if (1) {
	return Net::DBus::Test::MockMessage-&gt;new_signal(object_path =&gt; $object_path,
							interface =&gt; $interface,
							signal_name =&gt; $signal_name);
    } else {
	return Net::DBus::Binding::Message::Signal-&gt;new(object_path =&gt; $object_path,
							interface =&gt; $interface,
							signal_name =&gt; $signal_name);
    }
}


1;

=pod

=back

=head1 BUGS

It doesn't completely replicate the API of L&lt;Net::DBus::Binding::Connection&gt;,
merely enough to make the high level bindings work in a test scenario.

=head1 AUTHOR

Daniel P. Berrange

=head1 COPYRIGHT

Copyright (C) 2005-2009 Daniel P. Berrange

=head1 SEE ALSO

L&lt;Net::DBus&gt;, L&lt;Net::DBus::Test::MockObject&gt;, L&lt;Net::DBus::Binding::Connection&gt;,
L&lt;http://www.mockobjects.com/Faq.html&gt;

=cut
</pre></body></html>