Agent-TCLI-Package-Net

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

	POE::Component::Client::HTTP
	POE::Component::Server::SimpleHTTP
	FormValidator::Simple::Plugin::NetAddr::IP
	Email::MIME::Creator
	File::Slurp

USING

Passive Agents:
One creates a script, see agent_net.pl, that loads up a TCLI transport with
users, packages and other pertinent information. The agent will log in, join
chatrooms if in the user list, and wait for further commands from authorized
users or in a chatroom.

Test script:
A test script is written, see ex/probe.pl, that loads up a Test Transport,
other necessary transports, necessary local packages, and testees.

Using testees, one creates tests ala Test::More with the $testee->ok or
$testee->like_body and other Testee tests. These will run asynchronously
after the testing starts.

bin/agent_net.pl  view on Meta::CPAN

[B<verbose>]

=back

=head1 OPTIONS AND ARGUMENTS

=over 8

=item B<username>

The XMPP user the Agent will log in as, without the domain.
Required unless the script has been edited to enable a default user.

=item B<password>

The password to be used by the Agent to log in to the XMPP server.
Required unless the script has been edited to enable a default password.

=item B<domain>

The XMPP domain of the user account of the Agent.
Required unless the script has been edited to enable a default domain.

=item B<resource>

The XMPP resource. Defaults to 'tcli' if not provided.

bin/agent_net.pl  view on Meta::CPAN

pod2usage(VERBOSE => 2)  if ($opt->get_man);

# Hidden switch for dev testing
if ($opt->get_blib)
{
	use lib 'blib/lib';
}

$verbose = $opt->get_verbose ? $opt->get_verbose : VERBOSE;

# Optionally set default jabber/xmpp parameters to log in with
$username = $opt->get_username ? $opt->get_username : 'agent';
$password = $opt->get_password ? $opt->get_password : 'agent';
$resource = $opt->get_resource ? $opt->get_resource : 'tcli';
$domain = $opt->get_domain ? $opt->get_domain : 'example.com';
$host = $opt->get_host ? $opt->get_host : $domain;

# Error if options not set and not provided.
pod2usage(1) if ($username eq 'agent' or $domain eq 'example.com');

# Load required modules

ex/target.t  view on Meta::CPAN

		Param("username|u"),
		Param("password|p"),
		Param("resource|r"),
		Param("host"),
		Counter("verbose|v"),
	])};
if($@) {die "ERROR: $@";}

$verbose = $opt->get_verbose ? $opt->get_verbose : VERBOSE;

# xmpp username/password to log in with
$username = $opt->get_username ? $opt->get_username : 'username';
$password = $opt->get_password ? $opt->get_password : 'password';
$domain = $opt->get_domain ? $opt->get_domain : 'example.com';
$host = $opt->get_host ? $opt->get_host : $domain;
$resource = $opt->get_resource ? $opt->get_resource : 'test';

use POE;
use Agent::TCLI::Transport::Test;
use Agent::TCLI::Testee;
use Agent::TCLI::Transport::XMPP;

ex/target.t  view on Meta::CPAN

# Remote up?
$remote->ok('status');

# get remotes IP address
$remote->ok('Control show local_address');
my $target = $remote->get_param('local_address','',30);

#add a new response to the webserver
$remote->ok('httpd uri add regex=/test2.* response=OK200');

# start remote web server with logging
$remote->ok('httpd set logging');
$remote->ok('httpd spawn port=8080');

# make sure those completed before proceeding
$test_master->done;

# have local query target webserver.
$local->ok('http tget url=http://'.$target.':8080/test1.htm resp=404');
$local->ok('http tget url=http://'.$target.':8080/test2.htm resp=200');

# check to see if it's in the logs
$remote->ok('tail test add like=test1', 'passed test test1');
$remote->ok('tail test add like=test2', 'passed test test2');

# shut down httpd
$remote->ok('httpd stop port=8080');

# make sure to shut down the transport or else the script will not stop.
$local->ok('xmpp shutdown');

# Though tests will start during building of the tests, POE isn't fully running

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN


This module provides a package of commands for the TCLI environment. Currently
one must use the TCLI environment (or browse the source) to see documentation
for the commands it supports within the TCLI Agent.

This package starts a specialized HTTPD on the local system. It does not
return files but does return 404 or 200 values for user defined URLs. It can
also be set to completely ignore a request. URLs may be defined with
regular expressions.

It can also log directly to the log being monitored by the Tail command
in memory with no disk writes.

=head1 INTERFACE

This module must be loaded into a Agent::TCLI::Control by an
Agent::TCLI::Transport in order for a user to interface with it.

=cut

use warnings;

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN

  constraints:
    - ASCII
  help: A regular expression pattern
  manual: >
    The regex should be a string that can be evaluated inside a regular
    expression. It should not include the delimeters or be provided in
    qr() form.
  type: Param
