perkeep/server/sigserver/test/CamsigdTest.pm

79 lines
1.7 KiB
Perl
Raw Normal View History

2010-11-15 03:27:41 +00:00
#!/usr/bin/perl
#
# Common test library for camsigd (sigserver)
2010-11-15 03:27:41 +00:00
package CamsigdTest;
use strict;
use Test::More;
use FindBin;
2010-11-15 04:05:20 +00:00
use LWP::UserAgent;
use HTTP::Request;
use Fcntl;
2010-11-15 03:27:41 +00:00
our $BINARY = "$FindBin::Bin/../sigserver";
2010-11-15 04:31:21 +00:00
sub start {
my ($port_rd, $port_wr, $exit_rd, $exit_wr);
my $flags;
pipe $port_rd, $port_wr;
pipe $exit_rd, $exit_wr;
$flags = fcntl($port_wr, F_GETFD, 0);
fcntl($port_wr, F_SETFD, $flags & ~FD_CLOEXEC);
$flags = fcntl($exit_rd, F_GETFD, 0);
fcntl($exit_rd, F_SETFD, $flags & ~FD_CLOEXEC);
$ENV{TESTING_PORT_WRITE_FD} = fileno($port_wr);
$ENV{TESTING_CONTROL_READ_FD} = fileno($exit_rd);
2010-11-15 05:29:46 +00:00
$ENV{CAMLI_PASSWORD} = "test";
die "Binary $BINARY doesn't exist\n" unless -x $BINARY;
2010-11-15 04:31:21 +00:00
2010-11-15 04:05:20 +00:00
my $pid = fork;
die "Failed to fork" unless defined($pid);
2010-11-15 04:31:21 +00:00
if ($pid == 0) {
2010-11-15 04:05:20 +00:00
# child
2010-11-29 15:54:20 +00:00
exec $BINARY, "-listen=:0";
2010-11-15 04:05:20 +00:00
die "failed to exec: $!\n";
}
close($exit_rd); # child owns this side
close($port_wr); # child owns this side
2010-11-15 05:29:46 +00:00
print "Waiting for server to start...\n";
my $line = <$port_rd>;
close($port_rd);
2010-11-29 15:54:20 +00:00
# Parse the port line out
chomp $line;
# print "Got port line: $line\n";
2010-11-29 15:54:20 +00:00
die "Failed to start, no port info." unless $line =~ /:(\d+)$/;
my $port = $1;
return CamsigdTest::Server->new($pid, $port, $exit_wr);
2010-11-15 03:27:41 +00:00
}
package CamsigdTest::Server;
sub new {
my ($class, $pid, $port, $pipe_writer) = @_;
return bless {
pid => $pid,
port => $port,
pipe_writer => $pipe_writer,
};
2010-11-15 03:27:41 +00:00
}
2010-11-15 04:05:20 +00:00
sub DESTROY {
my $self = shift;
my $pipe = $self->{pipe_writer};
syswrite($pipe, "EXIT\n", 5);
2010-11-15 04:05:20 +00:00
}
sub root {
2010-11-29 15:54:20 +00:00
my $self = shift;
return "http://localhost:$self->{port}";
2010-11-15 04:05:20 +00:00
}
2010-11-15 03:27:41 +00:00
1;