about summary refs log tree commit
diff options
context:
space:
mode:
authorSteve Hay <steve.m.hay@googlemail.com>2013-08-06 13:19:31 +0100
committerSteve Hay <steve.m.hay@googlemail.com>2013-08-06 13:19:31 +0100
commitc211f4dbe0f55dea6e321ae29c08361ecfe39286 (patch)
tree023fd073fa2f5a98619e44156eb6791fdbd95be9
parentc241f063e5795f05d95e4885a89f2c7891b7eda9 (diff)
downloadperl-libnet-c211f4dbe0f55dea6e321ae29c08361ecfe39286.tar.gz
Sync test suite with bleadperl
Apply patch from CPAN RT #87469.
-rw-r--r--t/hostname.t5
-rw-r--r--t/time.t133
2 files changed, 137 insertions, 1 deletions
diff --git a/t/hostname.t b/t/hostname.t
index 758d304..4013d74 100644
--- a/t/hostname.t
+++ b/t/hostname.t
@@ -26,7 +26,10 @@ print "1..5\n";
 $domain = domainname();
 
 if(defined $domain && $domain ne "") {
- print "ok 1\n";
+ print "ok 1 - defined, non-empty domainname\n";
+}
+elsif (not defined $domain) {
+ print "ok 1 # SKIP domain not fully defined\n";
 }
 else {
  print "not ok 1\n";
diff --git a/t/time.t b/t/time.t
new file mode 100644
index 0000000..a8d416e
--- /dev/null
+++ b/t/time.t
@@ -0,0 +1,133 @@
+#!./perl -w
+
+BEGIN {
+    if ($ENV{PERL_CORE}) {
+        chdir 't' if -d 't';
+        @INC = '../lib';
+    }
+    if (!eval "require Socket") {
+        print "1..0 # no Socket\n"; exit 0;
+    }
+    if (ord('A') == 193 && !eval "require Convert::EBCDIC") {
+        print "1..0 # EBCDIC but no Convert::EBCDIC\n"; exit 0;
+    }
+    $INC{'IO/Socket.pm'} = 1;
+    $INC{'IO/Select.pm'} = 1;
+    $INC{'IO/Socket/INET.pm'} = 1;
+}
+
+(my $libnet_t = __FILE__) =~ s/time.t/libnet_t.pl/;
+require $libnet_t;
+
+print "1..12\n";
+# cannot use(), otherwise it will use IO::Socket and IO::Select
+eval{ require Net::Time; };
+ok( !$@, 'should be able to require() Net::Time safely' );
+ok( exists $INC{'Net/Time.pm'}, 'should be able to use Net::Time' );
+
+# force the socket to fail
+make_fail('IO::Socket::INET', 'new');
+my $badsock = Net::Time::_socket('foo', 1, 'bar', 'baz');
+is( $badsock, undef, '_socket() should fail if Socket creation fails' );
+
+# if socket is created with protocol UDP (default), it will send a newline
+my $sock = Net::Time::_socket('foo', 2, 'bar');
+ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
+is( $sock->{sent}, "\n", 'should send \n with UDP protocol set' );
+is( $sock->{timeout}, 120, 'timeout should default to 120' );
+
+# now try it with a custom timeout and a different protocol
+$sock = Net::Time::_socket('foo', 3, 'bar', 'tcp', 11);
+ok( $sock->isa('IO::Socket::INET'), 'should be an IO::Socket::INET object' );
+is( $sock->{sent}, undef, '_socket() should send nothing unless UDP protocol' );
+is( $sock->{PeerAddr}, 'bar', '_socket() should set PeerAddr in socket' );
+is( $sock->{timeout}, 11, '_socket() should respect custom timeout value' );
+
+# inet_daytime
+# check for correct args (daytime, 13)
+IO::Socket::INET::set_message('z');
+is( Net::Time::inet_daytime('bob'), 'z', 'inet_daytime() should receive data' );
+
+# magic numbers defined in Net::Time
+my $offset = $^O eq 'MacOS' ?
+        (4 * 31536000) : (70 * 31536000 + 17 * 86400);
+
+# check for correct args (time, 13)
+# pretend it is only six seconds since the offset, create a fake message
+# inet_time
+IO::Socket::INET::set_message(pack("N", $offset + 6));
+is( Net::Time::inet_time('foo'), 6,
+        'inet_time() should calculate time since offset for time()' );
+
+
+my %fail;
+
+sub make_fail {
+        my ($pack, $func, $num) = @_;
+        $num = 1 unless defined $num;
+
+        $fail{$pack}{$func} = $num;
+}
+
+package IO::Socket::INET;
+
+$fail{'IO::Socket::INET'} = {
+        new                => 0,
+        'send'        => 0,
+};
+
+sub new {
+        my $class = shift;
+        return if $fail{$class}{new} and $fail{$class}{new}--;
+        bless( { @_ }, $class );
+}
+
+sub send {
+        my $self = shift;
+        my $class = ref($self);
+        return if $fail{$class}{'send'} and $fail{$class}{'send'}--;
+        $self->{sent} .= shift;
+}
+
+my $msg;
+sub set_message {
+        if (ref($_[0])) {
+                $_[0]->{msg} = $_[1];
+        } else {
+                $msg = shift;
+        }
+}
+
+sub do_recv  {
+        my ($len, $msg) = @_[1,2];
+        $_[0] .= substr($msg, 0, $len);
+}
+
+sub recv {
+        my ($self, $buf, $length, $flags) = @_;
+        my $message = exists $self->{msg} ?
+                $self->{msg} : $msg;
+
+        if (defined($message)) {
+                do_recv($_[1], $length, $message);
+        }
+        1;
+}
+
+package IO::Select;
+
+sub new {
+        my $class = shift;
+        return if defined $fail{$class}{new} and $fail{$class}{new}--;
+        bless({sock => shift}, $class);
+}
+
+sub can_read {
+        my ($self, $timeout) = @_;
+        my $class = ref($self);
+        return if defined $fail{$class}{can_read} and $fail{class}{can_read}--;
+        $self->{sock}{timeout} = $timeout;
+        1;
+}
+
+1;