---
Agent::TCLI::Parameter:
  name: logging
  help: Turn on logging to the Tail command line queue
  manual: >
    The logging switch turns on logging to the line queue monitored by the
    Tail command. This does not write to logs disk. One may then set up
    Tail tests for specific URI's to determine exactly which ones got through.
    To turn off, use no_logging.
    By default the line queue will only hold 10 lines so memory cannot
    accidentally be exhausted.
  type: Switch
---
Agent::TCLI::Parameter:
  name: response
  aliases: resp
  constraints:
    - ASCII
    -

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN

---
Agent::TCLI::Command:
  name: httpd
  call_style: session
  command: tcli_httpd
  contexts:
    ROOT: httpd
  handler: establish_context
  help: simple http web server
  manual: >
    httpd provides a simple web server that can respond to and log requests.
    By default it responds with a Status code 404 to all requests. One may add
    a select few other status code responses using regular expression pattern
    matching if desired. One cannot change content, only status codes.
    Httpd is useful for network testing situations where the response codes
    are being monitored by the network.
  topic: net
  usage: httpd spawn port=8080
---
Agent::TCLI::Command:
  name: spawn

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN

---
Agent::TCLI::Command:
  name: set
  call_style: session
  command: tcli_httpd
  contexts:
    httpd: set
  handler: settings
  help: adjust default settings
  parameters:
    logging:
    address:
    port:
    hostname:
    regex:
    response:
  topic: net
  usage: httpd set hostname=example.com
---
Agent::TCLI::Command:
  name: show
  call_style: session
  command: tcli_httpd
  contexts:
    httpd: show
  handler: show
  help: show tail default settings and state
  parameters:
    logging:
    address:
    port:
    hostname:
    regex:
    response:
    handlers:
    ports:
  topic: testing
  usage: httpd show settings
...

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN

			$i++;
		}
	}

	$request->Respond($kernel,$txt,$code);
}

=item BeGone

This POE Event handler is used as a target event for URIs. It simply drops the
connection. It will log the conenction if logging is turned on.

=cut

sub BeGone {
	# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object,
	# ARG2 = the DIR that matched
	my ($kernel, $self, $request, $response, $dirmatch ) =
	  @_[KERNEL, OBJECT, ARG0 .. ARG2 ];

	my $port = $response->connection->local_port;

	# Do our stuff to HTTP::Response
	$response->code( 0 );

	$kernel->call($self->name => 'Log' => $request, $response ) if $self->logging;

	$kernel->post( 'HTTPD'.$port  , 'CLOSE', $response );
}

=item OK200

This POE Event handler is used as a target event for URIs. It will
send an HTTP response code of 200 with the content 'OK'.
It will log the conenction if logging is turned on.

=cut

sub OK200 {
	# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object,
	# ARG2 = the DIR that matched
	my ($kernel, $self, $request, $response, $dirmatch ) =
	  @_[KERNEL, OBJECT,    ARG0,      ARG1,      ARG2 ];

	my $port = $response->connection->local_port;

	# Do our stuff to HTTP::Response
	$response->code( 200 );
	$response->content( 'OK' );

	$kernel->call( $self->name => 'Log' => $request, $response ) if $self->logging;

	$kernel->post('HTTPD'.$port, 'DONE', $response );
}

=item NA404

This POE Event handler is used as a target event for URIs. It will
send an HTTP response code of 404 with an error message.
It will log the conenction if logging is turned on.

=cut

sub NA404 {
	# ARG0 = HTTP::Request object, ARG1 = HTTP::Response object,
	# ARG2 = the DIR that matched
	my ($kernel, $self, $request, $response, $dirmatch ) =
	  @_[KERNEL, OBJECT, ARG0 .. ARG2 ];

	my $port = $response->connection->local_port;

lib/Agent/TCLI/Package/Net/HTTPD.pm  view on Meta::CPAN

	if ( ! defined $request ) {
		$_[KERNEL]->post( 'HTTPD'.$port, 'DONE', $response );
		return;
	}

	# Do our stuff to HTTP::Response
	$response->code( 404 );
	$response->content( "Hi visitor from " . $response->connection->remote_ip.
		", Page not found -> '" . $request->uri->path . "'" );

	$kernel->call($self->name => 'Log' => $request, $response ) if $self->logging;

	$kernel->post('HTTPD'.$port, 'DONE', $response );
}

=item Log

This POE Event handler is used internally to provide the logging. It sends
the time, remote ip:port, local ip:port, uri and optionally the SSL cipher
to the Tail session.

=back

=cut

