diff --git a/t/03-packet.t b/t/03-packet.t index 1cea07e..f1ddbc7 100644 --- a/t/03-packet.t +++ b/t/03-packet.t @@ -30,6 +30,15 @@ my $fh = do { local *FH; *FH }; tie $fh, 'StringThing'; $ssh->{session}{sock} = $fh; +sub alarmhack (&) { + my $cb = shift; + local $SIG{ALRM} = sub { }; + alarm 1; + my $res = $cb->(); + alarm 0; + $res; +} + { ## Test basic functionality: send a packet with a string... my $packet = Net::SSH::Perl::Packet->new( $ssh, type => SSH_CMSG_USER ); @@ -40,7 +49,7 @@ $ssh->{session}{sock} = $fh; { ## ... And read it back. - my $packet = Net::SSH::Perl::Packet->read($ssh); + my $packet = alarmhack { Net::SSH::Perl::Packet->read($ssh) }; ok( $packet, 'read a packet back' ); is( $packet->type, SSH_CMSG_USER, 'packet type is SSH_CMSG_USER' ); is( $packet->get_str, "foo", 'get_str returns "foo"' ); @@ -50,7 +59,7 @@ $ssh->{session}{sock} = $fh; ## Test read_expect. Send a SUCCESS message, expect a FAILURE ## message. This should croak. Net::SSH::Perl::Packet->new( $ssh, type => SSH_SMSG_SUCCESS )->send; - eval { my $packet = Net::SSH::Perl::Packet->read_expect($ssh, SSH_SMSG_FAILURE) }; + eval { my $packet = alarmhack { Net::SSH::Perl::Packet->read_expect($ssh, SSH_SMSG_FAILURE) }; }; ok( $@, 'sending success and expecting a failure message croaks' ); my $expected = sprintf "type %s, got %s", SSH_SMSG_FAILURE, SSH_SMSG_SUCCESS; @@ -62,7 +71,7 @@ $ssh->{session}{sock} = $fh; ## disconnect message. It also dropped the session socket, so we ## need to reinstate it. $ssh->{session}{sock} = $fh; - eval { Net::SSH::Perl::Packet->read($ssh) }; + eval { alarmhack { Net::SSH::Perl::Packet->read($ssh) }; }; ok( $@, 'read fails after disconnect' ); like( $@, qr/^Received disconnect.+Protocol error/, 'error message on read after disconnect' ); @@ -79,11 +88,11 @@ $ssh->{session}{sock} = $fh; ## buffer: *both* packets. The internal leftover buffer should be ## split up based on the packet lengths. First read reads entire ## buffer, grabs first packet... - my $packet = Net::SSH::Perl::Packet->read($ssh); + my $packet = alarmhack { Net::SSH::Perl::Packet->read($ssh) }; is( $packet->type, SSH_SMSG_FAILURE, 'packet type is SSH_SMSG_FAILURE' ); ## ... Second read grabs leftover buffer, grabs second packet. - $packet = Net::SSH::Perl::Packet->read($ssh); + $packet = alarmhack { Net::SSH::Perl::Packet->read($ssh) }; is( $packet->type, SSH_CMSG_EOF, 'second packet type is SSH_CMSG_EOF' ); }