File: io_unix.t

package info (click to toggle)
libio-socket-timeout-perl 0.32-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 330; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!./perl
#
# This file is part of IO-Socket-Timeout
#
# This software is copyright (c) 2013 by Damien "dams" Krotkine.
#
# This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#

use Config;

BEGIN {
    my $reason;
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
	$reason = 'Socket extension unavailable';
    }
    elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
	$reason = 'IO extension unavailable';
    }
    elsif ($^O eq 'os2') {
	require IO::Socket;

	eval {IO::Socket::pack_sockaddr_un('/foo/bar') || 1}
	  or $@ !~ /not implemented/ or
	    $reason = 'compiled without TCP/IP stack v4';
    }
    elsif ($^O =~ m/^(?:qnx|nto|vos|MSWin32)$/ ) {
	$reason = "UNIX domain sockets not implemented on $^O";
    }
    elsif (! $Config{'d_fork'}) {
	$reason = 'no fork';
    }
    if ($reason) {
	print "1..0 # Skip: $reason\n";
	exit 0;
    }
}

$PATH = "sock-$$";

if ($^O eq 'os2') {	# Can't create sockets with relative path...
  require Cwd;
  my $d = Cwd::cwd();
  $d =~ s/^[a-z]://i;
  $PATH = "$d/$PATH";
}

# Test if we can create the file within the tmp directory
if (-e $PATH or not open(TEST, ">$PATH") and $^O ne 'os2') {
    print "1..0 # Skip: cannot open '$PATH' for write\n";
    exit 0;
}
close(TEST);
unlink($PATH) or $^O eq 'os2' or die "Can't unlink $PATH: $!";

# Start testing
$| = 1;
print "1..5\n";

use IO::Socket;
use IO::Socket::Timeout;

my $TimeoutRead = 5;
my $TimeoutWrite = 5;

$listen = IO::Socket::UNIX->new(Local => $PATH, Listen => 0);
IO::Socket::Timeout->enable_timeouts_on($listen);
$listen->read_timeout($TimeoutRead);
$listen->write_timeout($TimeoutWrite);

# Sometimes UNIX filesystems are mounted for security reasons
# with "nodev" option which spells out "no" for creating UNIX
# local sockets.  Therefore we will retry with a File::Temp
# generated filename from a temp directory.
unless (defined $listen) {
    eval { require File::Temp };
    unless ($@) {
	import File::Temp 'mktemp';
	for my $TMPDIR ($ENV{TMPDIR}, "/tmp") {
	    if (defined $TMPDIR && -d $TMPDIR && -w $TMPDIR) {
		$PATH = mktemp("$TMPDIR/sXXXXXXXX");
		last if $listen = IO::Socket::UNIX->new(Local => $PATH,
							Listen => 0);
	    }
	}
    IO::Socket::Timeout->enable_timeouts_on($listen);
    $listen->read_timeout($TimeoutRead);
    $listen->write_timeout($TimeoutWrite);

    }
    defined $listen or die "$PATH: $!";
}
print "ok 1\n";

if($pid = fork()) {

    $sock = $listen->accept();

    if (defined $sock) {
	print "ok 2\n";

	print $sock->getline();

	print $sock "ok 4\n";

	$sock->close;

	waitpid($pid,0);
	unlink($PATH) || $^O eq 'os2' || warn "Can't unlink $PATH: $!";

	print "ok 5\n";
    } else {
	print "# accept() failed: $!\n";
	for (2..5) {
	    print "not ok $_ # accept failed\n";
	}
    }
} elsif(defined $pid) {

    $sock = IO::Socket::UNIX->new(Peer => $PATH) or die "$!";
    IO::Socket::Timeout->enable_timeouts_on($sock);
    $sock->read_timeout($TimeoutRead);
    $sock->write_timeout($TimeoutWrite);

    print $sock "ok 3\n";

    print $sock->getline();

    $sock->close;

    exit;
} else {
 die;
}