github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/server/sigserver/test/CamsigdTest.pm (about) 1 #!/usr/bin/perl 2 # 3 # Common test library for camsigd (sigserver) 4 5 package CamsigdTest; 6 7 use strict; 8 use Test::More; 9 use FindBin; 10 use LWP::UserAgent; 11 use HTTP::Request; 12 use Fcntl; 13 14 our $BINARY = "$FindBin::Bin/../sigserver"; 15 16 sub start { 17 my ($port_rd, $port_wr, $exit_rd, $exit_wr); 18 my $flags; 19 pipe $port_rd, $port_wr; 20 pipe $exit_rd, $exit_wr; 21 22 $flags = fcntl($port_wr, F_GETFD, 0); 23 fcntl($port_wr, F_SETFD, $flags & ~FD_CLOEXEC); 24 $flags = fcntl($exit_rd, F_GETFD, 0); 25 fcntl($exit_rd, F_SETFD, $flags & ~FD_CLOEXEC); 26 27 $ENV{TESTING_PORT_WRITE_FD} = fileno($port_wr); 28 $ENV{TESTING_CONTROL_READ_FD} = fileno($exit_rd); 29 $ENV{CAMLI_PASSWORD} = "test"; 30 31 die "Binary $BINARY doesn't exist\n" unless -x $BINARY; 32 33 my $pid = fork; 34 die "Failed to fork" unless defined($pid); 35 if ($pid == 0) { 36 # child 37 exec $BINARY, "-listen=:0"; 38 die "failed to exec: $!\n"; 39 } 40 close($exit_rd); # child owns this side 41 close($port_wr); # child owns this side 42 43 print "Waiting for server to start...\n"; 44 my $line = <$port_rd>; 45 close($port_rd); 46 47 # Parse the port line out 48 chomp $line; 49 # print "Got port line: $line\n"; 50 die "Failed to start, no port info." unless $line =~ /:(\d+)$/; 51 my $port = $1; 52 53 return CamsigdTest::Server->new($pid, $port, $exit_wr); 54 } 55 56 package CamsigdTest::Server; 57 58 sub new { 59 my ($class, $pid, $port, $pipe_writer) = @_; 60 return bless { 61 pid => $pid, 62 port => $port, 63 pipe_writer => $pipe_writer, 64 }; 65 } 66 67 sub DESTROY { 68 my $self = shift; 69 my $pipe = $self->{pipe_writer}; 70 syswrite($pipe, "EXIT\n", 5); 71 } 72 73 sub root { 74 my $self = shift; 75 return "http://localhost:$self->{port}"; 76 } 77 78 1;