sub Log {
	my ($kernel,  $self, $request, $response) =
	  @_[KERNEL, OBJECT, ARG0    , ARG1];

	$self->Verbose("Log: request(".$request->uri);
	my $port = $response->connection->local_port;

	my $log;
	# If the request was malformed, $request = undef
	if ( $request )
	{
		 $log = join (' ',
		 		time(),
		 		$response->connection->remote_ip.':'.$response->connection->remote_port,
		 		$response->connection->local_ip.':'.$port,
		 		$response->code,
		 		$request->uri,
		 		$response->connection->ssl ? $response->connection->sslcipher : '',
		 	)."\n";
	}
	else
	{
		 $log = join (' ',
		 		time(),
		 		$response->connection->remote_ip.':'.$response->connection->remote_port,
		 		$response->connection->local_ip.':'.$port,
		 		$response->code,
				'Bad request',
		 		$response->connection->ssl ? $response->connection->sslcipher : '',
			)."\n";
	}

	# In the future we'll need to resolve port to control to send to correct tail
	my $control = $self->GetWheelKey( $port, 'control');

	$kernel->post('tcli_tail', 'Append', $log );
	return;
}

1;
#__END__

=head3 INHERITED METHODS

This module is an Object::InsideOut object that inherits from Agent::TCLI::Package::Base. It
inherits methods from both. Please refer to their documentation for more

t/TCLI.Package.Net.HTTPD.t  view on Meta::CPAN

	])};
if($@) {die "ERROR: $@";}

$verbose = $opt->get_verbose ? $opt->get_verbose : VERBOSE;

if ( $opt->get_blib )
{
	use lib 'blib/lib';
}

# xmpp username/password to log in with
$poe_td = $opt->get_poe_debug;
$poe_te = $opt->get_poe_event;

sub POE::Kernel::TRACE_DEFAULT  () { $poe_td }
sub POE::Kernel::TRACE_EVENTS  () { $poe_te }

use POE;
use Agent::TCLI::Transport::Test;
use Agent::TCLI::Testee;
use Agent::TCLI::Package::Net::HTTP;

t/TCLI.Package.Net.HTTPD.t  view on Meta::CPAN

$t->ok('set response OK200');

$t->ok('uri add regex=foo');
$t->like_body('show handlers',qr(foo));
$t->ok('spawn' );
$t->ok('/http cget url=http://127.0.0.1:8080/foo.htm');
$t->ok('stop' );
$t->ok('uri delete regex=foo');
$t->unlike_body('show handlers',qr(foo),'foo gone');

$t->ok('set logging');
$t->like_body('show logging',qr(logging: 1) );
$t->ok('uri add regex=bar.*');
$t->like_body('show handlers',qr(bar.*));
$t->ok('spawn' );

$t->ok('/tail test add like 200.*?bar');
$t->ok('/http cget url=http://127.0.0.1:8080/bar.htm');
$t->ok('/tail test add like 404.*?foo');
$t->ok('/http cget url=http://127.0.0.1:8080/foo.htm');
$t->ok('/tail test add like 200.*?foobar');
$t->ok('/http cget url=http://127.0.0.1:8080/foobar.htm');

t/dev/target.t  view on Meta::CPAN

		Param("username|u"),
		Param("password|p"),
		Param("resource|r"),
		Param("host"),
		Counter("verbose|v"),
	])};
if($@) {die "ERROR: $@";}

$verbose = $opt->get_verbose ? $opt->get_verbose : VERBOSE;

# xmpp username/password to log in with
$username = $opt->get_username ? $opt->get_username : 'username';
$password = $opt->get_password ? $opt->get_password : 'password';
$domain = $opt->get_domain ? $opt->get_domain : 'example.com';
$host = $opt->get_host ? $opt->get_host : $domain;

use POE;
use Agent::TCLI::Transport::Test;
use Agent::TCLI::Testee;
use Agent::TCLI::Transport::XMPP;
use Agent::TCLI::Package::XMPP;

t/dev/target.t  view on Meta::CPAN

# Remote up?
$remote->ok('status');

# get remotes IP address
$remote->ok('Control show local_address');
my $target = $remote->get_param('local_address','',30);

#add a new response to the webserver
$remote->ok('httpd uri add regex=/test2.* response=OK200');

# start remote web server with logging
$remote->ok('httpd set logging');
$remote->ok('httpd spawn port=8080');

# make sure those completed before proceeding
$test_master->done;

# have local query target webserver.
$local->ok('http tget url=http://'.$target.':8080/test1.htm resp=404');
$local->ok('http tget url=http://'.$target.':8080/test2.htm resp=200');

# check to see if it's in the logs
$remote->ok('tail test add like=test1', 'passed test test1');
$remote->ok('tail test add like=test2', 'passed test test2');

# shut down httpd
$remote->ok('httpd stop port=8080');

# make sure to shut down the transport or else the script will not stop.
$local->ok('xmpp shutdown');

# Though tests will start during building of the tests, POE isn't fully running



( run in 0.638 second using v1.01-cache-2.11-cpan-0371d4a6215 )